jaxp/src/java.xml/share/classes/com/sun/org/apache/xerces/internal/xinclude/XIncludeHandler.java
changeset 39907 db51759e3695
parent 39080 57563e513b11
child 40582 1dddef49982c
equal deleted inserted replaced
39906:230d872f56ea 39907:db51759e3695
     1 /*
     1 /*
     2  * reserved comment block
     2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT REMOVE OR ALTER!
       
     4  */
     3  */
     5 /*
     4 /*
     6  * Copyright 2003-2005 The Apache Software Foundation.
     5  * Licensed to the Apache Software Foundation (ASF) under one or more
     7  *
     6  * contributor license agreements.  See the NOTICE file distributed with
     8  * Licensed under the Apache License, Version 2.0 (the "License");
     7  * this work for additional information regarding copyright ownership.
     9  * you may not use this file except in compliance with the License.
     8  * The ASF licenses this file to You under the Apache License, Version 2.0
    10  * You may obtain a copy of the License at
     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  *
    11  *
    12  *      http://www.apache.org/licenses/LICENSE-2.0
    12  *      http://www.apache.org/licenses/LICENSE-2.0
    13  *
    13  *
    14  * Unless required by applicable law or agreed to in writing, software
    14  * Unless required by applicable law or agreed to in writing, software
    15  * distributed under the License is distributed on an "AS IS" BASIS,
    15  * distributed under the License is distributed on an "AS IS" BASIS,
    16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    17  * See the License for the specific language governing permissions and
    17  * See the License for the specific language governing permissions and
    18  * limitations under the License.
    18  * limitations under the License.
    19  */
    19  */
       
    20 
    20 package com.sun.org.apache.xerces.internal.xinclude;
    21 package com.sun.org.apache.xerces.internal.xinclude;
    21 
    22 
    22 import java.io.CharConversionException;
    23 import java.io.CharConversionException;
    23 import java.io.IOException;
    24 import java.io.IOException;
    24 import java.util.ArrayList;
    25 import java.util.ArrayList;
    68 import com.sun.org.apache.xerces.internal.xpointer.XPointerHandler;
    69 import com.sun.org.apache.xerces.internal.xpointer.XPointerHandler;
    69 import com.sun.org.apache.xerces.internal.xpointer.XPointerProcessor;
    70 import com.sun.org.apache.xerces.internal.xpointer.XPointerProcessor;
    70 import com.sun.org.apache.xerces.internal.utils.ObjectFactory;
    71 import com.sun.org.apache.xerces.internal.utils.ObjectFactory;
    71 import com.sun.org.apache.xerces.internal.utils.XMLSecurityPropertyManager;
    72 import com.sun.org.apache.xerces.internal.utils.XMLSecurityPropertyManager;
    72 import java.util.Objects;
    73 import java.util.Objects;
       
    74 import javax.xml.catalog.CatalogException;
       
    75 import javax.xml.catalog.CatalogFeatures;
       
    76 import javax.xml.catalog.CatalogManager;
       
    77 import javax.xml.catalog.CatalogResolver;
       
    78 import javax.xml.catalog.CatalogUriResolver;
       
    79 import javax.xml.transform.Source;
       
    80 import jdk.xml.internal.JdkXmlUtils;
       
    81 import org.xml.sax.InputSource;
    73 
    82 
    74 /**
    83 /**
    75  * <p>
    84  * <p>
    76  * This is a pipeline component which performs XInclude handling, according to the
    85  * This is a pipeline component which performs XInclude handling, according to the
    77  * W3C specification for XML Inclusions.
    86  * W3C specification for XML Inclusions.
   245     /** Recognized properties. */
   254     /** Recognized properties. */
   246     private static final String[] RECOGNIZED_PROPERTIES =
   255     private static final String[] RECOGNIZED_PROPERTIES =
   247         { ERROR_REPORTER, ENTITY_RESOLVER, SECURITY_MANAGER, BUFFER_SIZE };
   256         { ERROR_REPORTER, ENTITY_RESOLVER, SECURITY_MANAGER, BUFFER_SIZE };
   248 
   257 
   249     /** Property defaults. */
   258     /** Property defaults. */
   250     private static final Object[] PROPERTY_DEFAULTS = { null, null, null, new Integer(XMLEntityManager.DEFAULT_BUFFER_SIZE) };
   259     private static final Object[] PROPERTY_DEFAULTS = { null, null, null, XMLEntityManager.DEFAULT_BUFFER_SIZE};
   251 
   260 
   252     // instance variables
   261     // instance variables
   253 
   262 
   254     // for XMLDocumentFilter
   263     // for XMLDocumentFilter
   255     protected XMLDocumentHandler fDocumentHandler;
   264     protected XMLDocumentHandler fDocumentHandler;
   333 
   342 
   334     // The state of the processor at each given depth.
   343     // The state of the processor at each given depth.
   335     private int[] fState = new int[INITIAL_SIZE];
   344     private int[] fState = new int[INITIAL_SIZE];
   336 
   345 
   337     // buffering the necessary DTD events
   346     // buffering the necessary DTD events
   338     private ArrayList fNotations;
   347     private final ArrayList<Notation> fNotations;
   339     private ArrayList fUnparsedEntities;
   348     private final ArrayList<UnparsedEntity> fUnparsedEntities;
   340 
   349 
   341     // flags which control whether base URI or language fixup is performed.
   350     // flags which control whether base URI or language fixup is performed.
   342     private boolean fFixupBaseURIs = true;
   351     private boolean fFixupBaseURIs = true;
   343     private boolean fFixupLanguage = true;
   352     private boolean fFixupLanguage = true;
   344 
   353 
   356     private boolean fSeenRootElement;
   365     private boolean fSeenRootElement;
   357 
   366 
   358     // track whether the child config needs its features refreshed
   367     // track whether the child config needs its features refreshed
   359     private boolean fNeedCopyFeatures = true;
   368     private boolean fNeedCopyFeatures = true;
   360 
   369 
       
   370     /** indicate whether Catalog should be used for resolving external resources */
       
   371     private boolean fUseCatalog = true;
       
   372     CatalogFeatures fCatalogFeatures;
       
   373     CatalogResolver fCatalogResolver;
       
   374     CatalogUriResolver fCatalogUriResolver;
       
   375 
       
   376     private String fCatalogFile;
       
   377     private String fDefer;
       
   378     private String fPrefer;
       
   379     private String fResolve;
       
   380 
   361     // Constructors
   381     // Constructors
   362 
   382 
   363     public XIncludeHandler() {
   383     public XIncludeHandler() {
   364         fDepth = 0;
   384         fDepth = 0;
   365 
   385 
   366         fSawFallback[fDepth] = false;
   386         fSawFallback[fDepth] = false;
   367         fSawInclude[fDepth] = false;
   387         fSawInclude[fDepth] = false;
   368         fState[fDepth] = STATE_NORMAL_PROCESSING;
   388         fState[fDepth] = STATE_NORMAL_PROCESSING;
   369         fNotations = new ArrayList();
   389         fNotations = new ArrayList<>();
   370         fUnparsedEntities = new ArrayList();
   390         fUnparsedEntities = new ArrayList<>();
   371 
   391 
   372         fBaseURIScope = new IntStack();
   392         fBaseURIScope = new IntStack();
   373         fBaseURI = new Stack();
   393         fBaseURI = new Stack();
   374         fLiteralSystemID = new Stack();
   394         fLiteralSystemID = new Stack();
   375         fExpandedSystemID = new Stack();
   395         fExpandedSystemID = new Stack();
   532         }
   552         }
   533 
   553 
   534         fSecurityPropertyMgr = (XMLSecurityPropertyManager)
   554         fSecurityPropertyMgr = (XMLSecurityPropertyManager)
   535                 componentManager.getProperty(Constants.XML_SECURITY_PROPERTY_MANAGER);
   555                 componentManager.getProperty(Constants.XML_SECURITY_PROPERTY_MANAGER);
   536 
   556 
       
   557         //Use Catalog
       
   558         fUseCatalog = componentManager.getFeature(XMLConstants.USE_CATALOG);
       
   559         fCatalogFile = (String)componentManager.getProperty(CatalogFeatures.Feature.FILES.getPropertyName());
       
   560         fDefer = (String)componentManager.getProperty(CatalogFeatures.Feature.DEFER.getPropertyName());
       
   561         fPrefer = (String)componentManager.getProperty(CatalogFeatures.Feature.PREFER.getPropertyName());
       
   562         fResolve = (String)componentManager.getProperty(CatalogFeatures.Feature.RESOLVE.getPropertyName());
       
   563 
   537         // Get buffer size.
   564         // Get buffer size.
   538         try {
   565         try {
   539             Integer value =
   566             Integer value =
   540                 (Integer)componentManager.getProperty(
   567                 (Integer)componentManager.getProperty(
   541                     BUFFER_SIZE);
   568                     BUFFER_SIZE);
   542 
   569 
   543             if (value != null && value.intValue() > 0) {
   570             if (value != null && value > 0) {
   544                 fBufferSize = value.intValue();
   571                 fBufferSize = value;
   545                 if (fChildConfig != null) {
   572                 if (fChildConfig != null) {
   546                     fChildConfig.setProperty(BUFFER_SIZE, value);
   573                     fChildConfig.setProperty(BUFFER_SIZE, value);
   547                 }
   574                 }
   548             }
   575             }
   549             else {
   576             else {
   550                 fBufferSize = ((Integer)getPropertyDefault(BUFFER_SIZE)).intValue();
   577                 fBufferSize = ((Integer)getPropertyDefault(BUFFER_SIZE));
   551             }
   578             }
   552         }
   579         }
   553         catch (XMLConfigurationException e) {
   580         catch (XMLConfigurationException e) {
   554                 fBufferSize = ((Integer)getPropertyDefault(BUFFER_SIZE)).intValue();
   581             fBufferSize = ((Integer)getPropertyDefault(BUFFER_SIZE));
   555         }
   582         }
   556 
   583 
   557         // Reset XML 1.0 text reader.
   584         // Reset XML 1.0 text reader.
   558         if (fXInclude10TextReader != null) {
   585         if (fXInclude10TextReader != null) {
   559                 fXInclude10TextReader.setBufferSize(fBufferSize);
   586                 fXInclude10TextReader.setBufferSize(fBufferSize);
  1596                             fCurrentBaseURI.getExpandedSystemId(),
  1623                             fCurrentBaseURI.getExpandedSystemId(),
  1597                             false));
  1624                             false));
  1598 
  1625 
  1599                 includedSource =
  1626                 includedSource =
  1600                     fEntityResolver.resolveEntity(resourceIdentifier);
  1627                     fEntityResolver.resolveEntity(resourceIdentifier);
       
  1628 
       
  1629                 if (includedSource == null) {
       
  1630                     if (fCatalogFeatures == null) {
       
  1631                         fCatalogFeatures = JdkXmlUtils.getCatalogFeatures(fDefer, fCatalogFile, fPrefer, fResolve);
       
  1632                     }
       
  1633                     fCatalogFile = fCatalogFeatures.get(CatalogFeatures.Feature.FILES);
       
  1634                     if (fUseCatalog && fCatalogFile != null) {
       
  1635                         /*
       
  1636                            Although URI entry is preferred for resolving XInclude, system entry
       
  1637                            is allowed as well.
       
  1638                         */
       
  1639                         Source source = null;
       
  1640                         try {
       
  1641                             if (fCatalogUriResolver == null) {
       
  1642                                 fCatalogUriResolver = CatalogManager.catalogUriResolver(fCatalogFeatures);
       
  1643                             }
       
  1644                             source = fCatalogUriResolver.resolve(href, fCurrentBaseURI.getExpandedSystemId());
       
  1645                         } catch (CatalogException e) {}
       
  1646 
       
  1647                         if (source != null && !source.isEmpty()) {
       
  1648                             includedSource = new XMLInputSource(null, source.getSystemId(),
       
  1649                                     fCurrentBaseURI.getExpandedSystemId(), true);
       
  1650                         } else {
       
  1651                             if (fCatalogResolver == null) {
       
  1652                                 fCatalogResolver = CatalogManager.catalogResolver(fCatalogFeatures);
       
  1653                             }
       
  1654                             InputSource is = fCatalogResolver.resolveEntity(href, href);
       
  1655                             if (is != null && !is.isEmpty()) {
       
  1656                                 includedSource = new XMLInputSource(is, true);
       
  1657                             }
       
  1658                         }
       
  1659                     }
       
  1660                 }
  1601 
  1661 
  1602                 if (includedSource != null &&
  1662                 if (includedSource != null &&
  1603                     !(includedSource instanceof HTTPInputSource) &&
  1663                     !(includedSource instanceof HTTPInputSource) &&
  1604                     (accept != null || acceptLanguage != null) &&
  1664                     (accept != null || acceptLanguage != null) &&
  1605                     includedSource.getCharacterStream() == null &&
  1665                     includedSource.getCharacterStream() == null &&