root / trunk / libraries / libRaster / src / org / gvsig / raster / dataset / io / rmf / RmfBlocksManager.java @ 11336
History | View | Annotate | Download (5.27 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*/
|
19 |
package org.gvsig.raster.dataset.io.rmf; |
20 |
|
21 |
import java.io.BufferedReader; |
22 |
import java.io.File; |
23 |
import java.io.FileInputStream; |
24 |
import java.io.FileNotFoundException; |
25 |
import java.io.FileWriter; |
26 |
import java.io.IOException; |
27 |
import java.io.InputStreamReader; |
28 |
import java.util.ArrayList; |
29 |
|
30 |
/**
|
31 |
* Gestor para la escritura de bloques XML en el fichero RMF. Cada cliente que quiere
|
32 |
* escribir en el se registrar? a traves de ClientRegister y esta clase ser? la encargada
|
33 |
* de gestionar la lectura y escritura de bloques.
|
34 |
*
|
35 |
* 21-abr-2007
|
36 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
37 |
*/
|
38 |
public class RmfBlocksManager extends ClientRegister implements IRmfBlock { |
39 |
|
40 |
public static final String MAIN_TAG = "RasterMetaFile"; |
41 |
|
42 |
private String path = null; |
43 |
|
44 |
/**
|
45 |
* Constructor. Asigna la ruta del fichero.
|
46 |
* @param path
|
47 |
*/
|
48 |
public RmfBlocksManager(String path) { |
49 |
this.path = path;
|
50 |
} |
51 |
|
52 |
/**
|
53 |
* Asigna la ruta del fichero
|
54 |
* @param path
|
55 |
*/
|
56 |
public void setPath(String path) { |
57 |
this.path = path;
|
58 |
} |
59 |
|
60 |
/*
|
61 |
* (non-Javadoc)
|
62 |
* @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#read(java.lang.String)
|
63 |
*/
|
64 |
public void read(String xml) throws ParsingException { |
65 |
File file = new File(path); |
66 |
ArrayList lines = new ArrayList(); |
67 |
|
68 |
BufferedReader inGrf = null; |
69 |
try {
|
70 |
inGrf = new BufferedReader(new InputStreamReader(new FileInputStream(file))); |
71 |
String str = inGrf.readLine();
|
72 |
while(str != null) { |
73 |
for (int i = 0; i < clients.size(); i++) { |
74 |
IRmfBlock block = ((IRmfBlock)clients.get(i)); |
75 |
String main = block.getMainTag();
|
76 |
if(str.startsWith("<" + main)) { |
77 |
lines.clear(); |
78 |
while(str.compareTo("</" + main + ">") != 0) { |
79 |
lines.add(str); |
80 |
str = inGrf.readLine(); |
81 |
} |
82 |
lines.add(str); |
83 |
StringBuffer buf = new StringBuffer(); |
84 |
for (int j = 0; j < lines.size(); j++) |
85 |
buf.append((String)lines.get(j));
|
86 |
block.read(buf.toString()); |
87 |
break;
|
88 |
} |
89 |
} |
90 |
|
91 |
str = inGrf.readLine(); |
92 |
} |
93 |
} catch (FileNotFoundException e) { |
94 |
throw new ParsingException("File Input error: creating BufferedReader"); |
95 |
} catch (IOException ex) { |
96 |
throw new ParsingException("File Input error: reading lines"); |
97 |
} |
98 |
|
99 |
} |
100 |
|
101 |
/**
|
102 |
* Creaci?n de nuevo fichero RMF. A?ade la cabecera y vuelca el contenido de
|
103 |
* todos los IRmfBlock.
|
104 |
* @param file Fichero
|
105 |
* @return true si el fichero no existia y se ha creado nuevo
|
106 |
* @throws IOException
|
107 |
*/
|
108 |
private boolean create(File file) throws IOException { |
109 |
if(!file.exists()) {
|
110 |
file.createNewFile(); |
111 |
FileWriter writer = new FileWriter(file); |
112 |
writer.write("<?xml version=\"1.0\" encoding=\"ISO-8859-15\"?>\n");
|
113 |
writer.write("<RasterMetaFile>\n");
|
114 |
for (int i = 0; i < clients.size(); i++) { |
115 |
IRmfBlock block = ((IRmfBlock)clients.get(i)); |
116 |
writer.write(block.write()); |
117 |
} |
118 |
writer.write("\n</RasterMetaFile>\n");
|
119 |
writer.close(); |
120 |
return true; |
121 |
} |
122 |
return false; |
123 |
} |
124 |
|
125 |
/*
|
126 |
* (non-Javadoc)
|
127 |
* @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#write()
|
128 |
*/
|
129 |
public String write() throws IOException { |
130 |
File file = new File(path); |
131 |
ArrayList lines = new ArrayList(); |
132 |
|
133 |
if(create(file))
|
134 |
return null; |
135 |
|
136 |
//A?adir bloques al fichero.
|
137 |
BufferedReader inGrf = new BufferedReader(new InputStreamReader(new FileInputStream(file))); |
138 |
String str = inGrf.readLine();
|
139 |
while(str != null) { |
140 |
lines.add(str); |
141 |
str = inGrf.readLine(); |
142 |
} |
143 |
|
144 |
for (int i = 0; i < clients.size(); i++) { |
145 |
IRmfBlock block = ((IRmfBlock)clients.get(i)); |
146 |
String tag = block.getMainTag();
|
147 |
|
148 |
for (int line = 0; line < lines.size(); line++) { |
149 |
str = (String)lines.get(line);
|
150 |
if(str.compareTo("</" + getMainTag() + ">") == 0 || str.startsWith("<" + tag )) { |
151 |
String xmlBlock = block.write();
|
152 |
if(line != 0) |
153 |
lines.add(line, xmlBlock); |
154 |
if(str.startsWith("<" + tag)) { |
155 |
while(((String)lines.get(line + 1)).compareTo("</" + tag + ">") != 0) |
156 |
lines.remove(line + 1);
|
157 |
lines.remove(line + 1);
|
158 |
} |
159 |
break;
|
160 |
} |
161 |
} |
162 |
} |
163 |
|
164 |
//Escribir fichero de salida.
|
165 |
file.delete(); |
166 |
file.createNewFile(); |
167 |
FileWriter writer = new FileWriter(file); |
168 |
|
169 |
for (int i = 0; i < lines.size(); i++) |
170 |
writer.write((String)lines.get(i) + "\n"); |
171 |
writer.close(); |
172 |
|
173 |
return null; |
174 |
} |
175 |
|
176 |
/*
|
177 |
* (non-Javadoc)
|
178 |
* @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#getMainTag()
|
179 |
*/
|
180 |
public String getMainTag() { |
181 |
return MAIN_TAG;
|
182 |
} |
183 |
|
184 |
/*
|
185 |
* (non-Javadoc)
|
186 |
* @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#getResult()
|
187 |
*/
|
188 |
public Object getResult() { |
189 |
return null; |
190 |
} |
191 |
} |