1 16 19 20 package org.apache.xalan.xsltc.compiler; 21 22 import java.util.Vector ; 23 24 import org.apache.bcel.generic.ALOAD; 25 import org.apache.bcel.generic.ASTORE; 26 import org.apache.bcel.generic.ConstantPoolGen; 27 import org.apache.bcel.generic.GETFIELD; 28 import org.apache.bcel.generic.INVOKESTATIC; 29 import org.apache.bcel.generic.INVOKEVIRTUAL; 30 import org.apache.bcel.generic.InstructionList; 31 import org.apache.bcel.generic.LocalVariableGen; 32 import org.apache.bcel.generic.PUSH; 33 import org.apache.xalan.xsltc.compiler.util.ClassGenerator; 34 import org.apache.xalan.xsltc.compiler.util.ErrorMsg; 35 import org.apache.xalan.xsltc.compiler.util.MethodGenerator; 36 import org.apache.xalan.xsltc.compiler.util.Type; 37 import org.apache.xalan.xsltc.compiler.util.TypeCheckError; 38 import org.apache.xalan.xsltc.compiler.util.Util; 39 import org.apache.xml.utils.XMLChar; 40 41 import org.apache.xml.serializer.ElemDesc; 42 import org.apache.xml.serializer.SerializationHandler; 43 44 51 final class XslAttribute extends Instruction { 52 53 private String _prefix; 54 private AttributeValue _name; private AttributeValueTemplate _namespace = null; 56 private boolean _ignore = false; 57 private boolean _isLiteral = false; 59 62 public AttributeValue getName() { 63 return _name; 64 } 65 66 69 public void display(int indent) { 70 indent(indent); 71 Util.println("Attribute " + _name); 72 displayContents(indent + IndentIncrement); 73 } 74 75 78 public void parseContents(Parser parser) { 79 boolean generated = false; 80 final SymbolTable stable = parser.getSymbolTable(); 81 82 String name = getAttribute("name"); 83 String namespace = getAttribute("namespace"); 84 QName qname = parser.getQName(name, false); 85 final String prefix = qname.getPrefix(); 86 87 if (((prefix != null) && (prefix.equals(XMLNS_PREFIX)))||(name.equals(XMLNS_PREFIX))) { 88 reportError(this, parser, ErrorMsg.ILLEGAL_ATTR_NAME_ERR, name); 89 return; 90 } 91 92 _isLiteral = Util.isLiteral(name); 93 if (_isLiteral) { 94 if (!XMLChar.isValidQName(name)) { 95 reportError(this, parser, ErrorMsg.ILLEGAL_ATTR_NAME_ERR, name); 96 return; 97 } 98 } 99 100 final SyntaxTreeNode parent = getParent(); 102 final Vector siblings = parent.getContents(); 103 for (int i = 0; i < parent.elementCount(); i++) { 104 SyntaxTreeNode item = (SyntaxTreeNode)siblings.elementAt(i); 105 if (item == this) break; 106 107 if (item instanceof XslAttribute) continue; 109 if (item instanceof UseAttributeSets) continue; 110 if (item instanceof LiteralAttribute) continue; 111 if (item instanceof Text) continue; 112 113 if (item instanceof If) continue; 116 if (item instanceof Choose) continue; 117 if (item instanceof CopyOf) continue; 118 if (item instanceof VariableBase) continue; 119 120 reportWarning(this, parser, ErrorMsg.STRAY_ATTRIBUTE_ERR, name); 122 } 123 124 if (namespace != null && namespace != Constants.EMPTYSTRING) { 126 _prefix = lookupPrefix(namespace); 127 _namespace = new AttributeValueTemplate(namespace, parser, this); 128 } 129 else if (prefix != null && prefix != Constants.EMPTYSTRING) { 131 _prefix = prefix; 132 namespace = lookupNamespace(prefix); 133 if (namespace != null) { 134 _namespace = new AttributeValueTemplate(namespace, parser, this); 135 } 136 } 137 138 if (_namespace != null) { 140 if (_prefix == null || _prefix == Constants.EMPTYSTRING) { 142 if (prefix != null) { 143 _prefix = prefix; 144 } 145 else { 146 _prefix = stable.generateNamespacePrefix(); 147 generated = true; 148 } 149 } 150 else if (prefix != null && !prefix.equals(_prefix)) { 151 _prefix = prefix; 152 } 153 154 name = _prefix + ":" + qname.getLocalPart(); 155 156 161 if ((parent instanceof LiteralElement) && (!generated)) { 162 ((LiteralElement)parent).registerNamespace(_prefix, 163 namespace, 164 stable, false); 165 } 166 } 167 168 if (parent instanceof LiteralElement) { 169 ((LiteralElement)parent).addAttribute(this); 170 } 171 172 _name = AttributeValue.create(this, name, parser); 173 parseChildren(parser); 174 } 175 176 public Type typeCheck(SymbolTable stable) throws TypeCheckError { 177 if (!_ignore) { 178 _name.typeCheck(stable); 179 if (_namespace != null) { 180 _namespace.typeCheck(stable); 181 } 182 typeCheckContents(stable); 183 } 184 return Type.Void; 185 } 186 187 190 public void translate(ClassGenerator classGen, MethodGenerator methodGen) { 191 final ConstantPoolGen cpg = classGen.getConstantPool(); 192 final InstructionList il = methodGen.getInstructionList(); 193 194 if (_ignore) return; 195 _ignore = true; 196 197 if (_namespace != null) { 199 il.append(methodGen.loadHandler()); 201 il.append(new PUSH(cpg,_prefix)); 202 _namespace.translate(classGen,methodGen); 203 il.append(methodGen.namespace()); 204 } 205 206 if (!_isLiteral) { 207 LocalVariableGen nameValue = methodGen.addLocalVariable2("nameValue", 209 Util.getJCRefType(STRING_SIG), 210 il.getEnd()); 211 212 _name.translate(classGen, methodGen); 214 il.append(new ASTORE(nameValue.getIndex())); 215 il.append(new ALOAD(nameValue.getIndex())); 216 217 final int check = cpg.addMethodref(BASIS_LIBRARY_CLASS, "checkAttribQName", 219 "(" 220 +STRING_SIG 221 +")V"); 222 il.append(new INVOKESTATIC(check)); 223 224 il.append(methodGen.loadHandler()); 226 il.append(DUP); 228 il.append(new ALOAD(nameValue.getIndex())); 230 } else { 231 il.append(methodGen.loadHandler()); 233 il.append(DUP); 235 _name.translate(classGen, methodGen); 238 } 239 240 if ((elementCount() == 1) && (elementAt(0) instanceof Text)) { 242 il.append(new PUSH(cpg, ((Text)elementAt(0)).getText())); 243 } 244 else { 245 il.append(classGen.loadTranslet()); 246 il.append(new GETFIELD(cpg.addFieldref(TRANSLET_CLASS, 247 "stringValueHandler", 248 STRING_VALUE_HANDLER_SIG))); 249 il.append(DUP); 250 il.append(methodGen.storeHandler()); 251 translateContents(classGen, methodGen); 253 il.append(new INVOKEVIRTUAL(cpg.addMethodref(STRING_VALUE_HANDLER, 255 "getValue", 256 "()" + STRING_SIG))); 257 } 258 259 SyntaxTreeNode parent = getParent(); 260 if (parent instanceof LiteralElement 261 && ((LiteralElement)parent).allAttributesUnique()) { 262 int flags = 0; 263 ElemDesc elemDesc = ((LiteralElement)parent).getElemDesc(); 264 265 if (elemDesc != null && _name instanceof SimpleAttributeValue) { 267 String attrName = ((SimpleAttributeValue)_name).toString(); 268 if (elemDesc.isAttrFlagSet(attrName, ElemDesc.ATTREMPTY)) { 269 flags = flags | SerializationHandler.HTML_ATTREMPTY; 270 } 271 else if (elemDesc.isAttrFlagSet(attrName, ElemDesc.ATTRURL)) { 272 flags = flags | SerializationHandler.HTML_ATTRURL; 273 } 274 } 275 il.append(new PUSH(cpg, flags)); 276 il.append(methodGen.uniqueAttribute()); 277 } 278 else { 279 il.append(methodGen.attribute()); 281 } 282 283 il.append(methodGen.storeHandler()); 285 286 287 288 } 289 290 } 291 | Popular Tags |