KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > style > XSLStringConstructor


1 package net.sf.saxon.style;
2
3 import net.sf.saxon.expr.Expression;
4 import net.sf.saxon.instruct.Executable;
5 import net.sf.saxon.instruct.SimpleContentConstructor;
6 import net.sf.saxon.instruct.SimpleNodeConstructor;
7 import net.sf.saxon.om.Axis;
8 import net.sf.saxon.om.AxisIterator;
9 import net.sf.saxon.om.NodeInfo;
10 import net.sf.saxon.trans.StaticError;
11 import net.sf.saxon.trans.XPathException;
12 import net.sf.saxon.type.Type;
13 import net.sf.saxon.value.StringValue;
14
15 /**
16  * Common superclass for XSLT elements whose content template produces a text
17  * value: xsl:attribute, xsl:comment, and xsl:processing-instruction
18  */

19
20 public abstract class XSLStringConstructor extends StyleElement {
21
22     //protected String stringValue = null;
23
protected Expression select = null;
24
25     /**
26      * Determine whether this node is an instruction.
27      *
28      * @return true - it is an instruction
29      */

30
31     public boolean isInstruction() {
32         return true;
33     }
34
35     /**
36      * Determine whether this type of element is allowed to contain a template-body
37      *
38      * @return true: yes, it may contain a template-body
39      */

40
41     public boolean mayContainSequenceConstructor() {
42         return true;
43     }
44
45     public void validate() throws XPathException {
46         if (select != null && hasChildNodes()) {
47             compileError("An " + getDisplayName() + " element with a select attribute must be empty");
48         }
49         AxisIterator kids = iterateAxis(Axis.CHILD);
50         NodeInfo first = (NodeInfo)kids.next();
51         if (select == null) {
52             if (first == null) {
53                 // there are no child nodes and no select attribute
54
//stringValue = "";
55
select = StringValue.EMPTY_STRING;
56             } else {
57                 if (kids.next() == null) {
58                     // there is exactly one child node
59
if (first.getNodeKind() == Type.TEXT) {
60                         // it is a text node: optimize for this case
61
select = StringValue.makeStringValue(first.getStringValueCS());
62                     }
63                 }
64             }
65         }
66     }
67
68     protected void compileContent(Executable exec, SimpleNodeConstructor inst, Expression separator) throws XPathException {
69         if (separator == null) {
70             separator = StringValue.SINGLE_SPACE;
71         }
72         try {
73             if (select != null) {
74                 inst.setSelect(new SimpleContentConstructor(select, separator).simplify(getStaticContext()));
75             } else {
76                 Expression content = compileSequenceConstructor(exec, iterateAxis(Axis.CHILD), true);
77                 inst.setSelect(new SimpleContentConstructor(content, separator).simplify(getStaticContext()));
78             }
79         } catch (StaticError err) {
80             compileError(err);
81         }
82     }
83
84
85 }
86
87 //
88
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
89
// you may not use this file except in compliance with the License. You may obtain a copy of the
90
// License at http://www.mozilla.org/MPL/
91
//
92
// Software distributed under the License is distributed on an "AS IS" basis,
93
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
94
// See the License for the specific language governing rights and limitations under the License.
95
//
96
// The Original Code is: all this file.
97
//
98
// The Initial Developer of the Original Code is Michael H. Kay.
99
//
100
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
101
//
102
// Contributor(s): none.
103
//
104
Popular Tags