Source Code Cross Referenced for EntityResolver2Wrapper.java in  » XML » xerces-2_9_1 » org » apache » xerces » util » 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 » XML » xerces 2_9_1 » org.apache.xerces.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         * 
009:         *      http://www.apache.org/licenses/LICENSE-2.0
010:         * 
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */
017:
018:        package org.apache.xerces.util;
019:
020:        import java.io.InputStream;
021:        import java.io.IOException;
022:        import java.io.Reader;
023:
024:        import org.apache.xerces.impl.ExternalSubsetResolver;
025:        import org.apache.xerces.impl.XMLEntityDescription;
026:        import org.apache.xerces.xni.XMLResourceIdentifier;
027:        import org.apache.xerces.xni.XNIException;
028:        import org.apache.xerces.xni.grammars.XMLDTDDescription;
029:        import org.apache.xerces.xni.parser.XMLInputSource;
030:
031:        import org.xml.sax.ext.EntityResolver2;
032:        import org.xml.sax.InputSource;
033:        import org.xml.sax.SAXException;
034:
035:        /**
036:         * <p>This class wraps a SAX entity resolver (EntityResolver2) in an XNI entity resolver.</p>
037:         * 
038:         * @author Michael Glavassevich, IBM
039:         * 
040:         * @version $Id: EntityResolver2Wrapper.java 447241 2006-09-18 05:12:57Z mrglavas $
041:         */
042:        public class EntityResolver2Wrapper implements  ExternalSubsetResolver {
043:
044:            //
045:            // Data
046:            //
047:
048:            /** An instance of SAX2 Extensions 1.1's EntityResolver2. */
049:            protected EntityResolver2 fEntityResolver;
050:
051:            //
052:            // Constructors
053:            //
054:
055:            /** Default constructor. */
056:            public EntityResolver2Wrapper() {
057:            }
058:
059:            /**
060:             * <p>Creates a new instance wrapping the given SAX entity resolver.</p>
061:             * 
062:             * @param entityResolver the SAX entity resolver to wrap
063:             */
064:            public EntityResolver2Wrapper(EntityResolver2 entityResolver) {
065:                setEntityResolver(entityResolver);
066:            } // <init>(EntityResolver2)
067:
068:            //
069:            // Public methods
070:            //
071:
072:            /**
073:             * <p>Sets the SAX entity resolver wrapped by this object.</p>
074:             * 
075:             * @param entityResolver the SAX entity resolver to wrap
076:             */
077:            public void setEntityResolver(EntityResolver2 entityResolver) {
078:                fEntityResolver = entityResolver;
079:            } // setEntityResolver(EntityResolver2)
080:
081:            /**
082:             * <p>Returns the SAX entity resolver wrapped by this object.</p>
083:             * 
084:             * @return the SAX entity resolver wrapped by this object
085:             */
086:            public EntityResolver2 getEntityResolver() {
087:                return fEntityResolver;
088:            } // getEntityResolver():EntityResolver2
089:
090:            //
091:            // ExternalSubsetResolver methods
092:            //
093:
094:            /**
095:             * <p>Locates an external subset for documents which do not explicitly
096:             * provide one. If no external subset is provided, this method should
097:             * return <code>null</code>.</p>
098:             *
099:             * @param grammarDescription a description of the DTD
100:             *
101:             * @throws XNIException Thrown on general error.
102:             * @throws IOException  Thrown if resolved entity stream cannot be
103:             *                      opened or some other i/o error occurs.
104:             */
105:            public XMLInputSource getExternalSubset(
106:                    XMLDTDDescription grammarDescription) throws XNIException,
107:                    IOException {
108:
109:                if (fEntityResolver != null) {
110:
111:                    String name = grammarDescription.getRootName();
112:                    String baseURI = grammarDescription.getBaseSystemId();
113:
114:                    // Resolve using EntityResolver2
115:                    try {
116:                        InputSource inputSource = fEntityResolver
117:                                .getExternalSubset(name, baseURI);
118:                        return (inputSource != null) ? createXMLInputSource(
119:                                inputSource, baseURI) : null;
120:                    }
121:                    // error resolving external subset
122:                    catch (SAXException e) {
123:                        Exception ex = e.getException();
124:                        if (ex == null) {
125:                            ex = e;
126:                        }
127:                        throw new XNIException(ex);
128:                    }
129:                }
130:
131:                // unable to resolve external subset
132:                return null;
133:
134:            } // getExternalSubset(XMLDTDDescription):XMLInputSource
135:
136:            //
137:            // XMLEntityResolver methods
138:            //
139:
140:            /**
141:             * Resolves an external parsed entity. If the entity cannot be
142:             * resolved, this method should return null.
143:             *
144:             * @param resourceIdentifier contains the physical co-ordinates of the resource to be resolved
145:             *
146:             * @throws XNIException Thrown on general error.
147:             * @throws IOException  Thrown if resolved entity stream cannot be
148:             *                      opened or some other i/o error occurs.
149:             */
150:            public XMLInputSource resolveEntity(
151:                    XMLResourceIdentifier resourceIdentifier)
152:                    throws XNIException, IOException {
153:
154:                if (fEntityResolver != null) {
155:
156:                    String pubId = resourceIdentifier.getPublicId();
157:                    String sysId = resourceIdentifier.getLiteralSystemId();
158:                    String baseURI = resourceIdentifier.getBaseSystemId();
159:                    String name = null;
160:                    if (resourceIdentifier instanceof  XMLDTDDescription) {
161:                        name = "[dtd]";
162:                    } else if (resourceIdentifier instanceof  XMLEntityDescription) {
163:                        name = ((XMLEntityDescription) resourceIdentifier)
164:                                .getEntityName();
165:                    }
166:
167:                    // When both pubId and sysId are null, the user's entity resolver
168:                    // can do nothing about it. We'd better not bother calling it.
169:                    // This happens when the resourceIdentifier is a GrammarDescription,
170:                    // which describes a schema grammar of some namespace, but without
171:                    // any schema location hint. -Sg
172:                    if (pubId == null && sysId == null) {
173:                        return null;
174:                    }
175:
176:                    // Resolve using EntityResolver2
177:                    try {
178:                        InputSource inputSource = fEntityResolver
179:                                .resolveEntity(name, pubId, baseURI, sysId);
180:                        return (inputSource != null) ? createXMLInputSource(
181:                                inputSource, baseURI) : null;
182:                    }
183:                    // error resolving entity
184:                    catch (SAXException e) {
185:                        Exception ex = e.getException();
186:                        if (ex == null) {
187:                            ex = e;
188:                        }
189:                        throw new XNIException(ex);
190:                    }
191:                }
192:
193:                // unable to resolve entity
194:                return null;
195:
196:            } // resolveEntity(XMLResourceIdentifier):XMLInputSource
197:
198:            /**
199:             * Creates an XMLInputSource from a SAX InputSource.
200:             */
201:            private XMLInputSource createXMLInputSource(InputSource source,
202:                    String baseURI) {
203:
204:                String publicId = source.getPublicId();
205:                String systemId = source.getSystemId();
206:                String baseSystemId = baseURI;
207:                InputStream byteStream = source.getByteStream();
208:                Reader charStream = source.getCharacterStream();
209:                String encoding = source.getEncoding();
210:                XMLInputSource xmlInputSource = new XMLInputSource(publicId,
211:                        systemId, baseSystemId);
212:                xmlInputSource.setByteStream(byteStream);
213:                xmlInputSource.setCharacterStream(charStream);
214:                xmlInputSource.setEncoding(encoding);
215:                return xmlInputSource;
216:
217:            } // createXMLInputSource(InputSource,String):XMLInputSource
218:
219:        } // class EntityResolver2Wrapper
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.