1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * $Id: LiteralAttribute.java,v 1.8 2004/02/16 22:24:29 minchau Exp $
  18. */
  19. package com.sun.org.apache.xalan.internal.xsltc.compiler;
  20. import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
  21. import com.sun.org.apache.bcel.internal.generic.InstructionList;
  22. import com.sun.org.apache.bcel.internal.generic.PUSH;
  23. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
  24. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
  25. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
  26. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
  27. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util;
  28. import com.sun.org.apache.xml.internal.serializer.ElemDesc;
  29. import com.sun.org.apache.xml.internal.serializer.SerializationHandler;
  30. /**
  31. * @author Jacek Ambroziak
  32. * @author Santiago Pericas-Geertsen
  33. * @author Morten Jorgensen
  34. */
  35. final class LiteralAttribute extends Instruction {
  36. private final String _name; // Attribute name (incl. prefix)
  37. private final AttributeValue _value; // Attribute value
  38. /**
  39. * Creates a new literal attribute (but does not insert it into the AST).
  40. * @param name the attribute name (incl. prefix) as a String.
  41. * @param value the attribute value.
  42. * @param parser the XSLT parser (wraps XPath parser).
  43. */
  44. public LiteralAttribute(String name, String value, Parser parser,
  45. SyntaxTreeNode parent)
  46. {
  47. _name = name;
  48. setParent(parent);
  49. _value = AttributeValue.create(this, value, parser);
  50. }
  51. public void display(int indent) {
  52. indent(indent);
  53. Util.println("LiteralAttribute name=" + _name + " value=" + _value);
  54. }
  55. public Type typeCheck(SymbolTable stable) throws TypeCheckError {
  56. _value.typeCheck(stable);
  57. typeCheckContents(stable);
  58. return Type.Void;
  59. }
  60. protected boolean contextDependent() {
  61. return _value.contextDependent();
  62. }
  63. public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
  64. final ConstantPoolGen cpg = classGen.getConstantPool();
  65. final InstructionList il = methodGen.getInstructionList();
  66. // push handler
  67. il.append(methodGen.loadHandler());
  68. // push attribute name - namespace prefix set by parent node
  69. il.append(new PUSH(cpg, _name));
  70. // push attribute value
  71. _value.translate(classGen, methodGen);
  72. // Generate code that calls SerializationHandler.addUniqueAttribute()
  73. // if all attributes are unique.
  74. SyntaxTreeNode parent = getParent();
  75. if (parent instanceof LiteralElement
  76. && ((LiteralElement)parent).allAttributesUnique()) {
  77. int flags = 0;
  78. boolean isHTMLAttrEmpty = false;
  79. ElemDesc elemDesc = ((LiteralElement)parent).getElemDesc();
  80. // Set the HTML flags
  81. if (elemDesc != null) {
  82. if (elemDesc.isAttrFlagSet(_name, ElemDesc.ATTREMPTY)) {
  83. flags = flags | SerializationHandler.HTML_ATTREMPTY;
  84. isHTMLAttrEmpty = true;
  85. }
  86. else if (elemDesc.isAttrFlagSet(_name, ElemDesc.ATTRURL)) {
  87. flags = flags | SerializationHandler.HTML_ATTRURL;
  88. }
  89. }
  90. if (_value instanceof SimpleAttributeValue) {
  91. String attrValue = ((SimpleAttributeValue)_value).toString();
  92. if (!hasBadChars(attrValue) && !isHTMLAttrEmpty) {
  93. flags = flags | SerializationHandler.NO_BAD_CHARS;
  94. }
  95. }
  96. il.append(new PUSH(cpg, flags));
  97. il.append(methodGen.uniqueAttribute());
  98. }
  99. else {
  100. // call attribute
  101. il.append(methodGen.attribute());
  102. }
  103. }
  104. /**
  105. * Return true if at least one character in the String is considered to
  106. * be a "bad" character. A bad character is one whose code is:
  107. * less than 32 (a space),
  108. * or greater than 126,
  109. * or it is one of '<', '>', '&' or '\"'.
  110. * This helps the serializer to decide whether the String needs to be escaped.
  111. */
  112. private boolean hasBadChars(String value) {
  113. char[] chars = value.toCharArray();
  114. int size = chars.length;
  115. for (int i = 0; i < size; i++) {
  116. char ch = chars[i];
  117. if (ch < 32 || 126 < ch || ch == '<' || ch == '>' || ch == '&' || ch == '\"')
  118. return true;
  119. }
  120. return false;
  121. }
  122. /**
  123. * Return the name of the attribute
  124. */
  125. public String getName() {
  126. return _name;
  127. }
  128. /**
  129. * Return the value of the attribute
  130. */
  131. public AttributeValue getValue() {
  132. return _value;
  133. }
  134. }