root / trunk / libraries / libGPE-XML / src / org / gvsig / gpe / xml / writer / GPEXmlWriterHandlerImplementor.java @ 19687
History | View | Annotate | Download (8.53 KB)
1 |
package org.gvsig.gpe.xml.writer; |
---|---|
2 |
|
3 |
import java.io.BufferedInputStream; |
4 |
import java.io.BufferedOutputStream; |
5 |
import java.io.File; |
6 |
import java.io.FileInputStream; |
7 |
import java.io.FileNotFoundException; |
8 |
import java.io.FileOutputStream; |
9 |
import java.io.IOException; |
10 |
import java.io.InputStream; |
11 |
import java.net.URI; |
12 |
import java.util.Enumeration; |
13 |
import java.util.zip.ZipEntry; |
14 |
import java.util.zip.ZipOutputStream; |
15 |
|
16 |
import org.gvsig.gpe.GPEDefaults; |
17 |
import org.gvsig.gpe.writer.GPEWriterHandlerImplementor; |
18 |
import org.gvsig.gpe.xml.exceptions.GPEXmlToZipFileException; |
19 |
import org.gvsig.gpe.xml.utils.XMLTags; |
20 |
import org.gvsig.xmlschema.som.IXSSchema; |
21 |
|
22 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
23 |
*
|
24 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
25 |
*
|
26 |
* This program is free software; you can redistribute it and/or
|
27 |
* modify it under the terms of the GNU General Public License
|
28 |
* as published by the Free Software Foundation; either version 2
|
29 |
* of the License, or (at your option) any later version.
|
30 |
*
|
31 |
* This program is distributed in the hope that it will be useful,
|
32 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
33 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
34 |
* GNU General Public License for more details.
|
35 |
*
|
36 |
* You should have received a copy of the GNU General Public License
|
37 |
* along with this program; if not, write to the Free Software
|
38 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
39 |
*
|
40 |
* For more information, contact:
|
41 |
*
|
42 |
* Generalitat Valenciana
|
43 |
* Conselleria d'Infraestructures i Transport
|
44 |
* Av. Blasco Ib??ez, 50
|
45 |
* 46010 VALENCIA
|
46 |
* SPAIN
|
47 |
*
|
48 |
* +34 963862235
|
49 |
* gvsig@gva.es
|
50 |
* www.gvsig.gva.es
|
51 |
*
|
52 |
* or
|
53 |
*
|
54 |
* IVER T.I. S.A
|
55 |
* Salamanca 50
|
56 |
* 46005 Valencia
|
57 |
* Spain
|
58 |
*
|
59 |
* +34 963163400
|
60 |
* dac@iver.es
|
61 |
*/
|
62 |
/* CVS MESSAGES:
|
63 |
*
|
64 |
* $Id: GPEXmlWriterHandlerImplementor.java 350 2008-01-09 12:53:07Z jpiera $
|
65 |
* $Log$
|
66 |
* Revision 1.12 2007/06/28 13:05:09 jorpiell
|
67 |
* The Qname has been updated to the 1.5 JVM machine. The schema validation is made in the GPEWriterHandlerImplementor class
|
68 |
*
|
69 |
* Revision 1.11 2007/06/22 12:22:40 jorpiell
|
70 |
* The typeNotFoundException has been deleted. It never was thrown
|
71 |
*
|
72 |
* Revision 1.10 2007/06/14 13:50:05 jorpiell
|
73 |
* The schema jar name has been changed
|
74 |
*
|
75 |
* Revision 1.9 2007/06/07 14:53:30 jorpiell
|
76 |
* Add the schema support
|
77 |
*
|
78 |
* Revision 1.8 2007/05/07 07:07:04 jorpiell
|
79 |
* Add a constructor with the name and the description fields
|
80 |
*
|
81 |
* Revision 1.7 2007/04/26 14:40:03 jorpiell
|
82 |
* Some writer handler methods updated
|
83 |
*
|
84 |
* Revision 1.6 2007/04/19 07:25:49 jorpiell
|
85 |
* Add the add methods to teh contenhandler and change the register mode
|
86 |
*
|
87 |
* Revision 1.5 2007/04/17 10:30:11 jorpiell
|
88 |
* Add a method to compress a file
|
89 |
*
|
90 |
* Revision 1.4 2007/04/14 16:07:30 jorpiell
|
91 |
* The writer has been created
|
92 |
*
|
93 |
* Revision 1.3 2007/04/13 07:17:57 jorpiell
|
94 |
* Add the writting tests for the simple geometries
|
95 |
*
|
96 |
* Revision 1.2 2007/04/12 17:06:44 jorpiell
|
97 |
* First GML writing tests
|
98 |
*
|
99 |
* Revision 1.1 2007/04/12 10:23:41 jorpiell
|
100 |
* Add some writers and the GPEXml parser
|
101 |
*
|
102 |
*
|
103 |
*/
|
104 |
/**
|
105 |
* @author Jorge Piera LLodr? (jorge.piera@iver.es)
|
106 |
*/
|
107 |
public abstract class GPEXmlWriterHandlerImplementor extends GPEWriterHandlerImplementor { |
108 |
protected IWriter writer = null; |
109 |
private String targetNamespace = null; |
110 |
private String targetNamespacePrefix = null; |
111 |
|
112 |
public GPEXmlWriterHandlerImplementor() {
|
113 |
super();
|
114 |
} |
115 |
|
116 |
/**
|
117 |
* Writes the XML header
|
118 |
* @throws IOException
|
119 |
*/
|
120 |
protected void initXmlHeader() throws IOException{ |
121 |
if (getOutputStream() == null){ |
122 |
writer = createWriter(); |
123 |
}else{
|
124 |
writer = new OutputStreamWriter(getOutputStream()); |
125 |
} |
126 |
writer.write("<?xml version=\"");
|
127 |
writer.write(GPEDefaults.getStringProperty(GPEDefaults.XML_VERSION).toString()); |
128 |
writer.write("\" encoding=\"");
|
129 |
writer.write(GPEDefaults.getStringProperty(GPEDefaults.XML_ENCODING).toString()); |
130 |
writer.write("\"?>");
|
131 |
} |
132 |
|
133 |
/**
|
134 |
* Creates an input stream from a file.
|
135 |
* @param file
|
136 |
* @return
|
137 |
* @throws FileNotFoundException
|
138 |
* @throws FileNotFoundException
|
139 |
*/
|
140 |
protected abstract IWriter createWriter() throws IOException; |
141 |
|
142 |
/**
|
143 |
* @return the schemaLocation tag
|
144 |
*/
|
145 |
protected String getSchemaLocations(){ |
146 |
StringBuffer schemaLocation = new StringBuffer(); |
147 |
Enumeration uris = getSchemaDocument().getURIs();
|
148 |
int i = 0; |
149 |
while (uris.hasMoreElements()){
|
150 |
if (i > 0){ |
151 |
schemaLocation.append("\b");
|
152 |
} |
153 |
URI uri = (URI)uris.nextElement(); |
154 |
IXSSchema schema = getSchemaDocument().getSchema(uri); |
155 |
schemaLocation.append(schema.getTargetNamespace()); |
156 |
schemaLocation.append(" ");
|
157 |
schemaLocation.append(getSchemaDocument().getSchemaLocation(uri)); |
158 |
i++; |
159 |
} |
160 |
return schemaLocation.toString();
|
161 |
} |
162 |
|
163 |
/**
|
164 |
* @return the XML header prefixes
|
165 |
*/
|
166 |
protected String getDocumentPrefixes(){ |
167 |
StringBuffer buffer = new StringBuffer(); |
168 |
if (getSchemaDocument().hasPrefixes() ||
|
169 |
(getSchemaDocument().getTargetNamespace() != null)){
|
170 |
//The TargetNamespace for the unqualified names
|
171 |
if (getSchemaDocument().getTargetNamespace() != null){ |
172 |
String prefix = getSchemaDocument().getTargetNamespace();
|
173 |
buffer.append(XMLTags.XML_NAMESPACE); |
174 |
buffer.append("=\"");
|
175 |
buffer.append(prefix); |
176 |
buffer.append("\" ");
|
177 |
} |
178 |
//The other schemas
|
179 |
Enumeration prefixes = getSchemaDocument().getPrefixes();
|
180 |
int i = 0; |
181 |
while (prefixes.hasMoreElements()){
|
182 |
if (i > 0){ |
183 |
buffer.append("\b");
|
184 |
} |
185 |
String prefix = (String)prefixes.nextElement(); |
186 |
String namespaceURI = getSchemaDocument().getNamespaceURI(prefix);
|
187 |
buffer.append(XMLTags.XML_NAMESPACE + ":" + prefix);
|
188 |
buffer.append("=\"");
|
189 |
buffer.append(namespaceURI); |
190 |
buffer.append("\" ");
|
191 |
i++; |
192 |
} |
193 |
}else{
|
194 |
String prefix = GPEDefaults.getStringProperty(GPEDefaults.NAMESPACE_PREFIX);
|
195 |
String uri = GPEDefaults.getStringProperty(GPEDefaults.NAMESPACE_URI);
|
196 |
if ((prefix != null) && (uri != null)){ |
197 |
buffer.append(" " + XMLTags.XML_NAMESPACE + ":" + prefix + "=\""); |
198 |
buffer.append(uri + "\" ");
|
199 |
} |
200 |
} |
201 |
return buffer.toString();
|
202 |
} |
203 |
|
204 |
/**
|
205 |
* Returns the selected target namespace prefix. If
|
206 |
* the namespace doesn't exists it returns the
|
207 |
* default Namespace prefix;
|
208 |
* @return the namespace prefix
|
209 |
*/
|
210 |
protected String getTargetNamespacePrefix() { |
211 |
if (targetNamespacePrefix == null){ |
212 |
String prefix = null; |
213 |
if (getSchemaDocument().getTargetNamespace() != null){ |
214 |
prefix = getSchemaDocument().getNamespacePrefix( |
215 |
getSchemaDocument().getTargetNamespace()); |
216 |
}else{
|
217 |
prefix = GPEDefaults.getStringProperty(GPEDefaults.NAMESPACE_PREFIX); |
218 |
} |
219 |
if (prefix != null){ |
220 |
targetNamespacePrefix = prefix + ":";
|
221 |
}else{
|
222 |
targetNamespacePrefix = "";
|
223 |
} |
224 |
} |
225 |
return targetNamespacePrefix;
|
226 |
} |
227 |
|
228 |
/**
|
229 |
* Returns the selected target namespace. If
|
230 |
* the namespace doesn't exists it returns the
|
231 |
* default Namespace;
|
232 |
* @return the namespace
|
233 |
*/
|
234 |
protected String getTargetNamespace() { |
235 |
if (targetNamespace == null){ |
236 |
if (getSchemaDocument().getTargetNamespace() != null){ |
237 |
targetNamespace = getSchemaDocument().getTargetNamespace(); |
238 |
}else{
|
239 |
targetNamespace = GPEDefaults.getStringProperty(GPEDefaults.NAMESPACE_URI); |
240 |
} |
241 |
} |
242 |
return targetNamespace;
|
243 |
} |
244 |
|
245 |
/**
|
246 |
* This method compress a file using the ZIP
|
247 |
* compression
|
248 |
* @throws IOException
|
249 |
*/
|
250 |
protected void compressFile() throws GPEXmlToZipFileException{ |
251 |
int BUFFER_SIZE = 1024; |
252 |
int i = 0; |
253 |
boolean fileExists = true; |
254 |
File zipFile = null; |
255 |
String fileNameAux = null; |
256 |
while (fileExists){
|
257 |
fileNameAux = getOutputFile().getAbsolutePath() + i; |
258 |
zipFile = new File(fileNameAux); |
259 |
if (!zipFile.exists()){
|
260 |
fileExists = false;
|
261 |
} |
262 |
i++; |
263 |
} |
264 |
try{
|
265 |
ZipOutputStream out = new ZipOutputStream( |
266 |
new BufferedOutputStream( |
267 |
new FileOutputStream(zipFile))); |
268 |
byte[] data = new byte[BUFFER_SIZE]; |
269 |
FileInputStream fi = new FileInputStream(getOutputFile()); |
270 |
InputStream origin = new BufferedInputStream( fi, BUFFER_SIZE ); |
271 |
ZipEntry entry = new ZipEntry(getOutputFile().getName()); |
272 |
out.putNextEntry( entry ); |
273 |
int count;
|
274 |
while(( count = origin.read(data, 0, BUFFER_SIZE ) ) != -1 ){ |
275 |
out.write(data, 0, count);
|
276 |
} |
277 |
//Close the file
|
278 |
origin.close(); |
279 |
//Close the zip file
|
280 |
out.close(); |
281 |
//Change the file name and delete the first
|
282 |
zipFile.renameTo(getOutputFile()); |
283 |
new File(fileNameAux).delete(); |
284 |
} catch (FileNotFoundException e) { |
285 |
throw new GPEXmlToZipFileException(getFileName(),e); |
286 |
} catch (IOException e) { |
287 |
throw new GPEXmlToZipFileException(getFileName(),e); |
288 |
} |
289 |
} |
290 |
|
291 |
} |
292 |
|
293 |
|