jaxp/src/com/sun/org/apache/xalan/internal/xsltc/compiler/TransletOutput.java
author joehw
Thu, 12 Apr 2012 08:38:26 -0700
changeset 12457 c348e06f0e82
parent 6 jaxp/src/share/classes/com/sun/org/apache/xalan/internal/xsltc/compiler/TransletOutput.java@7f561c08de6b
permissions -rw-r--r--
7160496: Rename JDK8 JAXP source directory Summary: moving src/share/classes to src Reviewed-by: ohair
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     1
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
     2
 * reserved comment block
7f561c08de6b Initial load
duke
parents:
diff changeset
     3
 * DO NOT REMOVE OR ALTER!
7f561c08de6b Initial load
duke
parents:
diff changeset
     4
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
     5
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
     6
 * Copyright 2001-2004 The Apache Software Foundation.
7f561c08de6b Initial load
duke
parents:
diff changeset
     7
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
     8
 * Licensed under the Apache License, Version 2.0 (the "License");
7f561c08de6b Initial load
duke
parents:
diff changeset
     9
 * you may not use this file except in compliance with the License.
7f561c08de6b Initial load
duke
parents:
diff changeset
    10
 * You may obtain a copy of the License at
7f561c08de6b Initial load
duke
parents:
diff changeset
    11
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    12
 *     http://www.apache.org/licenses/LICENSE-2.0
7f561c08de6b Initial load
duke
parents:
diff changeset
    13
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    14
 * Unless required by applicable law or agreed to in writing, software
7f561c08de6b Initial load
duke
parents:
diff changeset
    15
 * distributed under the License is distributed on an "AS IS" BASIS,
7f561c08de6b Initial load
duke
parents:
diff changeset
    16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
7f561c08de6b Initial load
duke
parents:
diff changeset
    17
 * See the License for the specific language governing permissions and
7f561c08de6b Initial load
duke
parents:
diff changeset
    18
 * limitations under the License.
7f561c08de6b Initial load
duke
parents:
diff changeset
    19
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    20
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
    21
 * $Id: TransletOutput.java,v 1.2.4.1 2005/09/05 09:19:44 pvedula Exp $
7f561c08de6b Initial load
duke
parents:
diff changeset
    22
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
7f561c08de6b Initial load
duke
parents:
diff changeset
    24
package com.sun.org.apache.xalan.internal.xsltc.compiler;
7f561c08de6b Initial load
duke
parents:
diff changeset
    25
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
import com.sun.org.apache.bcel.internal.generic.INVOKESTATIC;
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
import com.sun.org.apache.bcel.internal.generic.INVOKEVIRTUAL;
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
import com.sun.org.apache.bcel.internal.generic.InstructionList;
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
import com.sun.org.apache.bcel.internal.generic.PUSH;
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.StringType;
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util;
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
 * @author Morten Jorgensen
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
final class TransletOutput extends Instruction {
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
    private Expression _filename;
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
    private boolean _append;
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
     * Displays the contents of this <xsltc:output> element.
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
    public void display(int indent) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
        indent(indent);
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
        Util.println("TransletOutput: " + _filename);
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
     * Parse the contents of this <xsltc:output> element. The only attribute
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
     * we recognise is the 'file' attribute that contains teh output filename.
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
    public void parseContents(Parser parser) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
        // Get the output filename from the 'file' attribute
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
        String filename = getAttribute("file");
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
        // If the 'append' attribute is set to "yes" or "true",
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
        // the output is appended to the file.
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
        String append   = getAttribute("append");
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
        // Verify that the filename is in fact set
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
        if ((filename == null) || (filename.equals(EMPTYSTRING))) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
            reportError(this, parser, ErrorMsg.REQUIRED_ATTR_ERR, "file");
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
        // Save filename as an attribute value template
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
        _filename = AttributeValue.create(this, filename, parser);
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
        if (append != null && (append.toLowerCase().equals("yes") ||
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
            append.toLowerCase().equals("true"))) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
          _append = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
        else
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
          _append = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
        parseChildren(parser);
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
     * Type checks the 'file' attribute (must be able to convert it to a str).
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
    public Type typeCheck(SymbolTable stable) throws TypeCheckError {
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
        final Type type = _filename.typeCheck(stable);
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
        if (type instanceof StringType == false) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
            _filename = new CastExpr(_filename, Type.String);
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
        typeCheckContents(stable);
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
        return Type.Void;
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
     * Compile code that opens the give file for output, dumps the contents of
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
     * the element to the file, then closes the file.
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
    public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
        final ConstantPoolGen cpg = classGen.getConstantPool();
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
        final InstructionList il = methodGen.getInstructionList();
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
        final boolean isSecureProcessing = classGen.getParser().getXSLTC()
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
                                           .isSecureProcessing();
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
        if (isSecureProcessing) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
            int index = cpg.addMethodref(BASIS_LIBRARY_CLASS,
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
                                         "unallowed_extension_elementF",
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
                                         "(Ljava/lang/String;)V");
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
            il.append(new PUSH(cpg, "redirect"));
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
            il.append(new INVOKESTATIC(index));
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
            return;
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
        // Save the current output handler on the stack
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
        il.append(methodGen.loadHandler());
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
        final int open =  cpg.addMethodref(TRANSLET_CLASS,
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
                                           "openOutputHandler",
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
                                           "(" + STRING_SIG + "Z)" +
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
                                           TRANSLET_OUTPUT_SIG);
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
        final int close =  cpg.addMethodref(TRANSLET_CLASS,
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
                                            "closeOutputHandler",
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
                                            "("+TRANSLET_OUTPUT_SIG+")V");
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
        // Create the new output handler (leave it on stack)
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
        il.append(classGen.loadTranslet());
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
        _filename.translate(classGen, methodGen);
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
        il.append(new PUSH(cpg, _append));
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
        il.append(new INVOKEVIRTUAL(open));
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
        // Overwrite current handler
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
        il.append(methodGen.storeHandler());
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
        // Translate contents with substituted handler
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
        translateContents(classGen, methodGen);
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
        // Close the output handler (close file)
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
        il.append(classGen.loadTranslet());
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
        il.append(methodGen.loadHandler());
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
        il.append(new INVOKEVIRTUAL(close));
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
        // Restore old output handler from stack
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
        il.append(methodGen.storeHandler());
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
}