1 package com.icl.saxon.style; 2 import com.icl.saxon.tree.AttributeCollection; 3 import com.icl.saxon.Context; 4 import com.icl.saxon.Controller; 5 import com.icl.saxon.om.Name; 6 import com.icl.saxon.om.NodeInfo; 7 import com.icl.saxon.om.NamePool; 8 import com.icl.saxon.om.NamespaceException; 9 import com.icl.saxon.tree.NodeImpl; 10 import com.icl.saxon.expr.*; 11 import com.icl.saxon.output.Outputter; 12 import javax.xml.transform.TransformerException ; 13 import javax.xml.transform.TransformerConfigurationException ; 14 15 18 19 public final class XSLAttribute extends XSLStringConstructor { 20 21 private Expression attributeName; 22 private Expression namespace=null; 23 private boolean disable = false; 24 25 public void prepareAttributes() throws TransformerConfigurationException { 26 27 StandardNames sn = getStandardNames(); 28 AttributeCollection atts = getAttributeList(); 29 30 String nameAtt = null; 31 String namespaceAtt = null; 32 String disableAtt = null; 33 34 for (int a=0; a<atts.getLength(); a++) { 35 int nc = atts.getNameCode(a); 36 int f = nc & 0xfffff; 37 if (f==sn.NAME) { 38 nameAtt = atts.getValue(a); 39 } else if (f==sn.NAMESPACE) { 40 namespaceAtt = atts.getValue(a); 41 } else if (f==sn.SAXON_DISABLE_OUTPUT_ESCAPING) { 42 disableAtt = atts.getValue(a); 43 } else { 44 checkUnknownAttribute(nc); 45 } 46 } 47 48 if (nameAtt==null) { 49 reportAbsence("name"); 50 return; 51 } else { 52 attributeName = makeAttributeValueTemplate(nameAtt); 53 if (attributeName instanceof StringValue) { 54 if (!Name.isQName(((StringValue)attributeName).asString())) { 55 compileError("Attribute name is not a valid QName"); 56 } 57 } 58 } 59 60 if (namespaceAtt!=null) { 61 namespace = makeAttributeValueTemplate(namespaceAtt); 62 } 63 64 disable = (disableAtt != null && disableAtt.equals("yes")); 65 66 } 67 68 public void validate() throws TransformerConfigurationException { 69 if (!(getParentNode() instanceof XSLAttributeSet)) { 70 checkWithinTemplate(); 71 } 72 optimize(); 73 } 74 75 public void process(Context context) throws TransformerException 76 { 77 String expandedName = attributeName.evaluateAsString(context); 78 Controller controller = context.getController(); 79 NamePool pool = controller.getNamePool(); 80 81 if (!Name.isQName(expandedName)) { 82 controller.reportRecoverableError( 83 "Invalid attribute name: " + expandedName, this); 84 return; 85 } 86 87 if (expandedName.equals("xmlns")) { 88 if (namespace==null) { 89 controller.reportRecoverableError( 90 "Invalid attribute name: " + expandedName, this); 91 return; 92 } 93 } 94 if (expandedName.length()>6 && expandedName.substring(0,6).equals("xmlns:")) { 95 if (namespace==null) { 96 controller.reportRecoverableError( 97 "Invalid attribute name: " + expandedName, this); 98 return; 99 } else { 100 expandedName = expandedName.substring(6); 102 } 103 } 104 105 String prefix = Name.getPrefix(expandedName); 106 short uriCode; 107 108 if (namespace==null) { 109 110 113 if (prefix.equals("")) { 114 uriCode = 0; 115 } else { 116 try { 117 uriCode = getURICodeForPrefix(prefix); } catch (NamespaceException err) { 119 throw styleError(err.getMessage()); 121 } 122 } 123 124 } else { 125 126 128 String uri = namespace.evaluateAsString(context); 129 if (uri.equals("")) { 130 prefix = ""; 133 134 } else { 135 if (prefix.equals("")) { 136 prefix = getPrefixForURI(uri); 137 if (prefix==null || prefix=="") { 140 prefix="ns0"; } 143 } 144 } 145 146 uriCode = pool.allocateCodeForURI(uri); 148 149 } 150 151 String localName = Name.getLocalName(expandedName); 152 int nameCode = pool.allocate(prefix, uriCode, localName); 153 154 Outputter out = controller.getOutputter(); 155 156 if (out.thereIsAnOpenStartTag()) { 159 if (((nameCode>>20)&0xff) != 0) { nameCode = out.checkAttributePrefix(nameCode); 161 } 162 out.writeAttribute(nameCode, expandChildren(context), disable); 163 } else { 164 context.getController().reportRecoverableError( 165 "Cannot write an attribute node when no element start tag is open", 166 this); 167 } 168 } 169 170 } 171 172 | Popular Tags |