001: package net.sf.saxon.style;
002:
003: import net.sf.saxon.expr.Expression;
004: import net.sf.saxon.instruct.Executable;
005: import net.sf.saxon.instruct.SimpleContentConstructor;
006: import net.sf.saxon.instruct.SimpleNodeConstructor;
007: import net.sf.saxon.om.Axis;
008: import net.sf.saxon.om.AxisIterator;
009: import net.sf.saxon.om.NodeInfo;
010: import net.sf.saxon.trans.StaticError;
011: import net.sf.saxon.trans.XPathException;
012: import net.sf.saxon.type.Type;
013: import net.sf.saxon.value.StringValue;
014:
015: /**
016: * Common superclass for XSLT elements whose content template produces a text
017: * value: xsl:attribute, xsl:comment, and xsl:processing-instruction
018: */
019:
020: public abstract class XSLStringConstructor extends StyleElement {
021:
022: //protected String stringValue = null;
023: protected Expression select = null;
024:
025: /**
026: * Determine whether this node is an instruction.
027: *
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: *
038: * @return true: yes, it may contain a template-body
039: */
040:
041: public boolean mayContainSequenceConstructor() {
042: return true;
043: }
044:
045: public void validate() throws XPathException {
046: if (select != null && hasChildNodes()) {
047: compileError("An " + getDisplayName()
048: + " element with a select attribute must be empty");
049: }
050: AxisIterator kids = iterateAxis(Axis.CHILD);
051: NodeInfo first = (NodeInfo) kids.next();
052: if (select == null) {
053: if (first == null) {
054: // there are no child nodes and no select attribute
055: //stringValue = "";
056: select = StringValue.EMPTY_STRING;
057: } else {
058: if (kids.next() == null) {
059: // there is exactly one child node
060: if (first.getNodeKind() == Type.TEXT) {
061: // it is a text node: optimize for this case
062: select = StringValue.makeStringValue(first
063: .getStringValueCS());
064: }
065: }
066: }
067: }
068: }
069:
070: protected void compileContent(Executable exec,
071: SimpleNodeConstructor inst, Expression separator)
072: throws XPathException {
073: if (separator == null) {
074: separator = StringValue.SINGLE_SPACE;
075: }
076: try {
077: if (select != null) {
078: inst.setSelect(new SimpleContentConstructor(select,
079: separator).simplify(getStaticContext()));
080: } else {
081: Expression content = compileSequenceConstructor(exec,
082: iterateAxis(Axis.CHILD), true);
083: inst.setSelect(new SimpleContentConstructor(content,
084: separator).simplify(getStaticContext()));
085: }
086: } catch (StaticError err) {
087: compileError(err);
088: }
089: }
090:
091: }
092:
093: //
094: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
095: // you may not use this file except in compliance with the License. You may obtain a copy of the
096: // License at http://www.mozilla.org/MPL/
097: //
098: // Software distributed under the License is distributed on an "AS IS" basis,
099: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
100: // See the License for the specific language governing rights and limitations under the License.
101: //
102: // The Original Code is: all this file.
103: //
104: // The Initial Developer of the Original Code is Michael H. Kay.
105: //
106: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
107: //
108: // Contributor(s): none.
109: //
|