00001 /* 00002 * The Apache Software License, Version 1.1 00003 * 00004 * Copyright (c) 2002 Berin Lautenbach. All rights reserved. 00005 * 00006 * Redistribution and use in source and binary forms, with or without 00007 * modification, are permitted provided that the following conditions 00008 * are met: 00009 * 00010 * 1. Redistributions of source code must retain the above copyright 00011 * notice, this list of conditions and the following disclaimer. 00012 * 00013 * 2. Redistributions in binary form must reproduce the above copyright 00014 * notice, this list of conditions and the following disclaimer in 00015 * the documentation and/or other materials provided with the 00016 * distribution. 00017 * 00018 * 3. The end-user documentation included with the redistribution, 00019 * if any, must include the following acknowledgment: 00020 * "This product includes software developed by 00021 * Berin Lautenbach" 00022 * Alternately, this acknowledgment may appear in the software itself, 00023 * if and wherever such third-party acknowledgments normally appear. 00024 * 00025 * 4. The names "XSEC", "xml-security-c" and Berin Lautenbach must 00026 * not be used to endorse or promote products derived from this 00027 * software without prior written permission. For written 00028 * permission, please contact berin@users.sourceforge.net. 00029 * 00030 * 5. Products derived from this software may not be called "xml-security-c", 00031 * nor may "xml-security-c" appear in their name, without prior written 00032 * permission of Berin Lautenbach. 00033 * 00034 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 00035 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00036 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00037 * DISCLAIMED. IN NO EVENT SHALL BERIN LAUTENBACH OR OTHER 00038 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00039 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00040 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 00041 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00042 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00043 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 00044 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00045 * SUCH DAMAGE. 00046 * ==================================================================== 00047 */ 00048 00049 /* 00050 * XSEC 00051 * 00052 * XSECSafeBufferFormatter := Class for formatting DOMStrings into SafeBuffers 00053 * 00054 */ 00055 00056 #ifndef XSECSAFEBUFFERFORMATTER_INCLUDE 00057 #define XSECSAFEBUFFERFORMATTER_INCLUDE 00058 00059 // XSEC includes 00060 00061 #include <xsec/utils/XSECSafeBuffer.hpp> 00062 00063 // Xerces includes 00064 00065 #include <xercesc/framework/XMLFormatter.hpp> 00066 //#include <xercesc/dom/DOMString.hpp> 00067 00068 class sbFormatTarget : public XMLFormatTarget 00069 { 00070 public: 00071 00072 safeBuffer * buffer; // Buffer to write to 00073 00074 sbFormatTarget() {}; 00075 ~sbFormatTarget() {}; 00076 00077 void setBuffer (safeBuffer * toSet) {buffer = toSet;}; 00078 00079 00080 // ----------------------------------------------------------------------- 00081 // Implementations of the format target interface 00082 // Based on Xerces/Xalan example code 00083 // ----------------------------------------------------------------------- 00084 00085 void writeChars(const XMLByte* const toWrite, 00086 const unsigned int count, 00087 XMLFormatter * const formatter) 00088 { 00089 buffer->sbMemcpyIn((char *) toWrite, (int) count); 00090 (*buffer)[count] = '\0'; 00091 }; 00092 00093 private: 00094 00095 sbFormatTarget(const sbFormatTarget& other); 00096 void operator=(const sbFormatTarget& rhs); 00097 00098 00099 }; 00100 00101 // For easy copying to a buffer 00102 00103 //XMLFormatter& operator<< (XMLFormatter& strm, const DOMString& s); 00104 00105 class XSECSafeBufferFormatter { 00106 00107 XMLFormatter * formatter; // To actually perform the formatting 00108 safeBuffer formatBuffer; // Storage of translated strings 00109 sbFormatTarget * sbf; // Format target used by XMLFormatter 00110 00111 public: 00112 00113 // Constructor 00114 00115 XSECSafeBufferFormatter( 00116 const XMLCh * const outEncoding, 00117 const XMLFormatter::EscapeFlags escapeFlags=XMLFormatter::NoEscapes, 00118 const XMLFormatter::UnRepFlags unrepFlags=XMLFormatter::UnRep_Fail); 00119 00120 XSECSafeBufferFormatter( 00121 const char * const outEncoding, 00122 const XMLFormatter::EscapeFlags escapeFlags=XMLFormatter::NoEscapes, 00123 const XMLFormatter::UnRepFlags unrepFlags=XMLFormatter::UnRep_Fail); 00124 00125 // Destructor 00126 00127 ~XSECSafeBufferFormatter(); 00128 00129 // Reimplementation of XMLFormatter functions 00130 00131 void formatBuf ( 00132 const XMLCh *const toFormat, 00133 const unsigned int count, 00134 const XMLFormatter::EscapeFlags escapeFlags=XMLFormatter::DefaultEscape, 00135 const XMLFormatter::UnRepFlags unrepFlags=XMLFormatter::DefaultUnRep); // Format a buffer 00136 00137 XSECSafeBufferFormatter& operator<< ( 00138 const XMLCh *const toFormat); // Format a buffer 00139 00140 XSECSafeBufferFormatter& operator<< ( 00141 const XMLCh toFormat); // Format a character 00142 00143 const XMLCh* getEncodingName ()const; // Get current encoding 00144 00145 void setEscapeFlags (const XMLFormatter::EscapeFlags newFlags); 00146 void setUnRepFlags (const XMLFormatter::UnRepFlags newFlags); 00147 00148 XSECSafeBufferFormatter& operator<< (const XMLFormatter::EscapeFlags newFlags); 00149 XSECSafeBufferFormatter& operator<< (const XMLFormatter::UnRepFlags newFlags); 00150 //XSECSafeBufferFormatter& operator<< (const DOMString &s); 00151 00152 // Friends for working with safestrings 00153 00154 friend safeBuffer& operator<< (safeBuffer &to, const XSECSafeBufferFormatter & from); 00155 00156 private: 00157 00158 // Unimplemented 00159 00160 XSECSafeBufferFormatter() {}; 00161 00162 }; 00163 00164 #endif /* XSECSAFEBUFFERFORMATTER_INCLUDE */