KickJava   Java API By Example, From Geeks To Geeks.

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

127     private boolean hasBadChars(String JavaDoc 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     /**
139      * Return the name of the attribute
140      */

141     public String JavaDoc getName() {
142         return _name;
143     }
144     
145     /**
146      * Return the value of the attribute
147      */

148     public AttributeValue getValue() {
149         return _value;
150     }
151     
152 }
153
Popular Tags