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