KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > style > XSLStringConstructor


1 package com.icl.saxon.style;
2 import com.icl.saxon.Context;
3 import com.icl.saxon.Controller;
4 import com.icl.saxon.om.Name;
5 import com.icl.saxon.om.NodeInfo;
6 import com.icl.saxon.tree.NodeImpl;
7 import com.icl.saxon.expr.Expression;
8 import com.icl.saxon.output.Outputter;
9 import javax.xml.transform.TransformerException JavaDoc;
10 import javax.xml.transform.TransformerConfigurationException JavaDoc;
11
12 /**
13 * Common superclass for XSLT elements whose content template produces a text
14 * value: xsl:attribute, xsl:comment, and xsl:processing-instruction
15 */

16
17 public abstract class XSLStringConstructor extends StyleElement {
18
19     private String JavaDoc stringValue = null;
20     private Expression valueExpression = null;
21     
22     /**
23     * Determine whether this node is an instruction.
24     * @return true - it is an instruction
25     */

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

35
36     public boolean mayContainTemplateBody() {
37         return true;
38     }
39
40     protected void optimize() throws TransformerConfigurationException JavaDoc {
41         NodeImpl first = (NodeImpl)getFirstChild();
42         if (first==null) {
43             // there are no child nodes
44
stringValue = "";
45         } else {
46             NodeImpl next = (NodeImpl)first.getNextSibling();
47             if (next==null) {
48                 // there is exactly one child node
49
if (first.getNodeType() == NodeInfo.TEXT) {
50                     // it is a text node: optimize for this case
51
stringValue = first.getStringValue();
52                 } else if (first instanceof XSLValueOf) {
53                     // it is an xsl:value-of instruction: optimize this case
54
XSLValueOf v = (XSLValueOf)first;
55                     valueExpression = v.getSelectExpression();
56                     if (v.getDisableOutputEscaping()) {
57                         v.compileError("disable-output-escaping is not allowed for a non-text node");
58                     }
59                 }
60             }
61         }
62     }
63
64     /**
65     * Expand the stylesheet elements subordinate to this one, returning the result
66     * as a string. The expansion must not generate any element or attribute nodes.
67     * @param context The context in the source document
68     */

69
70     public String JavaDoc expandChildren(Context context) throws TransformerException JavaDoc {
71
72         if (stringValue != null) {
73             return stringValue;
74             
75         } else if (valueExpression != null) {
76             return valueExpression.evaluateAsString(context);
77             
78         } else {
79             Controller c = context.getController();
80             Outputter old = c.getOutputter();
81             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
82             c.changeToTextOutputDestination(buffer);
83             processChildren(context);
84             c.resetOutputDestination(old);
85             return buffer.toString();
86             
87         }
88     }
89
90
91 }
92
93 //
94
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
95
// you may not use this file except in compliance with the License. You may obtain a copy of the
96
// License at http://www.mozilla.org/MPL/
97
//
98
// Software distributed under the License is distributed on an "AS IS" basis,
99
// 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
105
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
106
//
107
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
108
//
109
// Contributor(s): none.
110
//
111
Popular Tags