KickJava   Java API By Example, From Geeks To Geeks.

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


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: TransletOutput.java,v 1.12 2004/02/16 22:25:10 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.INVOKEVIRTUAL;
24 import org.apache.bcel.generic.InstructionList;
25 import org.apache.bcel.generic.PUSH;
26 import org.apache.xalan.xsltc.compiler.util.ClassGenerator;
27 import org.apache.xalan.xsltc.compiler.util.ErrorMsg;
28 import org.apache.xalan.xsltc.compiler.util.MethodGenerator;
29 import org.apache.xalan.xsltc.compiler.util.StringType;
30 import org.apache.xalan.xsltc.compiler.util.Type;
31 import org.apache.xalan.xsltc.compiler.util.TypeCheckError;
32 import org.apache.xalan.xsltc.compiler.util.Util;
33
34 /**
35  * @author Morten Jorgensen
36  */

37 final class TransletOutput extends Instruction {
38
39     private Expression _filename;
40     private boolean _append;
41
42     /**
43      * Displays the contents of this <xsltc:output> element.
44      */

45     public void display(int indent) {
46     indent(indent);
47     Util.println("TransletOutput: " + _filename);
48     }
49         
50     /**
51      * Parse the contents of this <xsltc:output> element. The only attribute
52      * we recognise is the 'file' attribute that contains teh output filename.
53      */

54     public void parseContents(Parser parser) {
55     // Get the output filename from the 'file' attribute
56
String JavaDoc filename = getAttribute("file");
57         
58         // If the 'append' attribute is set to "yes" or "true",
59
// the output is appended to the file.
60
String JavaDoc append = getAttribute("append");
61
62     // Verify that the filename is in fact set
63
if ((filename == null) || (filename.equals(EMPTYSTRING))) {
64         reportError(this, parser, ErrorMsg.REQUIRED_ATTR_ERR, "file");
65     }
66
67     // Save filename as an attribute value template
68
_filename = AttributeValue.create(this, filename, parser);
69         
70         if (append != null && (append.toLowerCase().equals("yes") ||
71             append.toLowerCase().equals("true"))) {
72           _append = true;
73         }
74         else
75           _append = false;
76           
77     parseChildren(parser);
78     }
79     
80     /**
81      * Type checks the 'file' attribute (must be able to convert it to a str).
82      */

83     public Type typeCheck(SymbolTable stable) throws TypeCheckError {
84     final Type type = _filename.typeCheck(stable);
85     if (type instanceof StringType == false) {
86         _filename = new CastExpr(_filename, Type.String);
87     }
88     typeCheckContents(stable);
89     return Type.Void;
90     }
91     
92     /**
93      * Compile code that opens the give file for output, dumps the contents of
94      * the element to the file, then closes the file.
95      */

96     public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
97     final ConstantPoolGen cpg = classGen.getConstantPool();
98     final InstructionList il = methodGen.getInstructionList();
99
100     // Save the current output handler on the stack
101
il.append(methodGen.loadHandler());
102     
103     final int open = cpg.addMethodref(TRANSLET_CLASS,
104                        "openOutputHandler",
105                                            "(" + STRING_SIG + "Z)" +
106                        TRANSLET_OUTPUT_SIG);
107
108     final int close = cpg.addMethodref(TRANSLET_CLASS,
109                         "closeOutputHandler",
110                         "("+TRANSLET_OUTPUT_SIG+")V");
111
112     // Create the new output handler (leave it on stack)
113
il.append(classGen.loadTranslet());
114     _filename.translate(classGen, methodGen);
115         il.append(new PUSH(cpg, _append));
116     il.append(new INVOKEVIRTUAL(open));
117
118     // Overwrite current handler
119
il.append(methodGen.storeHandler());
120     
121     // Translate contents with substituted handler
122
translateContents(classGen, methodGen);
123
124     // Close the output handler (close file)
125
il.append(classGen.loadTranslet());
126     il.append(methodGen.loadHandler());
127     il.append(new INVOKEVIRTUAL(close));
128
129     // Restore old output handler from stack
130
il.append(methodGen.storeHandler());
131     }
132 }
133
134
Popular Tags