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 ; 11 12 13 17 18 public class XSLImportSchema extends StyleElement { 19 20 public void prepareAttributes() throws XPathException { 21 22 AttributeCollection atts = getAttributeList(); 23 String namespace = null; 24 25 for (int a=0; a<atts.getLength(); a++) { 26 int nc = atts.getNameCode(a); 27 String f = getNamePool().getClarkName(nc); 28 if (f==StandardNames.SCHEMA_LOCATION) { 29 } 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 checkTopLevel(null); 46 } 47 48 public void readSchema() throws SchemaException, XPathException { 49 try { 50 String schemaLoc = getAttributeValue(StandardNames.SCHEMA_LOCATION); 51 if (schemaLoc != null) { 52 schemaLoc = schemaLoc.trim(); 53 } 54 String 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 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 } 112 } 113 114 | Popular Tags |