Source Code Cross Referenced for StreamingParser.java in  » GIS » GeoTools-2.4.1 » org » geotools » xml » 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.xml 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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;
017:
018:        import java.io.InputStream;
019:
020:        import javax.xml.namespace.QName;
021:        import javax.xml.parsers.ParserConfigurationException;
022:        import javax.xml.parsers.SAXParser;
023:        import javax.xml.parsers.SAXParserFactory;
024:
025:        import org.geotools.xml.impl.ElementNameStreamingParserHandler;
026:        import org.geotools.xml.impl.StreamingParserHandler;
027:        import org.geotools.xml.impl.TypeStreamingParserHandler;
028:        import org.geotools.xml.impl.jxpath.JXPathStreamingParserHandler;
029:        import org.xml.sax.SAXException;
030:
031:        /**
032:         * XML parser capable of streaming.
033:         * <p>
034:         * Performs the same task as {@link org.geotools.xml.Parser}, with the addition
035:         * that objects are streamed back to the client. Streaming can occur in a 
036:         * number of different modes.
037:         * </p>
038:         * <p>
039:         * As an example consider the following gml document:
040:         * <pre>
041:         * &lt;test:TestFeatureCollection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
042:         *	xmlns:gml="http://www.opengis.net/gml"
043:         *	xmlns:test="http://www.geotools.org/test"
044:         *	xsi:schemaLocation="http://www.geotools.org/test test.xsd"&gt;
045:         *
046:         *   &lt;gml:featureMember&gt;
047:         *      &lt;test:TestFeature fid="0"&gt;
048:         *	      ...
049:         *      &lt;/test:TestFeature&gt;		
050:         *   &lt;/gml:featureMember&gt;
051:         *	
052:         *   &lt;gml:featureMember&gt;
053:         *      &lt;test:TestFeature fid="1"&gt;
054:         *	      ...
055:         *      &lt;/test:TestFeature&gt;
056:         *   &lt;/gml:featureMember&gt;
057:         *  	
058:         *   &lt;gml:featureMember&gt;
059:         *      &lt;test:TestFeature fid="2"&gt;
060:         *	      ....
061:         *      &lt;/test:TestFeature&gt;
062:         *   &lt;/gml:featureMember&gt;
063:         *
064:         * &lt;/test:TestFeatureCollection&gt;
065:         * </pre>
066:         * And suppose we want to stream back each feature as it is parsed.
067:         * </p>
068:         * <p>
069:         *	<h3>1. Element Name</h3>
070:         *	Objects are streamed back when an element of a particular name has been 
071:         *	parsed. 
072:         *	<pre>
073:         *    Configuration configuration = new GMLConfiguration();
074:         *    QName elementName = new QName( "http://www.geotools.org/test", "TestFeature" );
075:         *    
076:         *    StreamingParser parser = new StreamingParser( configuration, elementName );
077:         *    
078:         *    Feature f = null;
079:         *    while ( ( f = parser.parse() ) != null ) {
080:         *       ...
081:         *    }
082:         *  </pre>
083:         * </p>
084:         * <p>
085:         * 	<h3>2. Type</h3>
086:         * 	Objects are streamed back when an element has been parsed into an object 
087:         * 	of a particular type. 
088:         *  <pre>
089:         *    Configuration configuration = new GMLConfiguration();
090:         *    StreamingParser parser = new StreamingParser( configuration, Feature.class );
091:         *    
092:         *    Feature f = null;
093:         *    while ( ( f = parser.parse() ) != null ) {
094:         *       ...
095:         *    }
096:         *  </pre>
097:         * </p>
098:         * <p>
099:         * 	<h3>3. Xpath Expression</h3>
100:         * 	Objects are streamed back when an element has been parsed which matches 
101:         * 	a particular xpath expression.
102:         *  <pre>
103:         *    Configuration configuration = new GMLConfiguration();
104:         *    String xpath = "//TestFeature";
105:         *    StreamingParser parser = new StreamingParser( configuration, xpath );
106:         *    
107:         *    Feature f = null;
108:         *    while ( ( f = parser.parse() ) != null ) {
109:         *       ...
110:         *    }
111:         *  </pre>
112:         * </p>
113:         *
114:         * @author Justin Deoliveira, The Open Planning Project
115:         */
116:        public class StreamingParser {
117:            /**
118:             * The sax driver / handler.
119:             */
120:            private StreamingParserHandler handler;
121:            /**
122:             * The sax parser.
123:             */
124:            private SAXParser parser;
125:            /**
126:             * The xml input.
127:             */
128:            private InputStream input;
129:            /**
130:             * The parsing thread.
131:             */
132:            private Thread thread;
133:
134:            /**
135:             * Creates a new instance of the type based streaming parser.
136:             *
137:             * @param configuration Object representing the configuration of the parser.
138:             * @param input The input stream representing the instance document to be parsed.
139:             * @param type The type of parsed objects to stream back.
140:             *
141:             * @throws ParserConfigurationException
142:             * @throws SAXException
143:             */
144:            public StreamingParser(Configuration configuration,
145:                    InputStream input, Class type)
146:                    throws ParserConfigurationException, SAXException {
147:
148:                this (configuration, input, new TypeStreamingParserHandler(
149:                        configuration, type));
150:            }
151:
152:            /**
153:             * Creates a new instance of the element name based streaming parser.
154:             *
155:             * @param configuration Object representing the configuration of the parser.
156:             * @param input The input stream representing the instance document to be parsed.
157:             * @param elementName The name of elements to stream back.
158:             *
159:             * @throws ParserConfigurationException
160:             * @throws SAXException
161:             */
162:            public StreamingParser(Configuration configuration,
163:                    InputStream input, QName elementName)
164:                    throws ParserConfigurationException, SAXException {
165:
166:                this (configuration, input,
167:                        new ElementNameStreamingParserHandler(configuration,
168:                                elementName));
169:            }
170:
171:            /**
172:             * Creates a new instance of the xpath based streaming parser. 
173:             *
174:             * @param configuration Object representing the configuration of the parser.
175:             * @param input The input stream representing the instance document to be parsed.
176:             * @param xpath An xpath expression which dictates how the parser streams
177:             * objects back to the client.
178:             *
179:             * @throws ParserConfigurationException
180:             * @throws SAXException
181:             */
182:            public StreamingParser(Configuration configuration,
183:                    InputStream input, String xpath)
184:                    throws ParserConfigurationException, SAXException {
185:
186:                this (configuration, input, new JXPathStreamingParserHandler(
187:                        configuration, xpath));
188:            }
189:
190:            /**
191:             * Internal constructor.
192:             */
193:            protected StreamingParser(Configuration configuration,
194:                    InputStream input, StreamingParserHandler handler)
195:                    throws ParserConfigurationException, SAXException {
196:
197:                SAXParserFactory spf = SAXParserFactory.newInstance();
198:                spf.setNamespaceAware(true);
199:                parser = spf.newSAXParser();
200:
201:                this .handler = handler;
202:                this .input = input;
203:            }
204:
205:            /**
206:             * Streams the parser to the next element in the instance document which
207:             * matches the xpath query specified in the contstructor. This method
208:             * returns null when there are no more objects to stream.
209:             *
210:             * @return The next object in the stream, or null if no such object is
211:             * available.
212:             */
213:            public Object parse() {
214:
215:                if (thread == null) {
216:                    Runnable runnable = new Runnable() {
217:                        public void run() {
218:                            try {
219:                                parser.parse(input, handler);
220:                            } catch (Exception e) {
221:                                //close the buffer
222:                                handler.getBuffer().close();
223:                                throw new RuntimeException(e);
224:                            }
225:                        };
226:                    };
227:
228:                    thread = new Thread(runnable);
229:                    thread.start();
230:                }
231:
232:                return handler.getBuffer().get();
233:            }
234:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.