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