KickJava   Java API By Example, From Geeks To Geeks.

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


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

19
20 package com.sun.org.apache.xalan.internal.xsltc.compiler;
21
22 import com.sun.org.apache.bcel.internal.generic.ALOAD;
23 import com.sun.org.apache.bcel.internal.generic.ASTORE;
24 import com.sun.org.apache.bcel.internal.generic.BranchHandle;
25 import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
26 import com.sun.org.apache.bcel.internal.generic.IFEQ;
27 import com.sun.org.apache.bcel.internal.generic.IFNULL;
28 import com.sun.org.apache.bcel.internal.generic.ILOAD;
29 import com.sun.org.apache.bcel.internal.generic.INVOKEINTERFACE;
30 import com.sun.org.apache.bcel.internal.generic.INVOKEVIRTUAL;
31 import com.sun.org.apache.bcel.internal.generic.ISTORE;
32 import com.sun.org.apache.bcel.internal.generic.InstructionHandle;
33 import com.sun.org.apache.bcel.internal.generic.InstructionList;
34 import com.sun.org.apache.bcel.internal.generic.LocalVariableGen;
35 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
36 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
37 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
38 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
39 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
40 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util;
41
42 /**
43  * @author Jacek Ambroziak
44  * @author Santiago Pericas-Geertsen
45  */

46 final class Copy extends Instruction {
47     private UseAttributeSets _useSets;
48     
49     public void parseContents(Parser parser) {
50     final String JavaDoc useSets = getAttribute("use-attribute-sets");
51     if (useSets.length() > 0) {
52             if (!Util.isValidQNames(useSets)) {
53                 ErrorMsg err = new ErrorMsg(ErrorMsg.INVALID_QNAME_ERR, useSets, this);
54                 parser.reportError(Constants.ERROR, err);
55             }
56         _useSets = new UseAttributeSets(useSets, parser);
57     }
58     parseChildren(parser);
59     }
60     
61     public void display(int indent) {
62     indent(indent);
63     Util.println("Copy");
64     indent(indent + IndentIncrement);
65     displayContents(indent + IndentIncrement);
66     }
67
68     public Type typeCheck(SymbolTable stable) throws TypeCheckError {
69     if (_useSets != null) {
70         _useSets.typeCheck(stable);
71     }
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     final LocalVariableGen name =
81         methodGen.addLocalVariable2("name",
82                     Util.getJCRefType(STRING_SIG),
83                     il.getEnd());
84     final LocalVariableGen length =
85         methodGen.addLocalVariable2("length",
86                     Util.getJCRefType("I"),
87                     il.getEnd());
88
89     // Get the name of the node to copy and save for later
90
il.append(methodGen.loadDOM());
91     il.append(methodGen.loadCurrentNode());
92     il.append(methodGen.loadHandler());
93     final int cpy = cpg.addInterfaceMethodref(DOM_INTF,
94                           "shallowCopy",
95                           "("
96                           + NODE_SIG
97                           + TRANSLET_OUTPUT_SIG
98                           + ")" + STRING_SIG);
99     il.append(new INVOKEINTERFACE(cpy, 3));
100     il.append(DUP);
101     il.append(new ASTORE(name.getIndex()));
102     final BranchHandle ifBlock1 = il.append(new IFNULL(null));
103
104     // Get the length of the node name and save for later
105
il.append(new ALOAD(name.getIndex()));
106     final int lengthMethod = cpg.addMethodref(STRING_CLASS,"length","()I");
107     il.append(new INVOKEVIRTUAL(lengthMethod));
108     il.append(new ISTORE(length.getIndex()));
109
110     // Copy in attribute sets if specified
111
if (_useSets != null) {
112         // If the parent of this element will result in an element being
113
// output then we know that it is safe to copy out the attributes
114
final SyntaxTreeNode parent = getParent();
115         if ((parent instanceof LiteralElement) ||
116         (parent instanceof LiteralElement)) {
117         _useSets.translate(classGen, methodGen);
118         }
119         // If not we have to check to see if the copy will result in an
120
// element being output.
121
else {
122         // check if element; if not skip to translate body
123
il.append(new ILOAD(length.getIndex()));
124         final BranchHandle ifBlock2 = il.append(new IFEQ(null));
125         // length != 0 -> element -> do attribute sets
126
_useSets.translate(classGen, methodGen);
127         // not an element; root
128
ifBlock2.setTarget(il.append(NOP));
129         }
130     }
131
132     // Instantiate body of xsl:copy
133
translateContents(classGen, methodGen);
134
135     // Call the output handler's endElement() if we copied an element
136
// (The DOM.shallowCopy() method calls startElement().)
137
il.append(new ILOAD(length.getIndex()));
138     final BranchHandle ifBlock3 = il.append(new IFEQ(null));
139     il.append(methodGen.loadHandler());
140     il.append(new ALOAD(name.getIndex()));
141     il.append(methodGen.endElement());
142     
143     final InstructionHandle end = il.append(NOP);
144     ifBlock1.setTarget(end);
145     ifBlock3.setTarget(end);
146     methodGen.removeLocalVariable(name);
147     methodGen.removeLocalVariable(length);
148     }
149 }
150
Popular Tags