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: ElemTextLiteral.java 468643 2006-10-28 06:56:03Z minchau $ 020 */ 021 package org.apache.xalan.templates; 022 023 import javax.xml.transform.TransformerException; 024 025 import org.apache.xalan.transformer.TransformerImpl; 026 import org.apache.xml.serializer.SerializationHandler; 027 import org.xml.sax.SAXException; 028 029 /** 030 * Implement a text literal. 031 * @see <a href="http://www.w3.org/TR/xslt#section-Creating-Text">section-Creating-Text in XSLT Specification</a> 032 * @xsl.usage advanced 033 */ 034 public class ElemTextLiteral extends ElemTemplateElement 035 { 036 static final long serialVersionUID = -7872620006767660088L; 037 038 /** 039 * Tell if space should be preserved. 040 * @serial 041 */ 042 private boolean m_preserveSpace; 043 044 /** 045 * Set whether or not space should be preserved. 046 * 047 * @param v Boolean flag indicating whether 048 * or not space should be preserved 049 */ 050 public void setPreserveSpace(boolean v) 051 { 052 m_preserveSpace = v; 053 } 054 055 /** 056 * Get whether or not space should be preserved. 057 * 058 * @return Boolean flag indicating whether 059 * or not space should be preserved 060 */ 061 public boolean getPreserveSpace() 062 { 063 return m_preserveSpace; 064 } 065 066 /** 067 * The character array. 068 * @serial 069 */ 070 private char m_ch[]; 071 072 /** 073 * The character array as a string. 074 * @serial 075 */ 076 private String m_str; 077 078 /** 079 * Set the characters that will be output to the result tree.. 080 * 081 * @param v Array of characters that will be output to the result tree 082 */ 083 public void setChars(char[] v) 084 { 085 m_ch = v; 086 } 087 088 /** 089 * Get the characters that will be output to the result tree.. 090 * 091 * @return Array of characters that will be output to the result tree 092 */ 093 public char[] getChars() 094 { 095 return m_ch; 096 } 097 098 /** 099 * Get the value of the node as a string. 100 * 101 * @return null 102 */ 103 public synchronized String getNodeValue() 104 { 105 106 if(null == m_str) 107 { 108 m_str = new String(m_ch); 109 } 110 111 return m_str; 112 } 113 114 115 /** 116 * Tells if this element should disable escaping. 117 * @serial 118 */ 119 private boolean m_disableOutputEscaping = false; 120 121 /** 122 * Set the "disable-output-escaping" attribute. 123 * Normally, the xml output method escapes & and < (and 124 * possibly other characters) when outputting text nodes. 125 * This ensures that the output is well-formed XML. However, 126 * it is sometimes convenient to be able to produce output 127 * that is almost, but not quite well-formed XML; for 128 * example, the output may include ill-formed sections 129 * which are intended to be transformed into well-formed 130 * XML by a subsequent non-XML aware process. For this reason, 131 * XSLT provides a mechanism for disabling output escaping. 132 * An xsl:value-of or xsl:text element may have a 133 * disable-output-escaping attribute; the allowed values 134 * are yes or no; the default is no; if the value is yes, 135 * then a text node generated by instantiating the xsl:value-of 136 * or xsl:text element should be output without any escaping. 137 * @see <a href="http://www.w3.org/TR/xslt#disable-output-escaping">disable-output-escaping in XSLT Specification</a> 138 * 139 * @param v Boolean value for "disable-output-escaping" attribute. 140 */ 141 public void setDisableOutputEscaping(boolean v) 142 { 143 m_disableOutputEscaping = v; 144 } 145 146 /** 147 * Get the "disable-output-escaping" attribute. 148 * Normally, the xml output method escapes & and < (and 149 * possibly other characters) when outputting text nodes. 150 * This ensures that the output is well-formed XML. However, 151 * it is sometimes convenient to be able to produce output 152 * that is almost, but not quite well-formed XML; for 153 * example, the output may include ill-formed sections 154 * which are intended to be transformed into well-formed 155 * XML by a subsequent non-XML aware process. For this reason, 156 * XSLT provides a mechanism for disabling output escaping. 157 * An xsl:value-of or xsl:text element may have a 158 * disable-output-escaping attribute; the allowed values 159 * are yes or no; the default is no; if the value is yes, 160 * then a text node generated by instantiating the xsl:value-of 161 * or xsl:text element should be output without any escaping. 162 * @see <a href="http://www.w3.org/TR/xslt#disable-output-escaping">disable-output-escaping in XSLT Specification</a> 163 * 164 * @return Boolean value of "disable-output-escaping" attribute. 165 */ 166 public boolean getDisableOutputEscaping() 167 { 168 return m_disableOutputEscaping; 169 } 170 171 /** 172 * Get an integer representation of the element type. 173 * 174 * @return An integer representation of the element, defined in the 175 * Constants class. 176 * @see org.apache.xalan.templates.Constants 177 */ 178 public int getXSLToken() 179 { 180 return Constants.ELEMNAME_TEXTLITERALRESULT; 181 } 182 183 /** 184 * Return the node name. 185 * 186 * @return The element's name 187 */ 188 public String getNodeName() 189 { 190 return "#Text"; 191 } 192 193 /** 194 * Copy the text literal to the result tree. 195 * 196 * @param transformer non-null reference to the the current transform-time state. 197 * 198 * @throws TransformerException 199 */ 200 public void execute( 201 TransformerImpl transformer) 202 throws TransformerException 203 { 204 try 205 { 206 SerializationHandler rth = transformer.getResultTreeHandler(); 207 if (transformer.getDebug()) { 208 // flush any pending cached processing before the trace event. 209 rth.flushPending(); 210 transformer.getTraceManager().fireTraceEvent(this); 211 } 212 213 if (m_disableOutputEscaping) 214 { 215 rth.processingInstruction(javax.xml.transform.Result.PI_DISABLE_OUTPUT_ESCAPING, ""); 216 } 217 218 rth.characters(m_ch, 0, m_ch.length); 219 220 if (m_disableOutputEscaping) 221 { 222 rth.processingInstruction(javax.xml.transform.Result.PI_ENABLE_OUTPUT_ESCAPING, ""); 223 } 224 } 225 catch(SAXException se) 226 { 227 throw new TransformerException(se); 228 } 229 finally 230 { 231 if (transformer.getDebug()) { 232 try 233 { 234 // flush any pending cached processing before sending the trace event 235 transformer.getResultTreeHandler().flushPending(); 236 transformer.getTraceManager().fireTraceEndEvent(this); 237 } 238 catch (SAXException se) 239 { 240 throw new TransformerException(se); 241 } 242 } 243 } 244 } 245 }