KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xalan > internal > 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.1.2.1 2006/09/19 01:06:43 jeffsuttor 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.INVOKESTATIC;
24 import com.sun.org.apache.bcel.internal.generic.INVOKEVIRTUAL;
25 import com.sun.org.apache.bcel.internal.generic.InstructionList;
26 import com.sun.org.apache.bcel.internal.generic.PUSH;
27 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
28 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
29 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
30 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.StringType;
31 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
32 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
33 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util;
34
35 /**
36  * @author Morten Jorgensen
37  */

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

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

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

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

97     public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
98     final ConstantPoolGen cpg = classGen.getConstantPool();
99     final InstructionList il = methodGen.getInstructionList();
100     final boolean isSecureProcessing = classGen.getParser().getXSLTC()
101                                        .isSecureProcessing();
102
103     if (isSecureProcessing) {
104         int index = cpg.addMethodref(BASIS_LIBRARY_CLASS,
105                          "unallowed_extension_elementF",
106                          "(Ljava/lang/String;)V");
107         il.append(new PUSH(cpg, "redirect"));
108         il.append(new INVOKESTATIC(index));
109         return;
110     }
111
112         // Save the current output handler on the stack
113
il.append(methodGen.loadHandler());
114     
115     final int open = cpg.addMethodref(TRANSLET_CLASS,
116                        "openOutputHandler",
117                                            "(" + STRING_SIG + "Z)" +
118                        TRANSLET_OUTPUT_SIG);
119
120     final int close = cpg.addMethodref(TRANSLET_CLASS,
121                         "closeOutputHandler",
122                         "("+TRANSLET_OUTPUT_SIG+")V");
123
124     // Create the new output handler (leave it on stack)
125
il.append(classGen.loadTranslet());
126     _filename.translate(classGen, methodGen);
127         il.append(new PUSH(cpg, _append));
128     il.append(new INVOKEVIRTUAL(open));
129
130     // Overwrite current handler
131
il.append(methodGen.storeHandler());
132     
133     // Translate contents with substituted handler
134
translateContents(classGen, methodGen);
135
136     // Close the output handler (close file)
137
il.append(classGen.loadTranslet());
138     il.append(methodGen.loadHandler());
139     il.append(new INVOKEVIRTUAL(close));
140
141     // Restore old output handler from stack
142
il.append(methodGen.storeHandler());
143     }
144 }
145
146
Popular Tags