KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > style > XSLImportSchema


1 package net.sf.saxon.style;
2 import net.sf.saxon.Configuration;
3 import net.sf.saxon.trans.XPathException;
4 import net.sf.saxon.event.PipelineConfiguration;
5 import net.sf.saxon.expr.Expression;
6 import net.sf.saxon.instruct.Executable;
7 import net.sf.saxon.om.*;
8 import net.sf.saxon.type.SchemaException;
9
10 import javax.xml.transform.TransformerConfigurationException JavaDoc;
11
12
13 /**
14 * Compile-time representation of an xsl:import-schema declaration
15  * in a stylesheet
16 */

17
18 public class XSLImportSchema extends StyleElement {
19
20     public void prepareAttributes() throws XPathException {
21
22         AttributeCollection atts = getAttributeList();
23         String JavaDoc namespace = null;
24
25         for (int a=0; a<atts.getLength(); a++) {
26             int nc = atts.getNameCode(a);
27             String JavaDoc f = getNamePool().getClarkName(nc);
28             if (f==StandardNames.SCHEMA_LOCATION) {
29                 //
30
} else if (f==StandardNames.NAMESPACE) {
31                 namespace = atts.getValue(a).trim();
32             } else {
33                 checkUnknownAttribute(nc);
34             }
35         }
36
37         if ("".equals(namespace)) {
38             compileError("The zero-length string is not a valid namespace URI. "+
39                     "For a schema with no namespace, omit the namespace attribute");
40         }
41     }
42
43     public void validate() throws XPathException {
44         //checkEmpty();
45
checkTopLevel(null);
46     }
47
48     public void readSchema() throws SchemaException, XPathException {
49         try {
50             String JavaDoc schemaLoc = getAttributeValue(StandardNames.SCHEMA_LOCATION);
51             if (schemaLoc != null) {
52                 schemaLoc = schemaLoc.trim();
53             }
54             String JavaDoc namespace = getAttributeValue(StandardNames.NAMESPACE);
55             if (namespace==null) {
56                 namespace = "";
57             } else {
58                 namespace = namespace.trim();
59             }
60             Configuration config = getPreparedStylesheet().getConfiguration();
61             if (!config.isSchemaAware(Configuration.XSLT)) {
62                 compileError("To use xsl:import-schema, you need the schema-aware version of Saxon from http://www.saxonica.com/");
63                 return;
64             }
65             AxisIterator kids = iterateAxis(Axis.CHILD);
66             NodeInfo inlineSchema = null;
67             while (true) {
68                 Item child = kids.next();
69                 if (child==null) {
70                     break;
71                 }
72                 if (inlineSchema != null) {
73                     compileError(getDisplayName() + " must not have more than one child element");
74                 }
75                 inlineSchema = (NodeInfo)child;
76                 if (inlineSchema.getFingerprint() != StandardNames.XS_SCHEMA) {
77                     compileError("The only child element permitted for " + getDisplayName() + " is xs:schema");
78                 }
79                 if (schemaLoc != null) {
80                     compileError("The schema-location attribute must be absent if an inline schema is present");
81                 }
82                 PipelineConfiguration pipe = config.makePipelineConfiguration();
83                 config.readInlineSchema(pipe, inlineSchema, namespace);
84                 getPrincipalStylesheet().addImportedSchema(namespace);
85             }
86             if (inlineSchema != null) {
87                 return;
88             }
89             if (config.getSchema(namespace)==null) {
90                 if (schemaLoc == null) {
91                     compileError("The schema-location attribute is required (no schema for this namespace is known)");
92                     return;
93                 }
94                 PipelineConfiguration pipe = config.makePipelineConfiguration();
95                 namespace = config.readSchema(pipe, getBaseURI(), schemaLoc, namespace);
96             }
97             getPrincipalStylesheet().addImportedSchema(namespace);
98         } catch (SchemaException err) {
99             compileError(err.getMessage());
100         } catch (TransformerConfigurationException JavaDoc err) {
101             compileError(err.getMessage());
102         }
103
104     }
105
106
107     public Expression compile(Executable exec) throws XPathException {
108         exec.setReasonUnableToCompile("Cannot compile a stylesheet that imports a schema");
109         return null;
110         // No action. The effect of import-schema is compile-time only
111
}
112 }
113
114 //
115
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
116
// you may not use this file except in compliance with the License. You may obtain a copy of the
117
// License at http://www.mozilla.org/MPL/
118
//
119
// Software distributed under the License is distributed on an "AS IS" basis,
120
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
121
// See the License for the specific language governing rights and limitations under the License.
122
//
123
// The Original Code is: all this file.
124
//
125
// The Initial Developer of the Original Code is Michael H. Kay.
126
//
127
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
128
//
129
// Contributor(s): none.
130
//
131
Popular Tags