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 ; 15 16 17 20 21 public class XSLPerformSort extends StyleElement { 22 23 Expression select = null; 24 25 29 30 public boolean isInstruction() { 31 return true; 32 } 33 34 39 40 protected ItemType getReturnedItemType() { 41 if (select==null) { 42 return getCommonChildItemType(); 43 } else { 44 return select.getItemType(); 45 } 46 } 47 48 52 53 public boolean mayContainSequenceConstructor() { 54 return true; 55 } 56 57 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 selectAtt = null; 70 71 for (int a=0; a<atts.getLength(); a++) { 72 int nc = atts.getNameCode(a); 73 String 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 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 } else if (child.getNodeKind() == Type.TEXT && !Whitespace.isWhite(child.getStringValueCS())) { 102 compileError("Within xsl:perform-sort, significant text must not appear if there is a select attribute"); 105 } else { 106 ((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 | Popular Tags |