001: /*
002: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
003: * for visualizing and manipulating spatial features with geometry and attributes.
004: *
005: * Copyright (C) 2003 Vivid Solutions
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: *
021: * For more information, contact:
022: *
023: * Vivid Solutions
024: * Suite #1A
025: * 2328 Government Street
026: * Victoria BC V8T 5G5
027: * Canada
028: *
029: * (250)385-6040
030: * www.vividsolutions.com
031: */
032: package com.vividsolutions.jump.io.datasource;
033:
034: import java.util.Arrays;
035: import java.util.Collection;
036:
037: import com.vividsolutions.jump.feature.FeatureCollection;
038: import com.vividsolutions.jump.io.*;
039: import com.vividsolutions.jump.util.Block;
040: import com.vividsolutions.jump.util.CollectionUtil;
041:
042: /**
043: * Contains DataSource classes for the standard JUMP Readers and
044: * Writers. DataSource implementations cannot be anonymous classes if they are
045: * to be saved to a project file (because the class name is saved).
046: */
047: public abstract class StandardReaderWriterFileDataSource extends
048: ReaderWriterFileDataSource {
049:
050: protected String[] extensions;
051:
052: public static final String[] GML_EXTENSIONS = new String[] { "gml",
053: "xml" };
054:
055: public static final String OUTPUT_TEMPLATE_FILE_KEY = "Output Template File";
056:
057: public static final String INPUT_TEMPLATE_FILE_KEY = "Input Template File";
058:
059: public StandardReaderWriterFileDataSource(JUMPReader reader,
060: JUMPWriter writer, String[] extensions) {
061: super (reader, writer);
062: this .extensions = extensions;
063: }
064:
065: /**
066: * The first JUMP Readers took responsibility for handling .zip and
067: * .gz files (a more modular design choice would have been to handle
068: * compression outside of the Readers); this class uses a
069: * DelegatingCompressedFileHandler to ensure that these JUMP Readers
070: * receive the properties they need to do decompression.
071: */
072: private static class ClassicReaderWriterFileDataSource extends
073: StandardReaderWriterFileDataSource {
074: public ClassicReaderWriterFileDataSource(JUMPReader reader,
075: JUMPWriter writer, String[] extensions) {
076: super (new DelegatingCompressedFileHandler(reader,
077: toEndings(extensions)), writer, extensions);
078: this .extensions = extensions;
079: }
080: }
081:
082: //DataSources must have a parameterless constructor so they can be
083: //reconstructed by Java2XML. [Jon Aquino]
084:
085: public String[] getExtensions() {
086: return extensions;
087: }
088:
089: private static GMLWriter createGMLWriter() {
090: return new GMLWriter();
091: }
092:
093: private static DelegatingCompressedFileHandler createGMLReader() {
094: return new DelegatingCompressedFileHandler(
095: new GMLReader(),
096: toEndings(StandardReaderWriterFileDataSource.GML_EXTENSIONS)) {
097: public FeatureCollection read(DriverProperties dp)
098: throws Exception {
099: mangle(dp, "TemplateFile", "CompressedFileTemplate",
100: Arrays.asList(new String[] { "_input.xml",
101: ".input", ".template" }));
102: return super .read(dp);
103: }
104:
105: };
106: }
107:
108: public static Collection toEndings(String[] extensions) {
109: return CollectionUtil.collect(Arrays.asList(extensions),
110: new Block() {
111: public Object yield(Object extension) {
112: return "." + extension;
113: }
114: });
115: }
116:
117: public static class JML extends ClassicReaderWriterFileDataSource {
118: public JML() {
119: super (new JMLReader(), new JMLWriter(),
120: new String[] { "jml" });
121: }
122: }
123:
124: public static class WKT extends ClassicReaderWriterFileDataSource {
125: public WKT() {
126: super (new WKTReader(), new WKTWriter(), new String[] {
127: "wkt", "txt" });
128: }
129: }
130:
131: public static class Shapefile extends
132: ClassicReaderWriterFileDataSource {
133: public Shapefile() {
134: super (new ShapefileReader(), new ShapefileWriter(),
135: new String[] { "shp" });
136: }
137: }
138:
139: public static class FMEGML extends
140: ClassicReaderWriterFileDataSource {
141: public FMEGML() {
142: super (new FMEGMLReader(), new FMEGMLWriter(), new String[] {
143: "gml", "xml", "fme" });
144: }
145: }
146:
147: public static class GML extends ClassicReaderWriterFileDataSource {
148: public GML() {
149: super (createGMLReader(), createGMLWriter(),
150: StandardReaderWriterFileDataSource.GML_EXTENSIONS);
151: }
152:
153: protected DriverProperties getReaderDriverProperties() {
154: return super
155: .getReaderDriverProperties()
156: .set(
157: "TemplateFile",
158: (String) getProperties()
159: .get(
160: StandardReaderWriterFileDataSource.INPUT_TEMPLATE_FILE_KEY));
161: }
162:
163: protected DriverProperties getWriterDriverProperties() {
164: return super
165: .getWriterDriverProperties()
166: .set(
167: "TemplateFile",
168: (String) getProperties()
169: .get(
170: StandardReaderWriterFileDataSource.OUTPUT_TEMPLATE_FILE_KEY));
171: }
172:
173: public boolean isReadable() {
174: return getProperties()
175: .containsKey(
176: StandardReaderWriterFileDataSource.INPUT_TEMPLATE_FILE_KEY);
177: }
178:
179: public boolean isWritable() {
180: return getProperties()
181: .containsKey(
182: StandardReaderWriterFileDataSource.OUTPUT_TEMPLATE_FILE_KEY);
183: }
184:
185: }
186:
187: }
|