KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.sf.saxon.style;
2 import net.sf.saxon.expr.*;
3 import net.sf.saxon.instruct.Executable;
4 import net.sf.saxon.instruct.ValueOf;
5 import net.sf.saxon.om.AttributeCollection;
6 import net.sf.saxon.pattern.NodeKindTest;
7 import net.sf.saxon.trans.XPathException;
8 import net.sf.saxon.type.ItemType;
9 import net.sf.saxon.type.Type;
10 import net.sf.saxon.value.Cardinality;
11 import net.sf.saxon.value.StringValue;
12
13
14 /**
15 * An xsl:value-of element in the stylesheet. <br>
16 * The xsl:value-of element takes attributes:<ul>
17 * <li>a mandatory attribute select="expression".
18 * This must be a valid String expression</li>
19 * <li>an optional disable-output-escaping attribute, value "yes" or "no"</li>
20 * <li>an optional separator attribute</li>
21 * </ul>
22 */

23
24 public final class XSLValueOf extends XSLStringConstructor {
25
26     private boolean disable = false;
27     private Expression separator;
28
29     /**
30      * Determine the type of item returned by this instruction (only relevant if
31      * it is an instruction).
32      * @return the item type returned
33      */

34
35     protected ItemType getReturnedItemType() {
36         return NodeKindTest.TEXT;
37     }
38
39     public void prepareAttributes() throws XPathException {
40
41         String JavaDoc selectAtt = null;
42         String JavaDoc disableAtt = null;
43         String JavaDoc separatorAtt = null;
44
45         AttributeCollection atts = getAttributeList();
46
47         for (int a=0; a<atts.getLength(); a++) {
48             int nc = atts.getNameCode(a);
49             String JavaDoc f = getNamePool().getClarkName(nc);
50             if (f==StandardNames.DISABLE_OUTPUT_ESCAPING) {
51                 disableAtt = atts.getValue(a).trim();
52             } else if (f==StandardNames.SELECT) {
53                 selectAtt = atts.getValue(a);
54             } else if (f==StandardNames.SEPARATOR) {
55                 separatorAtt = atts.getValue(a);
56             } else {
57                 checkUnknownAttribute(nc);
58             }
59         }
60
61         if (selectAtt!=null) {
62             select = makeExpression(selectAtt);
63         }
64
65         if (separatorAtt != null) {
66             separator = makeAttributeValueTemplate(separatorAtt);
67         }
68
69         if (disableAtt != null) {
70             if (disableAtt.equals("yes")) {
71                 disable = true;
72             } else if (disableAtt.equals("no")) {
73                 disable = false;
74             } else {
75                 compileError("disable-output-escaping attribute must be either 'yes' or 'no'", "XTSE0020");
76             }
77         }
78     }
79
80     public void validate() throws XPathException {
81         super.validate();
82         checkWithinTemplate();
83         select = typeCheck("select", select);
84         separator = typeCheck("separator", separator);
85     }
86
87     public Expression compile(Executable exec) throws XPathException {
88
89         if (separator == null && select != null && backwardsCompatibleModeIsEnabled()) {
90             if (!Type.isSubType(select.getItemType(), Type.ANY_ATOMIC_TYPE)) {
91                 select = new Atomizer(select, getStaticContext().getConfiguration());
92             }
93             if (Cardinality.allowsMany(select.getCardinality())) {
94                 select = new FirstItemExpression(select);
95             }
96             if (!Type.isSubType(select.getItemType(), Type.STRING_TYPE)) {
97                 select = new AtomicSequenceConverter(select, Type.STRING_TYPE);
98             }
99         } else {
100             if (separator == null) {
101                 if (select == null) {
102                     separator = StringValue.EMPTY_STRING;
103                 } else {
104                     separator = StringValue.SINGLE_SPACE;
105                 }
106             }
107         }
108         ValueOf inst = new ValueOf(select, disable, false);
109         compileContent(exec, inst, separator);
110         ExpressionTool.makeParentReferences(inst);
111         return inst;
112     }
113
114 }
115
116 //
117
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
118
// you may not use this file except in compliance with the License. You may obtain a copy of the
119
// License at http://www.mozilla.org/MPL/
120
//
121
// Software distributed under the License is distributed on an "AS IS" basis,
122
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
123
// See the License for the specific language governing rights and limitations under the License.
124
//
125
// The Original Code is: all this file.
126
//
127
// The Initial Developer of the Original Code is Michael H. Kay.
128
//
129
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
130
//
131
// Contributor(s): none.
132
//
133
Popular Tags