001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements. See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership. The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the  "License");
007     * you may not use this file except in compliance with the License.
008     * You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    /*
019     * $Id: ProcessorOutputElem.java 468640 2006-10-28 06:53:53Z minchau $
020     */
021    package org.apache.xalan.processor;
022    
023    import javax.xml.transform.OutputKeys;
024    import javax.xml.transform.TransformerException;
025    
026    import org.apache.xalan.templates.ElemTemplateElement;
027    import org.apache.xalan.templates.OutputProperties;
028    import org.apache.xml.serializer.OutputPropertiesFactory;
029    import org.apache.xml.utils.QName;
030    import org.apache.xml.utils.SystemIDResolver;
031    import org.xml.sax.Attributes;
032    
033    /**
034     * TransformerFactory for xsl:output markup.
035     * @see <a href="http://www.w3.org/TR/xslt#dtd">XSLT DTD</a>
036     * @see <a href="http://www.w3.org/TR/xslt#output">output in XSLT Specification</a>
037     */
038    class ProcessorOutputElem extends XSLTElementProcessor
039    {
040        static final long serialVersionUID = 3513742319582547590L;
041    
042      /** The output properties, set temporarily while the properties are 
043       *  being set from the attributes, and then nulled after that operation 
044       *  is completed.  */
045      private OutputProperties m_outputProperties;
046    
047      /**
048       * Set the cdata-section-elements property from the attribute value.
049       * @see javax.xml.transform.OutputKeys#CDATA_SECTION_ELEMENTS
050       * @param newValue non-null reference to processed attribute value.
051       */
052      public void setCdataSectionElements(java.util.Vector newValue)
053      {
054        m_outputProperties.setQNameProperties(OutputKeys.CDATA_SECTION_ELEMENTS, newValue);
055      }
056    
057      /**
058       * Set the doctype-public property from the attribute value.
059       * @see javax.xml.transform.OutputKeys#DOCTYPE_PUBLIC
060       * @param newValue non-null reference to processed attribute value.
061       */
062      public void setDoctypePublic(String newValue)
063      {
064        m_outputProperties.setProperty(OutputKeys.DOCTYPE_PUBLIC, newValue);
065      }
066    
067      /**
068       * Set the doctype-system property from the attribute value.
069       * @see javax.xml.transform.OutputKeys#DOCTYPE_SYSTEM
070       * @param newValue non-null reference to processed attribute value.
071       */
072      public void setDoctypeSystem(String newValue)
073      {
074        m_outputProperties.setProperty(OutputKeys.DOCTYPE_SYSTEM, newValue);
075      }
076    
077      /**
078       * Set the encoding property from the attribute value.
079       * @see javax.xml.transform.OutputKeys#ENCODING
080       * @param newValue non-null reference to processed attribute value.
081       */
082      public void setEncoding(String newValue)
083      {
084        m_outputProperties.setProperty(OutputKeys.ENCODING, newValue);
085      }
086    
087      /**
088       * Set the indent property from the attribute value.
089       * @see javax.xml.transform.OutputKeys#INDENT
090       * @param newValue non-null reference to processed attribute value.
091       */
092      public void setIndent(boolean newValue)
093      {
094        m_outputProperties.setBooleanProperty(OutputKeys.INDENT, newValue);
095      }
096    
097      /**
098       * Set the media type property from the attribute value.
099       * @see javax.xml.transform.OutputKeys#MEDIA_TYPE
100       * @param newValue non-null reference to processed attribute value.
101       */
102      public void setMediaType(String newValue)
103      {
104        m_outputProperties.setProperty(OutputKeys.MEDIA_TYPE, newValue);
105      }
106    
107      /**
108       * Set the method property from the attribute value.
109       * @see javax.xml.transform.OutputKeys#METHOD
110       * @param newValue non-null reference to processed attribute value.
111       */
112      public void setMethod(org.apache.xml.utils.QName newValue)
113      {
114        m_outputProperties.setQNameProperty(OutputKeys.METHOD, newValue);
115      }
116    
117      /**
118       * Set the omit-xml-declaration property from the attribute value.
119       * @see javax.xml.transform.OutputKeys#OMIT_XML_DECLARATION
120       * @param newValue processed attribute value.
121       */
122      public void setOmitXmlDeclaration(boolean newValue)
123      {
124        m_outputProperties.setBooleanProperty(OutputKeys.OMIT_XML_DECLARATION, newValue);
125      }
126    
127      /**
128       * Set the standalone property from the attribute value.
129       * @see javax.xml.transform.OutputKeys#STANDALONE
130       * @param newValue processed attribute value.
131       */
132      public void setStandalone(boolean newValue)
133      {
134        m_outputProperties.setBooleanProperty(OutputKeys.STANDALONE, newValue);
135      }
136    
137      /**
138       * Set the version property from the attribute value.
139       * @see javax.xml.transform.OutputKeys#VERSION
140       * @param newValue non-null reference to processed attribute value.
141       */
142      public void setVersion(String newValue)
143      {
144        m_outputProperties.setProperty(OutputKeys.VERSION, newValue);
145      }
146      
147      /**
148       * Set a foreign property from the attribute value.
149       * @param newValue non-null reference to attribute value.
150       */
151      public void setForeignAttr(String attrUri, String attrLocalName, String attrRawName, String attrValue)
152      {
153        QName key = new QName(attrUri, attrLocalName);
154        m_outputProperties.setProperty(key, attrValue);
155      }
156      
157      /**
158       * Set a foreign property from the attribute value.
159       * @param newValue non-null reference to attribute value.
160       */
161      public void addLiteralResultAttribute(String attrUri, String attrLocalName, String attrRawName, String attrValue)
162      {
163        QName key = new QName(attrUri, attrLocalName);
164        m_outputProperties.setProperty(key, attrValue);
165      }
166    
167      /**
168       * Receive notification of the start of an xsl:output element.
169       *
170       * @param handler The calling StylesheetHandler/TemplatesBuilder.
171       * @param uri The Namespace URI, or the empty string if the
172       *        element has no Namespace URI or if Namespace
173       *        processing is not being performed.
174       * @param localName The local name (without prefix), or the
175       *        empty string if Namespace processing is not being
176       *        performed.
177       * @param rawName The raw XML 1.0 name (with prefix), or the
178       *        empty string if raw names are not available.
179       * @param attributes The attributes attached to the element.  If
180       *        there are no attributes, it shall be an empty
181       *        Attributes object.
182       *
183       * @throws org.xml.sax.SAXException
184       */
185      public void startElement(
186              StylesheetHandler handler, String uri, String localName, String rawName, Attributes attributes)
187                throws org.xml.sax.SAXException
188      {
189        // Hmmm... for the moment I don't think I'll have default properties set for this. -sb
190        m_outputProperties = new OutputProperties();
191    
192        m_outputProperties.setDOMBackPointer(handler.getOriginatingNode());
193        m_outputProperties.setLocaterInfo(handler.getLocator());
194        m_outputProperties.setUid(handler.nextUid());
195        setPropertiesFromAttributes(handler, rawName, attributes, this);
196        
197        // Access this only from the Hashtable level... we don't want to 
198        // get default properties.
199        String entitiesFileName =
200          (String) m_outputProperties.getProperties().get(OutputPropertiesFactory.S_KEY_ENTITIES);
201    
202        if (null != entitiesFileName)
203        {
204          try
205          {
206            String absURL = SystemIDResolver.getAbsoluteURI(entitiesFileName,
207                        handler.getBaseIdentifier());
208            m_outputProperties.getProperties().put(OutputPropertiesFactory.S_KEY_ENTITIES, absURL);
209          }
210          catch(TransformerException te)
211          {
212            handler.error(te.getMessage(), te);
213          }
214        }
215        
216        handler.getStylesheet().setOutput(m_outputProperties);
217        
218        ElemTemplateElement parent = handler.getElemTemplateElement();
219        parent.appendChild(m_outputProperties);
220        
221        m_outputProperties = null;
222      }
223    }