1 /* |
1 /* |
2 * Copyright (c) 2006, 2017, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2006, 2017, Oracle and/or its affiliates. All rights reserved. |
|
3 * @LastModified: Oct 2017 |
|
4 * @modifiedBy: Oracle Inc. |
|
5 * @modifiedOn: Nov 2015, Sept 2017 |
3 */ |
6 */ |
4 /* |
7 /* |
5 * Licensed to the Apache Software Foundation (ASF) under one or more |
8 * Licensed to the Apache Software Foundation (ASF) under one or more |
6 * contributor license agreements. See the NOTICE file distributed with |
9 * contributor license agreements. See the NOTICE file distributed with |
7 * this work for additional information regarding copyright ownership. |
10 * this work for additional information regarding copyright ownership. |
18 * limitations under the License. |
21 * limitations under the License. |
19 */ |
22 */ |
20 |
23 |
21 package com.sun.org.apache.xerces.internal.xinclude; |
24 package com.sun.org.apache.xerces.internal.xinclude; |
22 |
25 |
23 import java.io.CharConversionException; |
|
24 import java.io.IOException; |
|
25 import java.util.ArrayList; |
|
26 import java.util.Enumeration; |
|
27 import java.util.Locale; |
|
28 import java.util.Stack; |
|
29 import java.util.StringTokenizer; |
|
30 import javax.xml.XMLConstants; |
|
31 |
|
32 import com.sun.org.apache.xerces.internal.impl.Constants; |
26 import com.sun.org.apache.xerces.internal.impl.Constants; |
33 import com.sun.org.apache.xerces.internal.impl.XMLEntityManager; |
27 import com.sun.org.apache.xerces.internal.impl.XMLEntityManager; |
34 import com.sun.org.apache.xerces.internal.impl.XMLErrorReporter; |
28 import com.sun.org.apache.xerces.internal.impl.XMLErrorReporter; |
35 import com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException; |
29 import com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException; |
36 import com.sun.org.apache.xerces.internal.impl.msg.XMLMessageFormatter; |
30 import com.sun.org.apache.xerces.internal.impl.msg.XMLMessageFormatter; |
39 import com.sun.org.apache.xerces.internal.util.AugmentationsImpl; |
33 import com.sun.org.apache.xerces.internal.util.AugmentationsImpl; |
40 import com.sun.org.apache.xerces.internal.util.HTTPInputSource; |
34 import com.sun.org.apache.xerces.internal.util.HTTPInputSource; |
41 import com.sun.org.apache.xerces.internal.util.IntStack; |
35 import com.sun.org.apache.xerces.internal.util.IntStack; |
42 import com.sun.org.apache.xerces.internal.util.ParserConfigurationSettings; |
36 import com.sun.org.apache.xerces.internal.util.ParserConfigurationSettings; |
43 import com.sun.org.apache.xerces.internal.util.SymbolTable; |
37 import com.sun.org.apache.xerces.internal.util.SymbolTable; |
|
38 import com.sun.org.apache.xerces.internal.util.URI.MalformedURIException; |
44 import com.sun.org.apache.xerces.internal.util.URI; |
39 import com.sun.org.apache.xerces.internal.util.URI; |
45 import com.sun.org.apache.xerces.internal.util.XMLAttributesImpl; |
40 import com.sun.org.apache.xerces.internal.util.XMLAttributesImpl; |
|
41 import com.sun.org.apache.xerces.internal.util.XMLChar; |
46 import com.sun.org.apache.xerces.internal.util.XMLResourceIdentifierImpl; |
42 import com.sun.org.apache.xerces.internal.util.XMLResourceIdentifierImpl; |
47 import com.sun.org.apache.xerces.internal.util.XMLChar; |
|
48 import com.sun.org.apache.xerces.internal.util.XMLSymbols; |
43 import com.sun.org.apache.xerces.internal.util.XMLSymbols; |
49 import com.sun.org.apache.xerces.internal.util.URI.MalformedURIException; |
44 import com.sun.org.apache.xerces.internal.utils.ObjectFactory; |
50 import com.sun.org.apache.xerces.internal.utils.XMLSecurityManager; |
45 import com.sun.org.apache.xerces.internal.utils.XMLSecurityManager; |
|
46 import com.sun.org.apache.xerces.internal.utils.XMLSecurityPropertyManager; |
51 import com.sun.org.apache.xerces.internal.xni.Augmentations; |
47 import com.sun.org.apache.xerces.internal.xni.Augmentations; |
52 import com.sun.org.apache.xerces.internal.xni.NamespaceContext; |
48 import com.sun.org.apache.xerces.internal.xni.NamespaceContext; |
53 import com.sun.org.apache.xerces.internal.xni.QName; |
49 import com.sun.org.apache.xerces.internal.xni.QName; |
54 import com.sun.org.apache.xerces.internal.xni.XMLAttributes; |
50 import com.sun.org.apache.xerces.internal.xni.XMLAttributes; |
55 import com.sun.org.apache.xerces.internal.xni.XMLDTDHandler; |
51 import com.sun.org.apache.xerces.internal.xni.XMLDTDHandler; |
68 import com.sun.org.apache.xerces.internal.xni.parser.XMLEntityResolver; |
64 import com.sun.org.apache.xerces.internal.xni.parser.XMLEntityResolver; |
69 import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource; |
65 import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource; |
70 import com.sun.org.apache.xerces.internal.xni.parser.XMLParserConfiguration; |
66 import com.sun.org.apache.xerces.internal.xni.parser.XMLParserConfiguration; |
71 import com.sun.org.apache.xerces.internal.xpointer.XPointerHandler; |
67 import com.sun.org.apache.xerces.internal.xpointer.XPointerHandler; |
72 import com.sun.org.apache.xerces.internal.xpointer.XPointerProcessor; |
68 import com.sun.org.apache.xerces.internal.xpointer.XPointerProcessor; |
73 import com.sun.org.apache.xerces.internal.utils.ObjectFactory; |
69 import java.io.CharConversionException; |
74 import com.sun.org.apache.xerces.internal.utils.XMLSecurityPropertyManager; |
70 import java.io.IOException; |
|
71 import java.util.ArrayList; |
|
72 import java.util.Enumeration; |
|
73 import java.util.List; |
|
74 import java.util.Locale; |
75 import java.util.Objects; |
75 import java.util.Objects; |
|
76 import java.util.Stack; |
|
77 import java.util.StringTokenizer; |
|
78 import javax.xml.XMLConstants; |
76 import javax.xml.catalog.CatalogException; |
79 import javax.xml.catalog.CatalogException; |
77 import javax.xml.catalog.CatalogFeatures; |
80 import javax.xml.catalog.CatalogFeatures; |
78 import javax.xml.catalog.CatalogManager; |
81 import javax.xml.catalog.CatalogManager; |
79 import javax.xml.catalog.CatalogResolver; |
82 import javax.xml.catalog.CatalogResolver; |
80 import javax.xml.transform.Source; |
83 import javax.xml.transform.Source; |
304 protected XIncludeTextReader fXInclude11TextReader; |
307 protected XIncludeTextReader fXInclude11TextReader; |
305 |
308 |
306 // these are needed for XML Base processing |
309 // these are needed for XML Base processing |
307 protected XMLResourceIdentifier fCurrentBaseURI; |
310 protected XMLResourceIdentifier fCurrentBaseURI; |
308 protected IntStack fBaseURIScope; |
311 protected IntStack fBaseURIScope; |
309 protected Stack fBaseURI; |
312 protected Stack<String> fBaseURI; |
310 protected Stack fLiteralSystemID; |
313 protected Stack<String> fLiteralSystemID; |
311 protected Stack fExpandedSystemID; |
314 protected Stack<String> fExpandedSystemID; |
312 |
315 |
313 // these are needed for Language Fixup |
316 // these are needed for Language Fixup |
314 protected IntStack fLanguageScope; |
317 protected IntStack fLanguageScope; |
315 protected Stack fLanguageStack; |
318 protected Stack<String> fLanguageStack; |
316 protected String fCurrentLanguage; |
319 protected String fCurrentLanguage; |
317 |
320 |
318 // used for passing features on to child XIncludeHandler objects |
321 // used for passing features on to child XIncludeHandler objects |
319 protected ParserConfigurationSettings fSettings; |
322 protected ParserConfigurationSettings fSettings; |
320 |
323 |
341 |
344 |
342 // The state of the processor at each given depth. |
345 // The state of the processor at each given depth. |
343 private int[] fState = new int[INITIAL_SIZE]; |
346 private int[] fState = new int[INITIAL_SIZE]; |
344 |
347 |
345 // buffering the necessary DTD events |
348 // buffering the necessary DTD events |
346 private final ArrayList<Notation> fNotations; |
349 private final List<Notation> fNotations; |
347 private final ArrayList<UnparsedEntity> fUnparsedEntities; |
350 private final List<UnparsedEntity> fUnparsedEntities; |
348 |
351 |
349 // flags which control whether base URI or language fixup is performed. |
352 // flags which control whether base URI or language fixup is performed. |
350 private boolean fFixupBaseURIs = true; |
353 private boolean fFixupBaseURIs = true; |
351 private boolean fFixupLanguage = true; |
354 private boolean fFixupLanguage = true; |
352 |
355 |
386 fState[fDepth] = STATE_NORMAL_PROCESSING; |
389 fState[fDepth] = STATE_NORMAL_PROCESSING; |
387 fNotations = new ArrayList<>(); |
390 fNotations = new ArrayList<>(); |
388 fUnparsedEntities = new ArrayList<>(); |
391 fUnparsedEntities = new ArrayList<>(); |
389 |
392 |
390 fBaseURIScope = new IntStack(); |
393 fBaseURIScope = new IntStack(); |
391 fBaseURI = new Stack(); |
394 fBaseURI = new Stack<>(); |
392 fLiteralSystemID = new Stack(); |
395 fLiteralSystemID = new Stack<>(); |
393 fExpandedSystemID = new Stack(); |
396 fExpandedSystemID = new Stack<>(); |
394 fCurrentBaseURI = new XMLResourceIdentifierImpl(); |
397 fCurrentBaseURI = new XMLResourceIdentifierImpl(); |
395 |
398 |
396 fLanguageScope = new IntStack(); |
399 fLanguageScope = new IntStack(); |
397 fLanguageStack = new Stack(); |
400 fLanguageStack = new Stack<>(); |
398 fCurrentLanguage = null; |
401 fCurrentLanguage = null; |
399 } |
402 } |
400 |
403 |
401 // XMLComponent methods |
404 // XMLComponent methods |
402 |
405 |
2089 fCurrentLanguage); |
2092 fCurrentLanguage); |
2090 attributes.setSpecified(index, true); |
2093 attributes.setSpecified(index, true); |
2091 } |
2094 } |
2092 |
2095 |
2093 // Modify attributes of included items to do namespace-fixup. (spec 4.5.4) |
2096 // Modify attributes of included items to do namespace-fixup. (spec 4.5.4) |
2094 Enumeration inscopeNS = fNamespaceContext.getAllPrefixes(); |
2097 Enumeration<String> inscopeNS = fNamespaceContext.getAllPrefixes(); |
2095 while (inscopeNS.hasMoreElements()) { |
2098 while (inscopeNS.hasMoreElements()) { |
2096 String prefix = (String)inscopeNS.nextElement(); |
2099 String prefix = inscopeNS.nextElement(); |
2097 String parentURI = |
2100 String parentURI = |
2098 fNamespaceContext.getURIFromIncludeParent(prefix); |
2101 fNamespaceContext.getURIFromIncludeParent(prefix); |
2099 String uri = fNamespaceContext.getURI(prefix); |
2102 String uri = fNamespaceContext.getURI(prefix); |
2100 if (parentURI != uri && attributes != null) { |
2103 if (parentURI != uri && attributes != null) { |
2101 if (prefix == XMLSymbols.EMPTY_STRING) { |
2104 if (prefix == XMLSymbols.EMPTY_STRING) { |
2522 protected void checkUnparsedEntity(String entName) { |
2525 protected void checkUnparsedEntity(String entName) { |
2523 UnparsedEntity ent = new UnparsedEntity(); |
2526 UnparsedEntity ent = new UnparsedEntity(); |
2524 ent.name = entName; |
2527 ent.name = entName; |
2525 int index = fUnparsedEntities.indexOf(ent); |
2528 int index = fUnparsedEntities.indexOf(ent); |
2526 if (index != -1) { |
2529 if (index != -1) { |
2527 ent = (UnparsedEntity)fUnparsedEntities.get(index); |
2530 ent = fUnparsedEntities.get(index); |
2528 // first check the notation of the unparsed entity |
2531 // first check the notation of the unparsed entity |
2529 checkNotation(ent.notation); |
2532 checkNotation(ent.notation); |
2530 checkAndSendUnparsedEntity(ent); |
2533 checkAndSendUnparsedEntity(ent); |
2531 } |
2534 } |
2532 } |
2535 } |
2618 if (fSendUEAndNotationEvents && fDTDHandler != null) { |
2620 if (fSendUEAndNotationEvents && fDTDHandler != null) { |
2619 fDTDHandler.notationDecl(not.name, id, not.augmentations); |
2621 fDTDHandler.notationDecl(not.name, id, not.augmentations); |
2620 } |
2622 } |
2621 } |
2623 } |
2622 else { |
2624 else { |
2623 Notation localNotation = (Notation)fNotations.get(index); |
2625 Notation localNotation = fNotations.get(index); |
2624 if (!not.isDuplicate(localNotation)) { |
2626 if (!not.isDuplicate(localNotation)) { |
2625 reportFatalError( |
2627 reportFatalError( |
2626 "NonDuplicateNotation", |
2628 "NonDuplicateNotation", |
2627 new Object[] { not.name }); |
2629 new Object[] { not.name }); |
2628 } |
2630 } |
2679 // It would be nice if we didn't have to repeat code like this, but there's no interface that has |
2681 // It would be nice if we didn't have to repeat code like this, but there's no interface that has |
2680 // setFeature() and addRecognizedFeatures() that the objects have in common. |
2682 // setFeature() and addRecognizedFeatures() that the objects have in common. |
2681 protected void copyFeatures( |
2683 protected void copyFeatures( |
2682 XMLComponentManager from, |
2684 XMLComponentManager from, |
2683 ParserConfigurationSettings to) { |
2685 ParserConfigurationSettings to) { |
2684 Enumeration features = Constants.getXercesFeatures(); |
2686 Enumeration<Object> features = Constants.getXercesFeatures(); |
2685 copyFeatures1(features, Constants.XERCES_FEATURE_PREFIX, from, to); |
2687 copyFeatures1(features, Constants.XERCES_FEATURE_PREFIX, from, to); |
2686 features = Constants.getSAXFeatures(); |
2688 features = Constants.getSAXFeatures(); |
2687 copyFeatures1(features, Constants.SAX_FEATURE_PREFIX, from, to); |
2689 copyFeatures1(features, Constants.SAX_FEATURE_PREFIX, from, to); |
2688 } |
2690 } |
2689 |
2691 |
2690 protected void copyFeatures( |
2692 protected void copyFeatures( |
2691 XMLComponentManager from, |
2693 XMLComponentManager from, |
2692 XMLParserConfiguration to) { |
2694 XMLParserConfiguration to) { |
2693 Enumeration features = Constants.getXercesFeatures(); |
2695 Enumeration<Object> features = Constants.getXercesFeatures(); |
2694 copyFeatures1(features, Constants.XERCES_FEATURE_PREFIX, from, to); |
2696 copyFeatures1(features, Constants.XERCES_FEATURE_PREFIX, from, to); |
2695 features = Constants.getSAXFeatures(); |
2697 features = Constants.getSAXFeatures(); |
2696 copyFeatures1(features, Constants.SAX_FEATURE_PREFIX, from, to); |
2698 copyFeatures1(features, Constants.SAX_FEATURE_PREFIX, from, to); |
2697 } |
2699 } |
2698 |
2700 |
2699 private void copyFeatures1( |
2701 private void copyFeatures1( |
2700 Enumeration features, |
2702 Enumeration<Object> features, |
2701 String featurePrefix, |
2703 String featurePrefix, |
2702 XMLComponentManager from, |
2704 XMLComponentManager from, |
2703 ParserConfigurationSettings to) { |
2705 ParserConfigurationSettings to) { |
2704 while (features.hasMoreElements()) { |
2706 while (features.hasMoreElements()) { |
2705 String featureId = featurePrefix + (String)features.nextElement(); |
2707 String featureId = featurePrefix + (String)features.nextElement(); |
2715 } |
2717 } |
2716 } |
2718 } |
2717 } |
2719 } |
2718 |
2720 |
2719 private void copyFeatures1( |
2721 private void copyFeatures1( |
2720 Enumeration features, |
2722 Enumeration<Object> features, |
2721 String featurePrefix, |
2723 String featurePrefix, |
2722 XMLComponentManager from, |
2724 XMLComponentManager from, |
2723 XMLParserConfiguration to) { |
2725 XMLParserConfiguration to) { |
2724 while (features.hasMoreElements()) { |
2726 while (features.hasMoreElements()) { |
2725 String featureId = featurePrefix + (String)features.nextElement(); |
2727 String featureId = featurePrefix + (String)features.nextElement(); |
2870 * @param depth |
2872 * @param depth |
2871 * @return the base URI |
2873 * @return the base URI |
2872 */ |
2874 */ |
2873 public String getBaseURI(int depth) { |
2875 public String getBaseURI(int depth) { |
2874 int scope = scopeOfBaseURI(depth); |
2876 int scope = scopeOfBaseURI(depth); |
2875 return (String)fExpandedSystemID.elementAt(scope); |
2877 return fExpandedSystemID.get(scope); |
2876 } |
2878 } |
2877 |
2879 |
2878 /** |
2880 /** |
2879 * Gets the language that was in use at that depth. |
2881 * Gets the language that was in use at that depth. |
2880 * @param depth |
2882 * @param depth |
2881 * @return the language |
2883 * @return the language |
2882 */ |
2884 */ |
2883 public String getLanguage(int depth) { |
2885 public String getLanguage(int depth) { |
2884 int scope = scopeOfLanguage(depth); |
2886 int scope = scopeOfLanguage(depth); |
2885 return (String)fLanguageStack.elementAt(scope); |
2887 return fLanguageStack.get(scope); |
2886 } |
2888 } |
2887 |
2889 |
2888 /** |
2890 /** |
2889 * Returns a relative URI, which when resolved against the base URI at the |
2891 * Returns a relative URI, which when resolved against the base URI at the |
2890 * specified depth, will create the current base URI. |
2892 * specified depth, will create the current base URI. |
2900 int start = scopeOfBaseURI(depth) + 1; |
2902 int start = scopeOfBaseURI(depth) + 1; |
2901 if (start == fBaseURIScope.size()) { |
2903 if (start == fBaseURIScope.size()) { |
2902 // If that is the last system id, then we don't need a relative URI |
2904 // If that is the last system id, then we don't need a relative URI |
2903 return ""; |
2905 return ""; |
2904 } |
2906 } |
2905 URI uri = new URI("file", (String)fLiteralSystemID.elementAt(start)); |
2907 URI uri = new URI("file", fLiteralSystemID.get(start)); |
2906 for (int i = start + 1; i < fBaseURIScope.size(); i++) { |
2908 for (int i = start + 1; i < fBaseURIScope.size(); i++) { |
2907 uri = new URI(uri, (String)fLiteralSystemID.elementAt(i)); |
2909 uri = new URI(uri, fLiteralSystemID.get(i)); |
2908 } |
2910 } |
2909 return uri.getPath(); |
2911 return uri.getPath(); |
2910 } |
2912 } |
2911 |
2913 |
2912 // We need to find two consecutive elements in the scope stack, |
2914 // We need to find two consecutive elements in the scope stack, |