Statistics
| Revision:

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

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