KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xalan > internal > xsltc > compiler > LiteralAttribute


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
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 /**
35  * @author Jacek Ambroziak
36  * @author Santiago Pericas-Geertsen
37  * @author Morten Jorgensen
38  */

39 final class LiteralAttribute extends Instruction {
40
41     private final String JavaDoc _name; // Attribute name (incl. prefix)
42
private final AttributeValue _value; // Attribute value
43

44     /**
45      * Creates a new literal attribute (but does not insert it into the AST).
46      * @param name the attribute name (incl. prefix) as a String.
47      * @param value the attribute value.
48      * @param parser the XSLT parser (wraps XPath parser).
49      */

50     public LiteralAttribute(String JavaDoc name, String JavaDoc 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     // push handler
78
il.append(methodGen.loadHandler());
79     // push attribute name - namespace prefix set by parent node
80
il.append(new PUSH(cpg, _name));
81     // push attribute value
82
_value.translate(classGen, methodGen);
83     
84     // Generate code that calls SerializationHandler.addUniqueAttribute()
85
// if all attributes are unique.
86
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         // Set the HTML flags
95
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 JavaDoc 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         // call attribute
118
il.append(methodGen.attribute());
119     }
120     }
121     
122     /**
123      * Return true if at least one character in the String is considered to
124      * be a "bad" character. A bad character is one whose code is:
125      * less than 32 (a space),
126      * or greater than 126,
127      * or it is one of '<', '>', '&' or '\"'.
128      * This helps the serializer to decide whether the String needs to be escaped.
129      */

130     private boolean hasBadChars(String JavaDoc 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     /**
142      * Return the name of the attribute
143      */

144     public String JavaDoc getName() {
145         return _name;
146     }
147     
148     /**
149      * Return the value of the attribute
150      */

151     public AttributeValue getValue() {
152         return _value;
153     }
154     
155 }
156
Popular Tags