Debian Codegraph @

net/sf/saxon/style/XSLGeneralIncorporate.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.style;
9
10 import net.sf.saxon.Configuration;
11 import net.sf.saxon.functions.DocumentFn;
12 import net.sf.saxon.om.AttributeCollection;
13 import net.sf.saxon.om.DocumentURI;
14 import net.sf.saxon.om.TreeInfo;
15 import net.sf.saxon.trans.XPathException;
16 import net.sf.saxon.tree.linked.DocumentImpl;
17 import net.sf.saxon.tree.linked.ElementImpl;
18 import net.sf.saxon.value.Whitespace;
19
20 import javax.xml.transform.Source;
21 import javax.xml.transform.URIResolver;
22 import java.util.Map;
23
24
25 /**
26 * Abstract class to represent an xsl:include or xsl:import element in the stylesheet.
27 * The xsl:include and xsl:import elements have mandatory attribute href
28 */
29
30 public abstract class XSLGeneralIncorporate extends StyleElement {
31
32 private String href;
33
34 /**
35 * Ask whether this node is a declaration, that is, a permitted child of xsl:stylesheet
36 * (including xsl:include and xsl:import).
37 *
38 * @return true for this element
39 */
40
41 @Override
42 public boolean isDeclaration() {
43 return true;
44 }
45
46
47 /**
48 * isImport() returns true if this is an xsl:import declaration rather than an xsl:include
49 *
50 * @return true if this is an xsl:import declaration, false if it is an xsl:include
51 */
52
53 public abstract boolean isImport();
54
55 public void prepareAttributes() throws XPathException {
56
57 AttributeCollection atts = getAttributeList();
58
59 for (int a = 0; a < atts.getLength(); a++) {
60 String f = atts.getQName(a);
61 if (f.equals("href")) {
62 href = Whitespace.trim(atts.getValue(a));
63 } else {
64 checkUnknownAttribute(atts.getNodeName(a));
65 }
66 }
67
68 if (href == null) {
69 reportAbsence("href");
70 }
71 }
72
73 public void validate(ComponentDeclaration decl) throws XPathException {
74 validateInstruction();
75 }
76
77 public void validateInstruction() {
78 checkEmpty();
79 checkTopLevel(isImport() ? "XTSE0190" : "XTSE0170", false);
80 }
81
82 /**
83 * Get the included or imported stylesheet module
84 *
85 * @param importer the module that requested the include or import (used to check for cycles)
86 * @param precedence the import precedence to be allocated to the included or imported module
87 * @return the xsl:stylesheet element at the root of the included/imported module
88 */
89
90 /*@Nullable*/
91 public StylesheetModule getIncludedStylesheet(StylesheetModule importer, int precedence) {
92
93 if (href == null) {
94 // error already reported
95 return null;
96 }
97
98 //checkEmpty();
99 //checkTopLevel((this instanceof XSLInclude ? "XTSE0170" : "XTSE0190"));
100
101 try {
102 PrincipalStylesheetModule psm = importer.getPrincipalStylesheetModule();
103 //PreparedStylesheet pss = psm.getPreparedStylesheet();
104 URIResolver resolver = getCompilation().getCompilerInfo().getURIResolver();
105 Configuration config = getConfiguration();
106 XSLStylesheet includedSheet;
107 StylesheetModule incModule;
108
109 DocumentURI key = DocumentFn.computeDocumentKey(href, getBaseURI(), getCompilation().getPackageData(), resolver, false);
110 includedSheet = (XSLStylesheet)psm.getStylesheetDocument(key);
111 if (includedSheet != null) {
112 // we already have the stylesheet document in cache; but we need to create a new module,
113 // because the import precedence might be different. See test impincl30.
114 incModule = new StylesheetModule(includedSheet, precedence);
115 incModule.setImporter(importer);
116
117 // check for recursion
118 if (checkForRecursion(importer, incModule.getRootElement())) {
119 return null;
120 }
121
122 } else {
123
124 Map<DocumentURI, TreeInfo> map = getCompilation().getStylesheetModules();
125 DocumentImpl includedDoc = (DocumentImpl)map.get(key);
126
127 ElementImpl outermost = includedDoc.getDocumentElement();
128
129 if (outermost instanceof LiteralResultElement) {
130 includedDoc = ((LiteralResultElement) outermost).makeStylesheet(false);
131 outermost = includedDoc.getDocumentElement();
132 }
133
134 if (!(outermost instanceof XSLStylesheet)) {
135 String verb = this instanceof XSLImport ? "Imported" : "Included";
136 compileError(verb + " document " + href + " is not a stylesheet", "XTSE0165");
137 return null;
138 }
139 includedSheet = (XSLStylesheet) outermost;
140 psm.putStylesheetDocument(key, includedSheet);
141
142 incModule = new StylesheetModule(includedSheet, precedence);
143 incModule.setImporter(importer);
144 ComponentDeclaration decl = new ComponentDeclaration(incModule, includedSheet);
145 includedSheet.validate(decl);
146
147 if (includedSheet.validationError != null) {
148 if (reportingCircumstances == REPORT_ALWAYS) {
149 includedSheet.compileError(includedSheet.validationError);
150 } else if (includedSheet.reportingCircumstances == REPORT_UNLESS_FORWARDS_COMPATIBLE
151 // not sure if this can still happen
152 /*&& !incSheet.forwardsCompatibleModeIsEnabled()*/) {
153 includedSheet.compileError(includedSheet.validationError);
154 }
155 }
156 }
157
158 incModule.spliceIncludes(); // resolve any nested imports and includes;
159
160 // Check the consistency of input-type-annotations
161 //assert thisSheet != null;
162 importer.setInputTypeAnnotations(includedSheet.getInputTypeAnnotationsAttribute() |
163 incModule.getInputTypeAnnotations());
164
165 return incModule;
166
167 } catch (XPathException err) {
168 err.setErrorCode("XTSE0165");
169 err.setIsStaticError(true);
170 compileError(err);
171 return null;
172 }
173 }
174
175 private boolean checkForRecursion(StylesheetModule importer, Source source) {
176 StylesheetModule anc = importer;
177
178 if (source.getSystemId() != null) {
179 while (anc != null) {
180 if (DocumentURI.normalizeURI(source.getSystemId())
181 .equals(DocumentURI.normalizeURI(anc.getRootElement().getSystemId()))) {
182 compileError("A stylesheet cannot " + getLocalPart() + " itself",
183 this instanceof XSLInclude ? "XTSE0180" : "XTSE0210");
184 return true;
185 }
186 anc = anc.getImporter();
187 }
188 }
189 return false;
190 }
191
192 public void compileDeclaration(Compilation compilation, ComponentDeclaration decl) {
193 // no action. The node will never be compiled, because it replaces itself
194 // by the contents of the included file.
195 }
196 }
197