1 package com.icl.saxon.style; 2 import com.icl.saxon.tree.AttributeCollection; 3 import com.icl.saxon.*; 4 import com.icl.saxon.expr.*; 5 import javax.xml.transform.*; 6 7 import com.icl.saxon.trace.TraceListener; 8 import com.icl.saxon.om.NodeInfo; 9 import com.icl.saxon.om.NodeEnumeration; 10 11 14 15 public class XSLForEach extends StyleElement { 16 17 Expression select = null; 18 19 23 24 public boolean isInstruction() { 25 return true; 26 } 27 28 32 33 public boolean mayContainTemplateBody() { 34 return true; 35 } 36 37 public void prepareAttributes() throws TransformerConfigurationException { 38 39 StandardNames sn = getStandardNames(); 40 AttributeCollection atts = getAttributeList(); 41 42 String selectAtt = null; 43 44 for (int a=0; a<atts.getLength(); a++) { 45 int nc = atts.getNameCode(a); 46 int f = nc & 0xfffff; 47 if (f==sn.SELECT) { 48 selectAtt = atts.getValue(a); 49 } else { 50 checkUnknownAttribute(nc); 51 } 52 } 53 54 if (selectAtt==null) { 55 reportAbsence("select"); 56 } else { 57 select = makeExpression(selectAtt); 58 } 59 60 } 61 62 public void validate() throws TransformerConfigurationException { 63 checkWithinTemplate(); 64 select = handleSortKeys(select); 65 } 66 67 public void process(Context context) throws TransformerException 68 { 69 XSLTemplate saveCurrent = context.getCurrentTemplate(); 70 context.setCurrentTemplate(null); 71 NodeEnumeration selection = select.enumerate(context, false); 72 if (!(selection instanceof LastPositionFinder)) { 73 selection = new LookaheadEnumerator(selection); 74 } 75 76 Context c = context.newContext(); 77 c.setLastPositionFinder((LastPositionFinder)selection); 78 int position = 1; 79 80 if (context.getController().isTracing()) { 81 TraceListener listener = context.getController().getTraceListener(); 82 while(selection.hasMoreElements()) { 83 NodeInfo node = selection.nextElement(); 84 c.setPosition(position++); 85 c.setCurrentNode(node); 86 c.setContextNode(node); 87 listener.enterSource(null, c); 88 processChildren(c); 89 listener.leaveSource(null, c); 90 context.setReturnValue(c.getReturnValue()); 91 } 92 } else { 93 while(selection.hasMoreElements()) { 94 NodeInfo node = selection.nextElement(); 95 c.setPosition(position++); 96 c.setCurrentNode(node); 97 c.setContextNode(node); 98 processChildren(c); 99 context.setReturnValue(c.getReturnValue()); 100 } 101 } 102 context.setCurrentTemplate(saveCurrent); 103 } 104 105 106 } 107 108 | Popular Tags |