Source Code Cross Referenced for DocumentParser.java in  » J2EE » ow2-easybeans » org » ow2 » easybeans » util » 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 » J2EE » ow2 easybeans » org.ow2.easybeans.util.xml 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * EasyBeans
003:         * Copyright (C) 2006-2007 Bull S.A.S.
004:         * Contact: easybeans@ow2.org
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; either
009:         * version 2.1 of the License, or any later version.
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:         * You should have received a copy of the GNU Lesser General Public
017:         * License along with this library; if not, write to the Free Software
018:         * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
019:         * USA
020:         *
021:         * --------------------------------------------------------------------------
022:         * $Id: DocumentParser.java 1970 2007-10-16 11:49:25Z benoitf $
023:         * --------------------------------------------------------------------------
024:         */package org.ow2.easybeans.util.xml;
025:
026:        import java.io.IOException;
027:        import java.io.InputStreamReader;
028:        import java.io.Reader;
029:        import java.net.URL;
030:        import java.net.URLConnection;
031:
032:        import javax.xml.parsers.DocumentBuilder;
033:        import javax.xml.parsers.DocumentBuilderFactory;
034:        import javax.xml.parsers.ParserConfigurationException;
035:
036:        import org.w3c.dom.Document;
037:        import org.xml.sax.EntityResolver;
038:        import org.xml.sax.InputSource;
039:        import org.xml.sax.SAXException;
040:
041:        /**
042:         * Allows to parse an xml file.
043:         * @author Florent Benoit
044:         */
045:        public final class DocumentParser {
046:
047:            /**
048:             * Utility class.
049:             */
050:            private DocumentParser() {
051:
052:            }
053:
054:            /**
055:             * Builds a new Document for a given xml file.
056:             * @param url the URL of the the XML file.
057:             * @param isValidating validate or not the xml file ?
058:             * @param entityResolver the entityResolver used to validate document (if
059:             *        validating = true)
060:             * @throws DocumentParserException if creating of builder fails or parsing
061:             *         fails.
062:             * @return an application object.
063:             */
064:            public static Document getDocument(final URL url,
065:                    final boolean isValidating,
066:                    final EntityResolver entityResolver)
067:                    throws DocumentParserException {
068:                // build factory
069:                DocumentBuilderFactory factory = DocumentBuilderFactory
070:                        .newInstance();
071:
072:                // XML files use schemas.
073:                factory.setNamespaceAware(true);
074:                factory.setValidating(isValidating);
075:
076:                // ignore white space can only be set if parser is validating
077:                if (isValidating) {
078:                    factory.setIgnoringElementContentWhitespace(true);
079:                    factory.setAttribute(
080:                            "http://apache.org/xml/features/validation/schema",
081:                            Boolean.valueOf(isValidating));
082:                    factory
083:                            .setAttribute(
084:                                    "http://apache.org/xml/features/validation/schema-full-checking",
085:                                    Boolean.valueOf(true));
086:                }
087:
088:                // Add schema location
089:                //TODO: FIXME: set other schemas ?
090:                if (isValidating) {
091:                    factory
092:                            .setAttribute(
093:                                    "http://apache.org/xml/properties/schema/external-schemaLocation",
094:                                    "http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd");
095:                }
096:
097:                // Build a document builder
098:                DocumentBuilder builder = null;
099:                try {
100:                    builder = factory.newDocumentBuilder();
101:                } catch (ParserConfigurationException e) {
102:                    throw new DocumentParserException(
103:                            "Cannot build a document builder", e);
104:                }
105:
106:                // Error handler (throwing exceptions)
107:                builder.setErrorHandler(new EasyBeansErrorHandler());
108:
109:                // Dummy entity resolver if there is none
110:                if (entityResolver == null) {
111:                    builder.setEntityResolver(new EmptyEntityResolver());
112:                } else {
113:                    builder.setEntityResolver(entityResolver);
114:                }
115:
116:                // Parse document
117:                URLConnection urlConnection = null;
118:                try {
119:                    urlConnection = url.openConnection();
120:                } catch (IOException e) {
121:                    throw new DocumentParserException(
122:                            "Cannot open a connection on URL '" + url + "'", e);
123:                }
124:                urlConnection.setDefaultUseCaches(false);
125:                Reader reader = null;
126:                try {
127:                    reader = new InputStreamReader(urlConnection
128:                            .getInputStream());
129:                } catch (IOException e) {
130:                    throw new DocumentParserException(
131:                            "Cannot build an input stream reader on URL '"
132:                                    + url + "'", e);
133:                }
134:
135:                InputSource inputSource = new InputSource(reader);
136:                Document document = null;
137:                try {
138:                    document = builder.parse(inputSource);
139:                } catch (SAXException e) {
140:                    throw new DocumentParserException(
141:                            "Cannot parse the XML file '" + url + "'.", e);
142:                } catch (IOException e) {
143:                    throw new DocumentParserException(
144:                            "Cannot parse the XML file '" + url + "'.", e);
145:                } finally {
146:                    // close InputStream when parsing is finished
147:                    try {
148:                        reader.close();
149:                    } catch (IOException e) {
150:                        throw new DocumentParserException(
151:                                "Cannot close the inputsource of the XML file'"
152:                                        + url + "'.", e);
153:                    }
154:                }
155:
156:                return document;
157:            }
158:
159:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.