Debian Codegraph @

net/sf/saxon/style/XSLImportSchema.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.event.PipelineConfiguration;
12 import net.sf.saxon.lib.Feature;
13 import net.sf.saxon.lib.NamespaceConstant;
14 import net.sf.saxon.om.AttributeCollection;
15 import net.sf.saxon.om.AxisInfo;
16 import net.sf.saxon.om.Item;
17 import net.sf.saxon.om.StandardNames;
18 import net.sf.saxon.trans.LicenseException;
19 import net.sf.saxon.trans.SaxonErrorCode;
20 import net.sf.saxon.trans.XPathException;
21 import net.sf.saxon.tree.iter.AxisIterator;
22 import net.sf.saxon.tree.linked.NodeImpl;
23 import net.sf.saxon.type.SchemaException;
24 import net.sf.saxon.value.Whitespace;
25
26
27 /**
28 * Compile-time representation of an xsl:import-schema declaration
29 * in a stylesheet
30 */
31
32 public class XSLImportSchema extends StyleElement {
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 public void prepareAttributes() throws XPathException {
47
48 AttributeCollection atts = getAttributeList();
49 String namespace = null;
50
51 for (int a = 0; a < atts.getLength(); a++) {
52 String f = atts.getQName(a);
53 if (f.equals("schema-location")) {
54 //
55 } else if (f.equals("namespace")) {
56 namespace = Whitespace.trim(atts.getValue(a));
57 } else {
58 checkUnknownAttribute(atts.getNodeName(a));
59 }
60 }
61
62 if ("".equals(namespace)) {
63 compileError("The zero-length string is not a valid namespace URI. " +
64 "For a schema with no namespace, omit the namespace attribute");
65 }
66 }
67
68 public void validate(ComponentDeclaration decl) throws XPathException {
69 checkTopLevel("XTSE0010", false);
70 }
71
72 public void index(ComponentDeclaration decl, PrincipalStylesheetModule top) throws XPathException {
73 //
74 }
75
76 public void readSchema() throws XPathException {
77 try {
78 String schemaLoc = Whitespace.trim(getAttributeValue("", "schema-location"));
79 String namespace = Whitespace.trim(getAttributeValue("", "namespace"));
80 if (namespace == null) {
81 namespace = "";
82 } else {
83 namespace = namespace.trim();
84 }
85 Configuration config = getConfiguration();
86 try {
87 config.checkLicensedFeature(Configuration.LicenseFeature.ENTERPRISE_XSLT,
88 "xsl:import-schema",
89 getPackageData().getLocalLicenseId());
90 } catch (LicenseException err) {
91 XPathException xe = new XPathException(err);
92 xe.setErrorCode("XTSE1650");
93 xe.setLocator(this);
94 throw xe;
95 }
96 AxisIterator kids = iterateAxis(AxisInfo.CHILD);
97 NodeImpl inlineSchema = null;
98 Item child;
99 while ((child = kids.next()) != null) {
100 if (inlineSchema != null) {
101 compileError(getDisplayName() + " must not have more than one child element");
102 }
103 inlineSchema = (NodeImpl) child;
104 if (inlineSchema.getFingerprint() != StandardNames.XS_SCHEMA) {
105 compileError("The only child element permitted for " + getDisplayName() + " is xs:schema");
106 }
107 if (schemaLoc != null) {
108 compileError("The schema-location attribute must be absent if an inline schema is present", "XTSE0215");
109 }
110
111 if (namespace.isEmpty()) {
112 namespace = inlineSchema.getAttributeValue("", "targetNamespace");
113 if (namespace == null) {
114 namespace = "";
115 }
116 }
117
118 namespace = config.readInlineSchema(inlineSchema, namespace,
119 getCompilation().getCompilerInfo().getErrorListener());
120 getPrincipalStylesheetModule().addImportedSchema(namespace);
121 }
122 if (inlineSchema != null) {
123 return;
124 }
125 if (namespace.equals(NamespaceConstant.XML) ||
126 namespace.equals(NamespaceConstant.FN)||
127 namespace.equals(NamespaceConstant.SCHEMA_INSTANCE)) {
128 config.addSchemaForBuiltInNamespace(namespace);
129 getPrincipalStylesheetModule().addImportedSchema(namespace);
130 return;
131 }
132 boolean namespaceKnown = config.isSchemaAvailable(namespace);
133 if (schemaLoc == null && !namespaceKnown) {
134 compileWarning("No schema for this namespace is known, " +
135 "and no schema-location was supplied, so no schema has been imported",
136 SaxonErrorCode.SXWN9006);
137 return;
138 }
139 if (namespaceKnown && !config.getBooleanProperty(Feature.MULTIPLE_SCHEMA_IMPORTS)) {
140 if (schemaLoc != null) {
141 compileWarning("The schema document at " + schemaLoc +
142 " is ignored because a schema for this namespace is already loaded", SaxonErrorCode.SXWN9006);
143 }
144 }
145 if (!namespaceKnown) {
146 PipelineConfiguration pipe = config.makePipelineConfiguration();
147 namespace = config.readSchema(pipe, getBaseURI(), schemaLoc, namespace);
148 }
149 getPrincipalStylesheetModule().addImportedSchema(namespace);
150 } catch (SchemaException err) {
151 String errorCode = err.getErrorCodeLocalPart() == null ? "XTSE0220" : err.getErrorCodeLocalPart();
152 compileError(err.getMessage(), errorCode);
153 }
154
155 }
156
157
158 public void compileDeclaration(Compilation compilation, ComponentDeclaration decl) throws XPathException {
159 // No action. The effect of import-schema is compile-time only
160 }
161 }
162