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:
033: package com.vividsolutions.jump.workbench.driver;
034:
035: import java.io.File;
036:
037: import com.vividsolutions.jump.feature.FeatureCollection;
038: import com.vividsolutions.jump.io.CompressedFile;
039: import com.vividsolutions.jump.io.DriverProperties;
040: import com.vividsolutions.jump.io.GMLReader;
041: import com.vividsolutions.jump.workbench.model.Layer;
042: import com.vividsolutions.jump.workbench.model.LayerManager;
043: import com.vividsolutions.jump.workbench.ui.AbstractDriverPanel;
044: import com.vividsolutions.jump.workbench.ui.ErrorHandler;
045: import com.vividsolutions.jump.workbench.ui.GMLFileDriverPanel;
046: import com.vividsolutions.jump.workbench.ui.GUIUtil;
047: import com.vividsolutions.jump.workbench.ui.WorkbenchFileFilter;
048:
049: public class GMLFileInputDriver extends AbstractInputDriver {
050: private GMLFileDriverPanel panel;
051: private GMLReader reader = new GMLReader();
052:
053: public GMLFileInputDriver() {
054: }
055:
056: public void input(LayerManager layerManager, String categoryName)
057: throws Exception {
058: String extension_gml;
059: String extension_template;
060: File selectedFile = panel.getGMLFile();
061: String layerName = GUIUtil.nameWithoutExtension(selectedFile);
062: String fname = selectedFile.getAbsolutePath();
063:
064: DriverProperties dp = new DriverProperties();
065:
066: extension_gml = fname.substring(fname.length() - 3);
067: extension_template = panel.getTemplateFile().getAbsolutePath()
068: .substring(
069: panel.getTemplateFile().getAbsolutePath()
070: .length() - 3);
071:
072: if (extension_gml.equalsIgnoreCase("zip")) {
073: String internalName;
074:
075: dp.set("CompressedFile", fname);
076:
077: internalName = CompressedFile
078: .getInternalZipFnameByExtension(".gml", fname);
079:
080: if (internalName == null) {
081: internalName = CompressedFile
082: .getInternalZipFnameByExtension(".xml", fname);
083: }
084:
085: if (internalName == null) {
086: throw new Exception(
087: "Couldnt find a .xml or .gml file inside the .zip file: "
088: + fname);
089: }
090:
091: dp.set("File", internalName);
092: } else if (extension_gml.equalsIgnoreCase(".gz")) {
093: dp.set("CompressedFile", fname);
094: dp.set("File", fname); // not useed
095: } else {
096: dp.set("File", fname);
097: }
098:
099: if (extension_template.equalsIgnoreCase("zip")) {
100: String internalName;
101:
102: dp.set("CompressedFileTemplate", panel.getTemplateFile()
103: .getAbsolutePath());
104: internalName = CompressedFile
105: .getInternalZipFnameByExtension("_input.xml", panel
106: .getTemplateFile().getAbsolutePath());
107:
108: if (internalName == null) {
109: internalName = CompressedFile
110: .getInternalZipFnameByExtension(".input", panel
111: .getTemplateFile().getAbsolutePath());
112: }
113:
114: if (internalName == null) {
115: internalName = CompressedFile
116: .getInternalZipFnameByExtension(".template",
117: panel.getTemplateFile()
118: .getAbsolutePath());
119: }
120:
121: if (internalName == null) {
122: throw new Exception(
123: "Couldnt find a _input.xml, .input, or .template file inside the .zip file: "
124: + panel.getTemplateFile()
125: .getAbsolutePath());
126: }
127:
128: dp.set("TemplateFile", internalName);
129: } else if (extension_template.equalsIgnoreCase(".gz")) {
130: dp.set("CompressedFileTemplate", panel.getTemplateFile()
131: .getAbsolutePath());
132: dp.set("TemplateFile", panel.getTemplateFile()
133: .getAbsolutePath()); // not useed
134: } else {
135: dp.set("TemplateFile", panel.getTemplateFile()
136: .getAbsolutePath());
137: }
138:
139: FeatureCollection featureCollection = reader.read(dp);
140: Layer layer = layerManager.addLayer(categoryName, layerName,
141: featureCollection);
142: }
143:
144: public String toString() {
145: return "GML 2.0";
146: }
147:
148: public void initialize(DriverManager driverManager,
149: ErrorHandler errorHandler) {
150: super .initialize(driverManager, errorHandler);
151: panel = new GMLFileDriverPanel(errorHandler);
152: panel.setGMLFileMustExist(true);
153: panel.setTemplateFileDescription("JCS GML Input Template File");
154: panel.addPossibleTemplateExtension(".jit");
155: panel.addPossibleTemplateExtension("_input.xml");
156: panel.addPossibleTemplateExtension(".gz");
157: panel.addPossibleTemplateExtension(".zip");
158: }
159:
160: public AbstractDriverPanel getPanel() {
161: return panel;
162: }
163: }
|