001: package net.sf.saxon.style;
002:
003: import net.sf.saxon.Configuration;
004: import net.sf.saxon.expr.Expression;
005: import net.sf.saxon.expr.ExpressionTool;
006: import net.sf.saxon.instruct.DocumentInstr;
007: import net.sf.saxon.instruct.Executable;
008: import net.sf.saxon.om.AttributeCollection;
009: import net.sf.saxon.om.Axis;
010: import net.sf.saxon.om.Validation;
011: import net.sf.saxon.trans.XPathException;
012: import net.sf.saxon.type.SchemaType;
013: import net.sf.saxon.value.EmptySequence;
014:
015: /**
016: * An xsl:document instruction in the stylesheet. <BR>
017: * This instruction creates a document node in the result tree, optionally
018: * validating it.
019: */
020:
021: public class XSLDocument extends StyleElement {
022:
023: private int validationAction = Validation.STRIP;
024: private SchemaType schemaType = null;
025:
026: /**
027: * Determine whether this node is an instruction.
028: * @return true - it is an instruction
029: */
030:
031: public boolean isInstruction() {
032: return true;
033: }
034:
035: /**
036: * Determine whether this type of element is allowed to contain a template-body
037: * @return true: yes, it may contain a template-body
038: */
039:
040: public boolean mayContainSequenceConstructor() {
041: return true;
042: }
043:
044: public void prepareAttributes() throws XPathException {
045: AttributeCollection atts = getAttributeList();
046:
047: String validationAtt = null;
048: String typeAtt = null;
049:
050: for (int a = 0; a < atts.getLength(); a++) {
051: int nc = atts.getNameCode(a);
052: String f = getNamePool().getClarkName(nc);
053: if (f == StandardNames.VALIDATION) {
054: validationAtt = atts.getValue(a).trim();
055: } else if (f == StandardNames.TYPE) {
056: typeAtt = atts.getValue(a).trim();
057: } else {
058: checkUnknownAttribute(nc);
059: }
060: }
061:
062: if (validationAtt == null) {
063: validationAction = getContainingStylesheet()
064: .getDefaultValidation();
065: } else {
066: validationAction = Validation.getCode(validationAtt);
067: if (validationAction != Validation.STRIP
068: && !getConfiguration().isSchemaAware(
069: Configuration.XSLT)) {
070: compileError(
071: "To perform validation, a schema-aware XSLT processor is needed",
072: "XTSE1660");
073: }
074: if (validationAction == Validation.INVALID) {
075: compileError("Invalid value of @validation attribute",
076: "XTSE0020");
077: }
078: }
079: if (typeAtt != null) {
080: if (!getConfiguration().isSchemaAware(Configuration.XSLT)) {
081: compileError(
082: "The @type attribute is available only with a schema-aware XSLT processor",
083: "XTSE1660");
084: }
085: schemaType = getSchemaType(typeAtt);
086: }
087:
088: if (typeAtt != null && validationAtt != null) {
089: compileError(
090: "The @validation and @type attributes are mutually exclusive",
091: "XTSE1505");
092: }
093: }
094:
095: public void validate() throws XPathException {
096: checkWithinTemplate();
097: }
098:
099: public Expression compile(Executable exec) throws XPathException {
100:
101: DocumentInstr inst = new DocumentInstr(false, null,
102: getBaseURI());
103: inst.setValidationAction(validationAction);
104: inst.setSchemaType(schemaType);
105: Expression b = compileSequenceConstructor(exec,
106: iterateAxis(Axis.CHILD), true);
107: if (b == null) {
108: b = EmptySequence.getInstance();
109: }
110: inst.setContentExpression(b);
111: ExpressionTool.makeParentReferences(inst);
112: return inst;
113: }
114:
115: }
116:
117: //
118: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
119: // you may not use this file except in compliance with the License. You may obtain a copy of the
120: // License at http://www.mozilla.org/MPL/
121: //
122: // Software distributed under the License is distributed on an "AS IS" basis,
123: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
124: // See the License for the specific language governing rights and limitations under the License.
125: //
126: // The Original Code is: all this file.
127: //
128: // The Initial Developer of the Original Code is Michael H. Kay.
129: //
130: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
131: //
132: // Additional Contributor(s): Brett Knights [brett@knightsofthenet.com]
133: //
|