1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 #include <sax/fshelper.hxx> 23 #include "fastserializer.hxx" 24 #include <com/sun/star/xml/sax/XFastTokenHandler.hpp> 25 #include <comphelper/processfactory.hxx> 26 #include <rtl/ustrbuf.hxx> 27 28 using namespace ::com::sun::star; 29 using namespace ::com::sun::star::uno; 30 31 namespace sax_fastparser { 32 33 FastSerializerHelper::FastSerializerHelper(const Reference< io::XOutputStream >& xOutputStream ) : 34 mpSerializer(new FastSaxSerializer()) 35 { 36 Reference< XComponentContext > xContext( ::comphelper::getProcessComponentContext(), UNO_SET_THROW ); 37 Reference< lang::XMultiComponentFactory > xFactory( xContext->getServiceManager(), UNO_SET_THROW ); 38 mxTokenHandler.set( xFactory->createInstanceWithContext( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.sax.FastTokenHandler") ), xContext ), UNO_QUERY_THROW ); 39 40 mpSerializer->setFastTokenHandler( mxTokenHandler ); 41 mpSerializer->setOutputStream( xOutputStream ); 42 mpSerializer->startDocument(); 43 } 44 45 FastSerializerHelper::~FastSerializerHelper() 46 { 47 mpSerializer->endDocument(); 48 delete mpSerializer; 49 } 50 51 void FastSerializerHelper::startElement(const char* elementName, ...) 52 { 53 FastAttributeList* pAttrList = new FastAttributeList( mxTokenHandler ); 54 va_list args; 55 va_start(args, elementName); 56 while (true) 57 { 58 const char* pName = va_arg(args, const char*); 59 if (!pName) 60 break; 61 const char* pValue = va_arg(args, const char*); 62 if (pValue) 63 pAttrList->addUnknown(pName, pValue); 64 } 65 va_end(args); 66 const com::sun::star::uno::Reference<com::sun::star::xml::sax::XFastAttributeList> xAttrList(pAttrList); 67 mpSerializer->startUnknownElement(::rtl::OUString(), ::rtl::OUString::createFromAscii(elementName), xAttrList); 68 } 69 70 void FastSerializerHelper::singleElement(const char* elementName, ...) 71 { 72 FastAttributeList* pAttrList = new FastAttributeList( mxTokenHandler ); 73 va_list args; 74 va_start(args, elementName); 75 while (true) 76 { 77 const char* pName = va_arg(args, const char*); 78 if (!pName) 79 break; 80 const char* pValue = va_arg(args, const char*); 81 if (pValue) 82 pAttrList->addUnknown(pName, pValue); 83 } 84 va_end(args); 85 const com::sun::star::uno::Reference<com::sun::star::xml::sax::XFastAttributeList> xAttrList(pAttrList); 86 mpSerializer->singleUnknownElement(::rtl::OUString(), ::rtl::OUString::createFromAscii(elementName), xAttrList); 87 } 88 89 void FastSerializerHelper::endElement(const char* elementName) 90 { 91 mpSerializer->endUnknownElement(::rtl::OUString(), ::rtl::OUString::createFromAscii(elementName)); 92 } 93 94 void FastSerializerHelper::startElementV(sal_Int32 elementTokenId, va_list args) 95 { 96 FastAttributeList* pAttrList = new FastAttributeList( mxTokenHandler ); 97 98 while (true) 99 { 100 sal_Int32 nName = va_arg(args, sal_Int32); 101 if (nName == FSEND) 102 break; 103 const char* pValue = va_arg(args, const char*); 104 if (pValue) 105 pAttrList->add(nName, pValue); 106 } 107 108 const com::sun::star::uno::Reference<com::sun::star::xml::sax::XFastAttributeList> xAttrList(pAttrList); 109 mpSerializer->startFastElement(elementTokenId, xAttrList); 110 } 111 112 void FastSerializerHelper::singleElementV(sal_Int32 elementTokenId, va_list args) 113 { 114 FastAttributeList* pAttrList = new FastAttributeList( mxTokenHandler ); 115 116 while (true) 117 { 118 sal_Int32 nName = va_arg(args, sal_Int32); 119 if (nName == FSEND) 120 break; 121 const char* pValue = va_arg(args, const char*); 122 if (pValue) 123 pAttrList->add(nName, pValue); 124 } 125 126 const com::sun::star::uno::Reference<com::sun::star::xml::sax::XFastAttributeList> xAttrList(pAttrList); 127 mpSerializer->singleFastElement(elementTokenId, xAttrList); 128 } 129 130 void FastSerializerHelper::endElement(sal_Int32 elementTokenId) 131 { 132 mpSerializer->endFastElement(elementTokenId); 133 } 134 135 void FastSerializerHelper::startElementV(sal_Int32 elementTokenId, XFastAttributeListRef xAttrList) 136 { 137 mpSerializer->startFastElement(elementTokenId, xAttrList); 138 } 139 140 141 void FastSerializerHelper::singleElement(const char* elementName, XFastAttributeListRef xAttrList) 142 { 143 mpSerializer->singleUnknownElement(::rtl::OUString(), ::rtl::OUString::createFromAscii(elementName), xAttrList); 144 } 145 146 void FastSerializerHelper::singleElementV(sal_Int32 elementTokenId, XFastAttributeListRef xAttrList) 147 { 148 mpSerializer->singleFastElement(elementTokenId, xAttrList); 149 } 150 151 FastSerializerHelper* FastSerializerHelper::write(const char* value) 152 { 153 return write(rtl::OUString::createFromAscii(value)); 154 } 155 156 FastSerializerHelper* FastSerializerHelper::write(const rtl::OUString& value) 157 { 158 mpSerializer->characters(value); 159 return this; 160 } 161 162 FastSerializerHelper* FastSerializerHelper::write(sal_Int32 value) 163 { 164 return write(::rtl::OUString::valueOf(value)); 165 } 166 167 FastSerializerHelper* FastSerializerHelper::write(sal_Int64 value) 168 { 169 return write(::rtl::OUString::valueOf(value)); 170 } 171 172 FastSerializerHelper* FastSerializerHelper::write(float value) 173 { 174 return write(::rtl::OUString::valueOf(value)); 175 } 176 177 FastSerializerHelper* FastSerializerHelper::write(double value) 178 { 179 return write(::rtl::OUString::valueOf(value)); 180 } 181 182 FastSerializerHelper* FastSerializerHelper::writeEscaped(const char* value) 183 { 184 return writeEscaped(::rtl::OUString::createFromAscii(value)); 185 } 186 187 FastSerializerHelper* FastSerializerHelper::writeEscaped(const ::rtl::OUString& value) 188 { 189 return write(FastSaxSerializer::escapeXml(value)); 190 } 191 192 FastSerializerHelper* FastSerializerHelper::writeId(sal_Int32 tokenId) 193 { 194 mpSerializer->writeId(tokenId); 195 return this; 196 } 197 198 ::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream > FastSerializerHelper::getOutputStream() 199 { 200 return mpSerializer->getOutputStream(); 201 } 202 203 void FastSerializerHelper::mark() 204 { 205 mpSerializer->mark(); 206 } 207 208 void FastSerializerHelper::mergeTopMarks( MergeMarksEnum eMergeType ) 209 { 210 mpSerializer->mergeTopMarks( eMergeType ); 211 } 212 213 FastAttributeList * FastSerializerHelper::createAttrList() 214 { 215 return new FastAttributeList( mxTokenHandler ); 216 } 217 218 219 } 220