Statistics
| Revision:

root / trunk / libraries / libGPE-GML / src / org / gvsig / gpe / xml / writer / GPEXmlWriterHandler.java @ 11380

History | View | Annotate | Download (4.99 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 11380 2007-04-26 14:42:42Z jorpiell $
66
 * $Log$
67
 * Revision 1.7  2007-04-26 14:40:03  jorpiell
68
 * Some writer handler methods updated
69
 *
70
 * Revision 1.6  2007/04/19 07:25:49  jorpiell
71
 * Add the add methods to teh contenhandler and change the register mode
72
 *
73
 * Revision 1.5  2007/04/17 10:30:11  jorpiell
74
 * Add a method to compress a file
75
 *
76
 * Revision 1.4  2007/04/14 16:07:30  jorpiell
77
 * The writer has been created
78
 *
79
 * Revision 1.3  2007/04/13 07:17:57  jorpiell
80
 * Add the writting tests for the simple geometries
81
 *
82
 * Revision 1.2  2007/04/12 17:06:44  jorpiell
83
 * First GML writing tests
84
 *
85
 * Revision 1.1  2007/04/12 10:23:41  jorpiell
86
 * Add some writers and the GPEXml parser
87
 *
88
 *
89
 */
90
/**
91
 * @author Jorge Piera LLodr? (jorge.piera@iver.es)
92
 */
93
public abstract class GPEXmlWriterHandler extends GPEWriterHandler {
94
        protected Writer writer = null;
95
        private String targetNamespace = null;
96
        
97
        public GPEXmlWriterHandler() {
98
                
99
        }        
100
        
101
        /**
102
         * Writes the XML header
103
         * @throws IOException
104
         */
105
        protected void initXmlHeader() throws IOException{
106
                writer = createWriter();
107
                writer.write("<?xml version=\"");
108
                writer.write(GPEDefaults.getStringProperty(GPEDefaults.XML_VERSION).toString());
109
                writer.write("\" encoding=\"");
110
                writer.write(GPEDefaults.getStringProperty(GPEDefaults.XML_ENCODING).toString());
111
                writer.write("\"?>");
112
        }
113
        
114
        /**
115
         * Creates an input stream from a file. 
116
         * @param file
117
         * @return
118
         * @throws FileNotFoundException 
119
         * @throws FileNotFoundException 
120
         */
121
        protected abstract Writer createWriter() throws IOException;
122
        
123
        /**
124
         * Returns the selected target namespace. If
125
         * the namespace doesn't exists it returns the 
126
         * default Namespace (cit); 
127
         * @return the namespace
128
         */
129
        public String getNamespacePrefix() {
130
                if (targetNamespace == null){
131
                        String prefix = GPEDefaults.getStringProperty(GPEDefaults.NAMESPACE_PREFIX);
132
                        if (prefix != null){
133
                                targetNamespace = prefix + ":";
134
                        }else{
135
                                targetNamespace = "";
136
                        }
137
                }
138
                return targetNamespace;
139
        }
140
        
141
        /**
142
         * This method compress a file using the ZIP
143
         * compression
144
         */
145
        protected void compressFile(){
146
                int BUFFER_SIZE = 1024;
147
                int i = 0;
148
                boolean fileExists = true;
149
                File zipFile = null;
150
                String fileNameAux = null;
151
                while (fileExists){
152
                        fileNameAux = getOutputFile().getAbsolutePath() + i;
153
                        zipFile = new File(fileNameAux);
154
                        if (!zipFile.exists()){
155
                                fileExists = false;
156
                        }
157
                        i++;
158
                }
159
                try {
160
                        ZipOutputStream out = new ZipOutputStream(
161
                                        new BufferedOutputStream(
162
                                                        new FileOutputStream(zipFile)));
163
                        byte[] data = new byte[BUFFER_SIZE];
164
                        FileInputStream fi = new FileInputStream(getOutputFile());
165
                        InputStream origin = new BufferedInputStream( fi, BUFFER_SIZE );
166
                        ZipEntry entry = new ZipEntry(getOutputFile().getName());
167
                        out.putNextEntry( entry );
168
                        int count;
169
                        while(( count = origin.read(data, 0, BUFFER_SIZE ) ) != -1 ){
170
                                out.write(data, 0, count);
171
                        }
172
                        //Close the file
173
                        origin.close();
174
                        //Close the zip file
175
                        out.close();        
176
                        //Change the file name and delete the first
177
                        zipFile.renameTo(getOutputFile());
178
                        new File(fileNameAux).delete();
179
                } catch (FileNotFoundException e) {
180
                        //Never throwed;
181
                } catch (IOException e) {
182
                        // TODO Auto-generated catch block
183
                        e.printStackTrace();
184
                } 
185
        }
186
        
187
}
188
        
189