001: /*
002: * FMEGMLWriter.java
003: *
004: * Created on June 18, 2002, 1:59 PM
005: */
006: /*
007: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
008: * for visualizing and manipulating spatial features with geometry and attributes.
009: *
010: * Copyright (C) 2003 Vivid Solutions
011: *
012: * This program is free software; you can redistribute it and/or
013: * modify it under the terms of the GNU General Public License
014: * as published by the Free Software Foundation; either version 2
015: * of the License, or (at your option) any later version.
016: *
017: * This program is distributed in the hope that it will be useful,
018: * but WITHOUT ANY WARRANTY; without even the implied warranty of
019: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: * GNU General Public License for more details.
021: *
022: * You should have received a copy of the GNU General Public License
023: * along with this program; if not, write to the Free Software
024: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
025: *
026: * For more information, contact:
027: *
028: * Vivid Solutions
029: * Suite #1A
030: * 2328 Government Street
031: * Victoria BC V8T 5G5
032: * Canada
033: *
034: * (250)385-6040
035: * www.vividsolutions.com
036: */
037: package com.vividsolutions.jump.io;
038:
039: import java.text.SimpleDateFormat;
040: import java.util.Date;
041:
042: import com.vividsolutions.jump.feature.*;
043:
044: /**
045: * This class is a {@link JUMPWriter} specialized to write FMEGML.
046: *
047: * <p>
048: * DataProperties for the JCSWriter write(featureSchema,DataProperties)
049: * interface:
050: * </p>
051: *
052: * <table border='1' cellspacing='0' cellpadding='4'>
053: * <tr>
054: * <th>Parameter</th>
055: * <th>Meaning</th>
056: * </tr>
057: * <tr>
058: * <td>OutputFile or DefaultValue</td>
059: * <td>File name for output .xml file</td>
060: * </tr>
061: * <tr>
062: * <td>FMEFormatVersion</td>
063: * <td>'2000' or '2001'</td>
064: * </tr>
065: * </table>
066: * <br>
067: * </p>
068:
069: * <p>
070: * The format version specifies which version of FME GML this
071: * should produce.
072: * </p>
073: *
074: * <table border='1' cellspacing='0' cellpadding='4'>
075: * <tr>
076: * <td> 2000 </td>
077: * <td><pre>
078: <dataset
079: xmlns="http://www.safe.com/xml/namespaces/fmegml2"
080: xmlns:fme="http://www.safe.com/xml/namespaces/fmegml2"
081: xmlns:gml="http://www.opengis.net/gml"
082: xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
083: xsi:schemaLocation="http://www.safe.com/xml/schemas/fmegml2.xsd" >
084: </pre>
085: </td>
086: </tr>
087: * <tr>
088: * <td> 2001 </td>
089: <td><pre>
090: <dataset
091: xmlns="http://www.safe.com/xml/schemas/FMEFeatures"
092: xmlns:fme="http://www.safe.com/xml/schemas/FMEFeatures"
093: xmlns:gml="http://www.opengis.net/gml"
094: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
095: xsi:schemaLocation="http://www.safe.com/xml/schemas/FMEFeatures.xsd" >
096: </pre>
097: </td>
098: </tr>
099: * </table>
100: *
101: */
102: public class FMEGMLWriter implements JUMPWriter {
103: int outputFormatType = 1; // 0 = 2000, 1 = 2001, others to come
104:
105: /** Creates new FMEGMLWriter */
106: public FMEGMLWriter() {
107: }
108:
109: /**
110: * Cause a featureCollection to be written using the outputfile (and format) specified in the dp parameter.<br>
111: * A GMLOutputTemplate will be autogenerated, then the write request passed off to the {@link GMLWriter}.
112: *@param featureCollection set of features to be written
113: *@param dp where to write and format
114: */
115: public void write(FeatureCollection featureCollection,
116: DriverProperties dp) throws IllegalParametersException,
117: Exception {
118: GMLOutputTemplate gmlTemplate;
119: GMLWriter gmlWriter;
120: java.io.BufferedWriter w;
121: String outputfname;
122:
123: outputfname = dp.getProperty("File");
124:
125: if (outputfname == null) {
126: outputfname = dp.getProperty("DefaultValue");
127: }
128:
129: if (outputfname == null) {
130: throw new IllegalParametersException(
131: "call to FMEGMLWriter.write() has DataProperties w/o a OutputFile specified");
132: }
133:
134: if (dp.getProperty("FMEFormatVersion") != null) {
135: if (dp.getProperty("FMEFormatVersion").equals("2000")) {
136: outputFormatType = 0;
137: }
138:
139: if (dp.getProperty("FMEFormatVersion").equals("2001")) {
140: outputFormatType = 1;
141: }
142: }
143:
144: gmlTemplate = this .createOutputTemplate(featureCollection
145: .getFeatureSchema());
146: gmlWriter = new GMLWriter() {
147: protected String format(Date date) {
148: return fmeDateFormatter.format(date);
149: }
150: };
151: gmlWriter.setOutputTemplate(gmlTemplate);
152:
153: w = new java.io.BufferedWriter(new java.io.FileWriter(
154: outputfname));
155: gmlWriter.write(featureCollection, w);
156: w.close();
157: }
158:
159: private SimpleDateFormat fmeDateFormatter = new SimpleDateFormat(
160: "yyyyMMdd");
161:
162: /**
163: *Makes a {@link GMLOutputTemplate} from a featureSchema. Has two different version ('2000' and '2001'). <br>
164: *@param fs description of the column in the dataset.
165: **/
166: public GMLOutputTemplate createOutputTemplate(FeatureSchema fs)
167: throws ParseException, Exception {
168: GMLOutputTemplate result;
169: String templateText;
170: String column;
171: int t;
172: String colName;
173: String colType;
174:
175: templateText = "";
176:
177: switch (outputFormatType) {
178: case 0:
179: templateText = "<?xml version='1.0' encoding='UTF-8'?>\n<dataset xmlns=\"http://www.safe.com/xml/namespaces/fmegml2\" xmlns:fme=\"http://www.safe.com/xml/namespaces/fmegml2\" xmlns:gml=\"http://www.opengis.net/gml\" xmlns:xsi=\"http://www.w3.org/2000/10/XMLSchema-instance\" xsi:schemaLocation=\"http://www.safe.com/xml/schemas/fmegml2.xsd\">\n";
180:
181: break;
182:
183: case 1:
184: templateText = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<dataset xmlns=\"http://www.safe.com/xml/schemas/FMEFeatures\" xmlns:fme=\"http://www.safe.com/xml/schemas/FMEFeatures\" xmlns:gml=\"http://www.opengis.net/gml\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.safe.com/xml/schemas/FMEFeatures FMEFeatures.xsd\">\n";
185:
186: break;
187: }
188:
189: templateText = templateText
190: + "<schemaFeatures>\n<gml:featureMember>\n<Feature>\n<featureType>JCSOutput</featureType>\n";
191:
192: for (t = 0; t < fs.getAttributeCount(); t++) {
193: AttributeType attributeType;
194:
195: attributeType = fs.getAttributeType(t);
196:
197: if (t != fs.getGeometryIndex()) {
198: try {
199: colName = fs.getAttributeName(t);
200: colType = JCSattributeType2FMEtype(attributeType
201: .toString());
202: column = "";
203:
204: switch (outputFormatType) {
205: case 0:
206: column = "<property fme:name=\"" + colName
207: + "\">" + colType + "</property>\n";
208:
209: break;
210:
211: case 1:
212: column = "<property name=\"" + colName + "\">"
213: + colType + "</property>\n";
214:
215: break;
216: }
217:
218: // column = "<property fme:name=\""+colName+"\">"+colType+"</property>\n";
219: templateText = templateText + column;
220: } catch (Exception e) {
221: //do nothing - just dont export that column
222: }
223: }
224: }
225:
226: templateText = templateText
227: + "</Feature>\n</gml:featureMember>\n</schemaFeatures>\n<dataFeatures>\n";
228: templateText = templateText
229: + "<% FEATURE %>\n<gml:featureMember>\n<Feature>\n<featureType>JCSOutput</featureType>\n";
230:
231: for (t = 0; t < fs.getAttributeCount(); t++) {
232: colName = fs.getAttributeName(t);
233:
234: if (t != fs.getGeometryIndex()) {
235: //not geometry
236: switch (outputFormatType) {
237: case 0:
238: templateText = templateText
239: + "<property fme:name=\"" + colName + "\">";
240:
241: break;
242:
243: case 1:
244: templateText = templateText + "<property name=\""
245: + colName + "\">";
246:
247: break;
248: }
249:
250: // templateText = templateText +"<property fme:name=\""+colName+"\">";
251: templateText = templateText + "<%=COLUMN " + colName
252: + "%></property>\n";
253: } else {
254: //geometry
255: switch (outputFormatType) {
256: case 0:
257: templateText = templateText
258: + "<property fme:name=\"gml2_coordsys\"></property>\n";
259:
260: break;
261:
262: case 1:
263: templateText = templateText
264: + "<property name=\"gml2_coordsys\"></property>\n";
265:
266: break;
267: }
268:
269: // templateText = templateText +"<property fme:name=\"gml2_coordsys\"></property>\n";
270: templateText = templateText
271: + "<gml:<%=GEOMETRYTYPE%>Property>\n<%=GEOMETRY %>\n</gml:<%=GEOMETRYTYPE%>Property>\n";
272: }
273: }
274:
275: templateText = templateText
276: + "</Feature>\n</gml:featureMember>\n<% ENDFEATURE %>\n</dataFeatures>\n</dataset>\n";
277:
278: java.io.StringReader stringreader = new java.io.StringReader(
279: templateText);
280:
281: result = new GMLOutputTemplate();
282: result.load(stringreader,
283: "Auto Generated FME GML input template");
284: stringreader.close();
285:
286: return result;
287: }
288:
289: /**
290: * Convert a JCS column type to FME type (two different versions). <br>
291: * ie. STRING -> 'fme_char(1024)'
292: *
293: *@param jcsType JCS column type (ie. 'STRING','DOUBLE', or 'INTEGER'
294: */
295: String JCSattributeType2FMEtype(String jcsType)
296: throws ParseException {
297: switch (outputFormatType) {
298: case 0:
299:
300: if (jcsType.equalsIgnoreCase("STRING")) {
301: return "fme_char(1024)";
302: }
303:
304: if (jcsType.equalsIgnoreCase("INTEGER")) {
305: return "long";
306: }
307:
308: if (jcsType.equalsIgnoreCase("DOUBLE")) {
309: return "fme_decimal(15,15)";
310: }
311:
312: if (jcsType.equalsIgnoreCase("DATE")) {
313: //There is no FME GML "date" type. [Jon Aquino]
314: return "string";
315: }
316:
317: throw new ParseException("couldn't convert JCS type '"
318: + jcsType + "' to a FME type.");
319:
320: case 1:
321:
322: if (jcsType.equalsIgnoreCase("STRING")) {
323: return "string";
324: }
325:
326: if (jcsType.equalsIgnoreCase("INTEGER")) {
327: return "long";
328: }
329:
330: if (jcsType.equalsIgnoreCase("DOUBLE")) {
331: return "long"; //strange but true
332: }
333:
334: if (jcsType.equalsIgnoreCase("DATE")) {
335: //There is no FME GML "date" type. [Jon Aquino]
336: return "string";
337: }
338:
339: throw new ParseException("couldn't convert JCS type '"
340: + jcsType + "' to a FME type.");
341: }
342:
343: throw new ParseException("couldn't convert JCS type '"
344: + jcsType + "' to a FME type.");
345: }
346: }
|