KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.sf.saxon.style;
2 import net.sf.saxon.expr.Expression;
3 import net.sf.saxon.expr.ExpressionTool;
4 import net.sf.saxon.instruct.Executable;
5 import net.sf.saxon.om.*;
6 import net.sf.saxon.sort.SortExpression;
7 import net.sf.saxon.sort.SortKeyDefinition;
8 import net.sf.saxon.trans.XPathException;
9 import net.sf.saxon.type.ItemType;
10 import net.sf.saxon.type.Type;
11 import net.sf.saxon.value.EmptySequence;
12 import net.sf.saxon.value.Whitespace;
13
14 import javax.xml.transform.TransformerConfigurationException JavaDoc;
15
16
17 /**
18 * Handler for xsl:perform-sort elements in stylesheet (XSLT 2.0). <br>
19 */

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

29
30     public boolean isInstruction() {
31         return true;
32     }
33
34     /**
35      * Determine the type of item returned by this instruction (only relevant if
36      * it is an instruction).
37      * @return the item type returned
38      */

39
40     protected ItemType getReturnedItemType() {
41         if (select==null) {
42             return getCommonChildItemType();
43         } else {
44             return select.getItemType();
45         }
46     }
47
48     /**
49     * Determine whether this type of element is allowed to contain a template-body
50     * @return true: yes, it may contain a template-body
51     */

52
53     public boolean mayContainSequenceConstructor() {
54         return true;
55     }
56
57     /**
58      * Specify that xsl:sort is a permitted child
59      */

60
61     protected boolean isPermittedChild(StyleElement child) {
62         return (child instanceof XSLSort);
63     }
64
65     public void prepareAttributes() throws XPathException {
66
67         AttributeCollection atts = getAttributeList();
68
69         String JavaDoc selectAtt = null;
70
71         for (int a=0; a<atts.getLength(); a++) {
72             int nc = atts.getNameCode(a);
73             String JavaDoc f = getNamePool().getClarkName(nc);
74             if (f==StandardNames.SELECT) {
75                 selectAtt = atts.getValue(a);
76             } else {
77                 checkUnknownAttribute(nc);
78             }
79         }
80
81         if (selectAtt!=null) {
82             select = makeExpression(selectAtt);
83         }
84
85     }
86
87     public void validate() throws XPathException {
88         checkWithinTemplate();
89         checkSortComesFirst(true);
90
91         if (select != null) {
92             // if there is a select attribute, check that there are no children other than xsl:sort and xsl:fallback
93
AxisIterator kids = iterateAxis(Axis.CHILD);
94             while (true) {
95                 NodeInfo child = (NodeInfo)kids.next();
96                 if (child == null) {
97                     break;
98                 }
99                 if (child instanceof XSLSort || child instanceof XSLFallback) {
100                     // no action
101
} else if (child.getNodeKind() == Type.TEXT && !Whitespace.isWhite(child.getStringValueCS())) {
102                         // with xml:space=preserve, white space nodes may still be there
103
// ERR XT1040
104
compileError("Within xsl:perform-sort, significant text must not appear if there is a select attribute");
105                 } else {
106                     // ERR XT1040
107
((StyleElement)child).compileError("Within xsl:perform-sort, child instructions are not allowed if there is a select attribute");
108                 }
109             }
110         }
111         select = typeCheck("select", select);
112     }
113
114     public Expression compile(Executable exec) throws XPathException {
115         SortKeyDefinition[] sortKeys = makeSortKeys();
116         if (select != null) {
117             SortExpression sortedSequence = new SortExpression(select, sortKeys);
118             ExpressionTool.makeParentReferences(sortedSequence);
119             return sortedSequence;
120         } else {
121             Expression body = compileSequenceConstructor(exec, iterateAxis(Axis.CHILD), true);
122             if (body == null) {
123                 body = EmptySequence.getInstance();
124             }
125             try {
126                 SortExpression sortedSequence = new SortExpression(body.simplify(getStaticContext()), sortKeys);
127                 ExpressionTool.makeParentReferences(sortedSequence);
128                 return sortedSequence;
129             } catch (XPathException e) {
130                 compileError(e);
131                 return null;
132             }
133         }
134     }
135
136
137 }
138
139 //
140
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
141
// you may not use this file except in compliance with the License. You may obtain a copy of the
142
// License at http://www.mozilla.org/MPL/
143
//
144
// Software distributed under the License is distributed on an "AS IS" basis,
145
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
146
// See the License for the specific language governing rights and limitations under the License.
147
//
148
// The Original Code is: all this file.
149
//
150
// The Initial Developer of the Original Code is Michael H. Kay.
151
//
152
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
153
//
154
// Contributor(s): none.
155
//
156
Popular Tags