1 package com.icl.saxon.expr; 2 import com.icl.saxon.Context; 3 import com.icl.saxon.sort.*; 4 import com.icl.saxon.om.NodeEnumeration; 5 7 14 15 public class SortedSelection extends NodeSetExpression { 16 17 private Expression selection; 18 private SortKeyDefinition[] sortkeys; 19 private int numberOfSortKeys; 21 22 27 28 public SortedSelection(Expression s, int k) { 29 selection = s; 30 sortkeys = new SortKeyDefinition[k]; 31 numberOfSortKeys = k; 32 } 33 34 43 44 public void setSortKey(SortKeyDefinition sk, int k) { 45 sortkeys[k] = sk; 46 } 47 48 52 53 public Expression simplify() throws XPathException { 54 55 selection = selection.simplify(); 57 for (int i=0; i<numberOfSortKeys; i++) { 58 SortKeyDefinition sk = sortkeys[i]; 59 sk.setSortKey(sk.getSortKey().simplify()); 60 sk.setOrder(sk.getOrder().simplify()); 61 sk.setDataType(sk.getDataType().simplify()); 62 sk.setCaseOrder(sk.getCaseOrder().simplify()); 63 sk.setLanguage(sk.getLanguage().simplify()); 64 } 65 return this; 66 } 67 68 73 74 public int getDependencies() { 75 int dep = selection.getDependencies(); 76 for (int i=0; i<sortkeys.length; i++) { 77 SortKeyDefinition sk = sortkeys[i]; 78 dep |= (sk.getSortKey().getDependencies() & 81 (Context.VARIABLES | Context.CONTROLLER)); 82 dep |= sk.getOrder().getDependencies(); 83 dep |= sk.getDataType().getDependencies(); 84 dep |= sk.getCaseOrder().getDependencies(); 85 dep |= sk.getLanguage().getDependencies(); 86 } 87 return dep; 88 } 89 90 98 99 public Expression reduce(int dependencies, Context context) throws XPathException { 100 if ((dependencies & getDependencies()) != 0) { 101 Expression newselection = selection.reduce(dependencies, context); 102 SortedSelection newss = new SortedSelection(newselection, numberOfSortKeys); 103 newss.setStaticContext(getStaticContext()); 104 for (int i=0; i<numberOfSortKeys; i++) { 105 SortKeyDefinition sk = sortkeys[i]; 106 SortKeyDefinition sknew = new SortKeyDefinition(); 107 sknew.setStaticContext(getStaticContext()); 108 sknew.setSortKey( 109 sk.getSortKey().reduce( 110 dependencies & (Context.VARIABLES | Context.CONTROLLER), 111 context)); 112 sknew.setOrder(sk.getOrder().reduce(dependencies, context)); 113 sknew.setDataType(sk.getDataType().reduce(dependencies, context)); 114 sknew.setCaseOrder(sk.getCaseOrder().reduce(dependencies, context)); 115 sknew.setLanguage(sk.getLanguage().reduce(dependencies, context)); 116 newss.setSortKey(sknew, i); 117 } 118 return newss.simplify(); 119 } else { 120 return this; 121 } 122 } 123 124 130 131 public NodeEnumeration enumerate(Context context, boolean sort) throws XPathException 132 { 133 if (sort==true) { 134 throw new XPathException("SortedSelection doesn't provide nodes in document order"); 135 } 136 NodeEnumeration base = selection.enumerate(context, false); 137 return new SortKeyEnumeration(context, base, sortkeys); 138 } 139 140 143 144 public void display(int level) { 145 System.err.println(indent(level) + "sorted"); 146 selection.display(level+1); 147 } 148 149 } 150 151 | Popular Tags |