svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dalfile / src / org / gvsig / fmap / dal / FileHelper.java @ 40362
History | View | Annotate | Download (3.19 KB)
1 |
package org.gvsig.fmap.dal; |
---|---|
2 |
|
3 |
import java.io.InputStream; |
4 |
|
5 |
import org.gvsig.metadata.MetadataLocator; |
6 |
import org.gvsig.metadata.MetadataManager; |
7 |
import org.gvsig.metadata.exceptions.MetadataException; |
8 |
import org.gvsig.tools.ToolsLocator; |
9 |
import org.gvsig.tools.dynobject.DynObject; |
10 |
import org.gvsig.tools.dynobject.DynStruct; |
11 |
import org.gvsig.tools.exception.BaseRuntimeException; |
12 |
import org.gvsig.tools.persistence.PersistenceManager; |
13 |
import org.slf4j.Logger; |
14 |
import org.slf4j.LoggerFactory; |
15 |
|
16 |
public class FileHelper { |
17 |
|
18 |
private static final Logger LOG = LoggerFactory.getLogger(FileHelper.class); |
19 |
|
20 |
private FileHelper() {
|
21 |
// Do nothing
|
22 |
} |
23 |
|
24 |
public static DynStruct registerParametersDefinition(String name, Class theClass, String filename) { |
25 |
DynStruct definition = null;
|
26 |
|
27 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
28 |
definition = manager.getDefinition(name); |
29 |
if ( definition == null) { |
30 |
InputStream stream = theClass.getResourceAsStream(filename);
|
31 |
if( stream == null ) { |
32 |
throw new DefinitionNotFoundException(theClass,filename); |
33 |
} |
34 |
definition = manager.addDefinition( |
35 |
theClass, |
36 |
name, |
37 |
stream, |
38 |
theClass.getClassLoader(), |
39 |
null,
|
40 |
null
|
41 |
); |
42 |
} |
43 |
return definition;
|
44 |
} |
45 |
|
46 |
public static DynStruct registerMetadataDefinition(String name, Class theClass, String filename) throws MetadataException { |
47 |
DynStruct definition = null;
|
48 |
|
49 |
MetadataManager manager = MetadataLocator.getMetadataManager(); |
50 |
definition = manager.getDefinition(name); |
51 |
if ( definition == null) { |
52 |
InputStream stream = theClass.getResourceAsStream(filename);
|
53 |
if( stream == null ) { |
54 |
throw new DefinitionNotFoundException(theClass,filename); |
55 |
} |
56 |
definition = manager.addDefinition( |
57 |
name, |
58 |
theClass.getResourceAsStream(filename), |
59 |
theClass.getClassLoader() |
60 |
); |
61 |
} |
62 |
return definition;
|
63 |
} |
64 |
|
65 |
public static DynObject newParameters(String name) { |
66 |
return ToolsLocator.getDynObjectManager()
|
67 |
.createDynObject( |
68 |
ToolsLocator.getPersistenceManager().getDefinition(name) |
69 |
); |
70 |
} |
71 |
|
72 |
public static DynObject newMetadataContainer(String name) { |
73 |
try {
|
74 |
MetadataManager manager = MetadataLocator.getMetadataManager(); |
75 |
DynStruct definition = manager.getDefinition(name); |
76 |
return ToolsLocator.getDynObjectManager().createDynObject(definition);
|
77 |
} catch(NullPointerException e) { |
78 |
throw new CantCreateMetadataException(name,e); |
79 |
} |
80 |
} |
81 |
|
82 |
private static class DefinitionNotFoundException extends BaseRuntimeException { |
83 |
|
84 |
/**
|
85 |
*
|
86 |
*/
|
87 |
private static final long serialVersionUID = 7598155353307119897L; |
88 |
|
89 |
public DefinitionNotFoundException(Class theClass, String filename ) { |
90 |
super(
|
91 |
"Can't open parameters definition '%(filename)' from class %(classname).",
|
92 |
"_ResourceForParametersDefinitionNotFoundException",
|
93 |
serialVersionUID |
94 |
); |
95 |
this.setValue("filename", filename); |
96 |
this.setValue("classname", theClass.getClass().getName()); |
97 |
} |
98 |
} |
99 |
|
100 |
private static class CantCreateMetadataException extends RuntimeException { |
101 |
|
102 |
/**
|
103 |
*
|
104 |
*/
|
105 |
private static final long serialVersionUID = -8373306339537106075L; |
106 |
|
107 |
CantCreateMetadataException(String name, Throwable cause) { |
108 |
super("Can't retrieve metadata definition for '"+name+"'.", cause); |
109 |
} |
110 |
} |
111 |
} |