1 /*
2 * Copyright (c) 2016, 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.xinclude;
22
58 import com.sun.org.apache.xerces.internal.xni.XNIException;
59 import com.sun.org.apache.xerces.internal.xni.parser.XMLComponent;
60 import com.sun.org.apache.xerces.internal.xni.parser.XMLComponentManager;
61 import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException;
62 import com.sun.org.apache.xerces.internal.xni.parser.XMLDTDFilter;
63 import com.sun.org.apache.xerces.internal.xni.parser.XMLDTDSource;
64 import com.sun.org.apache.xerces.internal.xni.parser.XMLDocumentFilter;
65 import com.sun.org.apache.xerces.internal.xni.parser.XMLDocumentSource;
66 import com.sun.org.apache.xerces.internal.xni.parser.XMLEntityResolver;
67 import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource;
68 import com.sun.org.apache.xerces.internal.xni.parser.XMLParserConfiguration;
69 import com.sun.org.apache.xerces.internal.xpointer.XPointerHandler;
70 import com.sun.org.apache.xerces.internal.xpointer.XPointerProcessor;
71 import com.sun.org.apache.xerces.internal.utils.ObjectFactory;
72 import com.sun.org.apache.xerces.internal.utils.XMLSecurityPropertyManager;
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;
82
83 /**
84 * <p>
85 * This is a pipeline component which performs XInclude handling, according to the
86 * W3C specification for XML Inclusions.
87 * </p>
88 * <p>
89 * This component analyzes each event in the pipeline, looking for <include>
90 * elements. An <include> element is one which has a namespace of
91 * <code>http://www.w3.org/2001/XInclude</code> and a localname of <code>include</code>.
92 * When it finds an <include> element, it attempts to include the file specified
93 * in the <code>href</code> attribute of the element. If inclusion succeeds, all
94 * children of the <include> element are ignored (with the exception of
95 * checking for invalid children as outlined in the specification). If the inclusion
96 * fails, the <fallback> child of the <include> element is processed.
97 * </p>
98 * <p>
354 // for SAX compatibility.
355 // Has the value of the ALLOW_UE_AND_NOTATION_EVENTS feature
356 private boolean fSendUEAndNotationEvents;
357
358 // track the version of the document being parsed
359 private boolean fIsXML11;
360
361 // track whether a DTD is being parsed
362 private boolean fInDTD;
363
364 // track whether the root element of the result infoset has been processed
365 private boolean fSeenRootElement;
366
367 // track whether the child config needs its features refreshed
368 private boolean fNeedCopyFeatures = true;
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
381 // Constructors
382
383 public XIncludeHandler() {
384 fDepth = 0;
385
386 fSawFallback[fDepth] = false;
387 fSawInclude[fDepth] = false;
388 fState[fDepth] = STATE_NORMAL_PROCESSING;
389 fNotations = new ArrayList<>();
390 fUnparsedEntities = new ArrayList<>();
391
392 fBaseURIScope = new IntStack();
393 fBaseURI = new Stack();
394 fLiteralSystemID = new Stack();
1621 XMLEntityManager.expandSystemId(
1622 href,
1623 fCurrentBaseURI.getExpandedSystemId(),
1624 false));
1625
1626 includedSource =
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 }
1661
1662 if (includedSource != null &&
1663 !(includedSource instanceof HTTPInputSource) &&
1664 (accept != null || acceptLanguage != null) &&
1665 includedSource.getCharacterStream() == null &&
1666 includedSource.getByteStream() == null) {
1667
1668 includedSource = createInputSource(includedSource.getPublicId(), includedSource.getSystemId(),
1669 includedSource.getBaseSystemId(), accept, acceptLanguage);
1670 }
1671 }
1672 catch (IOException e) {
1673 reportResourceError(
1674 "XMLResourceError",
1675 new Object[] { href, e.getMessage()});
1676 return false;
1677 }
1678 }
1679
1680 if (includedSource == null) {
1681 // setup an HTTPInputSource if either of the content negotation attributes were specified.
1682 if (accept != null || acceptLanguage != null) {
1683 includedSource = createInputSource(null, href, fCurrentBaseURI.getExpandedSystemId(), accept, acceptLanguage);
1684 }
1685 else {
1686 includedSource = new XMLInputSource(null, href, fCurrentBaseURI.getExpandedSystemId(), false);
1687 }
1688 }
1689
1690 if (parse.equals(XINCLUDE_PARSE_XML)) {
1691 // Instead of always creating a new configuration, the first one can be reused
1692 if ((xpointer != null && fXPointerChildConfig == null)
|
1 /*
2 * Copyright (c) 2006, 2016, 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.xinclude;
22
58 import com.sun.org.apache.xerces.internal.xni.XNIException;
59 import com.sun.org.apache.xerces.internal.xni.parser.XMLComponent;
60 import com.sun.org.apache.xerces.internal.xni.parser.XMLComponentManager;
61 import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException;
62 import com.sun.org.apache.xerces.internal.xni.parser.XMLDTDFilter;
63 import com.sun.org.apache.xerces.internal.xni.parser.XMLDTDSource;
64 import com.sun.org.apache.xerces.internal.xni.parser.XMLDocumentFilter;
65 import com.sun.org.apache.xerces.internal.xni.parser.XMLDocumentSource;
66 import com.sun.org.apache.xerces.internal.xni.parser.XMLEntityResolver;
67 import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource;
68 import com.sun.org.apache.xerces.internal.xni.parser.XMLParserConfiguration;
69 import com.sun.org.apache.xerces.internal.xpointer.XPointerHandler;
70 import com.sun.org.apache.xerces.internal.xpointer.XPointerProcessor;
71 import com.sun.org.apache.xerces.internal.utils.ObjectFactory;
72 import com.sun.org.apache.xerces.internal.utils.XMLSecurityPropertyManager;
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.transform.Source;
79 import jdk.xml.internal.JdkXmlUtils;
80 import org.xml.sax.InputSource;
81
82 /**
83 * <p>
84 * This is a pipeline component which performs XInclude handling, according to the
85 * W3C specification for XML Inclusions.
86 * </p>
87 * <p>
88 * This component analyzes each event in the pipeline, looking for <include>
89 * elements. An <include> element is one which has a namespace of
90 * <code>http://www.w3.org/2001/XInclude</code> and a localname of <code>include</code>.
91 * When it finds an <include> element, it attempts to include the file specified
92 * in the <code>href</code> attribute of the element. If inclusion succeeds, all
93 * children of the <include> element are ignored (with the exception of
94 * checking for invalid children as outlined in the specification). If the inclusion
95 * fails, the <fallback> child of the <include> element is processed.
96 * </p>
97 * <p>
353 // for SAX compatibility.
354 // Has the value of the ALLOW_UE_AND_NOTATION_EVENTS feature
355 private boolean fSendUEAndNotationEvents;
356
357 // track the version of the document being parsed
358 private boolean fIsXML11;
359
360 // track whether a DTD is being parsed
361 private boolean fInDTD;
362
363 // track whether the root element of the result infoset has been processed
364 private boolean fSeenRootElement;
365
366 // track whether the child config needs its features refreshed
367 private boolean fNeedCopyFeatures = true;
368
369 /** indicate whether Catalog should be used for resolving external resources */
370 private boolean fUseCatalog = true;
371 CatalogFeatures fCatalogFeatures;
372 CatalogResolver fCatalogResolver;
373
374 private String fCatalogFile;
375 private String fDefer;
376 private String fPrefer;
377 private String fResolve;
378
379 // Constructors
380
381 public XIncludeHandler() {
382 fDepth = 0;
383
384 fSawFallback[fDepth] = false;
385 fSawInclude[fDepth] = false;
386 fState[fDepth] = STATE_NORMAL_PROCESSING;
387 fNotations = new ArrayList<>();
388 fUnparsedEntities = new ArrayList<>();
389
390 fBaseURIScope = new IntStack();
391 fBaseURI = new Stack();
392 fLiteralSystemID = new Stack();
1619 XMLEntityManager.expandSystemId(
1620 href,
1621 fCurrentBaseURI.getExpandedSystemId(),
1622 false));
1623
1624 includedSource =
1625 fEntityResolver.resolveEntity(resourceIdentifier);
1626
1627 if (includedSource == null) {
1628 if (fCatalogFeatures == null) {
1629 fCatalogFeatures = JdkXmlUtils.getCatalogFeatures(fDefer, fCatalogFile, fPrefer, fResolve);
1630 }
1631 fCatalogFile = fCatalogFeatures.get(CatalogFeatures.Feature.FILES);
1632 if (fUseCatalog && fCatalogFile != null) {
1633 /*
1634 Although URI entry is preferred for resolving XInclude, system entry
1635 is allowed as well.
1636 */
1637 Source source = null;
1638 try {
1639 if (fCatalogResolver == null) {
1640 fCatalogResolver = CatalogManager.catalogResolver(fCatalogFeatures);
1641 }
1642 source = fCatalogResolver.resolve(href, fCurrentBaseURI.getExpandedSystemId());
1643 } catch (CatalogException e) {}
1644
1645 if (source != null && !source.isEmpty()) {
1646 includedSource = new XMLInputSource(null, source.getSystemId(),
1647 fCurrentBaseURI.getExpandedSystemId(), true);
1648 } else {
1649 if (fCatalogResolver == null) {
1650 fCatalogResolver = CatalogManager.catalogResolver(fCatalogFeatures);
1651 }
1652 InputSource is = fCatalogResolver.resolveEntity(href, href);
1653 if (is != null && !is.isEmpty()) {
1654 includedSource = new XMLInputSource(is, true);
1655 }
1656 }
1657 }
1658 }
1659
1660 if (includedSource != null &&
1661 !(includedSource instanceof HTTPInputSource) &&
1662 (accept != null || acceptLanguage != null) &&
1663 includedSource.getCharacterStream() == null &&
1664 includedSource.getByteStream() == null) {
1665
1666 includedSource = createInputSource(includedSource.getPublicId(), includedSource.getSystemId(),
1667 includedSource.getBaseSystemId(), accept, acceptLanguage);
1668 }
1669 }
1670 catch (IOException | CatalogException e) {
1671 reportResourceError(
1672 "XMLResourceError",
1673 new Object[] { href, e.getMessage()});
1674 return false;
1675 }
1676 }
1677
1678 if (includedSource == null) {
1679 // setup an HTTPInputSource if either of the content negotation attributes were specified.
1680 if (accept != null || acceptLanguage != null) {
1681 includedSource = createInputSource(null, href, fCurrentBaseURI.getExpandedSystemId(), accept, acceptLanguage);
1682 }
1683 else {
1684 includedSource = new XMLInputSource(null, href, fCurrentBaseURI.getExpandedSystemId(), false);
1685 }
1686 }
1687
1688 if (parse.equals(XINCLUDE_PARSE_XML)) {
1689 // Instead of always creating a new configuration, the first one can be reused
1690 if ((xpointer != null && fXPointerChildConfig == null)
|