Source Code Cross Referenced for GazetteerNameValidationOnlineTest.java in  » GIS » GeoTools-2.4.1 » org » geotools » validation » attributes » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » GIS » GeoTools 2.4.1 » org.geotools.validation.attributes 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.