001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-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.gml2.bindings;
017:
018: import org.w3c.dom.Document;
019: import org.w3c.dom.Element;
020: import org.w3c.dom.Node;
021: import java.net.URI;
022: import java.util.Date;
023: import javax.xml.namespace.QName;
024: import com.vividsolutions.jts.geom.Coordinate;
025: import com.vividsolutions.jts.geom.CoordinateSequence;
026: import com.vividsolutions.jts.geom.GeometryFactory;
027: import com.vividsolutions.jts.geom.LineString;
028: import com.vividsolutions.jts.geom.LinearRing;
029: import com.vividsolutions.jts.geom.MultiLineString;
030: import com.vividsolutions.jts.geom.MultiPoint;
031: import com.vividsolutions.jts.geom.MultiPolygon;
032: import com.vividsolutions.jts.geom.Point;
033: import com.vividsolutions.jts.geom.Polygon;
034: import com.vividsolutions.jts.geom.impl.CoordinateArraySequence;
035: import org.geotools.feature.AttributeTypeFactory;
036: import org.geotools.feature.DefaultFeatureTypeFactory;
037: import org.geotools.feature.Feature;
038: import org.geotools.feature.FeatureType;
039: import org.geotools.feature.FeatureTypeBuilder;
040: import org.geotools.gml2.TEST;
041:
042: /**
043: * Collection of static methods for creating mock data for binding unit tests.
044: *
045: * @author Justin Deoliveira, The Open Planning Project
046: *
047: */
048: public class GML2MockData {
049: /** factory used to create geometries */
050: static GeometryFactory gf = new GeometryFactory();
051:
052: //
053: //Geometries
054: //
055: static Coordinate coordinate() {
056: return new Coordinate(1, 2);
057: }
058:
059: static Element coordinate(Document document, Node parent) {
060: Element coord = element(GML.coord, document, parent);
061:
062: Element x = element(new QName(GML.NAMESPACE, "X"), document,
063: coord);
064: x.appendChild(document.createTextNode("1.0"));
065:
066: Element y = element(new QName(GML.NAMESPACE, "Y"), document,
067: coord);
068: y.appendChild(document.createTextNode("2.0"));
069:
070: return coord;
071: }
072:
073: static CoordinateSequence coordinates() {
074: return new CoordinateArraySequence(new Coordinate[] {
075: new Coordinate(1, 2), new Coordinate(3, 4) });
076: }
077:
078: static Element coordinates(Document document, Node parent) {
079: Element coordinates = element(GML.coordinates, document, parent);
080: coordinates.appendChild(document
081: .createTextNode("1.0,2.0 3.0,4.0"));
082:
083: return coordinates;
084: }
085:
086: static Element boundedBy(Document document, Node parent) {
087: Element boundedBy = element(GML.boundedBy, document, parent);
088:
089: box(document, boundedBy);
090:
091: return boundedBy;
092: }
093:
094: static Element boundedByWithNull(Document document, Node parent) {
095: Element boundedBy = element(GML.boundedBy, document, parent);
096:
097: nil(document, boundedBy);
098:
099: return boundedBy;
100: }
101:
102: static Element box(Document document, Node parent) {
103: Element box = element(GML.Box, document, parent);
104:
105: coordinate(document, box);
106: coordinate(document, box);
107:
108: return box;
109: }
110:
111: static Element nil(Document document, Node parent) {
112: return element(new QName(GML.NAMESPACE, "null"), document,
113: parent);
114: }
115:
116: static Point point() {
117: return gf.createPoint(coordinate());
118: }
119:
120: static Element point(Document document, Node parent) {
121: Element point = element(GML.Point, document, parent);
122:
123: coordinate(document, point);
124:
125: return point;
126: }
127:
128: static Element pointProperty(Document document, Node parent) {
129: Element pointProperty = element(GML.pointProperty, document,
130: parent);
131:
132: point(document, pointProperty);
133:
134: return pointProperty;
135: }
136:
137: static LineString lineString() {
138: return gf.createLineString(coordinates());
139: }
140:
141: static Element lineString(Document document, Node parent) {
142: Element lineString = element(GML.LineString, document, parent);
143:
144: coordinates(document, lineString);
145:
146: return lineString;
147: }
148:
149: static LinearRing linearRing() {
150: return gf.createLinearRing(new Coordinate[] {
151: new Coordinate(1, 1), new Coordinate(2, 2),
152: new Coordinate(3, 3), new Coordinate(1, 1) });
153: }
154:
155: static Element lineStringProperty(Document document, Node parent) {
156: Element property = element(GML.lineStringProperty, document,
157: parent);
158:
159: lineString(document, property);
160:
161: return property;
162: }
163:
164: static Element linearRing(Document document, Node parent) {
165: Element linearRing = element(GML.LinearRing, document, parent);
166:
167: Element coordinates = element(GML.coordinates, document,
168: linearRing);
169: coordinates.appendChild(document
170: .createTextNode("1.0,2.0 3.0,4.0 5.0,6.0 1.0,2.0"));
171:
172: return linearRing;
173: }
174:
175: static Polygon polygon() {
176: return gf.createPolygon(linearRing(), null);
177: }
178:
179: static Element polygon(Document document, Node parent) {
180: Element polygon = element(GML.Polygon, document, parent);
181:
182: Element exterior = element(GML.outerBoundaryIs, document,
183: polygon);
184: linearRing(document, exterior);
185:
186: return polygon;
187: }
188:
189: static Element polygonProperty(Document document, Node parent) {
190: Element property = element(GML.polygonProperty, document,
191: parent);
192:
193: polygon(document, property);
194:
195: return property;
196: }
197:
198: static MultiPoint multiPoint() {
199: return gf.createMultiPoint(new Coordinate[] {
200: new Coordinate(1, 1), new Coordinate(2, 2) });
201: }
202:
203: static Element multiPoint(Document document, Node parent) {
204: Element multiPoint = element(GML.MultiPoint, document, parent);
205:
206: // 2 pointMember elements
207: Element pointMember = element(GML.pointMember, document,
208: multiPoint);
209: point(document, pointMember);
210:
211: pointMember = element(GML.pointMember, document, multiPoint);
212: point(document, pointMember);
213:
214: return multiPoint;
215: }
216:
217: static Element multiPointProperty(Document document, Node parent) {
218: Element multiPointProperty = element(GML.multiPointProperty,
219: document, parent);
220: multiPoint(document, multiPointProperty);
221:
222: return multiPointProperty;
223: }
224:
225: static MultiLineString multiLineString() {
226: return gf.createMultiLineString(new LineString[] {
227: lineString(), lineString() });
228: }
229:
230: static Element multiLineString(Document document, Node parent) {
231: Element multiLineString = element(GML.MultiLineString,
232: document, parent);
233:
234: Element lineStringMember = element(GML.lineStringMember,
235: document, multiLineString);
236: lineString(document, lineStringMember);
237:
238: lineStringMember = element(GML.lineStringMember, document,
239: multiLineString);
240: lineString(document, lineStringMember);
241:
242: return multiLineString;
243: }
244:
245: static Element multiLineStringProperty(Document document,
246: Node parent) {
247: Element multiLineStringProperty = element(
248: GML.multiLineStringProperty, document, parent);
249: multiLineString(document, multiLineStringProperty);
250:
251: return multiLineStringProperty;
252: }
253:
254: static MultiPolygon multiPolygon() {
255: return gf.createMultiPolygon(new Polygon[] { polygon(),
256: polygon() });
257: }
258:
259: static Element multiPolygon(Document document, Node parent) {
260: Element multiPolygon = element(GML.MultiPolygon, document,
261: parent);
262:
263: Element polygonMember = element(GML.polygonMember, document,
264: multiPolygon);
265: polygon(document, polygonMember);
266:
267: polygonMember = element(GML.polygonMember, document,
268: multiPolygon);
269: polygon(document, polygonMember);
270:
271: return multiPolygon;
272: }
273:
274: static Element multiPolygonProperty(Document document, Node parent) {
275: Element multiPolygonProperty = element(
276: GML.multiPolygonProperty, document, parent);
277: multiPolygon(document, multiPolygonProperty);
278:
279: return multiPolygonProperty;
280: }
281:
282: //
283: // features
284: //
285: static Element feature(Document document, Node parent) {
286: Element feature = element(TEST.TestFeature, document, parent);
287: Element geom = element(new QName(TEST.NAMESPACE, "geom"),
288: document, feature);
289: point(document, geom);
290:
291: Element count = element(new QName(TEST.NAMESPACE, "count"),
292: document, feature);
293: count.appendChild(document.createTextNode("1"));
294:
295: return feature;
296: }
297:
298: static Feature feature() throws Exception {
299: FeatureTypeBuilder typeBuilder = new DefaultFeatureTypeFactory();
300: typeBuilder.setName(TEST.TestFeature.getLocalPart());
301: typeBuilder.setNamespace(new URI(TEST.TestFeature
302: .getNamespaceURI()));
303:
304: typeBuilder.addType(AttributeTypeFactory.newAttributeType(
305: "name", String.class));
306: typeBuilder.addType(AttributeTypeFactory.newAttributeType(
307: "description", String.class));
308: typeBuilder.addType(AttributeTypeFactory.newAttributeType(
309: "geom", Point.class));
310: typeBuilder.addType(AttributeTypeFactory.newAttributeType(
311: "count", Integer.class));
312: typeBuilder.addType(AttributeTypeFactory.newAttributeType(
313: "date", Date.class));
314:
315: FeatureType type = typeBuilder.getFeatureType();
316:
317: return type.create(new Object[] { "theName", "theDescription",
318: point(), new Integer(1), new Date() }, "fid.1");
319: }
320:
321: static Element featureMember(Document document, Node parent) {
322: Element featureMember = element(GML.featureMember, document,
323: parent);
324: feature(document, featureMember);
325:
326: return featureMember;
327: }
328:
329: static Element element(QName name, Document document, Node parent) {
330: Element element = document.createElementNS(name
331: .getNamespaceURI(), name.getLocalPart());
332:
333: if (parent != null) {
334: parent.appendChild(element);
335: }
336:
337: return element;
338: }
339: }
|