svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.file / org.gvsig.fmap.dal.file.lib / src / main / java / org / gvsig / fmap / dal / FileHelper.java @ 40559
History | View | Annotate | Download (4.12 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.fmap.dal; |
25 |
|
26 |
import java.io.InputStream; |
27 |
|
28 |
import org.gvsig.metadata.MetadataLocator; |
29 |
import org.gvsig.metadata.MetadataManager; |
30 |
import org.gvsig.metadata.exceptions.MetadataException; |
31 |
import org.gvsig.tools.ToolsLocator; |
32 |
import org.gvsig.tools.dynobject.DynObject; |
33 |
import org.gvsig.tools.dynobject.DynStruct; |
34 |
import org.gvsig.tools.exception.BaseRuntimeException; |
35 |
import org.gvsig.tools.persistence.PersistenceManager; |
36 |
import org.slf4j.Logger; |
37 |
import org.slf4j.LoggerFactory; |
38 |
|
39 |
public class FileHelper { |
40 |
|
41 |
private static final Logger LOG = LoggerFactory.getLogger(FileHelper.class); |
42 |
|
43 |
private FileHelper() {
|
44 |
// Do nothing
|
45 |
} |
46 |
|
47 |
public static DynStruct registerParametersDefinition(String name, Class theClass, String filename) { |
48 |
DynStruct definition = null;
|
49 |
|
50 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
51 |
definition = manager.getDefinition(name); |
52 |
if ( definition == null) { |
53 |
InputStream stream = theClass.getResourceAsStream(filename);
|
54 |
if( stream == null ) { |
55 |
throw new DefinitionNotFoundException(theClass,filename); |
56 |
} |
57 |
definition = manager.addDefinition( |
58 |
theClass, |
59 |
name, |
60 |
stream, |
61 |
theClass.getClassLoader(), |
62 |
null,
|
63 |
null
|
64 |
); |
65 |
} |
66 |
return definition;
|
67 |
} |
68 |
|
69 |
public static DynStruct registerMetadataDefinition(String name, Class theClass, String filename) throws MetadataException { |
70 |
DynStruct definition = null;
|
71 |
|
72 |
MetadataManager manager = MetadataLocator.getMetadataManager(); |
73 |
definition = manager.getDefinition(name); |
74 |
if ( definition == null) { |
75 |
InputStream stream = theClass.getResourceAsStream(filename);
|
76 |
if( stream == null ) { |
77 |
throw new DefinitionNotFoundException(theClass,filename); |
78 |
} |
79 |
definition = manager.addDefinition( |
80 |
name, |
81 |
theClass.getResourceAsStream(filename), |
82 |
theClass.getClassLoader() |
83 |
); |
84 |
} |
85 |
return definition;
|
86 |
} |
87 |
|
88 |
public static DynObject newParameters(String name) { |
89 |
return ToolsLocator.getDynObjectManager()
|
90 |
.createDynObject( |
91 |
ToolsLocator.getPersistenceManager().getDefinition(name) |
92 |
); |
93 |
} |
94 |
|
95 |
public static DynObject newMetadataContainer(String name) { |
96 |
try {
|
97 |
MetadataManager manager = MetadataLocator.getMetadataManager(); |
98 |
DynStruct definition = manager.getDefinition(name); |
99 |
return ToolsLocator.getDynObjectManager().createDynObject(definition);
|
100 |
} catch(NullPointerException e) { |
101 |
throw new CantCreateMetadataException(name,e); |
102 |
} |
103 |
} |
104 |
|
105 |
private static class DefinitionNotFoundException extends BaseRuntimeException { |
106 |
|
107 |
/**
|
108 |
*
|
109 |
*/
|
110 |
private static final long serialVersionUID = 7598155353307119897L; |
111 |
|
112 |
public DefinitionNotFoundException(Class theClass, String filename ) { |
113 |
super(
|
114 |
"Can't open parameters definition '%(filename)' from class %(classname).",
|
115 |
"_ResourceForParametersDefinitionNotFoundException",
|
116 |
serialVersionUID |
117 |
); |
118 |
this.setValue("filename", filename); |
119 |
this.setValue("classname", theClass.getClass().getName()); |
120 |
} |
121 |
} |
122 |
|
123 |
private static class CantCreateMetadataException extends RuntimeException { |
124 |
|
125 |
/**
|
126 |
*
|
127 |
*/
|
128 |
private static final long serialVersionUID = -8373306339537106075L; |
129 |
|
130 |
CantCreateMetadataException(String name, Throwable cause) { |
131 |
super("Can't retrieve metadata definition for '"+name+"'.", cause); |
132 |
} |
133 |
} |
134 |
} |