Revision 17655 trunk/libraries/libRaster/src/org/gvsig/raster/dataset/io/rmf/RmfBlocksManager.java
RmfBlocksManager.java | ||
---|---|---|
27 | 27 |
import java.io.InputStreamReader; |
28 | 28 |
import java.util.ArrayList; |
29 | 29 |
|
30 |
import org.gvsig.raster.util.RasterUtilities; |
|
30 | 31 |
/** |
31 | 32 |
* Gestor para la escritura de bloques XML en el fichero RMF. Cada cliente que quiere |
32 | 33 |
* escribir en el se registrar? a traves de ClientRegister y esta clase ser? la encargada |
... | ... | |
36 | 37 |
* @author Nacho Brodin (nachobrodin@gmail.com) |
37 | 38 |
*/ |
38 | 39 |
public class RmfBlocksManager extends ClientRegister implements IRmfBlock { |
39 |
|
|
40 |
public static final String MAIN_TAG = "RasterMetaFile"; |
|
41 |
|
|
40 |
private final String MAIN_TAG = "RasterMetaFile"; |
|
42 | 41 |
private String path = null; |
43 |
|
|
42 |
|
|
44 | 43 |
/** |
45 |
* Constructor. Asigna la ruta del fichero.
|
|
44 |
* Constructor. Asigna la ruta del fichero. |
|
46 | 45 |
* @param path |
47 | 46 |
*/ |
48 | 47 |
public RmfBlocksManager(String path) { |
49 |
this.path = path;
|
|
48 |
setPath(path);
|
|
50 | 49 |
} |
51 |
|
|
50 |
|
|
52 | 51 |
/** |
53 | 52 |
* Asigna la ruta del fichero |
54 | 53 |
* @param path |
... | ... | |
56 | 55 |
public void setPath(String path) { |
57 | 56 |
this.path = path; |
58 | 57 |
} |
59 |
|
|
58 |
|
|
60 | 59 |
/** |
61 | 60 |
* Obtiene la ruta del fichero |
62 | 61 |
* @return path |
... | ... | |
70 | 69 |
* @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#read(java.lang.String) |
71 | 70 |
*/ |
72 | 71 |
public void read(String xml) throws ParsingException { |
73 |
File file = new File(path);
|
|
72 |
File file = new File(getPath());
|
|
74 | 73 |
ArrayList lines = new ArrayList(); |
75 |
|
|
74 |
|
|
76 | 75 |
BufferedReader inGrf = null; |
77 | 76 |
try { |
78 | 77 |
inGrf = new BufferedReader(new InputStreamReader(new FileInputStream(file))); |
79 | 78 |
String str = inGrf.readLine(); |
80 |
while(str != null) { |
|
79 |
while (str != null) {
|
|
81 | 80 |
for (int i = 0; i < clients.size(); i++) { |
82 |
IRmfBlock block = ((IRmfBlock)clients.get(i)); |
|
81 |
IRmfBlock block = ((IRmfBlock) clients.get(i));
|
|
83 | 82 |
String main = block.getMainTag(); |
84 |
if(str.startsWith("<" + main)) { |
|
83 |
if (str.startsWith("<" + main)) {
|
|
85 | 84 |
lines.clear(); |
86 |
while(str.compareTo("</" + main + ">") != 0) { |
|
85 |
while (str.compareTo("</" + main + ">") != 0) {
|
|
87 | 86 |
lines.add(str); |
88 | 87 |
str = inGrf.readLine(); |
89 | 88 |
} |
90 | 89 |
lines.add(str); |
91 | 90 |
StringBuffer buf = new StringBuffer(); |
92 | 91 |
for (int j = 0; j < lines.size(); j++) |
93 |
buf.append((String)lines.get(j)); |
|
92 |
buf.append((String) lines.get(j));
|
|
94 | 93 |
block.read(buf.toString()); |
95 | 94 |
break; |
96 | 95 |
} |
97 | 96 |
} |
98 |
|
|
97 |
|
|
99 | 98 |
str = inGrf.readLine(); |
100 | 99 |
} |
101 | 100 |
} catch (FileNotFoundException e) { |
... | ... | |
103 | 102 |
} catch (IOException ex) { |
104 | 103 |
throw new ParsingException("File Input error: reading lines"); |
105 | 104 |
} |
106 |
|
|
107 | 105 |
} |
108 | 106 |
|
109 | 107 |
/** |
110 |
* Creaci?n de nuevo fichero RMF. A?ade la cabecera y vuelca el contenido de
|
|
108 |
* Creaci?n de nuevo fichero RMF. A?ade la cabecera y vuelca el contenido de |
|
111 | 109 |
* todos los IRmfBlock. |
112 | 110 |
* @param file Fichero |
113 | 111 |
* @return true si el fichero no existia y se ha creado nuevo |
114 | 112 |
* @throws IOException |
115 | 113 |
*/ |
116 | 114 |
private boolean create(File file) throws IOException { |
117 |
if(!file.exists()) { |
|
115 |
if (!file.exists()) {
|
|
118 | 116 |
file.createNewFile(); |
119 | 117 |
FileWriter writer = new FileWriter(file); |
120 | 118 |
writer.write("<?xml version=\"1.0\" encoding=\"ISO-8859-15\"?>\n"); |
121 | 119 |
writer.write("<RasterMetaFile>\n"); |
122 | 120 |
for (int i = 0; i < clients.size(); i++) { |
123 |
IRmfBlock block = ((IRmfBlock)clients.get(i)); |
|
121 |
IRmfBlock block = ((IRmfBlock) clients.get(i));
|
|
124 | 122 |
writer.write(block.write()); |
125 | 123 |
} |
126 | 124 |
writer.write("</RasterMetaFile>\n"); |
... | ... | |
129 | 127 |
} |
130 | 128 |
return false; |
131 | 129 |
} |
132 |
|
|
130 |
|
|
133 | 131 |
/* |
134 | 132 |
* (non-Javadoc) |
135 | 133 |
* @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#write() |
136 | 134 |
*/ |
137 | 135 |
public String write() throws IOException { |
138 |
File file = new File(path);
|
|
136 |
File file = new File(getPath());
|
|
139 | 137 |
ArrayList lines = new ArrayList(); |
140 | 138 |
|
141 | 139 |
// Si no existe a?n el rmf se crea, se a?ade las cabeceras y se vuelca el |
... | ... | |
194 | 192 |
} |
195 | 193 |
|
196 | 194 |
/** |
195 |
* Obtiene un bloque XML que representa a las propiedades del objeto a |
|
196 |
* serializar. Antes de la operaci?n hace una copia de seguridad del fichero |
|
197 |
* RMF. |
|
198 |
* @param rmfBackup Especifica si debe hacer la copia de seguridad. |
|
199 |
* @return Texto XML que representa el objeto. |
|
200 |
*/ |
|
201 |
public String write(boolean rmfBackup) throws IOException, FileNotFoundException { |
|
202 |
if (rmfBackup) |
|
203 |
RasterUtilities.copyFile(getPath(), getPath() + "~"); |
|
204 |
|
|
205 |
return write(); |
|
206 |
} |
|
207 |
|
|
208 |
/** |
|
197 | 209 |
* M?todo que checkea si el fichero pasado es valido para ser rmf. Si existe |
198 | 210 |
* el fichero ser? valido si se puede escribir en el y no est? vacio y no es |
199 | 211 |
* un directorio y contiene los caracteres de cabecera de comienzo de XML (<?xml |
... | ... | |
202 | 214 |
* @return true si es un rmf valido y false si no lo es |
203 | 215 |
*/ |
204 | 216 |
public boolean checkRmf() { |
205 |
File f = new File(this.path);
|
|
217 |
File f = new File(getPath());
|
|
206 | 218 |
if (f.exists()) { |
207 | 219 |
if (!f.canWrite() || f.length() == 0 || f.isDirectory()) |
208 | 220 |
return false; |
... | ... | |
220 | 232 |
} |
221 | 233 |
return true; |
222 | 234 |
} |
223 |
|
|
235 |
|
|
224 | 236 |
/* |
225 |
* (non-Javadoc)
|
|
237 |
* (non-Javadoc) |
|
226 | 238 |
* @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#getMainTag() |
227 | 239 |
*/ |
228 | 240 |
public String getMainTag() { |
... | ... | |
236 | 248 |
public Object getResult() { |
237 | 249 |
return null; |
238 | 250 |
} |
239 |
} |
|
251 |
} |
Also available in: Unified diff