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.jaxp;
019:
020: import java.util.HashMap;
021:
022: import org.apache.xerces.impl.validation.EntityState;
023: import org.apache.xerces.impl.validation.ValidationManager;
024: import org.apache.xerces.xni.Augmentations;
025: import org.apache.xerces.xni.XMLDTDHandler;
026: import org.apache.xerces.xni.XMLLocator;
027: import org.apache.xerces.xni.XMLResourceIdentifier;
028: import org.apache.xerces.xni.XMLString;
029: import org.apache.xerces.xni.XNIException;
030: import org.apache.xerces.xni.parser.XMLDTDFilter;
031: import org.apache.xerces.xni.parser.XMLDTDSource;
032:
033: /**
034: * <p>This filter records which unparsed entities have been
035: * declared in the DTD and provides this information to a ValidationManager.
036: * Events are forwarded to the registered XMLDTDHandler without modification.</p>
037: *
038: * @author Michael Glavassevich, IBM
039: * @version $Id: UnparsedEntityHandler.java 520055 2007-03-19 19:25:57Z mrglavas $
040: */
041: final class UnparsedEntityHandler implements XMLDTDFilter, EntityState {
042:
043: /** DTD source and handler. **/
044: private XMLDTDSource fDTDSource;
045: private XMLDTDHandler fDTDHandler;
046:
047: /** Validation manager. */
048: private final ValidationManager fValidationManager;
049:
050: /** Map for tracking unparsed entities. */
051: private HashMap fUnparsedEntities = null;
052:
053: UnparsedEntityHandler(ValidationManager manager) {
054: fValidationManager = manager;
055: }
056:
057: /*
058: * XMLDTDHandler methods
059: */
060:
061: public void startDTD(XMLLocator locator, Augmentations augmentations)
062: throws XNIException {
063: fValidationManager.setEntityState(this );
064: if (fDTDHandler != null) {
065: fDTDHandler.startDTD(locator, augmentations);
066: }
067: }
068:
069: public void startParameterEntity(String name,
070: XMLResourceIdentifier identifier, String encoding,
071: Augmentations augmentations) throws XNIException {
072: if (fDTDHandler != null) {
073: fDTDHandler.startParameterEntity(name, identifier,
074: encoding, augmentations);
075: }
076: }
077:
078: public void textDecl(String version, String encoding,
079: Augmentations augmentations) throws XNIException {
080: if (fDTDHandler != null) {
081: fDTDHandler.textDecl(version, encoding, augmentations);
082: }
083: }
084:
085: public void endParameterEntity(String name,
086: Augmentations augmentations) throws XNIException {
087: if (fDTDHandler != null) {
088: fDTDHandler.endParameterEntity(name, augmentations);
089: }
090: }
091:
092: public void startExternalSubset(XMLResourceIdentifier identifier,
093: Augmentations augmentations) throws XNIException {
094: if (fDTDHandler != null) {
095: fDTDHandler.startExternalSubset(identifier, augmentations);
096: }
097: }
098:
099: public void endExternalSubset(Augmentations augmentations)
100: throws XNIException {
101: if (fDTDHandler != null) {
102: fDTDHandler.endExternalSubset(augmentations);
103: }
104: }
105:
106: public void comment(XMLString text, Augmentations augmentations)
107: throws XNIException {
108: if (fDTDHandler != null) {
109: fDTDHandler.comment(text, augmentations);
110: }
111: }
112:
113: public void processingInstruction(String target, XMLString data,
114: Augmentations augmentations) throws XNIException {
115: if (fDTDHandler != null) {
116: fDTDHandler.processingInstruction(target, data,
117: augmentations);
118: }
119: }
120:
121: public void elementDecl(String name, String contentModel,
122: Augmentations augmentations) throws XNIException {
123: if (fDTDHandler != null) {
124: fDTDHandler.elementDecl(name, contentModel, augmentations);
125: }
126: }
127:
128: public void startAttlist(String elementName,
129: Augmentations augmentations) throws XNIException {
130: if (fDTDHandler != null) {
131: fDTDHandler.startAttlist(elementName, augmentations);
132: }
133: }
134:
135: public void attributeDecl(String elementName, String attributeName,
136: String type, String[] enumeration, String defaultType,
137: XMLString defaultValue,
138: XMLString nonNormalizedDefaultValue,
139: Augmentations augmentations) throws XNIException {
140: if (fDTDHandler != null) {
141: fDTDHandler.attributeDecl(elementName, attributeName, type,
142: enumeration, defaultType, defaultValue,
143: nonNormalizedDefaultValue, augmentations);
144: }
145: }
146:
147: public void endAttlist(Augmentations augmentations)
148: throws XNIException {
149: if (fDTDHandler != null) {
150: fDTDHandler.endAttlist(augmentations);
151: }
152: }
153:
154: public void internalEntityDecl(String name, XMLString text,
155: XMLString nonNormalizedText, Augmentations augmentations)
156: throws XNIException {
157: if (fDTDHandler != null) {
158: fDTDHandler.internalEntityDecl(name, text,
159: nonNormalizedText, augmentations);
160: }
161: }
162:
163: public void externalEntityDecl(String name,
164: XMLResourceIdentifier identifier,
165: Augmentations augmentations) throws XNIException {
166: if (fDTDHandler != null) {
167: fDTDHandler.externalEntityDecl(name, identifier,
168: augmentations);
169: }
170: }
171:
172: public void unparsedEntityDecl(String name,
173: XMLResourceIdentifier identifier, String notation,
174: Augmentations augmentations) throws XNIException {
175: if (fUnparsedEntities == null) {
176: fUnparsedEntities = new HashMap();
177: }
178: fUnparsedEntities.put(name, name);
179: if (fDTDHandler != null) {
180: fDTDHandler.unparsedEntityDecl(name, identifier, notation,
181: augmentations);
182: }
183: }
184:
185: public void notationDecl(String name,
186: XMLResourceIdentifier identifier,
187: Augmentations augmentations) throws XNIException {
188: if (fDTDHandler != null) {
189: fDTDHandler.notationDecl(name, identifier, augmentations);
190: }
191: }
192:
193: public void startConditional(short type, Augmentations augmentations)
194: throws XNIException {
195: if (fDTDHandler != null) {
196: fDTDHandler.startConditional(type, augmentations);
197: }
198: }
199:
200: public void ignoredCharacters(XMLString text,
201: Augmentations augmentations) throws XNIException {
202: if (fDTDHandler != null) {
203: fDTDHandler.ignoredCharacters(text, augmentations);
204: }
205:
206: }
207:
208: public void endConditional(Augmentations augmentations)
209: throws XNIException {
210: if (fDTDHandler != null) {
211: fDTDHandler.endConditional(augmentations);
212: }
213: }
214:
215: public void endDTD(Augmentations augmentations) throws XNIException {
216: if (fDTDHandler != null) {
217: fDTDHandler.endDTD(augmentations);
218: }
219: }
220:
221: public void setDTDSource(XMLDTDSource source) {
222: fDTDSource = source;
223: }
224:
225: public XMLDTDSource getDTDSource() {
226: return fDTDSource;
227: }
228:
229: /*
230: * XMLDTDSource methods
231: */
232:
233: public void setDTDHandler(XMLDTDHandler handler) {
234: fDTDHandler = handler;
235: }
236:
237: public XMLDTDHandler getDTDHandler() {
238: return fDTDHandler;
239: }
240:
241: /*
242: * EntityState methods
243: */
244:
245: public boolean isEntityDeclared(String name) {
246: return false;
247: }
248:
249: public boolean isEntityUnparsed(String name) {
250: if (fUnparsedEntities != null) {
251: return fUnparsedEntities.containsKey(name);
252: }
253: return false;
254: }
255:
256: /*
257: * Other methods
258: */
259:
260: public void reset() {
261: if (fUnparsedEntities != null && !fUnparsedEntities.isEmpty()) {
262: // should only clear this if the last document contained unparsed entities
263: fUnparsedEntities.clear();
264: }
265: }
266: }
|