001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2003-2006, GeoTools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.gml.producer;
017:
018: import java.util.logging.Logger;
019:
020: import com.vividsolutions.jts.geom.Geometry;
021: import com.vividsolutions.jts.geom.GeometryCollection;
022: import com.vividsolutions.jts.geom.LineString;
023: import com.vividsolutions.jts.geom.MultiLineString;
024: import com.vividsolutions.jts.geom.MultiPoint;
025: import com.vividsolutions.jts.geom.MultiPolygon;
026: import com.vividsolutions.jts.geom.Point;
027: import com.vividsolutions.jts.geom.Polygon;
028:
029: //import org.geotools.feature.*;
030:
031: /*
032: * Utilities for gml and xml;
033: * @author Chris Holmes, TOPP
034: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/main/java/org/geotools/gml/producer/GMLUtils.java $
035: */
036: final class GMLUtils {
037: /** The logger for the filter module. */
038: private static final Logger LOGGER = org.geotools.util.logging.Logging
039: .getLogger("org.geotools.gml.producer");
040:
041: /** Internal representation of URL used to represent GML */
042: public static final String GML_URL = "http://www.opengis.net/gml";
043:
044: /** Internal representation of OGC SF Point */
045: protected static final int POINT = 1;
046:
047: /** Internal representation of OGC SF LineString */
048: protected static final int LINESTRING = 2;
049:
050: /** Internal representation of OGC SF Polygon */
051: protected static final int POLYGON = 3;
052:
053: /** Internal representation of OGC SF MultiPoint */
054: protected static final int MULTIPOINT = 4;
055:
056: /** Internal representation of OGC SF MultiLineString */
057: protected static final int MULTILINESTRING = 5;
058:
059: /** Internal representation of OGC SF MultiPolygon */
060: protected static final int MULTIPOLYGON = 6;
061:
062: /** Internal representation of OGC SF MultiGeometry */
063: protected static final int MULTIGEOMETRY = 7;
064:
065: /**
066: * private constructor so default is not used.
067: */
068: private GMLUtils() {
069: }
070:
071: /**
072: * Gets the String representation for the given Geometry
073: *
074: * @param geometry a Geometry
075: *
076: * @return String representation of geometry
077: */
078: public static String getGeometryName(Geometry geometry) {
079: LOGGER.entering("GMLUtils", "getGeometryName", geometry);
080:
081: Class geomClass = geometry.getClass();
082: String returnValue = null;
083:
084: if (geomClass.equals(Point.class)) {
085: returnValue = "Point";
086: } else if (geomClass.equals(LineString.class)) {
087: returnValue = "LineString";
088: } else if (geomClass.equals(Polygon.class)) {
089: returnValue = "Polygon";
090: } else if (geomClass.equals(MultiPoint.class)) {
091: returnValue = "MultiPoint";
092: } else if (geomClass.equals(MultiLineString.class)) {
093: returnValue = "MultiLineString";
094: } else if (geomClass.equals(MultiPolygon.class)) {
095: returnValue = "MultiPolygon";
096: } else if (geomClass.equals(GeometryCollection.class)) {
097: returnValue = "GeometryCollection";
098: } else {
099: //HACK!!! throw exception
100: returnValue = null;
101: }
102:
103: LOGGER.exiting("GMLUtils", "getGeometryName", returnValue);
104:
105: return returnValue;
106: }
107:
108: /**
109: * Gets the internal representation for the given Geometry
110: *
111: * @param geometry a Geometry
112: *
113: * @return int representation of Geometry
114: */
115: public static int getGeometryType(Geometry geometry) {
116: //LOGGER.entering("GMLUtils", "getGeometryType", geometry);
117:
118: Class geomClass = geometry.getClass();
119: int returnValue = -1;
120:
121: if (geomClass.equals(Point.class)) {
122: //LOGGER.finest("found point");
123: returnValue = POINT;
124: } else if (geomClass.equals(LineString.class)) {
125: //LOGGER.finest("found linestring");
126: returnValue = LINESTRING;
127: } else if (geomClass.equals(Polygon.class)) {
128: //LOGGER.finest("found polygon");
129: returnValue = POLYGON;
130: } else if (geomClass.equals(MultiPoint.class)) {
131: //LOGGER.finest("found multiPoint");
132: returnValue = MULTIPOINT;
133: } else if (geomClass.equals(MultiLineString.class)) {
134: returnValue = MULTILINESTRING;
135: } else if (geomClass.equals(MultiPolygon.class)) {
136: returnValue = MULTIPOLYGON;
137: } else if (geomClass.equals(GeometryCollection.class)) {
138: returnValue = MULTIGEOMETRY;
139: } else {
140: returnValue = -1;
141:
142: //HACK!!! throw exception.
143: }
144:
145: //LOGGER.exiting("GMLUtils", "getGeometryType", new Integer(returnValue));
146:
147: return returnValue;
148: }
149:
150: public static String getMemberName(int geometryType) {
151: //String member;
152:
153: switch (geometryType) {
154: case GMLUtils.MULTIPOINT:
155: return "pointMember";
156:
157: case GMLUtils.MULTILINESTRING:
158: return "lineStringMember";
159:
160: case GMLUtils.MULTIPOLYGON:
161: return "polygonMember";
162:
163: default:
164: return "geometryMember";
165: }
166: }
167:
168: /**
169: * Parses the passed string, and encodes the special characters (used in
170: * xml for special purposes) with the appropriate codes. e.g. right
171: * bracket char is changed to '<'
172: *
173: * @param inData the string to encode.
174: *
175: * @return the encoded string. Returns null, if null is passed as argument
176: *
177: * @task TODO: Take output as a param, write directly to out, send the
178: * characters straight out, doing translation on the fly.
179: */
180: public static String encodeXML(String inData) {
181: //LOGGER.entering("GMLUtils", "encodeXML", inData);
182:
183: //return null, if null is passed as argument
184: if (inData == null) {
185: return null;
186: }
187:
188: //if no special characters, just return
189: //(for optimization. Though may be an overhead, but for most of the
190: //strings, this will save time)
191: if ((inData.indexOf('&') == -1) && (inData.indexOf('<') == -1)
192: && (inData.indexOf('>') == -1)
193: && (inData.indexOf('\'') == -1)
194: && (inData.indexOf('\"') == -1)) {
195: return inData;
196: }
197:
198: //get the length of input String
199: int length = inData.length();
200:
201: //create a StringBuffer of double the size (size is just for guidance
202: //so as to reduce increase-capacity operations. The actual size of
203: //the resulting string may be even greater than we specified, but is
204: //extremely rare)
205: StringBuffer buffer = new StringBuffer(2 * length);
206:
207: char charToCompare;
208:
209: //iterate over the input String
210: for (int i = 0; i < length; i++) {
211: charToCompare = inData.charAt(i);
212:
213: //if the ith character is special character, replace by code
214: if (charToCompare == '&') {
215: buffer.append("&");
216: } else if (charToCompare == '<') {
217: buffer.append("<");
218: } else if (charToCompare == '>') {
219: buffer.append(">");
220: } else if (charToCompare == '\"') {
221: buffer.append(""");
222: } else if (charToCompare == '\'') {
223: buffer.append("'");
224: } else {
225: buffer.append(charToCompare);
226: }
227: }
228:
229: //LOGGER.exiting("GMLUtils", "encodeXML", buffer.toString());
230:
231: //return the encoded string
232: return buffer.toString();
233: }
234: }
|