1 /* 2 * reserved comment block 3 * DO NOT REMOVE OR ALTER! 4 */ 5 /* 6 * Copyright 2005 The Apache Software Foundation. 7 * 8 * Licensed under the Apache License, Version 2.0 (the "License"); 9 * you may not use this file except in compliance with the License. 10 * You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, software 15 * distributed under the License is distributed on an "AS IS" BASIS, 16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 * See the License for the specific language governing permissions and 18 * limitations under the License. 19 */ 20 21 package com.sun.org.apache.xerces.internal.jaxp; 22 23 import java.util.HashMap; 24 25 import com.sun.org.apache.xerces.internal.impl.validation.EntityState; 26 import com.sun.org.apache.xerces.internal.impl.validation.ValidationManager; 27 import com.sun.org.apache.xerces.internal.xni.Augmentations; 28 import com.sun.org.apache.xerces.internal.xni.XMLDTDHandler; 29 import com.sun.org.apache.xerces.internal.xni.XMLLocator; 30 import com.sun.org.apache.xerces.internal.xni.XMLResourceIdentifier; 31 import com.sun.org.apache.xerces.internal.xni.XMLString; 32 import com.sun.org.apache.xerces.internal.xni.XNIException; 33 import com.sun.org.apache.xerces.internal.xni.parser.XMLDTDFilter; 34 import com.sun.org.apache.xerces.internal.xni.parser.XMLDTDSource; 35 36 /** 37 * <p>This filter records which unparsed entities have been 38 * declared in the DTD and provides this information to a ValidationManager. 39 * Events are forwarded to the registered XMLDTDHandler without modification.</p> 40 * 41 * @author Michael Glavassevich, IBM 42 * @version $Id: UnparsedEntityHandler.java,v 1.6 2010-11-01 04:40:07 joehw Exp $ 43 */ 44 final class UnparsedEntityHandler implements XMLDTDFilter, EntityState { 45 46 /** DTD source and handler. **/ 47 private XMLDTDSource fDTDSource; 48 private XMLDTDHandler fDTDHandler; 49 50 /** Validation manager. */ 51 private final ValidationManager fValidationManager; 52 53 /** Map for tracking unparsed entities. */ 54 private HashMap fUnparsedEntities = null; 55 UnparsedEntityHandler(ValidationManager manager)56 UnparsedEntityHandler(ValidationManager manager) { 57 fValidationManager = manager; 58 } 59 60 /* 61 * XMLDTDHandler methods 62 */ 63 startDTD(XMLLocator locator, Augmentations augmentations)64 public void startDTD(XMLLocator locator, Augmentations augmentations) 65 throws XNIException { 66 fValidationManager.setEntityState(this); 67 if (fDTDHandler != null) { 68 fDTDHandler.startDTD(locator, augmentations); 69 } 70 } 71 startParameterEntity(String name, XMLResourceIdentifier identifier, String encoding, Augmentations augmentations)72 public void startParameterEntity(String name, 73 XMLResourceIdentifier identifier, String encoding, 74 Augmentations augmentations) throws XNIException { 75 if (fDTDHandler != null) { 76 fDTDHandler.startParameterEntity(name, identifier, encoding, augmentations); 77 } 78 } 79 textDecl(String version, String encoding, Augmentations augmentations)80 public void textDecl(String version, String encoding, 81 Augmentations augmentations) throws XNIException { 82 if (fDTDHandler != null) { 83 fDTDHandler.textDecl(version, encoding, augmentations); 84 } 85 } 86 endParameterEntity(String name, Augmentations augmentations)87 public void endParameterEntity(String name, Augmentations augmentations) 88 throws XNIException { 89 if (fDTDHandler != null) { 90 fDTDHandler.endParameterEntity(name, augmentations); 91 } 92 } 93 startExternalSubset(XMLResourceIdentifier identifier, Augmentations augmentations)94 public void startExternalSubset(XMLResourceIdentifier identifier, 95 Augmentations augmentations) throws XNIException { 96 if (fDTDHandler != null) { 97 fDTDHandler.startExternalSubset(identifier, augmentations); 98 } 99 } 100 endExternalSubset(Augmentations augmentations)101 public void endExternalSubset(Augmentations augmentations) 102 throws XNIException { 103 if (fDTDHandler != null) { 104 fDTDHandler.endExternalSubset(augmentations); 105 } 106 } 107 comment(XMLString text, Augmentations augmentations)108 public void comment(XMLString text, Augmentations augmentations) 109 throws XNIException { 110 if (fDTDHandler != null) { 111 fDTDHandler.comment(text, augmentations); 112 } 113 } 114 processingInstruction(String target, XMLString data, Augmentations augmentations)115 public void processingInstruction(String target, XMLString data, 116 Augmentations augmentations) throws XNIException { 117 if (fDTDHandler != null) { 118 fDTDHandler.processingInstruction(target, data, augmentations); 119 } 120 } 121 elementDecl(String name, String contentModel, Augmentations augmentations)122 public void elementDecl(String name, String contentModel, 123 Augmentations augmentations) throws XNIException { 124 if (fDTDHandler != null) { 125 fDTDHandler.elementDecl(name, contentModel, augmentations); 126 } 127 } 128 startAttlist(String elementName, Augmentations augmentations)129 public void startAttlist(String elementName, Augmentations augmentations) 130 throws XNIException { 131 if (fDTDHandler != null) { 132 fDTDHandler.startAttlist(elementName, augmentations); 133 } 134 } 135 attributeDecl(String elementName, String attributeName, String type, String[] enumeration, String defaultType, XMLString defaultValue, XMLString nonNormalizedDefaultValue, Augmentations augmentations)136 public void attributeDecl(String elementName, String attributeName, 137 String type, String[] enumeration, String defaultType, 138 XMLString defaultValue, XMLString nonNormalizedDefaultValue, 139 Augmentations augmentations) throws XNIException { 140 if (fDTDHandler != null) { 141 fDTDHandler.attributeDecl(elementName, attributeName, 142 type, enumeration, defaultType, 143 defaultValue, nonNormalizedDefaultValue, 144 augmentations); 145 } 146 } 147 endAttlist(Augmentations augmentations)148 public void endAttlist(Augmentations augmentations) throws XNIException { 149 if (fDTDHandler != null) { 150 fDTDHandler.endAttlist(augmentations); 151 } 152 } 153 internalEntityDecl(String name, XMLString text, XMLString nonNormalizedText, Augmentations augmentations)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 externalEntityDecl(String name, XMLResourceIdentifier identifier, Augmentations augmentations)163 public void externalEntityDecl(String name, 164 XMLResourceIdentifier identifier, Augmentations augmentations) 165 throws XNIException { 166 if (fDTDHandler != null) { 167 fDTDHandler.externalEntityDecl(name, identifier, augmentations); 168 } 169 } 170 unparsedEntityDecl(String name, XMLResourceIdentifier identifier, String notation, Augmentations augmentations)171 public void unparsedEntityDecl(String name, 172 XMLResourceIdentifier identifier, String notation, 173 Augmentations augmentations) throws XNIException { 174 if (fUnparsedEntities == null) { 175 fUnparsedEntities = new HashMap(); 176 } 177 fUnparsedEntities.put(name, name); 178 if (fDTDHandler != null) { 179 fDTDHandler.unparsedEntityDecl(name, identifier, notation, augmentations); 180 } 181 } 182 notationDecl(String name, XMLResourceIdentifier identifier, Augmentations augmentations)183 public void notationDecl(String name, XMLResourceIdentifier identifier, 184 Augmentations augmentations) throws XNIException { 185 if (fDTDHandler != null) { 186 fDTDHandler.notationDecl(name, identifier, augmentations); 187 } 188 } 189 startConditional(short type, Augmentations augmentations)190 public void startConditional(short type, Augmentations augmentations) 191 throws XNIException { 192 if (fDTDHandler != null) { 193 fDTDHandler.startConditional(type, augmentations); 194 } 195 } 196 ignoredCharacters(XMLString text, Augmentations augmentations)197 public void ignoredCharacters(XMLString text, Augmentations augmentations) 198 throws XNIException { 199 if (fDTDHandler != null) { 200 fDTDHandler.ignoredCharacters(text, augmentations); 201 } 202 203 } 204 endConditional(Augmentations augmentations)205 public void endConditional(Augmentations augmentations) throws XNIException { 206 if (fDTDHandler != null) { 207 fDTDHandler.endConditional(augmentations); 208 } 209 } 210 endDTD(Augmentations augmentations)211 public void endDTD(Augmentations augmentations) throws XNIException { 212 if (fDTDHandler != null) { 213 fDTDHandler.endDTD(augmentations); 214 } 215 } 216 setDTDSource(XMLDTDSource source)217 public void setDTDSource(XMLDTDSource source) { 218 fDTDSource = source; 219 } 220 getDTDSource()221 public XMLDTDSource getDTDSource() { 222 return fDTDSource; 223 } 224 225 /* 226 * XMLDTDSource methods 227 */ 228 setDTDHandler(XMLDTDHandler handler)229 public void setDTDHandler(XMLDTDHandler handler) { 230 fDTDHandler = handler; 231 } 232 getDTDHandler()233 public XMLDTDHandler getDTDHandler() { 234 return fDTDHandler; 235 } 236 237 /* 238 * EntityState methods 239 */ 240 isEntityDeclared(String name)241 public boolean isEntityDeclared(String name) { 242 return false; 243 } 244 isEntityUnparsed(String name)245 public boolean isEntityUnparsed(String name) { 246 if (fUnparsedEntities != null) { 247 return fUnparsedEntities.containsKey(name); 248 } 249 return false; 250 } 251 252 /* 253 * Other methods 254 */ 255 reset()256 public void reset() { 257 if (fUnparsedEntities != null && !fUnparsedEntities.isEmpty()) { 258 // should only clear this if the last document contained unparsed entities 259 fUnparsedEntities.clear(); 260 } 261 } 262 } 263