001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-2006, Geotools Project Managment Committee (PMC)
005: * (C) 2004 TOPP - www.openplans.org
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library 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 GNU
015: * Lesser General Public License for more details.
016: *
017: * Created on Jan 21, 2004
018: */
019: package org.geotools.validation.attributes;
020:
021: import java.io.BufferedReader;
022: import java.io.InputStream;
023: import java.io.InputStreamReader;
024: import java.net.ConnectException;
025: import java.net.HttpURLConnection;
026: import java.net.URL;
027: import java.util.HashMap;
028: import java.util.Map;
029:
030: import javax.xml.parsers.DocumentBuilderFactory;
031:
032: import junit.framework.TestCase;
033:
034: import org.geotools.feature.Feature;
035: import org.geotools.feature.FeatureCollection;
036: import org.geotools.feature.FeatureType;
037: import org.geotools.feature.IllegalAttributeException;
038: import org.geotools.geometry.jts.ReferencedEnvelope;
039: import org.w3c.dom.Document;
040: import org.w3c.dom.Element;
041: import org.w3c.dom.Node;
042: import org.w3c.dom.NodeList;
043: import org.xml.sax.InputSource;
044:
045: import com.vividsolutions.jts.geom.Envelope;
046: import com.vividsolutions.jts.geom.Geometry;
047:
048: /**
049: * GazetteerNameValidationTest purpose.
050: * <p>
051: * Description of GazetteerNameValidationTest ...
052: * </p>
053: *
054: * @author dzwiers, Refractions Research, Inc.
055: * @author $Author: sploreg $ (last modification)
056: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/extension/validation/src/test/java/org/geotools/validation/attributes/GazetteerNameValidationOnlineTest.java $
057: * @version $Id: GazetteerNameValidationOnlineTest.java 27002 2007-09-17 03:01:53Z jdeolive $
058: */
059: public class GazetteerNameValidationOnlineTest extends TestCase {
060:
061: public GazetteerNameValidationOnlineTest() {
062: super ("");
063: }
064:
065: public GazetteerNameValidationOnlineTest(String s) {
066: super (s);
067: }
068:
069: public void XtestValidate() {
070: class testFeature implements Feature {
071: Map attrs = new HashMap();
072:
073: public FeatureCollection getParent() {
074: return null;
075: }
076:
077: public void setParent(FeatureCollection collection) {
078: }
079:
080: public FeatureType getFeatureType() {
081: return null;
082: }
083:
084: public String getID() {
085: return "";
086: }
087:
088: public Object[] getAttributes(Object[] attributes) {
089: return attrs.entrySet().toArray();
090: }
091:
092: // used
093: public Object getAttribute(String xPath) {
094: return attrs.get(xPath);
095: }
096:
097: public Object getAttribute(int index) {
098: return attrs.entrySet().toArray()[index];
099: }
100:
101: public void setAttribute(int position, Object val)
102: throws IllegalAttributeException,
103: ArrayIndexOutOfBoundsException {
104: }
105:
106: public int getNumberOfAttributes() {
107: return attrs.size();
108: }
109:
110: public void setAttributes(Object[] attributes)
111: throws IllegalAttributeException {
112: }
113:
114: // used
115: public void setAttribute(String xPath, Object attribute)
116: throws IllegalAttributeException {
117: attrs.put(xPath, attribute);
118: }
119:
120: public Geometry getDefaultGeometry() {
121: return null;
122: }
123:
124: public void setDefaultGeometry(Geometry geometry)
125: throws IllegalAttributeException {
126: }
127:
128: public ReferencedEnvelope getBounds() {
129: return null;
130: }
131: }
132: Feature f = new testFeature();
133: try {
134: f.setAttribute("CityName", "Vancouver");
135: } catch (Exception e) {
136: }
137: GazetteerNameValidation gnv = new GazetteerNameValidation();
138: gnv.setAttributeName("CityName");
139: try {
140: gnv
141: .setGazetteer(new URL(
142: "http://cgdi-dev.geoconnections.org/cgi-bin/prototypes/cgdigaz/cgdigaz.cgi?version=1.0&request=GetPlacenameGeometry&wildcards=false&geomtype=bbox"));
143: } catch (Exception e) {
144: }
145: // ValidationResults results = new RoadValidationResults();
146: // if(!gnv.validate(f,null,results)){
147: // fail("Did not validate.");
148: // }
149:
150: }
151:
152: public void testURLConnection() {
153: String place = "Vancouver";
154: try {
155: URL gazetteerURL = new URL(
156: "http://cgdi-dev.geoconnections.org/cgi-bin/prototypes/cgdigaz/cgdigaz.cgi?version=1.0&request=GetPlacenameGeometry&wildcards=false&geomtype=bbox&placename="
157: + place);
158: HttpURLConnection gazetteerConnection = (HttpURLConnection) gazetteerURL
159: .openConnection();
160: // gazetteerConnection.setConnectTimeout(100);
161: if (!("OK".equals(gazetteerConnection.getResponseMessage())))
162: throw new Exception(
163: "An error occured creating the connection to the Gazetteer.");
164: InputStream gazetteerInputStream = gazetteerConnection
165: .getInputStream();
166: InputStreamReader gazetteerInputStreamReader = new InputStreamReader(
167: gazetteerInputStream);
168: BufferedReader gazetteerBufferedReader = new BufferedReader(
169: gazetteerInputStreamReader);
170:
171: InputSource gazetteerInputSource = new InputSource(
172: gazetteerBufferedReader);
173: DocumentBuilderFactory dfactory = DocumentBuilderFactory
174: .newInstance();
175: dfactory.setNamespaceAware(true);
176:
177: // TODO turn on validation
178: dfactory.setValidating(false);
179: dfactory.setIgnoringComments(true);
180: dfactory.setCoalescing(true);
181: dfactory.setIgnoringElementContentWhitespace(true);
182:
183: Document serviceDoc = dfactory.newDocumentBuilder().parse(
184: gazetteerInputSource);
185: Element elem = serviceDoc.getDocumentElement();
186:
187: // elem == featureCollection at this point
188:
189: elem = getChildElement(elem, "queryInfo");
190: if (elem == null)
191: throw new NullPointerException(
192: "Invalid DOM tree returned by gazetteer.");
193:
194: // this number is the number of instances found.
195: int number = Integer.parseInt(getChildText(elem,
196: "numberOfResults"));
197:
198: if (number > 0) {
199: // found vancouver!
200: } else {
201: // did not find vancouver
202: // (but out plugin worked so we still pass the test
203: }
204: } catch (ConnectException timedOut) {
205: return; // ignore server must be down
206: } catch (Exception e) {
207: e.printStackTrace();
208: fail(e.toString());
209: }
210: }
211:
212: /**
213: * getChildElement purpose.
214: *
215: * <p>
216: * Used to help with XML manipulations. Returns the first child element of
217: * the specified name.
218: * </p>
219: *
220: * @param root The root element to look for children in.
221: * @param name The name of the child element to look for.
222: *
223: * @return The child element found, null if not found.
224: *
225: * @see getChildElement(Element,String,boolean)
226: */
227: public static Element getChildElement(Element root, String name) {
228: Node child = root.getFirstChild();
229:
230: while (child != null) {
231: if (child.getNodeType() == Node.ELEMENT_NODE) {
232: if (name.equals(child.getNodeName())) {
233: return (Element) child;
234: }
235: }
236: child = child.getNextSibling();
237: }
238: return null;
239: }
240:
241: /**
242: * getChildText purpose.
243: *
244: * <p>
245: * Used to help with XML manipulations. Returns the first child text value
246: * of the specified element name.
247: * </p>
248: *
249: * @param root The root element to look for children in.
250: * @param childName The name of the attribute to look for.
251: *
252: * @return The value if the child was found, the null otherwise.
253: */
254: public static String getChildText(Element root, String childName) {
255: Element elem = getChildElement(root, childName);
256: if (elem != null) {
257: Node child;
258: NodeList childs = elem.getChildNodes();
259: int nChilds = childs.getLength();
260: for (int i = 0; i < nChilds; i++) {
261: child = childs.item(i);
262: if (child.getNodeType() == Node.TEXT_NODE) {
263: return child.getNodeValue();
264: }
265: }
266: }
267: return null;
268: }
269: }
|