Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <sax/fshelper.hxx>
21 : #include "fastserializer.hxx"
22 : #include <com/sun/star/xml/sax/XFastTokenHandler.hpp>
23 : #include <comphelper/processfactory.hxx>
24 : #include <rtl/ustrbuf.hxx>
25 :
26 : using namespace ::com::sun::star;
27 : using namespace ::com::sun::star::uno;
28 :
29 : namespace sax_fastparser {
30 :
31 343 : FastSerializerHelper::FastSerializerHelper(const Reference< io::XOutputStream >& xOutputStream, bool bWriteHeader ) :
32 343 : mpSerializer(new FastSaxSerializer())
33 : {
34 343 : Reference< XComponentContext > xContext( ::comphelper::getProcessComponentContext(), UNO_SET_THROW );
35 686 : Reference< lang::XMultiComponentFactory > xFactory( xContext->getServiceManager(), UNO_SET_THROW );
36 343 : mxTokenHandler.set( xFactory->createInstanceWithContext("com.sun.star.xml.sax.FastTokenHandler", xContext ), UNO_QUERY_THROW );
37 :
38 343 : mpSerializer->setFastTokenHandler( mxTokenHandler );
39 343 : mpSerializer->setOutputStream( xOutputStream );
40 343 : if( bWriteHeader )
41 686 : mpSerializer->startDocument();
42 343 : }
43 :
44 686 : FastSerializerHelper::~FastSerializerHelper()
45 : {
46 343 : mpSerializer->endDocument();
47 343 : delete mpSerializer;
48 343 : }
49 :
50 5709 : void FastSerializerHelper::startElementInternal(sal_Int32 elementTokenId, ...)
51 : {
52 : va_list args;
53 5709 : va_start( args, elementTokenId );
54 5709 : FastAttributeList* pAttrList = new FastAttributeList( mxTokenHandler );
55 :
56 : while (true)
57 : {
58 10777 : sal_Int32 nName = va_arg(args, sal_Int32);
59 10777 : if (nName == FSEND_internal)
60 5709 : break;
61 5068 : const char* pValue = va_arg(args, const char*);
62 5068 : if (pValue)
63 4833 : pAttrList->add(nName, pValue);
64 : }
65 :
66 11418 : const com::sun::star::uno::Reference<com::sun::star::xml::sax::XFastAttributeList> xAttrList(pAttrList);
67 5709 : mpSerializer->startFastElement(elementTokenId, xAttrList);
68 10777 : va_end( args );
69 5709 : }
70 :
71 4551 : void FastSerializerHelper::singleElementInternal(sal_Int32 elementTokenId, ...)
72 : {
73 : va_list args;
74 4551 : va_start( args, elementTokenId );
75 4551 : FastAttributeList* pAttrList = new FastAttributeList( mxTokenHandler );
76 :
77 : while (true)
78 : {
79 9344 : sal_Int32 nName = va_arg(args, sal_Int32);
80 9344 : if (nName == FSEND_internal)
81 4551 : break;
82 4793 : const char* pValue = va_arg(args, const char*);
83 4793 : if (pValue)
84 4793 : pAttrList->add(nName, pValue);
85 : }
86 :
87 9102 : const com::sun::star::uno::Reference<com::sun::star::xml::sax::XFastAttributeList> xAttrList(pAttrList);
88 4551 : mpSerializer->singleFastElement(elementTokenId, xAttrList);
89 9344 : va_end( args );
90 4551 : }
91 :
92 5792 : void FastSerializerHelper::endElement(sal_Int32 elementTokenId)
93 : {
94 5792 : mpSerializer->endFastElement(elementTokenId);
95 5792 : }
96 :
97 83 : void FastSerializerHelper::startElement(sal_Int32 elementTokenId, XFastAttributeListRef xAttrList)
98 : {
99 83 : mpSerializer->startFastElement(elementTokenId, xAttrList);
100 83 : }
101 :
102 :
103 1540 : void FastSerializerHelper::singleElement(sal_Int32 elementTokenId, XFastAttributeListRef xAttrList)
104 : {
105 1540 : mpSerializer->singleFastElement(elementTokenId, xAttrList);
106 1540 : }
107 :
108 589 : FastSerializerHelper* FastSerializerHelper::write(const char* value)
109 : {
110 589 : return write(OUString::createFromAscii(value));
111 : }
112 :
113 1730 : FastSerializerHelper* FastSerializerHelper::write(const OUString& value)
114 : {
115 1730 : mpSerializer->characters(value);
116 1730 : return this;
117 : }
118 :
119 20 : FastSerializerHelper* FastSerializerHelper::write(sal_Int32 value)
120 : {
121 20 : return write(OUString::valueOf(value));
122 : }
123 :
124 8 : FastSerializerHelper* FastSerializerHelper::write(sal_Int64 value)
125 : {
126 8 : return write(OUString::valueOf(value));
127 : }
128 :
129 238 : FastSerializerHelper* FastSerializerHelper::write(double value)
130 : {
131 238 : return write(OUString::valueOf(value));
132 : }
133 :
134 145 : FastSerializerHelper* FastSerializerHelper::writeEscaped(const char* value)
135 : {
136 145 : return writeEscaped(OUString::createFromAscii(value));
137 : }
138 :
139 763 : FastSerializerHelper* FastSerializerHelper::writeEscaped(const OUString& value)
140 : {
141 763 : return write(FastSaxSerializer::escapeXml(value));
142 : }
143 :
144 177 : FastSerializerHelper* FastSerializerHelper::writeId(sal_Int32 tokenId)
145 : {
146 177 : mpSerializer->writeId(tokenId);
147 177 : return this;
148 : }
149 :
150 184 : ::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream > FastSerializerHelper::getOutputStream()
151 : {
152 184 : return mpSerializer->getOutputStream();
153 : }
154 :
155 2938 : void FastSerializerHelper::mark( Sequence< sal_Int32 > aOrder )
156 : {
157 2938 : mpSerializer->mark( aOrder );
158 2938 : }
159 :
160 2938 : void FastSerializerHelper::mergeTopMarks( MergeMarksEnum eMergeType )
161 : {
162 2938 : mpSerializer->mergeTopMarks( eMergeType );
163 2938 : }
164 :
165 290 : void FastSerializerHelper::copyTopMarkPush()
166 : {
167 290 : mpSerializer->copyTopMarkPush();
168 290 : }
169 :
170 227 : void FastSerializerHelper::copyTopMarkPop()
171 : {
172 227 : mpSerializer->copyTopMarkPop();
173 227 : }
174 :
175 1622 : FastAttributeList * FastSerializerHelper::createAttrList()
176 : {
177 1622 : return new FastAttributeList( mxTokenHandler );
178 : }
179 :
180 :
181 423 : }
182 :
183 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|