1 /*
   2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
   3  */
   4 /*
   5  * Licensed to the Apache Software Foundation (ASF) under one or more
   6  * contributor license agreements.  See the NOTICE file distributed with
   7  * this work for additional information regarding copyright ownership.
   8  * The ASF licenses this file to You under the Apache License, Version 2.0
   9  * (the "License"); you may not use this file except in compliance with
  10  * the License.  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 com.sun.org.apache.xerces.internal.impl.validation.EntityState;
  24 import com.sun.org.apache.xerces.internal.impl.validation.ValidationManager;
  25 import com.sun.org.apache.xerces.internal.xni.Augmentations;
  26 import com.sun.org.apache.xerces.internal.xni.XMLDTDHandler;
  27 import com.sun.org.apache.xerces.internal.xni.XMLLocator;
  28 import com.sun.org.apache.xerces.internal.xni.XMLResourceIdentifier;
  29 import com.sun.org.apache.xerces.internal.xni.XMLString;
  30 import com.sun.org.apache.xerces.internal.xni.XNIException;
  31 import com.sun.org.apache.xerces.internal.xni.parser.XMLDTDFilter;
  32 import com.sun.org.apache.xerces.internal.xni.parser.XMLDTDSource;
  33 import java.util.HashMap;
  34 import java.util.Map;
  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  * @LastModified: Oct 2017
  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 Map<String, String> fUnparsedEntities = null;
  55 
  56     UnparsedEntityHandler(ValidationManager manager) {
  57         fValidationManager = manager;
  58     }
  59 
  60     /*
  61      * XMLDTDHandler methods
  62      */
  63 
  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 
  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 
  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 
  87     public void endParameterEntity(String name, Augmentations augmentations)
  88             throws XNIException {
  89         if (fDTDHandler != null) {
  90             fDTDHandler.endParameterEntity(name, augmentations);
  91         }
  92     }
  93 
  94     public void startExternalSubset(XMLResourceIdentifier identifier,
  95             Augmentations augmentations) throws XNIException {
  96         if (fDTDHandler != null) {
  97             fDTDHandler.startExternalSubset(identifier, augmentations);
  98         }
  99     }
 100 
 101     public void endExternalSubset(Augmentations augmentations)
 102             throws XNIException {
 103         if (fDTDHandler != null) {
 104             fDTDHandler.endExternalSubset(augmentations);
 105         }
 106     }
 107 
 108     public void comment(XMLString text, Augmentations augmentations)
 109             throws XNIException {
 110         if (fDTDHandler != null) {
 111             fDTDHandler.comment(text, augmentations);
 112         }
 113     }
 114 
 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 
 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 
 129     public void startAttlist(String elementName, Augmentations augmentations)
 130             throws XNIException {
 131         if (fDTDHandler != null) {
 132             fDTDHandler.startAttlist(elementName, augmentations);
 133         }
 134     }
 135 
 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 
 148     public void endAttlist(Augmentations augmentations) 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, Augmentations augmentations)
 165             throws XNIException {
 166         if (fDTDHandler != null) {
 167             fDTDHandler.externalEntityDecl(name, identifier, augmentations);
 168         }
 169     }
 170 
 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 
 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 
 190     public void startConditional(short type, Augmentations augmentations)
 191             throws XNIException {
 192         if (fDTDHandler != null) {
 193             fDTDHandler.startConditional(type, augmentations);
 194         }
 195     }
 196 
 197     public void ignoredCharacters(XMLString text, Augmentations augmentations)
 198             throws XNIException {
 199         if (fDTDHandler != null) {
 200             fDTDHandler.ignoredCharacters(text, augmentations);
 201         }
 202 
 203     }
 204 
 205     public void endConditional(Augmentations augmentations) throws XNIException {
 206         if (fDTDHandler != null) {
 207             fDTDHandler.endConditional(augmentations);
 208         }
 209     }
 210 
 211     public void endDTD(Augmentations augmentations) throws XNIException {
 212         if (fDTDHandler != null) {
 213             fDTDHandler.endDTD(augmentations);
 214         }
 215     }
 216 
 217     public void setDTDSource(XMLDTDSource source) {
 218         fDTDSource = source;
 219     }
 220 
 221     public XMLDTDSource getDTDSource() {
 222         return fDTDSource;
 223     }
 224 
 225     /*
 226      * XMLDTDSource methods
 227      */
 228 
 229     public void setDTDHandler(XMLDTDHandler handler) {
 230         fDTDHandler = handler;
 231     }
 232 
 233     public XMLDTDHandler getDTDHandler() {
 234         return fDTDHandler;
 235     }
 236 
 237     /*
 238      * EntityState methods
 239      */
 240 
 241     public boolean isEntityDeclared(String name) {
 242         return false;
 243     }
 244 
 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 
 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 }