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.xml.ogc;
017:
018: import java.io.File;
019: import java.io.IOException;
020: import java.io.StringWriter;
021: import java.net.URI;
022: import java.util.HashMap;
023: import java.util.logging.Level;
024:
025: import javax.xml.parsers.SAXParser;
026: import javax.xml.parsers.SAXParserFactory;
027:
028: import junit.framework.TestCase;
029:
030: import org.geotools.TestData;
031: import org.geotools.xml.DocumentWriter;
032: import org.geotools.xml.PrintHandler;
033: import org.geotools.xml.XSISAXHandler;
034: import org.geotools.xml.schema.Element;
035: import org.geotools.xml.schema.Schema;
036: import org.xml.sax.Attributes;
037:
038: import com.vividsolutions.jts.geom.Coordinate;
039: import com.vividsolutions.jts.geom.GeometryFactory;
040: import com.vividsolutions.jts.geom.LinearRing;
041: import com.vividsolutions.jts.geom.MultiPolygon;
042: import com.vividsolutions.jts.geom.Polygon;
043:
044: /**
045: * @author Jesse
046: *
047: */
048: public class GeometryEncoderTest extends TestCase {
049:
050: protected void setUp() throws Exception {
051: super .setUp();
052: }
053:
054: public void testEncodeChoiceGeometryType() throws Exception {
055: File f = TestData.copy(this , "xml/feature-type-choice.xsd");
056: URI u = f.toURI();
057: XSISAXHandler contentHandler = new XSISAXHandler(u);
058: XSISAXHandler.setLogLevel(Level.WARNING);
059:
060: SAXParserFactory spf = SAXParserFactory.newInstance();
061: spf.setNamespaceAware(true);
062: spf.setValidating(false);
063: SAXParser parser = spf.newSAXParser();
064: parser.parse(f, contentHandler);
065:
066: Schema schema = contentHandler.getSchema();
067: Element geomElement = schema.getElements()[0]
068: .findChildElement("GEOM");
069: GeometryFactory factory = new GeometryFactory();
070: LinearRing ring = factory.createLinearRing(new Coordinate[] {
071: new Coordinate(0, 0), new Coordinate(10, 0),
072: new Coordinate(0, 10), new Coordinate(0, 0) });
073: Polygon polygon = factory
074: .createPolygon(ring, new LinearRing[0]);
075: polygon.setUserData("EPSG:4326");
076: MultiPolygon geom = factory
077: .createMultiPolygon(new Polygon[] { polygon });
078: geom.setUserData("EPSG:4326");
079: final StringWriter writer = new StringWriter();
080: // DocumentWriter.writeDocument(geom, schema, writer, new HashMap());
081:
082: PrintHandler output = new PrintHandler() {
083:
084: public void characters(char[] arg0, int arg1, int arg2)
085: throws IOException {
086: writer.write(arg0, arg1, arg2);
087: }
088:
089: public void characters(String s) throws IOException {
090: writer.write(s);
091: }
092:
093: public void element(URI namespaceURI, String localName,
094: Attributes attributes) throws IOException {
095: }
096:
097: public void endDocument() throws IOException {
098: }
099:
100: public void endElement(URI namespaceURI, String localName)
101: throws IOException {
102: writer.write("</" + localName + ">");
103: }
104:
105: public Element findElement(Object value) {
106: return null;
107: }
108:
109: public Element findElement(String name) {
110: return null;
111: }
112:
113: public Schema getDocumentSchema() {
114: return null;
115: }
116:
117: public Object getHint(Object key) {
118: return null;
119: }
120:
121: public void ignorableWhitespace(char[] arg0, int arg1,
122: int arg2) throws IOException {
123: }
124:
125: public void startDocument() throws IOException {
126: }
127:
128: public void startElement(URI namespaceURI,
129: String localName, Attributes attributes)
130: throws IOException {
131: writer.write("<" + localName);
132: if (attributes != null) {
133: for (int i = 0; i < attributes.getLength(); i++) {
134: writer.write(" " + attributes.getLocalName(i)
135: + "=" + attributes.getValue(i));
136: }
137: }
138: writer.write(">");
139: }
140:
141: };
142: geomElement.getType().encode(geomElement, geom, output,
143: new HashMap());
144: String expected = "<GEOM><MultiPolygon srsName=EPSG:4326><polygonMember><Polygon srsName=EPSG:4326><outerBoundaryIs><LinearRing><coordinates decimal=. cs=, ts= >0.0,0.0 10.0,0.0 0.0,10.0 0.0,0.0</coordinates></LinearRing></outerBoundaryIs></Polygon></polygonMember></MultiPolygon></GEOM>";
145: assertEquals(expected, writer.toString());
146: }
147:
148: }
|