1 /*
2 * reserved comment block
3 * DO NOT REMOVE OR ALTER!
4 */
5 // SAXParserHandler.java - An entity-resolving DefaultHandler
6
7 /*
8 * Copyright 2001-2004 The Apache Software Foundation or its licensors,
9 * as applicable.
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
22 */
23
24 package com.sun.org.apache.xml.internal.resolver.readers;
25
26 import java.io.IOException;
27
28 import org.xml.sax;
29 import org.xml.sax.helpers;
30
31 /**
32 * An entity-resolving DefaultHandler.
33 *
34 * <p>This class provides a SAXParser DefaultHandler that performs
35 * entity resolution.
36 * </p>
37 *
38 * @author Norman Walsh
39 * <a href="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
40 */
41 public class SAXParserHandler extends DefaultHandler {
42 private EntityResolver er = null;
43 private ContentHandler ch = null;
44
45 public SAXParserHandler() {
46 super();
47 }
48
49 public void setEntityResolver(EntityResolver er) {
50 this.er = er;
51 }
52
53 public void setContentHandler(ContentHandler ch) {
54 this.ch = ch;
55 }
56
57 // Entity Resolver
58 public InputSource resolveEntity(String publicId, String systemId)
59 throws SAXException {
60
61 if (er != null) {
62 try {
63 return er.resolveEntity(publicId, systemId);
64 } catch (IOException e) {
65 System.out.println("resolveEntity threw IOException!");
66 return null;
67 }
68 } else {
69 return null;
70 }
71 }
72
73 // Content Handler
74 public void characters(char[] ch, int start, int length)
75 throws SAXException {
76 if (this.ch != null) {
77 this.ch.characters(ch, start, length);
78 }
79 }
80
81 public void endDocument()
82 throws SAXException {
83 if (ch != null) {
84 ch.endDocument();
85 }
86 }
87
88 public void endElement(String namespaceURI, String localName, String qName)
89 throws SAXException {
90 if (ch != null) {
91 ch.endElement(namespaceURI, localName, qName);
92 }
93 }
94
95 public void endPrefixMapping(String prefix)
96 throws SAXException {
97 if (ch != null) {
98 ch.endPrefixMapping(prefix);
99 }
100 }
101
102 public void ignorableWhitespace(char[] ch, int start, int length)
103 throws SAXException {
104 if (this.ch != null) {
105 this.ch.ignorableWhitespace(ch, start, length);
106 }
107 }
108
109 public void processingInstruction(String target, String data)
110 throws SAXException {
111 if (ch != null) {
112 ch.processingInstruction(target, data);
113 }
114 }
115
116 public void setDocumentLocator(Locator locator) {
117 if (ch != null) {
118 ch.setDocumentLocator(locator);
119 }
120 }
121
122 public void skippedEntity(String name)
123 throws SAXException {
124 if (ch != null) {
125 ch.skippedEntity(name);
126 }
127 }
128
129 public void startDocument()
130 throws SAXException {
131 if (ch != null) {
132 ch.startDocument();
133 }
134 }
135
136 public void startElement(String namespaceURI, String localName,
137 String qName, Attributes atts)
138 throws SAXException {
139 if (ch != null) {
140 ch.startElement(namespaceURI, localName, qName, atts);
141 }
142 }
143
144 public void startPrefixMapping(String prefix, String uri)
145 throws SAXException {
146 if (ch != null) {
147 ch.startPrefixMapping(prefix, uri);
148 }
149 }
150 }