1 package com.icl.saxon.style; 2 import com.icl.saxon.tree.AttributeCollection; 3 import com.icl.saxon.tree.NodeImpl; 4 import com.icl.saxon.*; 5 import com.icl.saxon.om.Name; 6 import com.icl.saxon.om.NamespaceException; 7 import com.icl.saxon.om.NodeInfo; 8 9 import com.icl.saxon.om.Navigator; 10 import com.icl.saxon.expr.*; 11 import com.icl.saxon.om.Axis; 12 import com.icl.saxon.pattern.AnyNodeTest; 13 14 import javax.xml.transform.*; 15 import org.w3c.dom.Node ; 16 19 22 23 public class XSLApplyTemplates extends StyleElement { 24 25 private Expression select; 26 private boolean usesParams; 27 private int modeNameCode = -1; private Mode mode; 29 private String modeAttribute; 30 31 35 36 public boolean isInstruction() { 37 return true; 38 } 39 40 41 public void prepareAttributes() throws TransformerConfigurationException { 42 43 StandardNames sn = getStandardNames(); 44 AttributeCollection atts = getAttributeList(); 45 46 String selectAtt = null; 47 48 for (int a=0; a<atts.getLength(); a++) { 49 int nc = atts.getNameCode(a); 50 int f = nc & 0xfffff; 51 if (f==sn.MODE) { 52 modeAttribute = atts.getValue(a); 53 } else if (f==sn.SELECT) { 54 selectAtt = atts.getValue(a); 55 } else { 56 checkUnknownAttribute(nc); 57 } 58 } 59 60 if (modeAttribute!=null) { 61 if (!Name.isQName(modeAttribute)) { 62 compileError("Mode name is not a valid QName"); 63 } 64 try { 65 modeNameCode = makeNameCode(modeAttribute, false); 66 } catch (NamespaceException err) { 67 compileError(err.getMessage()); 68 } 69 } 70 71 if (selectAtt!=null) { 72 select = makeExpression(selectAtt); 73 } 74 } 75 76 public void validate() throws TransformerConfigurationException { 77 78 checkWithinTemplate(); 79 80 mode = getPrincipalStyleSheet().getRuleManager().getMode(modeNameCode); 82 83 85 boolean sorted = false; 86 NodeImpl child = (NodeImpl)getFirstChild(); 87 while (child!=null) { 88 if (child instanceof XSLSort) { 89 sorted = true; 90 } else if (child instanceof XSLWithParam) { 91 usesParams = true; 92 } else { 93 if (child.getNodeType() == NodeInfo.TEXT) { 94 if (!Navigator.isWhite(child.getStringValue())) { 96 compileError( 97 "No character data allowed within xsl:apply-templates"); 98 } 99 } else { 100 compileError("Invalid element within xsl:apply-templates: "); 101 } 102 } 103 child = (NodeImpl)child.getNextSibling(); 104 } 105 106 if (select==null && sorted) { 107 select = new PathExpression( 108 new ContextNodeExpression(), 109 new Step(Axis.CHILD, AnyNodeTest.getInstance())); 110 } 111 if (select!=null) { 112 select = handleSortKeys(select); 113 } 114 } 115 116 public void process(Context context) throws TransformerException 117 { 118 120 ParameterSet params = null; 121 if (usesParams) { 122 params = new ParameterSet(); 123 Node child = getFirstChild(); 124 while (child!=null) { 125 if (child instanceof XSLWithParam) { 126 XSLWithParam param = (XSLWithParam)child; 127 params.put(param.getVariableFingerprint(), param.getParamValue(context)); 128 } 129 child = child.getNextSibling(); 130 } 131 } 132 133 135 context.getController().applyTemplates(context, select, mode, params); 136 137 } 138 139 } 140 141 | Popular Tags |