KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > xsltc > compiler > ProcessingInstruction


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: ProcessingInstruction.java,v 1.11 2004/02/24 03:55:48 zongaro Exp $
18  */

19
20 package org.apache.xalan.xsltc.compiler;
21
22 import org.apache.bcel.generic.ALOAD;
23 import org.apache.bcel.generic.ASTORE;
24 import org.apache.bcel.generic.ConstantPoolGen;
25 import org.apache.bcel.generic.GETFIELD;
26 import org.apache.bcel.generic.INVOKEINTERFACE;
27 import org.apache.bcel.generic.INVOKESTATIC;
28 import org.apache.bcel.generic.INVOKEVIRTUAL;
29 import org.apache.bcel.generic.InstructionList;
30 import org.apache.bcel.generic.LocalVariableGen;
31 import org.apache.xalan.xsltc.compiler.util.ClassGenerator;
32 import org.apache.xalan.xsltc.compiler.util.ErrorMsg;
33 import org.apache.xalan.xsltc.compiler.util.MethodGenerator;
34 import org.apache.xalan.xsltc.compiler.util.Type;
35 import org.apache.xalan.xsltc.compiler.util.TypeCheckError;
36 import org.apache.xalan.xsltc.compiler.util.Util;
37 import org.apache.xml.utils.XMLChar;
38
39 /**
40  * @author Jacek Ambroziak
41  * @author Santiago Pericas-Geertsen
42  */

43 final class ProcessingInstruction extends Instruction {
44
45     private AttributeValue _name; // name treated as AVT (7.1.3)
46
private boolean _isLiteral = false; // specified name is not AVT
47

48     public void parseContents(Parser parser) {
49     final String JavaDoc name = getAttribute("name");
50     
51         if (name.length() > 0) {
52             _isLiteral = Util.isLiteral(name);
53             if (_isLiteral) {
54                 if (!XMLChar.isValidNCName(name)) {
55                     ErrorMsg err = new ErrorMsg(ErrorMsg.INVALID_NCNAME_ERR, name, this);
56                     parser.reportError(Constants.ERROR, err);
57                 }
58             }
59             _name = AttributeValue.create(this, name, parser);
60         }
61         else
62             reportError(this, parser, ErrorMsg.REQUIRED_ATTR_ERR, "name");
63             
64     if (name.equals("xml")) {
65         reportError(this, parser, ErrorMsg.ILLEGAL_PI_ERR, "xml");
66     }
67     parseChildren(parser);
68     }
69
70     public Type typeCheck(SymbolTable stable) throws TypeCheckError {
71     _name.typeCheck(stable);
72     typeCheckContents(stable);
73     return Type.Void;
74     }
75
76     public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
77     final ConstantPoolGen cpg = classGen.getConstantPool();
78     final InstructionList il = methodGen.getInstructionList();
79     
80         if (!_isLiteral) {
81             // if the ncname is an AVT, then the ncname has to be checked at runtime if it is a valid ncname
82
LocalVariableGen nameValue = methodGen.addLocalVariable2("nameValue",
83             Util.getJCRefType(STRING_SIG),
84             il.getEnd());
85             
86             // store the name into a variable first so _name.translate only needs to be called once
87
_name.translate(classGen, methodGen);
88             il.append(new ASTORE(nameValue.getIndex()));
89             il.append(new ALOAD(nameValue.getIndex()));
90             
91             // call checkNCName if the name is an AVT
92
final int check = cpg.addMethodref(BASIS_LIBRARY_CLASS, "checkNCName",
93                                 "("
94                                 +STRING_SIG
95                                 +")V");
96                                 il.append(new INVOKESTATIC(check));
97             
98             // Save the current handler base on the stack
99
il.append(methodGen.loadHandler());
100             il.append(DUP); // first arg to "attributes" call
101

102             // load name value again
103
il.append(new ALOAD(nameValue.getIndex()));
104         } else {
105             // Save the current handler base on the stack
106
il.append(methodGen.loadHandler());
107             il.append(DUP); // first arg to "attributes" call
108

109             // Push attribute name
110
_name.translate(classGen, methodGen);// 2nd arg
111

112         }
113         
114     il.append(classGen.loadTranslet());
115     il.append(new GETFIELD(cpg.addFieldref(TRANSLET_CLASS,
116                            "stringValueHandler",
117                            STRING_VALUE_HANDLER_SIG)));
118     il.append(DUP);
119     il.append(methodGen.storeHandler());
120
121     // translate contents with substituted handler
122
translateContents(classGen, methodGen);
123
124     // get String out of the handler
125
il.append(new INVOKEVIRTUAL(cpg.addMethodref(STRING_VALUE_HANDLER,
126                              "getValueOfPI",
127                              "()" + STRING_SIG)));
128     // call "processingInstruction"
129
final int processingInstruction =
130         cpg.addInterfaceMethodref(TRANSLET_OUTPUT_INTERFACE,
131                       "processingInstruction",
132                       "(" + STRING_SIG + STRING_SIG + ")V");
133     il.append(new INVOKEINTERFACE(processingInstruction, 3));
134     // Restore old handler base from stack
135
il.append(methodGen.storeHandler());
136     }
137 }
138
Popular Tags