001: package net.sf.saxon.style;
002:
003: import net.sf.saxon.expr.*;
004: import net.sf.saxon.instruct.Executable;
005: import net.sf.saxon.instruct.ValueOf;
006: import net.sf.saxon.om.AttributeCollection;
007: import net.sf.saxon.pattern.NodeKindTest;
008: import net.sf.saxon.trans.XPathException;
009: import net.sf.saxon.type.ItemType;
010: import net.sf.saxon.type.Type;
011: import net.sf.saxon.type.TypeHierarchy;
012: import net.sf.saxon.value.Cardinality;
013: import net.sf.saxon.value.StringValue;
014:
015: /**
016: * An xsl:value-of element in the stylesheet. <br>
017: * The xsl:value-of element takes attributes:<ul>
018: * <li>a mandatory attribute select="expression".
019: * This must be a valid String expression</li>
020: * <li>an optional disable-output-escaping attribute, value "yes" or "no"</li>
021: * <li>an optional separator attribute</li>
022: * </ul>
023: */
024:
025: public final class XSLValueOf extends XSLStringConstructor {
026:
027: private boolean disable = false;
028: private Expression separator;
029:
030: /**
031: * Determine the type of item returned by this instruction (only relevant if
032: * it is an instruction).
033: * @return the item type returned
034: */
035:
036: protected ItemType getReturnedItemType() {
037: return NodeKindTest.TEXT;
038: }
039:
040: public void prepareAttributes() throws XPathException {
041:
042: String selectAtt = null;
043: String disableAtt = null;
044: String separatorAtt = null;
045:
046: AttributeCollection atts = getAttributeList();
047:
048: for (int a = 0; a < atts.getLength(); a++) {
049: int nc = atts.getNameCode(a);
050: String f = getNamePool().getClarkName(nc);
051: if (f == StandardNames.DISABLE_OUTPUT_ESCAPING) {
052: disableAtt = atts.getValue(a).trim();
053: } else if (f == StandardNames.SELECT) {
054: selectAtt = atts.getValue(a);
055: } else if (f == StandardNames.SEPARATOR) {
056: separatorAtt = atts.getValue(a);
057: } else {
058: checkUnknownAttribute(nc);
059: }
060: }
061:
062: if (selectAtt != null) {
063: select = makeExpression(selectAtt);
064: }
065:
066: if (separatorAtt != null) {
067: separator = makeAttributeValueTemplate(separatorAtt);
068: }
069:
070: if (disableAtt != null) {
071: if (disableAtt.equals("yes")) {
072: disable = true;
073: } else if (disableAtt.equals("no")) {
074: disable = false;
075: } else {
076: compileError(
077: "disable-output-escaping attribute must be either 'yes' or 'no'",
078: "XTSE0020");
079: }
080: }
081: }
082:
083: public void validate() throws XPathException {
084: super .validate();
085: checkWithinTemplate();
086: select = typeCheck("select", select);
087: separator = typeCheck("separator", separator);
088: }
089:
090: public Expression compile(Executable exec) throws XPathException {
091: final TypeHierarchy th = getNamePool().getTypeHierarchy();
092: if (separator == null && select != null
093: && backwardsCompatibleModeIsEnabled()) {
094: if (!select.getItemType(th).isAtomicType()) {
095: select = new Atomizer(select, getStaticContext()
096: .getConfiguration());
097: }
098: if (Cardinality.allowsMany(select.getCardinality())) {
099: select = new FirstItemExpression(select);
100: }
101: if (!th.isSubType(select.getItemType(th), Type.STRING_TYPE)) {
102: select = new AtomicSequenceConverter(select,
103: Type.STRING_TYPE);
104: }
105: } else {
106: if (separator == null) {
107: if (select == null) {
108: separator = StringValue.EMPTY_STRING;
109: } else {
110: separator = StringValue.SINGLE_SPACE;
111: }
112: }
113: }
114: ValueOf inst = new ValueOf(select, disable, false);
115: compileContent(exec, inst, separator);
116: ExpressionTool.makeParentReferences(inst);
117: return inst;
118: }
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: //
|