jaxp/src/com/sun/org/apache/xalan/internal/xsltc/compiler/Output.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/Output.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: Output.java,v 1.2.4.1 2005/09/12 10:53:00 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 java.io.OutputStreamWriter;
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
import java.util.Properties;
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
import java.util.StringTokenizer;
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
import javax.xml.transform.OutputKeys;
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
import com.sun.org.apache.bcel.internal.generic.INVOKEVIRTUAL;
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
import com.sun.org.apache.bcel.internal.generic.InstructionList;
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
import com.sun.org.apache.bcel.internal.generic.PUSH;
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
import com.sun.org.apache.bcel.internal.generic.PUTFIELD;
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util;
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
import com.sun.org.apache.xml.internal.serializer.Encodings;
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
import com.sun.org.apache.xml.internal.utils.XML11Char;
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
 * @author Jacek Ambroziak
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
 * @author Santiago Pericas-Geertsen
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
 * @author Morten Jorgensen
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
final class Output extends TopLevelElement {
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
    // TODO: use three-value variables for boolean values: true/false/default
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
    // These attributes are extracted from the xsl:output element. They also
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
    // appear as fields (with the same type, only public) in the translet
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
    private String  _version;
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
    private String  _method;
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
    private String  _encoding;
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
    private boolean _omitHeader = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
    private String  _standalone;
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
    private String  _doctypePublic;
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
    private String  _doctypeSystem;
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
    private String  _cdata;
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
    private boolean _indent = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
    private String  _mediaType;
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
    private String _indentamount;
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
    // Disables this output element (when other element has higher precedence)
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
    private boolean _disabled = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
    // Some global constants
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
    private final static String STRING_SIG = "Ljava/lang/String;";
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
    private final static String XML_VERSION = "1.0";
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
    private final static String HTML_VERSION = "4.0";
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
     * Displays the contents of this element (for debugging)
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
    public void display(int indent) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
        indent(indent);
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
        Util.println("Output " + _method);
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
     * Disables this <xsl:output> element in case where there are some other
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
     * <xsl:output> element (from a different imported/included stylesheet)
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
     * with higher precedence.
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
    public void disable() {
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
        _disabled = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
    public boolean enabled() {
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
        return !_disabled;
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
    public String getCdata() {
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
        return _cdata;
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
    public String getOutputMethod() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
        return _method;
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
    private void transferAttribute(Output previous, String qname) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
        if (!hasAttribute(qname) && previous.hasAttribute(qname)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
            addAttribute(qname, previous.getAttribute(qname));
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
    public void mergeOutput(Output previous) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
        // Transfer attributes from previous xsl:output
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
        transferAttribute(previous, "version");
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
        transferAttribute(previous, "method");
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
        transferAttribute(previous, "encoding");
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
        transferAttribute(previous, "doctype-system");
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
        transferAttribute(previous, "doctype-public");
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
        transferAttribute(previous, "media-type");
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
        transferAttribute(previous, "indent");
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
        transferAttribute(previous, "omit-xml-declaration");
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
        transferAttribute(previous, "standalone");
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
        // Merge cdata-section-elements
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
        if (previous.hasAttribute("cdata-section-elements")) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
            // addAttribute works as a setter if it already exists
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
            addAttribute("cdata-section-elements",
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
                previous.getAttribute("cdata-section-elements") + ' ' +
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
                getAttribute("cdata-section-elements"));
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
        // Transfer non-standard attributes as well
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
        String prefix = lookupPrefix("http://xml.apache.org/xalan");
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
        if (prefix != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
            transferAttribute(previous, prefix + ':' + "indent-amount");
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
        prefix = lookupPrefix("http://xml.apache.org/xslt");
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
        if (prefix != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
            transferAttribute(previous, prefix + ':' + "indent-amount");
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
     * Scans the attribute list for the xsl:output instruction
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
    public void parseContents(Parser parser) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
        final Properties outputProperties = new Properties();
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
        // Ask the parser if it wants this <xsl:output> element
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
        parser.setOutput(this);
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
        // Do nothing if other <xsl:output> element has higher precedence
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
        if (_disabled) return;
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
        String attrib = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
        // Get the output version
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
        _version = getAttribute("version");
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
        if (_version.equals(Constants.EMPTYSTRING)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
            _version = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   160
        else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   161
            outputProperties.setProperty(OutputKeys.VERSION, _version);
7f561c08de6b Initial load
duke
parents:
diff changeset
   162
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   163
7f561c08de6b Initial load
duke
parents:
diff changeset
   164
        // Get the output method - "xml", "html", "text" or <qname> (but not ncname)
7f561c08de6b Initial load
duke
parents:
diff changeset
   165
        _method = getAttribute("method");
7f561c08de6b Initial load
duke
parents:
diff changeset
   166
        if (_method.equals(Constants.EMPTYSTRING)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   167
            _method = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   168
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   169
        if (_method != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   170
            _method = _method.toLowerCase();
7f561c08de6b Initial load
duke
parents:
diff changeset
   171
            if ((_method.equals("xml"))||
7f561c08de6b Initial load
duke
parents:
diff changeset
   172
                (_method.equals("html"))||
7f561c08de6b Initial load
duke
parents:
diff changeset
   173
                (_method.equals("text"))||
7f561c08de6b Initial load
duke
parents:
diff changeset
   174
                ((XML11Char.isXML11ValidQName(_method)&&(_method.indexOf(":") > 0)))) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   175
               outputProperties.setProperty(OutputKeys.METHOD, _method);
7f561c08de6b Initial load
duke
parents:
diff changeset
   176
            } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   177
                reportError(this, parser, ErrorMsg.INVALID_METHOD_IN_OUTPUT, _method);
7f561c08de6b Initial load
duke
parents:
diff changeset
   178
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   179
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   180
7f561c08de6b Initial load
duke
parents:
diff changeset
   181
        // Get the output encoding - any value accepted here
7f561c08de6b Initial load
duke
parents:
diff changeset
   182
        _encoding = getAttribute("encoding");
7f561c08de6b Initial load
duke
parents:
diff changeset
   183
        if (_encoding.equals(Constants.EMPTYSTRING)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   184
            _encoding = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   185
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   186
        else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   187
            try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   188
                // Create a write to verify encoding support
7f561c08de6b Initial load
duke
parents:
diff changeset
   189
                String canonicalEncoding;
7f561c08de6b Initial load
duke
parents:
diff changeset
   190
                canonicalEncoding = Encodings.convertMime2JavaEncoding(_encoding);
7f561c08de6b Initial load
duke
parents:
diff changeset
   191
                OutputStreamWriter writer =
7f561c08de6b Initial load
duke
parents:
diff changeset
   192
                    new OutputStreamWriter(System.out, canonicalEncoding);
7f561c08de6b Initial load
duke
parents:
diff changeset
   193
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   194
            catch (java.io.UnsupportedEncodingException e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   195
                ErrorMsg msg = new ErrorMsg(ErrorMsg.UNSUPPORTED_ENCODING,
7f561c08de6b Initial load
duke
parents:
diff changeset
   196
                                            _encoding, this);
7f561c08de6b Initial load
duke
parents:
diff changeset
   197
                parser.reportError(Constants.WARNING, msg);
7f561c08de6b Initial load
duke
parents:
diff changeset
   198
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   199
            outputProperties.setProperty(OutputKeys.ENCODING, _encoding);
7f561c08de6b Initial load
duke
parents:
diff changeset
   200
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   201
7f561c08de6b Initial load
duke
parents:
diff changeset
   202
        // Should the XML header be omitted - translate to true/false
7f561c08de6b Initial load
duke
parents:
diff changeset
   203
        attrib = getAttribute("omit-xml-declaration");
7f561c08de6b Initial load
duke
parents:
diff changeset
   204
        if (!attrib.equals(Constants.EMPTYSTRING)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   205
            if (attrib.equals("yes")) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   206
                _omitHeader = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   207
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   208
            outputProperties.setProperty(OutputKeys.OMIT_XML_DECLARATION, attrib);
7f561c08de6b Initial load
duke
parents:
diff changeset
   209
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   210
7f561c08de6b Initial load
duke
parents:
diff changeset
   211
        // Add 'standalone' decaration to output - use text as is
7f561c08de6b Initial load
duke
parents:
diff changeset
   212
        _standalone = getAttribute("standalone");
7f561c08de6b Initial load
duke
parents:
diff changeset
   213
        if (_standalone.equals(Constants.EMPTYSTRING)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   214
            _standalone = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   215
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   216
        else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   217
            outputProperties.setProperty(OutputKeys.STANDALONE, _standalone);
7f561c08de6b Initial load
duke
parents:
diff changeset
   218
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   219
7f561c08de6b Initial load
duke
parents:
diff changeset
   220
        // Get system/public identifiers for output DOCTYPE declaration
7f561c08de6b Initial load
duke
parents:
diff changeset
   221
        _doctypeSystem = getAttribute("doctype-system");
7f561c08de6b Initial load
duke
parents:
diff changeset
   222
        if (_doctypeSystem.equals(Constants.EMPTYSTRING)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   223
            _doctypeSystem = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   224
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   225
        else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   226
            outputProperties.setProperty(OutputKeys.DOCTYPE_SYSTEM, _doctypeSystem);
7f561c08de6b Initial load
duke
parents:
diff changeset
   227
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   228
7f561c08de6b Initial load
duke
parents:
diff changeset
   229
7f561c08de6b Initial load
duke
parents:
diff changeset
   230
        _doctypePublic = getAttribute("doctype-public");
7f561c08de6b Initial load
duke
parents:
diff changeset
   231
        if (_doctypePublic.equals(Constants.EMPTYSTRING)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   232
            _doctypePublic = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   233
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   234
        else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   235
            outputProperties.setProperty(OutputKeys.DOCTYPE_PUBLIC, _doctypePublic);
7f561c08de6b Initial load
duke
parents:
diff changeset
   236
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   237
7f561c08de6b Initial load
duke
parents:
diff changeset
   238
        // Names the elements of whose text contents should be output as CDATA
7f561c08de6b Initial load
duke
parents:
diff changeset
   239
        _cdata = getAttribute("cdata-section-elements");
7f561c08de6b Initial load
duke
parents:
diff changeset
   240
        if (_cdata.equals(Constants.EMPTYSTRING)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   241
            _cdata = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   242
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   243
        else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   244
            StringBuffer expandedNames = new StringBuffer();
7f561c08de6b Initial load
duke
parents:
diff changeset
   245
            StringTokenizer tokens = new StringTokenizer(_cdata);
7f561c08de6b Initial load
duke
parents:
diff changeset
   246
7f561c08de6b Initial load
duke
parents:
diff changeset
   247
            // Make sure to store names in expanded form
7f561c08de6b Initial load
duke
parents:
diff changeset
   248
            while (tokens.hasMoreTokens()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   249
                String qname = tokens.nextToken();
7f561c08de6b Initial load
duke
parents:
diff changeset
   250
                if (!XML11Char.isXML11ValidQName(qname)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   251
                    ErrorMsg err = new ErrorMsg(ErrorMsg.INVALID_QNAME_ERR, qname, this);
7f561c08de6b Initial load
duke
parents:
diff changeset
   252
                    parser.reportError(Constants.ERROR, err);
7f561c08de6b Initial load
duke
parents:
diff changeset
   253
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   254
                expandedNames.append(
7f561c08de6b Initial load
duke
parents:
diff changeset
   255
                   parser.getQName(qname).toString()).append(' ');
7f561c08de6b Initial load
duke
parents:
diff changeset
   256
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   257
            _cdata = expandedNames.toString();
7f561c08de6b Initial load
duke
parents:
diff changeset
   258
            outputProperties.setProperty(OutputKeys.CDATA_SECTION_ELEMENTS,
7f561c08de6b Initial load
duke
parents:
diff changeset
   259
                _cdata);
7f561c08de6b Initial load
duke
parents:
diff changeset
   260
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   261
7f561c08de6b Initial load
duke
parents:
diff changeset
   262
        // Get the indent setting - only has effect for xml and html output
7f561c08de6b Initial load
duke
parents:
diff changeset
   263
        attrib = getAttribute("indent");
7f561c08de6b Initial load
duke
parents:
diff changeset
   264
        if (!attrib.equals(EMPTYSTRING)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   265
            if (attrib.equals("yes")) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   266
                _indent = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   267
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   268
            outputProperties.setProperty(OutputKeys.INDENT, attrib);
7f561c08de6b Initial load
duke
parents:
diff changeset
   269
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   270
        else if (_method != null && _method.equals("html")) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   271
            _indent = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   272
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   273
7f561c08de6b Initial load
duke
parents:
diff changeset
   274
        // indent-amount: extension attribute of xsl:output
7f561c08de6b Initial load
duke
parents:
diff changeset
   275
        _indentamount = getAttribute(
7f561c08de6b Initial load
duke
parents:
diff changeset
   276
            lookupPrefix("http://xml.apache.org/xalan"), "indent-amount");
7f561c08de6b Initial load
duke
parents:
diff changeset
   277
        //  Hack for supporting Old Namespace URI.
7f561c08de6b Initial load
duke
parents:
diff changeset
   278
        if (_indentamount.equals(EMPTYSTRING)){
7f561c08de6b Initial load
duke
parents:
diff changeset
   279
            _indentamount = getAttribute(
7f561c08de6b Initial load
duke
parents:
diff changeset
   280
                lookupPrefix("http://xml.apache.org/xslt"), "indent-amount");
7f561c08de6b Initial load
duke
parents:
diff changeset
   281
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   282
        if (!_indentamount.equals(EMPTYSTRING)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   283
            outputProperties.setProperty("indent_amount", _indentamount);
7f561c08de6b Initial load
duke
parents:
diff changeset
   284
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   285
7f561c08de6b Initial load
duke
parents:
diff changeset
   286
        // Get the MIME type for the output file
7f561c08de6b Initial load
duke
parents:
diff changeset
   287
        _mediaType = getAttribute("media-type");
7f561c08de6b Initial load
duke
parents:
diff changeset
   288
        if (_mediaType.equals(Constants.EMPTYSTRING)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   289
            _mediaType = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   290
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   291
        else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   292
            outputProperties.setProperty(OutputKeys.MEDIA_TYPE, _mediaType);
7f561c08de6b Initial load
duke
parents:
diff changeset
   293
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   294
7f561c08de6b Initial load
duke
parents:
diff changeset
   295
        // Implied properties
7f561c08de6b Initial load
duke
parents:
diff changeset
   296
        if (_method != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   297
            if (_method.equals("html")) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   298
                if (_version == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   299
                    _version = HTML_VERSION;
7f561c08de6b Initial load
duke
parents:
diff changeset
   300
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   301
                if (_mediaType == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   302
                    _mediaType = "text/html";
7f561c08de6b Initial load
duke
parents:
diff changeset
   303
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   304
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   305
            else if (_method.equals("text")) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   306
                if (_mediaType == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   307
                    _mediaType = "text/plain";
7f561c08de6b Initial load
duke
parents:
diff changeset
   308
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   309
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   310
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   311
7f561c08de6b Initial load
duke
parents:
diff changeset
   312
        // Set output properties in current stylesheet
7f561c08de6b Initial load
duke
parents:
diff changeset
   313
        parser.getCurrentStylesheet().setOutputProperties(outputProperties);
7f561c08de6b Initial load
duke
parents:
diff changeset
   314
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   315
7f561c08de6b Initial load
duke
parents:
diff changeset
   316
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   317
     * Compile code that passes the information in this <xsl:output> element
7f561c08de6b Initial load
duke
parents:
diff changeset
   318
     * to the appropriate fields in the translet
7f561c08de6b Initial load
duke
parents:
diff changeset
   319
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   320
    public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   321
7f561c08de6b Initial load
duke
parents:
diff changeset
   322
        // Do nothing if other <xsl:output> element has higher precedence
7f561c08de6b Initial load
duke
parents:
diff changeset
   323
        if (_disabled) return;
7f561c08de6b Initial load
duke
parents:
diff changeset
   324
7f561c08de6b Initial load
duke
parents:
diff changeset
   325
        ConstantPoolGen cpg = classGen.getConstantPool();
7f561c08de6b Initial load
duke
parents:
diff changeset
   326
        InstructionList il = methodGen.getInstructionList();
7f561c08de6b Initial load
duke
parents:
diff changeset
   327
7f561c08de6b Initial load
duke
parents:
diff changeset
   328
        int field = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
   329
        il.append(classGen.loadTranslet());
7f561c08de6b Initial load
duke
parents:
diff changeset
   330
7f561c08de6b Initial load
duke
parents:
diff changeset
   331
        // Only update _version field if set and different from default
7f561c08de6b Initial load
duke
parents:
diff changeset
   332
        if ((_version != null) && (!_version.equals(XML_VERSION))) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   333
            field = cpg.addFieldref(TRANSLET_CLASS, "_version", STRING_SIG);
7f561c08de6b Initial load
duke
parents:
diff changeset
   334
            il.append(DUP);
7f561c08de6b Initial load
duke
parents:
diff changeset
   335
            il.append(new PUSH(cpg, _version));
7f561c08de6b Initial load
duke
parents:
diff changeset
   336
            il.append(new PUTFIELD(field));
7f561c08de6b Initial load
duke
parents:
diff changeset
   337
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   338
7f561c08de6b Initial load
duke
parents:
diff changeset
   339
        // Only update _method field if "method" attribute used
7f561c08de6b Initial load
duke
parents:
diff changeset
   340
        if (_method != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   341
            field = cpg.addFieldref(TRANSLET_CLASS, "_method", STRING_SIG);
7f561c08de6b Initial load
duke
parents:
diff changeset
   342
            il.append(DUP);
7f561c08de6b Initial load
duke
parents:
diff changeset
   343
            il.append(new PUSH(cpg, _method));
7f561c08de6b Initial load
duke
parents:
diff changeset
   344
            il.append(new PUTFIELD(field));
7f561c08de6b Initial load
duke
parents:
diff changeset
   345
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   346
7f561c08de6b Initial load
duke
parents:
diff changeset
   347
        // Only update if _encoding field is "encoding" attribute used
7f561c08de6b Initial load
duke
parents:
diff changeset
   348
        if (_encoding != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   349
            field = cpg.addFieldref(TRANSLET_CLASS, "_encoding", STRING_SIG);
7f561c08de6b Initial load
duke
parents:
diff changeset
   350
            il.append(DUP);
7f561c08de6b Initial load
duke
parents:
diff changeset
   351
            il.append(new PUSH(cpg, _encoding));
7f561c08de6b Initial load
duke
parents:
diff changeset
   352
            il.append(new PUTFIELD(field));
7f561c08de6b Initial load
duke
parents:
diff changeset
   353
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   354
7f561c08de6b Initial load
duke
parents:
diff changeset
   355
        // Only update if "omit-xml-declaration" used and set to 'yes'
7f561c08de6b Initial load
duke
parents:
diff changeset
   356
        if (_omitHeader) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   357
            field = cpg.addFieldref(TRANSLET_CLASS, "_omitHeader", "Z");
7f561c08de6b Initial load
duke
parents:
diff changeset
   358
            il.append(DUP);
7f561c08de6b Initial load
duke
parents:
diff changeset
   359
            il.append(new PUSH(cpg, _omitHeader));
7f561c08de6b Initial load
duke
parents:
diff changeset
   360
            il.append(new PUTFIELD(field));
7f561c08de6b Initial load
duke
parents:
diff changeset
   361
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   362
7f561c08de6b Initial load
duke
parents:
diff changeset
   363
        // Add 'standalone' decaration to output - use text as is
7f561c08de6b Initial load
duke
parents:
diff changeset
   364
        if (_standalone != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   365
            field = cpg.addFieldref(TRANSLET_CLASS, "_standalone", STRING_SIG);
7f561c08de6b Initial load
duke
parents:
diff changeset
   366
            il.append(DUP);
7f561c08de6b Initial load
duke
parents:
diff changeset
   367
            il.append(new PUSH(cpg, _standalone));
7f561c08de6b Initial load
duke
parents:
diff changeset
   368
            il.append(new PUTFIELD(field));
7f561c08de6b Initial load
duke
parents:
diff changeset
   369
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   370
7f561c08de6b Initial load
duke
parents:
diff changeset
   371
        // Set system/public doctype only if both are set
7f561c08de6b Initial load
duke
parents:
diff changeset
   372
        field = cpg.addFieldref(TRANSLET_CLASS,"_doctypeSystem",STRING_SIG);
7f561c08de6b Initial load
duke
parents:
diff changeset
   373
        il.append(DUP);
7f561c08de6b Initial load
duke
parents:
diff changeset
   374
        il.append(new PUSH(cpg, _doctypeSystem));
7f561c08de6b Initial load
duke
parents:
diff changeset
   375
        il.append(new PUTFIELD(field));
7f561c08de6b Initial load
duke
parents:
diff changeset
   376
        field = cpg.addFieldref(TRANSLET_CLASS,"_doctypePublic",STRING_SIG);
7f561c08de6b Initial load
duke
parents:
diff changeset
   377
        il.append(DUP);
7f561c08de6b Initial load
duke
parents:
diff changeset
   378
        il.append(new PUSH(cpg, _doctypePublic));
7f561c08de6b Initial load
duke
parents:
diff changeset
   379
        il.append(new PUTFIELD(field));
7f561c08de6b Initial load
duke
parents:
diff changeset
   380
7f561c08de6b Initial load
duke
parents:
diff changeset
   381
        // Add 'medye-type' decaration to output - if used
7f561c08de6b Initial load
duke
parents:
diff changeset
   382
        if (_mediaType != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   383
            field = cpg.addFieldref(TRANSLET_CLASS, "_mediaType", STRING_SIG);
7f561c08de6b Initial load
duke
parents:
diff changeset
   384
            il.append(DUP);
7f561c08de6b Initial load
duke
parents:
diff changeset
   385
            il.append(new PUSH(cpg, _mediaType));
7f561c08de6b Initial load
duke
parents:
diff changeset
   386
            il.append(new PUTFIELD(field));
7f561c08de6b Initial load
duke
parents:
diff changeset
   387
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   388
7f561c08de6b Initial load
duke
parents:
diff changeset
   389
        // Compile code to set output indentation on/off
7f561c08de6b Initial load
duke
parents:
diff changeset
   390
        if (_indent) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   391
            field = cpg.addFieldref(TRANSLET_CLASS, "_indent", "Z");
7f561c08de6b Initial load
duke
parents:
diff changeset
   392
            il.append(DUP);
7f561c08de6b Initial load
duke
parents:
diff changeset
   393
            il.append(new PUSH(cpg, _indent));
7f561c08de6b Initial load
duke
parents:
diff changeset
   394
            il.append(new PUTFIELD(field));
7f561c08de6b Initial load
duke
parents:
diff changeset
   395
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   396
7f561c08de6b Initial load
duke
parents:
diff changeset
   397
        //Compile code to set indent amount.
7f561c08de6b Initial load
duke
parents:
diff changeset
   398
        if(_indentamount != null && !_indentamount.equals(EMPTYSTRING)){
7f561c08de6b Initial load
duke
parents:
diff changeset
   399
            field = cpg.addFieldref(TRANSLET_CLASS, "_indentamount", "I");
7f561c08de6b Initial load
duke
parents:
diff changeset
   400
            il.append(DUP);
7f561c08de6b Initial load
duke
parents:
diff changeset
   401
            il.append(new PUSH(cpg, Integer.parseInt(_indentamount)));
7f561c08de6b Initial load
duke
parents:
diff changeset
   402
            il.append(new PUTFIELD(field));
7f561c08de6b Initial load
duke
parents:
diff changeset
   403
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   404
7f561c08de6b Initial load
duke
parents:
diff changeset
   405
        // Forward to the translet any elements that should be output as CDATA
7f561c08de6b Initial load
duke
parents:
diff changeset
   406
        if (_cdata != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   407
            int index = cpg.addMethodref(TRANSLET_CLASS,
7f561c08de6b Initial load
duke
parents:
diff changeset
   408
                                         "addCdataElement",
7f561c08de6b Initial load
duke
parents:
diff changeset
   409
                                         "(Ljava/lang/String;)V");
7f561c08de6b Initial load
duke
parents:
diff changeset
   410
7f561c08de6b Initial load
duke
parents:
diff changeset
   411
            StringTokenizer tokens = new StringTokenizer(_cdata);
7f561c08de6b Initial load
duke
parents:
diff changeset
   412
            while (tokens.hasMoreTokens()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   413
                il.append(DUP);
7f561c08de6b Initial load
duke
parents:
diff changeset
   414
                il.append(new PUSH(cpg, tokens.nextToken()));
7f561c08de6b Initial load
duke
parents:
diff changeset
   415
                il.append(new INVOKEVIRTUAL(index));
7f561c08de6b Initial load
duke
parents:
diff changeset
   416
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   417
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   418
        il.append(POP); // Cleanup - pop last translet reference off stack
7f561c08de6b Initial load
duke
parents:
diff changeset
   419
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   420
7f561c08de6b Initial load
duke
parents:
diff changeset
   421
}