32 |
32 |
import org.gvsig.fmap.dal.DataStoreParameters;
|
33 |
33 |
import org.gvsig.fmap.dal.coverage.RasterLocator;
|
34 |
34 |
import org.gvsig.fmap.dal.coverage.store.parameter.RasterFileStoreParameters;
|
|
35 |
import org.gvsig.i18n.Messages;
|
35 |
36 |
import org.gvsig.raster.swing.RasterSwingLibrary;
|
36 |
37 |
import org.kxml2.io.KXmlParser;
|
37 |
38 |
import org.xmlpull.v1.XmlPullParser;
|
... | ... | |
45 |
46 |
public class PrepareDataStoreParametersRaw implements
|
46 |
47 |
PrepareDataStoreParameters {
|
47 |
48 |
|
48 |
|
/* (non-Javadoc)
|
49 |
|
* @see org.gvsig.PrepareDataStoreParameters#prepare(org.gvsig.fmap.dal.DataStoreParameters, org.gvsig.PrepareDataStoreContext)
|
50 |
|
*/
|
51 |
49 |
public DataStoreParameters prepare(DataStoreParameters storeParamters,
|
52 |
50 |
PrepareContext context) {
|
53 |
51 |
if (!(storeParamters instanceof RasterFileStoreParameters))
|
54 |
52 |
return storeParamters;
|
55 |
53 |
RasterFileStoreParameters rasterParams = (RasterFileStoreParameters) storeParamters;
|
56 |
54 |
File file = rasterParams.getFile();
|
57 |
|
if (RasterLocator.getManager().getFileUtils().getExtensionFromFileName(file.getAbsolutePath())
|
58 |
|
.equals("vrt"))
|
|
55 |
if (file == null)
|
|
56 |
return null;
|
|
57 |
|
|
58 |
if (RasterLocator.getManager().getFileUtils().getExtensionFromFileName(file.getAbsolutePath()).equals("vrt")) {
|
59 |
59 |
try {
|
60 |
60 |
checkFileVRT(file);
|
61 |
61 |
} catch (FileOpenVRTException e) {
|
62 |
62 |
RasterSwingLibrary.messageBoxError(PluginServices.getText(this, "error_abrir_fichero") + " " + file.getName() + "\n\n" + PluginServices.getText(this, "informacion_adicional") + ":\n\n " + e.getMessage(), this, e);
|
63 |
63 |
return null;
|
64 |
64 |
}
|
|
65 |
}
|
65 |
66 |
|
66 |
|
if (file == null)
|
67 |
|
return null;
|
68 |
67 |
rasterParams.setFile(file);
|
69 |
68 |
return rasterParams;
|
70 |
69 |
}
|
... | ... | |
78 |
77 |
*/
|
79 |
78 |
private void checkFileVRT(File file) throws FileOpenVRTException {
|
80 |
79 |
KXmlParser parser = new KXmlParser();
|
|
80 |
boolean end = false;
|
|
81 |
int currentTag;
|
81 |
82 |
|
82 |
83 |
FileReader fileReader = null;
|
83 |
84 |
try {
|
84 |
85 |
fileReader = new FileReader(file);
|
85 |
86 |
parser.setInput(fileReader);
|
86 |
87 |
|
87 |
|
parser.nextTag();
|
|
88 |
currentTag = parser.nextTag();
|
88 |
89 |
|
89 |
90 |
parser.require(XmlPullParser.START_TAG, null, "VRTDataset");
|
90 |
91 |
|
91 |
|
while (parser.nextTag() != XmlPullParser.END_TAG) {
|
92 |
|
parser.require(XmlPullParser.START_TAG, null, "VRTRasterBand");
|
93 |
|
|
94 |
|
String name;
|
95 |
|
while (parser.nextTag() != XmlPullParser.END_TAG) {
|
96 |
|
parser.require(XmlPullParser.START_TAG, null, null);
|
97 |
|
boolean relativePath = false;
|
98 |
|
for (int i = 0; i < parser.getAttributeCount(); i++)
|
99 |
|
if (parser.getAttributeName(i).equals("relativetoVRT")
|
100 |
|
&& parser.getAttributeValue(i).equals("1"))
|
101 |
|
relativePath = true;
|
102 |
|
name = parser.getName();
|
103 |
|
String nameFile = parser.nextText();
|
104 |
|
if (name.equals("SourceFilename")) {
|
105 |
|
if (relativePath)
|
106 |
|
nameFile = file.getParent() + File.separator
|
107 |
|
+ nameFile;
|
108 |
|
File tryFile = new File(nameFile);
|
109 |
|
if (!tryFile.exists())
|
110 |
|
throw new FileOpenVRTException(PluginServices
|
111 |
|
.getText(this, "no_existe_fichero")
|
112 |
|
+ " " + nameFile);
|
113 |
|
}
|
114 |
|
parser.require(XmlPullParser.END_TAG, null, name);
|
|
92 |
while (!end) {
|
|
93 |
switch(currentTag) {
|
|
94 |
case KXmlParser.START_TAG:
|
|
95 |
if (compareName(parser, "VRTRasterBand")) {
|
|
96 |
checkSubTreeVRTRasterBand(parser, file);
|
|
97 |
}
|
|
98 |
break;
|
|
99 |
case KXmlParser.END_TAG:
|
|
100 |
if (compareName(parser, "VRTRasterBand"))
|
|
101 |
end = true;
|
|
102 |
break;
|
|
103 |
case KXmlParser.TEXT:
|
|
104 |
break;
|
115 |
105 |
}
|
|
106 |
if (!end)
|
|
107 |
currentTag = parser.next();
|
|
108 |
}
|
116 |
109 |
|
117 |
|
parser.require(XmlPullParser.END_TAG, null, "VRTRasterBand");
|
118 |
|
}
|
119 |
|
parser.require(XmlPullParser.END_TAG, null, "VRTDataset");
|
120 |
|
parser.next();
|
121 |
|
parser.require(XmlPullParser.END_DOCUMENT, null, null);
|
122 |
110 |
} catch (XmlPullParserException e) {
|
123 |
111 |
throw new FileOpenVRTException(PluginServices.getText(this,
|
124 |
112 |
"el_fichero")
|
125 |
113 |
+ " "
|
126 |
114 |
+ file.getName().toString()
|
127 |
115 |
+ " "
|
128 |
|
+ PluginServices.getText(this, "esta_formato_desconocido"));
|
|
116 |
+ Messages.getText("esta_formato_desconocido"));
|
129 |
117 |
} catch (IOException e) {
|
130 |
118 |
throw new FileOpenVRTException(PluginServices.getText(this,
|
131 |
119 |
"no_puede_abrir_fichero")
|
... | ... | |
138 |
126 |
}
|
139 |
127 |
}
|
140 |
128 |
}
|
|
129 |
|
|
130 |
private void checkSubTreeVRTRasterBand(KXmlParser parser, File file) throws XmlPullParserException, IOException, FileOpenVRTException {
|
|
131 |
int currentTag;
|
|
132 |
boolean end = false;
|
141 |
133 |
|
|
134 |
currentTag = parser.nextTag();
|
|
135 |
|
|
136 |
while (!end) {
|
|
137 |
switch(currentTag) {
|
|
138 |
case KXmlParser.START_TAG:
|
|
139 |
if (compareName(parser, "SourceFilename")) {
|
|
140 |
checkTagSourceFileName(parser, file);
|
|
141 |
end = true;
|
|
142 |
} else if(compareName(parser, "SimpleSource") ||
|
|
143 |
compareName(parser, "ComplexSource") ||
|
|
144 |
compareName(parser, "AveragedSource")) {
|
|
145 |
checkSubTreeSource(parser, file);
|
|
146 |
}
|
|
147 |
break;
|
|
148 |
case KXmlParser.END_TAG:
|
|
149 |
if (compareName(parser, "SourceFilename") ||
|
|
150 |
compareName(parser, "SimpleSource") ||
|
|
151 |
compareName(parser, "ComplexSource") ||
|
|
152 |
compareName(parser, "AveragedSource"))
|
|
153 |
end = true;
|
|
154 |
break;
|
|
155 |
case KXmlParser.TEXT:
|
|
156 |
break;
|
|
157 |
}
|
|
158 |
if (!end)
|
|
159 |
currentTag = parser.next();
|
|
160 |
}
|
|
161 |
}
|
|
162 |
|
|
163 |
private void checkSubTreeSource(KXmlParser parser, File file) throws XmlPullParserException, IOException, FileOpenVRTException {
|
|
164 |
int currentTag;
|
|
165 |
boolean end = false;
|
|
166 |
|
|
167 |
currentTag = parser.nextTag();
|
|
168 |
|
|
169 |
while (!end) {
|
|
170 |
switch(currentTag) {
|
|
171 |
case KXmlParser.START_TAG:
|
|
172 |
if (compareName(parser, "SourceFilename")) {
|
|
173 |
checkTagSourceFileName(parser, file);
|
|
174 |
}
|
|
175 |
case KXmlParser.END_TAG:
|
|
176 |
if (compareName(parser, "SourceFilename"))
|
|
177 |
end = true;
|
|
178 |
break;
|
|
179 |
case KXmlParser.TEXT:
|
|
180 |
break;
|
|
181 |
}
|
|
182 |
if (!end)
|
|
183 |
currentTag = parser.next();
|
|
184 |
}
|
|
185 |
}
|
|
186 |
|
|
187 |
private void checkTagSourceFileName(KXmlParser parser, File file) throws XmlPullParserException, IOException, FileOpenVRTException {
|
|
188 |
for (int i = 0; i < parser.getAttributeCount(); i++) {
|
|
189 |
if (parser.getAttributeName(i).equals("relativetoVRT") &&
|
|
190 |
parser.getAttributeValue(i).equals("1")) {
|
|
191 |
String nameFile = parser.nextText();
|
|
192 |
nameFile = file.getParent() + File.separator + nameFile;
|
|
193 |
File tryFile = new File(nameFile);
|
|
194 |
if (!tryFile.exists())
|
|
195 |
throw new FileOpenVRTException(Messages.getText("no_existe_fichero") + " " + nameFile);
|
|
196 |
}
|
|
197 |
}
|
|
198 |
}
|
|
199 |
|
|
200 |
/**
|
|
201 |
* Compares the next name in the selected parser with the string passed
|
|
202 |
* by value.
|
|
203 |
* @param parser
|
|
204 |
* @param name
|
|
205 |
* @return
|
|
206 |
*/
|
|
207 |
public boolean compareName(KXmlParser parser, String name) {
|
|
208 |
String s = parser.getName();
|
|
209 |
if(s.compareTo(name) == 0 || s.compareTo("ows:" + name) == 0)
|
|
210 |
return true;
|
|
211 |
return false;
|
|
212 |
}
|
|
213 |
|
142 |
214 |
public String getDescription() {
|
143 |
215 |
return "Prepare Raster Raw Stores";
|
144 |
216 |
}
|
... | ... | |
155 |
227 |
return this;
|
156 |
228 |
}
|
157 |
229 |
|
|
230 |
@SuppressWarnings("rawtypes")
|
158 |
231 |
public Object create(Map args) {
|
159 |
232 |
return this;
|
160 |
233 |
}
|
161 |
234 |
|
162 |
235 |
public void post(DataStoreParameters storeParameters, PrepareContext context) {
|
163 |
|
// TODO Auto-generated method stub
|
164 |
236 |
|
165 |
237 |
}
|
166 |
238 |
|
167 |
239 |
public void pre(DataStoreParameters storeParameters, PrepareContext context) {
|
168 |
|
// TODO Auto-generated method stub
|
169 |
240 |
|
170 |
241 |
}
|
171 |
|
|
172 |
242 |
}
|