1 16 19 20 package com.sun.org.apache.xalan.internal.xsltc.compiler; 21 22 import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen; 23 import com.sun.org.apache.bcel.internal.generic.InstructionList; 24 import com.sun.org.apache.bcel.internal.generic.PUSH; 25 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator; 26 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator; 27 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type; 28 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError; 29 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util; 30 31 import com.sun.org.apache.xml.internal.serializer.ElemDesc; 32 import com.sun.org.apache.xml.internal.serializer.SerializationHandler; 33 34 39 final class LiteralAttribute extends Instruction { 40 41 private final String _name; private final AttributeValue _value; 44 50 public LiteralAttribute(String name, String value, Parser parser, 51 SyntaxTreeNode parent) 52 { 53 _name = name; 54 setParent(parent); 55 _value = AttributeValue.create(this, value, parser); 56 } 57 58 public void display(int indent) { 59 indent(indent); 60 Util.println("LiteralAttribute name=" + _name + " value=" + _value); 61 } 62 63 public Type typeCheck(SymbolTable stable) throws TypeCheckError { 64 _value.typeCheck(stable); 65 typeCheckContents(stable); 66 return Type.Void; 67 } 68 69 protected boolean contextDependent() { 70 return _value.contextDependent(); 71 } 72 73 public void translate(ClassGenerator classGen, MethodGenerator methodGen) { 74 final ConstantPoolGen cpg = classGen.getConstantPool(); 75 final InstructionList il = methodGen.getInstructionList(); 76 77 il.append(methodGen.loadHandler()); 79 il.append(new PUSH(cpg, _name)); 81 _value.translate(classGen, methodGen); 83 84 SyntaxTreeNode parent = getParent(); 87 if (parent instanceof LiteralElement 88 && ((LiteralElement)parent).allAttributesUnique()) { 89 90 int flags = 0; 91 boolean isHTMLAttrEmpty = false; 92 ElemDesc elemDesc = ((LiteralElement)parent).getElemDesc(); 93 94 if (elemDesc != null) { 96 if (elemDesc.isAttrFlagSet(_name, ElemDesc.ATTREMPTY)) { 97 flags = flags | SerializationHandler.HTML_ATTREMPTY; 98 isHTMLAttrEmpty = true; 99 } 100 else if (elemDesc.isAttrFlagSet(_name, ElemDesc.ATTRURL)) { 101 flags = flags | SerializationHandler.HTML_ATTRURL; 102 } 103 } 104 105 if (_value instanceof SimpleAttributeValue) { 106 String attrValue = ((SimpleAttributeValue)_value).toString(); 107 108 if (!hasBadChars(attrValue) && !isHTMLAttrEmpty) { 109 flags = flags | SerializationHandler.NO_BAD_CHARS; 110 } 111 } 112 113 il.append(new PUSH(cpg, flags)); 114 il.append(methodGen.uniqueAttribute()); 115 } 116 else { 117 il.append(methodGen.attribute()); 119 } 120 } 121 122 130 private boolean hasBadChars(String value) { 131 char[] chars = value.toCharArray(); 132 int size = chars.length; 133 for (int i = 0; i < size; i++) { 134 char ch = chars[i]; 135 if (ch < 32 || 126 < ch || ch == '<' || ch == '>' || ch == '&' || ch == '\"') 136 return true; 137 } 138 return false; 139 } 140 141 144 public String getName() { 145 return _name; 146 } 147 148 151 public AttributeValue getValue() { 152 return _value; 153 } 154 155 } 156 | Popular Tags |