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