286N/A/*
286N/A * reserved comment block
286N/A * DO NOT REMOVE OR ALTER!
286N/A */
286N/A/*
286N/A * Copyright 2001-2004 The Apache Software Foundation.
286N/A *
286N/A * Licensed under the Apache License, Version 2.0 (the "License");
286N/A * you may not use this file except in compliance with the License.
286N/A * You may obtain a copy of the License at
286N/A *
286N/A * http://www.apache.org/licenses/LICENSE-2.0
286N/A *
286N/A * Unless required by applicable law or agreed to in writing, software
286N/A * distributed under the License is distributed on an "AS IS" BASIS,
286N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
286N/A * See the License for the specific language governing permissions and
286N/A * limitations under the License.
286N/A */
286N/A/*
286N/A * $Id: Output.java,v 1.2.4.1 2005/09/12 10:53:00 pvedula Exp $
286N/A */
286N/A
286N/Apackage com.sun.org.apache.xalan.internal.xsltc.compiler;
286N/A
286N/Aimport java.io.OutputStreamWriter;
286N/Aimport java.util.Properties;
286N/Aimport java.util.StringTokenizer;
286N/A
286N/Aimport javax.xml.transform.OutputKeys;
286N/A
286N/Aimport com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
286N/Aimport com.sun.org.apache.bcel.internal.generic.INVOKEVIRTUAL;
286N/Aimport com.sun.org.apache.bcel.internal.generic.InstructionList;
286N/Aimport com.sun.org.apache.bcel.internal.generic.PUSH;
286N/Aimport com.sun.org.apache.bcel.internal.generic.PUTFIELD;
286N/Aimport com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
286N/Aimport com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
286N/Aimport com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
286N/Aimport com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util;
286N/Aimport com.sun.org.apache.xml.internal.serializer.Encodings;
286N/Aimport com.sun.org.apache.xml.internal.utils.XML11Char;
286N/A
286N/A/**
286N/A * @author Jacek Ambroziak
286N/A * @author Santiago Pericas-Geertsen
286N/A * @author Morten Jorgensen
286N/A */
286N/Afinal class Output extends TopLevelElement {
286N/A
286N/A // TODO: use three-value variables for boolean values: true/false/default
286N/A
286N/A // These attributes are extracted from the xsl:output element. They also
286N/A // appear as fields (with the same type, only public) in the translet
286N/A private String _version;
286N/A private String _method;
286N/A private String _encoding;
286N/A private boolean _omitHeader = false;
286N/A private String _standalone;
286N/A private String _doctypePublic;
286N/A private String _doctypeSystem;
286N/A private String _cdata;
286N/A private boolean _indent = false;
286N/A private String _mediaType;
286N/A private String _indentamount;
286N/A
286N/A // Disables this output element (when other element has higher precedence)
286N/A private boolean _disabled = false;
286N/A
286N/A // Some global constants
286N/A private final static String STRING_SIG = "Ljava/lang/String;";
286N/A private final static String XML_VERSION = "1.0";
286N/A private final static String HTML_VERSION = "4.0";
286N/A
286N/A /**
286N/A * Displays the contents of this element (for debugging)
286N/A */
286N/A public void display(int indent) {
286N/A indent(indent);
286N/A Util.println("Output " + _method);
286N/A }
286N/A
286N/A /**
286N/A * Disables this <xsl:output> element in case where there are some other
286N/A * <xsl:output> element (from a different imported/included stylesheet)
286N/A * with higher precedence.
286N/A */
286N/A public void disable() {
286N/A _disabled = true;
286N/A }
286N/A
286N/A public boolean enabled() {
286N/A return !_disabled;
286N/A }
286N/A
286N/A public String getCdata() {
286N/A return _cdata;
286N/A }
286N/A
286N/A public String getOutputMethod() {
286N/A return _method;
286N/A }
286N/A
286N/A private void transferAttribute(Output previous, String qname) {
286N/A if (!hasAttribute(qname) && previous.hasAttribute(qname)) {
286N/A addAttribute(qname, previous.getAttribute(qname));
286N/A }
286N/A }
286N/A
286N/A public void mergeOutput(Output previous) {
286N/A // Transfer attributes from previous xsl:output
286N/A transferAttribute(previous, "version");
286N/A transferAttribute(previous, "method");
286N/A transferAttribute(previous, "encoding");
286N/A transferAttribute(previous, "doctype-system");
286N/A transferAttribute(previous, "doctype-public");
286N/A transferAttribute(previous, "media-type");
286N/A transferAttribute(previous, "indent");
286N/A transferAttribute(previous, "omit-xml-declaration");
286N/A transferAttribute(previous, "standalone");
286N/A
286N/A // Merge cdata-section-elements
286N/A if (previous.hasAttribute("cdata-section-elements")) {
286N/A // addAttribute works as a setter if it already exists
286N/A addAttribute("cdata-section-elements",
286N/A previous.getAttribute("cdata-section-elements") + ' ' +
286N/A getAttribute("cdata-section-elements"));
286N/A }
286N/A
286N/A // Transfer non-standard attributes as well
286N/A String prefix = lookupPrefix("http://xml.apache.org/xalan");
286N/A if (prefix != null) {
286N/A transferAttribute(previous, prefix + ':' + "indent-amount");
286N/A }
286N/A prefix = lookupPrefix("http://xml.apache.org/xslt");
286N/A if (prefix != null) {
286N/A transferAttribute(previous, prefix + ':' + "indent-amount");
286N/A }
286N/A }
286N/A
286N/A /**
286N/A * Scans the attribute list for the xsl:output instruction
286N/A */
286N/A public void parseContents(Parser parser) {
286N/A final Properties outputProperties = new Properties();
286N/A
286N/A // Ask the parser if it wants this <xsl:output> element
286N/A parser.setOutput(this);
286N/A
286N/A // Do nothing if other <xsl:output> element has higher precedence
286N/A if (_disabled) return;
286N/A
286N/A String attrib = null;
286N/A
286N/A // Get the output version
286N/A _version = getAttribute("version");
286N/A if (_version.equals(Constants.EMPTYSTRING)) {
286N/A _version = null;
286N/A }
286N/A else {
286N/A outputProperties.setProperty(OutputKeys.VERSION, _version);
286N/A }
286N/A
286N/A // Get the output method - "xml", "html", "text" or <qname> (but not ncname)
286N/A _method = getAttribute("method");
286N/A if (_method.equals(Constants.EMPTYSTRING)) {
286N/A _method = null;
286N/A }
286N/A if (_method != null) {
286N/A _method = _method.toLowerCase();
286N/A if ((_method.equals("xml"))||
286N/A (_method.equals("html"))||
286N/A (_method.equals("text"))||
286N/A ((XML11Char.isXML11ValidQName(_method)&&(_method.indexOf(":") > 0)))) {
286N/A outputProperties.setProperty(OutputKeys.METHOD, _method);
286N/A } else {
286N/A reportError(this, parser, ErrorMsg.INVALID_METHOD_IN_OUTPUT, _method);
286N/A }
286N/A }
286N/A
286N/A // Get the output encoding - any value accepted here
286N/A _encoding = getAttribute("encoding");
286N/A if (_encoding.equals(Constants.EMPTYSTRING)) {
286N/A _encoding = null;
286N/A }
286N/A else {
286N/A try {
286N/A // Create a write to verify encoding support
286N/A String canonicalEncoding;
286N/A canonicalEncoding = Encodings.convertMime2JavaEncoding(_encoding);
286N/A OutputStreamWriter writer =
286N/A new OutputStreamWriter(System.out, canonicalEncoding);
286N/A }
286N/A catch (java.io.UnsupportedEncodingException e) {
286N/A ErrorMsg msg = new ErrorMsg(ErrorMsg.UNSUPPORTED_ENCODING,
286N/A _encoding, this);
286N/A parser.reportError(Constants.WARNING, msg);
286N/A }
286N/A outputProperties.setProperty(OutputKeys.ENCODING, _encoding);
286N/A }
286N/A
286N/A // Should the XML header be omitted - translate to true/false
286N/A attrib = getAttribute("omit-xml-declaration");
286N/A if (!attrib.equals(Constants.EMPTYSTRING)) {
286N/A if (attrib.equals("yes")) {
286N/A _omitHeader = true;
286N/A }
286N/A outputProperties.setProperty(OutputKeys.OMIT_XML_DECLARATION, attrib);
286N/A }
286N/A
286N/A // Add 'standalone' decaration to output - use text as is
286N/A _standalone = getAttribute("standalone");
286N/A if (_standalone.equals(Constants.EMPTYSTRING)) {
286N/A _standalone = null;
286N/A }
286N/A else {
286N/A outputProperties.setProperty(OutputKeys.STANDALONE, _standalone);
286N/A }
286N/A
286N/A // Get system/public identifiers for output DOCTYPE declaration
286N/A _doctypeSystem = getAttribute("doctype-system");
286N/A if (_doctypeSystem.equals(Constants.EMPTYSTRING)) {
286N/A _doctypeSystem = null;
286N/A }
286N/A else {
286N/A outputProperties.setProperty(OutputKeys.DOCTYPE_SYSTEM, _doctypeSystem);
286N/A }
286N/A
286N/A
286N/A _doctypePublic = getAttribute("doctype-public");
286N/A if (_doctypePublic.equals(Constants.EMPTYSTRING)) {
286N/A _doctypePublic = null;
286N/A }
286N/A else {
286N/A outputProperties.setProperty(OutputKeys.DOCTYPE_PUBLIC, _doctypePublic);
286N/A }
286N/A
286N/A // Names the elements of whose text contents should be output as CDATA
286N/A _cdata = getAttribute("cdata-section-elements");
286N/A if (_cdata.equals(Constants.EMPTYSTRING)) {
286N/A _cdata = null;
286N/A }
286N/A else {
286N/A StringBuffer expandedNames = new StringBuffer();
286N/A StringTokenizer tokens = new StringTokenizer(_cdata);
286N/A
286N/A // Make sure to store names in expanded form
286N/A while (tokens.hasMoreTokens()) {
286N/A String qname = tokens.nextToken();
286N/A if (!XML11Char.isXML11ValidQName(qname)) {
286N/A ErrorMsg err = new ErrorMsg(ErrorMsg.INVALID_QNAME_ERR, qname, this);
286N/A parser.reportError(Constants.ERROR, err);
286N/A }
286N/A expandedNames.append(
286N/A parser.getQName(qname).toString()).append(' ');
286N/A }
286N/A _cdata = expandedNames.toString();
286N/A outputProperties.setProperty(OutputKeys.CDATA_SECTION_ELEMENTS,
286N/A _cdata);
286N/A }
286N/A
286N/A // Get the indent setting - only has effect for xml and html output
286N/A attrib = getAttribute("indent");
286N/A if (!attrib.equals(EMPTYSTRING)) {
286N/A if (attrib.equals("yes")) {
286N/A _indent = true;
286N/A }
286N/A outputProperties.setProperty(OutputKeys.INDENT, attrib);
286N/A }
286N/A else if (_method != null && _method.equals("html")) {
286N/A _indent = true;
286N/A }
286N/A
286N/A // indent-amount: extension attribute of xsl:output
286N/A _indentamount = getAttribute(
286N/A lookupPrefix("http://xml.apache.org/xalan"), "indent-amount");
286N/A // Hack for supporting Old Namespace URI.
286N/A if (_indentamount.equals(EMPTYSTRING)){
286N/A _indentamount = getAttribute(
286N/A lookupPrefix("http://xml.apache.org/xslt"), "indent-amount");
286N/A }
286N/A if (!_indentamount.equals(EMPTYSTRING)) {
286N/A outputProperties.setProperty("indent_amount", _indentamount);
286N/A }
286N/A
286N/A // Get the MIME type for the output file
286N/A _mediaType = getAttribute("media-type");
286N/A if (_mediaType.equals(Constants.EMPTYSTRING)) {
286N/A _mediaType = null;
286N/A }
286N/A else {
286N/A outputProperties.setProperty(OutputKeys.MEDIA_TYPE, _mediaType);
286N/A }
286N/A
286N/A // Implied properties
286N/A if (_method != null) {
286N/A if (_method.equals("html")) {
286N/A if (_version == null) {
286N/A _version = HTML_VERSION;
286N/A }
286N/A if (_mediaType == null) {
286N/A _mediaType = "text/html";
286N/A }
286N/A }
286N/A else if (_method.equals("text")) {
286N/A if (_mediaType == null) {
286N/A _mediaType = "text/plain";
286N/A }
286N/A }
286N/A }
286N/A
286N/A // Set output properties in current stylesheet
286N/A parser.getCurrentStylesheet().setOutputProperties(outputProperties);
286N/A }
286N/A
286N/A /**
286N/A * Compile code that passes the information in this <xsl:output> element
286N/A * to the appropriate fields in the translet
286N/A */
286N/A public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
286N/A
286N/A // Do nothing if other <xsl:output> element has higher precedence
286N/A if (_disabled) return;
286N/A
286N/A ConstantPoolGen cpg = classGen.getConstantPool();
286N/A InstructionList il = methodGen.getInstructionList();
286N/A
286N/A int field = 0;
286N/A il.append(classGen.loadTranslet());
286N/A
286N/A // Only update _version field if set and different from default
286N/A if ((_version != null) && (!_version.equals(XML_VERSION))) {
286N/A field = cpg.addFieldref(TRANSLET_CLASS, "_version", STRING_SIG);
286N/A il.append(DUP);
286N/A il.append(new PUSH(cpg, _version));
286N/A il.append(new PUTFIELD(field));
286N/A }
286N/A
286N/A // Only update _method field if "method" attribute used
286N/A if (_method != null) {
286N/A field = cpg.addFieldref(TRANSLET_CLASS, "_method", STRING_SIG);
286N/A il.append(DUP);
286N/A il.append(new PUSH(cpg, _method));
286N/A il.append(new PUTFIELD(field));
286N/A }
286N/A
286N/A // Only update if _encoding field is "encoding" attribute used
286N/A if (_encoding != null) {
286N/A field = cpg.addFieldref(TRANSLET_CLASS, "_encoding", STRING_SIG);
286N/A il.append(DUP);
286N/A il.append(new PUSH(cpg, _encoding));
286N/A il.append(new PUTFIELD(field));
286N/A }
286N/A
286N/A // Only update if "omit-xml-declaration" used and set to 'yes'
286N/A if (_omitHeader) {
286N/A field = cpg.addFieldref(TRANSLET_CLASS, "_omitHeader", "Z");
286N/A il.append(DUP);
286N/A il.append(new PUSH(cpg, _omitHeader));
286N/A il.append(new PUTFIELD(field));
286N/A }
286N/A
286N/A // Add 'standalone' decaration to output - use text as is
286N/A if (_standalone != null) {
286N/A field = cpg.addFieldref(TRANSLET_CLASS, "_standalone", STRING_SIG);
286N/A il.append(DUP);
286N/A il.append(new PUSH(cpg, _standalone));
286N/A il.append(new PUTFIELD(field));
286N/A }
286N/A
286N/A // Set system/public doctype only if both are set
286N/A field = cpg.addFieldref(TRANSLET_CLASS,"_doctypeSystem",STRING_SIG);
286N/A il.append(DUP);
286N/A il.append(new PUSH(cpg, _doctypeSystem));
286N/A il.append(new PUTFIELD(field));
286N/A field = cpg.addFieldref(TRANSLET_CLASS,"_doctypePublic",STRING_SIG);
286N/A il.append(DUP);
286N/A il.append(new PUSH(cpg, _doctypePublic));
286N/A il.append(new PUTFIELD(field));
286N/A
286N/A // Add 'medye-type' decaration to output - if used
286N/A if (_mediaType != null) {
286N/A field = cpg.addFieldref(TRANSLET_CLASS, "_mediaType", STRING_SIG);
286N/A il.append(DUP);
286N/A il.append(new PUSH(cpg, _mediaType));
286N/A il.append(new PUTFIELD(field));
286N/A }
286N/A
286N/A // Compile code to set output indentation on/off
286N/A if (_indent) {
286N/A field = cpg.addFieldref(TRANSLET_CLASS, "_indent", "Z");
286N/A il.append(DUP);
286N/A il.append(new PUSH(cpg, _indent));
286N/A il.append(new PUTFIELD(field));
286N/A }
286N/A
286N/A //Compile code to set indent amount.
286N/A if(_indentamount != null && !_indentamount.equals(EMPTYSTRING)){
286N/A field = cpg.addFieldref(TRANSLET_CLASS, "_indentamount", "I");
286N/A il.append(DUP);
286N/A il.append(new PUSH(cpg, Integer.parseInt(_indentamount)));
286N/A il.append(new PUTFIELD(field));
286N/A }
286N/A
286N/A // Forward to the translet any elements that should be output as CDATA
286N/A if (_cdata != null) {
286N/A int index = cpg.addMethodref(TRANSLET_CLASS,
286N/A "addCdataElement",
286N/A "(Ljava/lang/String;)V");
286N/A
286N/A StringTokenizer tokens = new StringTokenizer(_cdata);
286N/A while (tokens.hasMoreTokens()) {
286N/A il.append(DUP);
286N/A il.append(new PUSH(cpg, tokens.nextToken()));
286N/A il.append(new INVOKEVIRTUAL(index));
286N/A }
286N/A }
286N/A il.append(POP); // Cleanup - pop last translet reference off stack
286N/A }
286N/A
286N/A}