root / trunk / libraries / libGPE-GML / src / org / gvsig / gpe / xml / writer / GPEXmlWriterHandler.java @ 11247
History | View | Annotate | Download (4.88 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.FileWriter; |
10 |
import java.io.IOException; |
11 |
import java.io.InputStream; |
12 |
import java.io.OutputStream; |
13 |
import java.util.zip.ZipEntry; |
14 |
import java.util.zip.ZipOutputStream; |
15 |
|
16 |
import org.gvsig.gpe.xml.writer.Writer; |
17 |
|
18 |
import org.apache.xml.utils.NameSpace; |
19 |
import org.gvsig.gpe.GPEDefaults; |
20 |
import org.gvsig.gpe.GPEErrorHandler; |
21 |
import org.gvsig.gpe.writers.GPEWriterHandler; |
22 |
|
23 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
24 |
*
|
25 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
26 |
*
|
27 |
* This program is free software; you can redistribute it and/or
|
28 |
* modify it under the terms of the GNU General Public License
|
29 |
* as published by the Free Software Foundation; either version 2
|
30 |
* of the License, or (at your option) any later version.
|
31 |
*
|
32 |
* This program is distributed in the hope that it will be useful,
|
33 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
34 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
35 |
* GNU General Public License for more details.
|
36 |
*
|
37 |
* You should have received a copy of the GNU General Public License
|
38 |
* along with this program; if not, write to the Free Software
|
39 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
40 |
*
|
41 |
* For more information, contact:
|
42 |
*
|
43 |
* Generalitat Valenciana
|
44 |
* Conselleria d'Infraestructures i Transport
|
45 |
* Av. Blasco Ib??ez, 50
|
46 |
* 46010 VALENCIA
|
47 |
* SPAIN
|
48 |
*
|
49 |
* +34 963862235
|
50 |
* gvsig@gva.es
|
51 |
* www.gvsig.gva.es
|
52 |
*
|
53 |
* or
|
54 |
*
|
55 |
* IVER T.I. S.A
|
56 |
* Salamanca 50
|
57 |
* 46005 Valencia
|
58 |
* Spain
|
59 |
*
|
60 |
* +34 963163400
|
61 |
* dac@iver.es
|
62 |
*/
|
63 |
/* CVS MESSAGES:
|
64 |
*
|
65 |
* $Id: GPEXmlWriterHandler.java 11247 2007-04-19 07:30:40Z jorpiell $
|
66 |
* $Log$
|
67 |
* Revision 1.6 2007-04-19 07:25:49 jorpiell
|
68 |
* Add the add methods to teh contenhandler and change the register mode
|
69 |
*
|
70 |
* Revision 1.5 2007/04/17 10:30:11 jorpiell
|
71 |
* Add a method to compress a file
|
72 |
*
|
73 |
* Revision 1.4 2007/04/14 16:07:30 jorpiell
|
74 |
* The writer has been created
|
75 |
*
|
76 |
* Revision 1.3 2007/04/13 07:17:57 jorpiell
|
77 |
* Add the writting tests for the simple geometries
|
78 |
*
|
79 |
* Revision 1.2 2007/04/12 17:06:44 jorpiell
|
80 |
* First GML writing tests
|
81 |
*
|
82 |
* Revision 1.1 2007/04/12 10:23:41 jorpiell
|
83 |
* Add some writers and the GPEXml parser
|
84 |
*
|
85 |
*
|
86 |
*/
|
87 |
/**
|
88 |
* @author Jorge Piera LLodr? (jorge.piera@iver.es)
|
89 |
*/
|
90 |
public abstract class GPEXmlWriterHandler extends GPEWriterHandler { |
91 |
protected Writer writer = null; |
92 |
private String targetNamespace = null; |
93 |
|
94 |
public GPEXmlWriterHandler() {
|
95 |
|
96 |
} |
97 |
|
98 |
/**
|
99 |
* Writes the XML header
|
100 |
* @throws IOException
|
101 |
*/
|
102 |
protected void initXmlHeader() throws IOException{ |
103 |
writer = createWriter(); |
104 |
writer.write("<?xml version=\"");
|
105 |
writer.write(GPEDefaults.getProperty(GPEDefaults.XML_VERSION).toString()); |
106 |
writer.write("\" encoding=\"");
|
107 |
writer.write(GPEDefaults.getProperty(GPEDefaults.XML_ENCODING).toString()); |
108 |
writer.write("\"?>");
|
109 |
} |
110 |
|
111 |
/**
|
112 |
* Creates an input stream from a file.
|
113 |
* @param file
|
114 |
* @return
|
115 |
* @throws FileNotFoundException
|
116 |
* @throws FileNotFoundException
|
117 |
*/
|
118 |
protected abstract Writer createWriter() throws IOException; |
119 |
|
120 |
/**
|
121 |
* Returns the selected target namespace. If
|
122 |
* the namespace doesn't exists it returns the
|
123 |
* default Namespace (cit);
|
124 |
* @return the namespace
|
125 |
*/
|
126 |
public String getNamespacePrefix() { |
127 |
if (targetNamespace == null){ |
128 |
String prefix = GPEDefaults.getProperty(GPEDefaults.NAMESPACE_PREFIX);
|
129 |
if (prefix != null){ |
130 |
targetNamespace = prefix + ":";
|
131 |
}else{
|
132 |
targetNamespace = "";
|
133 |
} |
134 |
} |
135 |
return targetNamespace;
|
136 |
} |
137 |
|
138 |
/**
|
139 |
* This method compress a file using the ZIP
|
140 |
* compression
|
141 |
*/
|
142 |
protected void compressFile(){ |
143 |
int BUFFER_SIZE = 1024; |
144 |
int i = 0; |
145 |
boolean fileExists = true; |
146 |
File zipFile = null; |
147 |
String fileNameAux = null; |
148 |
while (fileExists){
|
149 |
fileNameAux = getOutputFile().getAbsolutePath() + i; |
150 |
zipFile = new File(fileNameAux); |
151 |
if (!zipFile.exists()){
|
152 |
fileExists = false;
|
153 |
} |
154 |
i++; |
155 |
} |
156 |
try {
|
157 |
ZipOutputStream out = new ZipOutputStream( |
158 |
new BufferedOutputStream( |
159 |
new FileOutputStream(zipFile))); |
160 |
byte[] data = new byte[BUFFER_SIZE]; |
161 |
FileInputStream fi = new FileInputStream(getOutputFile()); |
162 |
InputStream origin = new BufferedInputStream( fi, BUFFER_SIZE ); |
163 |
ZipEntry entry = new ZipEntry(getOutputFile().getName()); |
164 |
out.putNextEntry( entry ); |
165 |
int count;
|
166 |
while(( count = origin.read(data, 0, BUFFER_SIZE ) ) != -1 ){ |
167 |
out.write(data, 0, count);
|
168 |
} |
169 |
//Close the file
|
170 |
origin.close(); |
171 |
//Close the zip file
|
172 |
out.close(); |
173 |
//Change the file name and delete the first
|
174 |
zipFile.renameTo(getOutputFile()); |
175 |
new File(fileNameAux).delete(); |
176 |
} catch (FileNotFoundException e) { |
177 |
//Never throwed;
|
178 |
} catch (IOException e) { |
179 |
// TODO Auto-generated catch block
|
180 |
e.printStackTrace(); |
181 |
} |
182 |
} |
183 |
|
184 |
} |
185 |
|
186 |
|