00001 /* 00002 * Licensed to the Apache Software Foundation (ASF) under one 00003 * or more contributor license agreements. See the NOTICE file 00004 * distributed with this work for additional information 00005 * regarding copyright ownership. The ASF licenses this file 00006 * to you under the Apache License, Version 2.0 (the "License"); 00007 * you may not use this file except in compliance with the License. 00008 * You may obtain a copy of the License at 00009 * 00010 * http://www.apache.org/licenses/LICENSE-2.0 00011 * 00012 * Unless required by applicable law or agreed to in writing, software 00013 * distributed under the License is distributed on an "AS IS" BASIS, 00014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00015 * See the License for the specific language governing permissions and 00016 * limitations under the License. 00017 */ 00018 #if !defined(XALANFORMATTERWRITER_HEADER_GUARD_1357924680) 00019 #define XALANFORMATTERWRITER_HEADER_GUARD_1357924680 00020 00021 00022 00023 // Base include file. Must be first. 00024 #include <xalanc/XMLSupport/XMLSupportDefinitions.hpp> 00025 00026 #include <xercesc/sax/SAXException.hpp> 00027 00028 #include <xalanc/PlatformSupport/DOMStringHelper.hpp> 00029 #include <xalanc/PlatformSupport/FormatterListener.hpp> 00030 #include <xalanc/PlatformSupport/Writer.hpp> 00031 #include <xalanc/PlatformSupport/XalanMessageLoader.hpp> 00032 #include <xalanc/PlatformSupport/XalanOutputStream.hpp> 00033 00034 00035 00036 XALAN_CPP_NAMESPACE_BEGIN 00037 00038 00039 00040 XALAN_USING_XERCES(MemoryManager) 00041 00042 00043 00044 class XalanFormatterWriter 00045 { 00046 public: 00047 00048 typedef FormatterListener::size_type size_type; 00049 00050 00051 template <class WriterType> 00052 class NewLineWriterFunctor 00053 { 00054 public: 00055 00056 typedef WriterType writer_type; 00057 00058 NewLineWriterFunctor(WriterType& writer) : 00059 m_writer(writer), 00060 m_newlineString(0), 00061 m_newlineStringLength(0) 00062 { 00063 XalanOutputStream* stream = writer.getStream(); 00064 00065 if(stream != 0) 00066 { 00067 m_newlineString = stream->getNewlineString(); 00068 } 00069 else 00070 { 00071 m_newlineString = XalanOutputStream::defaultNewlineString(); 00072 } 00073 00074 assert(m_newlineString != 0); 00075 00076 m_newlineStringLength = length(m_newlineString); 00077 } 00078 00079 void 00080 operator()() 00081 { 00082 assert(m_newlineString != 0 && length(m_newlineString) == m_newlineStringLength); 00083 00084 m_writer.write(m_newlineString, m_newlineStringLength); 00085 } 00086 00087 private: 00088 00089 WriterType& m_writer; 00090 00091 /** 00092 * The string of characters that represents the newline 00093 */ 00094 const XalanDOMChar* m_newlineString; 00095 00096 /** 00097 * The length of the the string of characters that represents the newline 00098 */ 00099 size_type m_newlineStringLength; 00100 }; 00101 00102 template<class WriterType> 00103 class WhiteSpaceWriterFunctor 00104 { 00105 typedef typename WriterType::value_type value_type; 00106 00107 public: 00108 typedef WriterType writer_type; 00109 00110 WhiteSpaceWriterFunctor(WriterType& writer) : 00111 m_writer(writer) 00112 { 00113 } 00114 00115 void 00116 operator()(size_type count) 00117 { 00118 for ( size_type i = 0 ; i < count ; i++ ) 00119 { 00120 m_writer.write(value_type(XalanUnicode::charSpace)); 00121 } 00122 } 00123 00124 private: 00125 00126 WriterType& m_writer; 00127 }; 00128 00129 class CommonRepresentableCharFunctor 00130 { 00131 public: 00132 00133 CommonRepresentableCharFunctor(const XalanOutputStream* stream) : 00134 m_stream(stream) 00135 { 00136 assert(stream != 0); 00137 } 00138 00139 bool 00140 operator()(XalanUnicodeChar theChar) const 00141 { 00142 bool result = true; 00143 00144 if (m_stream != 0) 00145 { 00146 result = m_stream->canTranscodeTo(theChar); 00147 } 00148 00149 return result; 00150 } 00151 00152 private: 00153 00154 const XalanOutputStream* const m_stream; 00155 }; 00156 00157 public: 00158 00159 XalanFormatterWriter( 00160 Writer& theWriter, 00161 MemoryManager& theMemoryManager) : 00162 m_writer(theWriter), 00163 m_memoryManager(theMemoryManager), 00164 m_stringBuffer(5, 0, theMemoryManager) 00165 { 00166 const XalanOutputStream* const theStream = 00167 theWriter.getStream(); 00168 00169 if (theStream == 0) 00170 { 00171 m_newlineString = XalanOutputStream::defaultNewlineString(); 00172 } 00173 else 00174 { 00175 m_newlineString = theStream->getNewlineString(); 00176 } 00177 00178 assert(m_newlineString != 0); 00179 00180 m_newlineStringLength = length(m_newlineString); 00181 00182 assert(m_newlineString != 0); 00183 } 00184 00185 MemoryManager& 00186 getMemoryManager() 00187 { 00188 return m_memoryManager; 00189 } 00190 00191 virtual 00192 ~XalanFormatterWriter() 00193 { 00194 } 00195 00196 Writer* 00197 getWriter() const 00198 { 00199 return &m_writer; 00200 } 00201 00202 XalanOutputStream* 00203 getStream() 00204 { 00205 return m_writer.getStream(); 00206 } 00207 00208 const XalanOutputStream* 00209 getStream() const 00210 { 00211 return m_writer.getStream(); 00212 } 00213 00214 void 00215 flushWriter() 00216 { 00217 m_writer.flush(); 00218 } 00219 00220 00221 static bool 00222 isUTF16HighSurrogate(XalanDOMChar theChar) 00223 { 00224 return 0xD800u <= theChar && theChar <= 0xDBFFu ? true : false; 00225 } 00226 00227 static bool 00228 isUTF16LowSurrogate(XalanDOMChar theChar) 00229 { 00230 return 0xDC00u <= theChar && theChar <= 0xDFFFu ? true : false; 00231 } 00232 00233 static XalanUnicodeChar 00234 decodeUTF16SurrogatePair( 00235 XalanDOMChar theHighSurrogate, 00236 XalanDOMChar theLowSurrogate, 00237 MemoryManager& theManager) 00238 { 00239 assert(isUTF16HighSurrogate(theHighSurrogate) == true); 00240 00241 if (isUTF16LowSurrogate(theLowSurrogate) == false) 00242 { 00243 throwInvalidUTF16SurrogateException(theHighSurrogate, theLowSurrogate, theManager); 00244 } 00245 00246 return ((theHighSurrogate - 0xD800u) << 10) + theLowSurrogate - 0xDC00u + 0x00010000u; 00247 } 00248 00249 static void 00250 throwInvalidCharacterException( 00251 XalanUnicodeChar ch, 00252 MemoryManager& theManager) 00253 { 00254 XalanDOMString theMessage(theManager); 00255 XalanDOMString theBuffer(theManager); 00256 00257 XalanMessageLoader::getMessage( 00258 theMessage, 00259 XalanMessages::InvalidScalar_1Param, 00260 NumberToHexDOMString(ch, theBuffer)); 00261 00262 XALAN_USING_XERCES(SAXException) 00263 00264 throw SAXException(theMessage.c_str(), &theManager); 00265 } 00266 00267 void 00268 throwUnrepresentableCharacterException( 00269 XalanUnicodeChar ch, 00270 MemoryManager& theManager) 00271 { 00272 XalanDOMString theBuffer(theManager); 00273 00274 const XalanOutputStream* const theStream = 00275 m_writer.getStream(); 00276 00277 throw XalanTranscodingServices::UnrepresentableCharacterException( 00278 ch, 00279 theStream->getOutputEncoding(), 00280 theBuffer); 00281 } 00282 00283 static void 00284 throwInvalidUTF16SurrogateException( 00285 XalanDOMChar ch, 00286 XalanDOMChar next, 00287 MemoryManager& theManager) 00288 { 00289 00290 XalanDOMString chStr(theManager); 00291 00292 XalanDOMString nextStr(theManager); 00293 00294 NumberToHexDOMString(ch, chStr); 00295 00296 NumberToHexDOMString(next, nextStr); 00297 00298 XalanDOMString theMessage(theManager); 00299 00300 XalanMessageLoader::getMessage( 00301 theMessage, 00302 XalanMessages::InvalidSurrogatePair_2Param, 00303 theMessage, 00304 chStr, 00305 nextStr); 00306 00307 XALAN_USING_XERCES(SAXException) 00308 00309 throw SAXException(theMessage.c_str(),&theManager); 00310 } 00311 00312 protected: 00313 00314 /** 00315 * The writer. 00316 */ 00317 Writer& m_writer; 00318 00319 /** 00320 * The MemoryManager instance to use for any dynamically- 00321 * allocated memory. 00322 */ 00323 MemoryManager& m_memoryManager; 00324 00325 XalanDOMString m_stringBuffer; 00326 00327 /** 00328 * The string of characters that represents the newline 00329 */ 00330 const XalanDOMChar* m_newlineString; 00331 00332 /** 00333 * The length of the the string of characters that represents the newline 00334 */ 00335 size_type m_newlineStringLength; 00336 00337 /** 00338 * Format a code point as a numeric character reference. 00339 * 00340 * @param theChar A Unicode code point. 00341 */ 00342 const XalanDOMString& 00343 formatNumericCharacterReference(XalanUnicodeChar theChar) 00344 { 00345 m_stringBuffer.clear(); 00346 00347 m_stringBuffer.push_back(XalanDOMChar(XalanUnicode::charAmpersand)); 00348 m_stringBuffer.push_back(XalanDOMChar(XalanUnicode::charNumberSign)); 00349 00350 NumberToDOMString(theChar, m_stringBuffer); 00351 00352 m_stringBuffer.push_back(XalanDOMChar(XalanUnicode::charSemicolon)); 00353 00354 return m_stringBuffer; 00355 } 00356 00357 private: 00358 00359 // These are not implemented. 00360 XalanFormatterWriter(); 00361 00362 XalanFormatterWriter& 00363 operator=(const XalanFormatterWriter&); 00364 }; 00365 00366 00367 00368 XALAN_CPP_NAMESPACE_END 00369 00370 00371 00372 #endif // XALANFORMATTERWRITER_HEADER_GUARD_1357924680
Doxygen and GraphViz are used to generate this API documentation from the Xalan-C header files.
Xalan-C++ XSLT Processor Version 1.11 |
|