1 16 19 package org.apache.xalan.templates; 20 21 import javax.xml.transform.TransformerException ; 22 23 import org.apache.xalan.res.XSLTErrorResources; 24 import org.apache.xalan.transformer.TransformerImpl; 25 import org.apache.xml.serializer.NamespaceMappings; 26 import org.apache.xml.serializer.SerializationHandler; 27 import org.apache.xml.utils.QName; 28 import org.apache.xml.utils.XMLChar; 29 30 import org.xml.sax.SAXException ; 31 32 45 public class ElemAttribute extends ElemElement 46 { 47 48 54 public int getXSLToken() 55 { 56 return Constants.ELEMNAME_ATTRIBUTE; 57 } 58 59 64 public String getNodeName() 65 { 66 return Constants.ELEMNAME_ATTRIBUTE_STRING; 67 } 68 69 77 public void execute( 78 TransformerImpl transformer) 79 throws TransformerException 80 { 81 SerializationHandler rhandler = transformer.getSerializationHandler(); 82 83 107 super.execute(transformer); 108 109 } 110 111 121 protected String resolvePrefix(SerializationHandler rhandler, 122 String prefix, String nodeNamespace) 123 throws TransformerException 124 { 125 126 if (null != prefix && (prefix.length() == 0 || prefix.equals("xmlns"))) 127 { 128 prefix = rhandler.getPrefix(nodeNamespace); 131 132 if (null == prefix || prefix.length() == 0 || prefix.equals("xmlns")) 134 { 135 if(nodeNamespace.length() > 0) 136 { 137 NamespaceMappings prefixMapping = rhandler.getNamespaceMappings(); 138 prefix = prefixMapping.generateNextPrefix(); 139 } 140 else 141 prefix = ""; 142 } 143 } 144 return prefix; 145 } 146 147 154 protected boolean validateNodeName(String nodeName) 155 { 156 if(null == nodeName) 157 return false; 158 if(nodeName.equals("xmlns")) 159 return false; 160 return XMLChar.isValidQName(nodeName); 161 } 162 163 176 void constructNode( 177 String nodeName, String prefix, String nodeNamespace, TransformerImpl transformer) 178 throws TransformerException 179 { 180 181 if(null != nodeName && nodeName.length() > 0) 182 { 183 SerializationHandler rhandler = transformer.getSerializationHandler(); 184 if(prefix != null && prefix.length() > 0) 185 { 186 try 187 { 188 rhandler.startPrefixMapping(prefix, nodeNamespace, false); 189 } 190 catch(SAXException se) 191 { 192 throw new TransformerException (se); 193 } 194 } 195 String val = transformer.transformToString(this); 196 String localName = QName.getLocalPart(nodeName); 197 try 198 { 199 if(prefix != null && prefix.length() > 0){ 200 rhandler.addAttribute(nodeNamespace, localName, nodeName, "CDATA", val); 201 }else{ 202 rhandler.addAttribute("", localName, nodeName, "CDATA", val); 203 } 204 } 205 catch (SAXException e) 206 { 207 } 208 } 209 } 210 211 212 227 public ElemTemplateElement appendChild(ElemTemplateElement newChild) 228 { 229 230 int type = ((ElemTemplateElement) newChild).getXSLToken(); 231 232 switch (type) 233 { 234 235 case Constants.ELEMNAME_TEXTLITERALRESULT : 237 case Constants.ELEMNAME_APPLY_TEMPLATES : 238 case Constants.ELEMNAME_APPLY_IMPORTS : 239 case Constants.ELEMNAME_CALLTEMPLATE : 240 case Constants.ELEMNAME_FOREACH : 241 case Constants.ELEMNAME_VALUEOF : 242 case Constants.ELEMNAME_COPY_OF : 243 case Constants.ELEMNAME_NUMBER : 244 case Constants.ELEMNAME_CHOOSE : 245 case Constants.ELEMNAME_IF : 246 case Constants.ELEMNAME_TEXT : 247 case Constants.ELEMNAME_COPY : 248 case Constants.ELEMNAME_VARIABLE : 249 case Constants.ELEMNAME_MESSAGE : 250 251 break; 257 default : 258 error(XSLTErrorResources.ER_CANNOT_ADD, 259 new Object []{ newChild.getNodeName(), 260 this.getNodeName() }); 262 } 264 265 return super.appendChild(newChild); 266 } 267 270 public void setName(AVT v) { 271 if (v.isSimple()) 272 { 273 if (v.getSimpleString().equals("xmlns")) 274 { 275 throw new IllegalArgumentException (); 276 } 277 } 278 super.setName(v); 279 } 280 281 } 282 | Popular Tags |