001: package net.sf.saxon.style;
002:
003: import net.sf.saxon.Configuration;
004: import net.sf.saxon.trans.XPathException;
005: import net.sf.saxon.event.PipelineConfiguration;
006: import net.sf.saxon.expr.Expression;
007: import net.sf.saxon.instruct.Executable;
008: import net.sf.saxon.om.*;
009: import net.sf.saxon.type.SchemaException;
010:
011: import javax.xml.transform.TransformerConfigurationException;
012:
013: /**
014: * Compile-time representation of an xsl:import-schema declaration
015: * in a stylesheet
016: */
017:
018: public class XSLImportSchema extends StyleElement {
019:
020: public void prepareAttributes() throws XPathException {
021:
022: AttributeCollection atts = getAttributeList();
023: String namespace = null;
024:
025: for (int a = 0; a < atts.getLength(); a++) {
026: int nc = atts.getNameCode(a);
027: String f = getNamePool().getClarkName(nc);
028: if (f == StandardNames.SCHEMA_LOCATION) {
029: //
030: } else if (f == StandardNames.NAMESPACE) {
031: namespace = atts.getValue(a).trim();
032: } else {
033: checkUnknownAttribute(nc);
034: }
035: }
036:
037: if ("".equals(namespace)) {
038: compileError("The zero-length string is not a valid namespace URI. "
039: + "For a schema with no namespace, omit the namespace attribute");
040: }
041: }
042:
043: public void validate() throws XPathException {
044: //checkEmpty();
045: checkTopLevel(null);
046: }
047:
048: public void readSchema() throws SchemaException, XPathException {
049: try {
050: String schemaLoc = getAttributeValue(StandardNames.SCHEMA_LOCATION);
051: if (schemaLoc != null) {
052: schemaLoc = schemaLoc.trim();
053: }
054: String namespace = getAttributeValue(StandardNames.NAMESPACE);
055: if (namespace == null) {
056: namespace = "";
057: } else {
058: namespace = namespace.trim();
059: }
060: Configuration config = getPreparedStylesheet()
061: .getConfiguration();
062: if (!config.isSchemaAware(Configuration.XSLT)) {
063: compileError("To use xsl:import-schema, you need the schema-aware version of Saxon from http://www.saxonica.com/");
064: return;
065: }
066: AxisIterator kids = iterateAxis(Axis.CHILD);
067: NodeInfo inlineSchema = null;
068: while (true) {
069: Item child = kids.next();
070: if (child == null) {
071: break;
072: }
073: if (inlineSchema != null) {
074: compileError(getDisplayName()
075: + " must not have more than one child element");
076: }
077: inlineSchema = (NodeInfo) child;
078: if (inlineSchema.getFingerprint() != StandardNames.XS_SCHEMA) {
079: compileError("The only child element permitted for "
080: + getDisplayName() + " is xs:schema");
081: }
082: if (schemaLoc != null) {
083: compileError("The schema-location attribute must be absent if an inline schema is present");
084: }
085: PipelineConfiguration pipe = config
086: .makePipelineConfiguration();
087: config.readInlineSchema(pipe, inlineSchema, namespace);
088: //config.sealNamespace(namespace);
089: getPrincipalStylesheet().addImportedSchema(namespace);
090: }
091: if (inlineSchema != null) {
092: return;
093: }
094: if (config.getSchema(namespace) == null) {
095: if (schemaLoc == null) {
096: compileError("The schema-location attribute is required (no schema for this namespace is known)");
097: return;
098: }
099: PipelineConfiguration pipe = config
100: .makePipelineConfiguration();
101: namespace = config.readSchema(pipe, getBaseURI(),
102: schemaLoc, namespace);
103: }
104: //config.sealNamespace(namespace);
105: getPrincipalStylesheet().addImportedSchema(namespace);
106: } catch (SchemaException err) {
107: compileError(err.getMessage());
108: } catch (TransformerConfigurationException err) {
109: compileError(err.getMessage());
110: }
111:
112: }
113:
114: public Expression compile(Executable exec) throws XPathException {
115: exec
116: .setReasonUnableToCompile("Cannot compile a stylesheet that imports a schema");
117: return null;
118: // No action. The effect of import-schema is compile-time only
119: }
120: }
121:
122: //
123: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
124: // you may not use this file except in compliance with the License. You may obtain a copy of the
125: // License at http://www.mozilla.org/MPL/
126: //
127: // Software distributed under the License is distributed on an "AS IS" basis,
128: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
129: // See the License for the specific language governing rights and limitations under the License.
130: //
131: // The Original Code is: all this file.
132: //
133: // The Initial Developer of the Original Code is Michael H. Kay.
134: //
135: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
136: //
137: // Contributor(s): none.
138: //
|