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 <libxml/xmlstring.h>
21 : #include <string.h>
22 : #include <resourcemodel/TagLogger.hxx>
23 : #include <resourcemodel/util.hxx>
24 : #include <resourcemodel/QNameToString.hxx>
25 : #include <boost/unordered_map.hpp>
26 :
27 : using namespace css;
28 :
29 : namespace writerfilter
30 : {
31 38 : TagLogger::TagLogger(const char* name)
32 38 : : pWriter( nullptr ), pName( name )
33 : {
34 38 : }
35 :
36 38 : TagLogger::~TagLogger()
37 : {
38 38 : pWriter = nullptr;
39 38 : pName = nullptr;
40 38 : }
41 :
42 : #ifdef DEBUG_WRITERFILTER
43 : void TagLogger::setFileName( const std::string & filename )
44 : {
45 : if ( pWriter )
46 : endDocument();
47 :
48 : std::string fileName;
49 : char * temp = getenv("TAGLOGGERTMP");
50 :
51 : if (temp != nullptr)
52 : fileName += temp;
53 : else
54 : fileName += "/tmp";
55 :
56 : std::string sPrefix = filename;
57 : size_t nLastSlash = sPrefix.find_last_of('/');
58 : size_t nLastBackslash = sPrefix.find_last_of('\\');
59 : size_t nCutPos = nLastSlash;
60 : if (nLastBackslash < nCutPos)
61 : nCutPos = nLastBackslash;
62 : if (nCutPos < sPrefix.size())
63 : sPrefix = sPrefix.substr(nCutPos + 1);
64 :
65 : fileName += "/";
66 : fileName += sPrefix;
67 : fileName += ".";
68 : fileName += pName;
69 : fileName += ".xml";
70 :
71 : pWriter = xmlNewTextWriterFilename( fileName.c_str(), 0 );
72 : xmlTextWriterSetIndent( pWriter, 4 );
73 : }
74 :
75 : void TagLogger::startDocument()
76 : {
77 : if (!pWriter)
78 : return;
79 : xmlTextWriterStartDocument( pWriter, nullptr, nullptr, nullptr );
80 : xmlTextWriterStartElement( pWriter, BAD_CAST( "root" ) );
81 : }
82 :
83 : void TagLogger::endDocument()
84 : {
85 : if (!pWriter)
86 : return;
87 : xmlTextWriterEndDocument( pWriter );
88 : xmlFreeTextWriter( pWriter );
89 : pWriter = nullptr;
90 : }
91 :
92 : #endif
93 :
94 38 : TagLogger::Pointer_t TagLogger::getInstance(const char * name)
95 : {
96 : typedef boost::unordered_map<std::string, TagLogger::Pointer_t> TagLoggerHashMap_t;
97 38 : static TagLoggerHashMap_t tagLoggers;
98 :
99 38 : TagLoggerHashMap_t::iterator aIt = tagLoggers.end();
100 :
101 38 : std::string sName = name;
102 38 : if (! tagLoggers.empty())
103 0 : aIt = tagLoggers.find(sName);
104 :
105 38 : if (aIt == tagLoggers.end())
106 : {
107 38 : TagLogger::Pointer_t pTagLogger(new TagLogger(name));
108 76 : std::pair<std::string, TagLogger::Pointer_t> entry(sName, pTagLogger);
109 76 : aIt = tagLoggers.insert(entry).first;
110 : }
111 :
112 38 : return aIt->second;
113 : }
114 :
115 : #ifdef DEBUG_WRITERFILTER
116 : void TagLogger::element(const std::string & name)
117 : {
118 : startElement(name);
119 : endElement();
120 : }
121 :
122 : void TagLogger::unoPropertySet(uno::Reference<beans::XPropertySet> rPropSet)
123 : {
124 : uno::Reference<beans::XPropertySetInfo> xPropSetInfo(rPropSet->getPropertySetInfo());
125 : uno::Sequence<beans::Property> aProps(xPropSetInfo->getProperties());
126 :
127 : startElement( "unoPropertySet" );
128 :
129 : for (int i = 0; i < aProps.getLength(); ++i)
130 : {
131 : startElement( "property" );
132 : OUString sName(aProps[i].Name);
133 :
134 : attribute( "name", sName );
135 : try
136 : {
137 : attribute( "value", rPropSet->getPropertyValue( sName ) );
138 : }
139 : catch (const uno::Exception &)
140 : {
141 : startElement( "exception" );
142 :
143 : chars(std::string("getPropertyValue(\""));
144 : chars(sName);
145 : chars(std::string("\")"));
146 :
147 : endElement( );
148 : }
149 : endElement( );
150 : }
151 : endElement( );
152 : }
153 :
154 : void TagLogger::startElement(const std::string & name)
155 : {
156 : if (!pWriter)
157 : return;
158 : xmlChar* xmlName = xmlCharStrdup( name.c_str() );
159 : xmlTextWriterStartElement( pWriter, xmlName );
160 : xmlFree( xmlName );
161 : }
162 : #endif
163 :
164 0 : void TagLogger::attribute(const std::string & name, const std::string & value)
165 : {
166 0 : if (!pWriter)
167 0 : return;
168 0 : xmlChar* xmlName = xmlCharStrdup( name.c_str() );
169 0 : xmlChar* xmlValue = xmlCharStrdup( value.c_str() );
170 0 : xmlTextWriterWriteAttribute( pWriter, xmlName, xmlValue );
171 :
172 0 : xmlFree( xmlValue );
173 0 : xmlFree( xmlName );
174 : }
175 :
176 : #ifdef DEBUG_WRITERFILTER
177 : void TagLogger::attribute(const std::string & name, const OUString & value)
178 : {
179 : attribute( name, OUStringToOString( value, RTL_TEXTENCODING_ASCII_US ).getStr() );
180 : }
181 :
182 : void TagLogger::attribute(const std::string & name, sal_uInt32 value)
183 : {
184 : if (!pWriter)
185 : return;
186 : xmlChar* xmlName = xmlCharStrdup( name.c_str() );
187 : xmlTextWriterWriteFormatAttribute( pWriter, xmlName,
188 : "%" SAL_PRIuUINT32, value );
189 : xmlFree( xmlName );
190 : }
191 :
192 : void TagLogger::attribute(const std::string & name, const uno::Any aAny)
193 : {
194 : if (!pWriter)
195 : return;
196 :
197 : sal_Int32 nInt = 0;
198 : float nFloat = 0.0;
199 : OUString aStr;
200 :
201 : xmlChar* xmlName = xmlCharStrdup( name.c_str() );
202 : if ( aAny >>= nInt )
203 : {
204 : xmlTextWriterWriteFormatAttribute( pWriter, xmlName,
205 : "%" SAL_PRIdINT32, nInt );
206 : }
207 : else if ( aAny >>= nFloat )
208 : {
209 : xmlTextWriterWriteFormatAttribute( pWriter, xmlName,
210 : "%f", nFloat );
211 : }
212 : else if ( aAny >>= aStr )
213 : {
214 : attribute( name, aStr );
215 : }
216 : xmlFree( xmlName );
217 : }
218 :
219 : void TagLogger::chars(const std::string & rChars)
220 : {
221 : if (!pWriter)
222 : return;
223 : xmlChar* xmlChars = xmlCharStrdup( rChars.c_str() );
224 : xmlTextWriterWriteString( pWriter, xmlChars );
225 : xmlFree( xmlChars );
226 : }
227 :
228 : void TagLogger::chars(const OUString & rChars)
229 : {
230 : chars(OUStringToOString(rChars, RTL_TEXTENCODING_ASCII_US).getStr());
231 : }
232 :
233 : void TagLogger::endElement()
234 : {
235 : if (!pWriter)
236 : return;
237 : xmlTextWriterEndElement( pWriter );
238 : }
239 :
240 : class PropertySetDumpHandler : public Properties
241 : {
242 : IdToString::Pointer_t mpIdToString;
243 : TagLogger* m_pLogger;
244 :
245 : public:
246 : PropertySetDumpHandler(TagLogger* pLogger,
247 : IdToString::Pointer_t pIdToString);
248 : virtual ~PropertySetDumpHandler();
249 :
250 : void resolve(writerfilter::Reference<Properties>::Pointer_t props);
251 :
252 : virtual void attribute(Id name, Value & val) SAL_OVERRIDE;
253 : virtual void sprm(Sprm & sprm) SAL_OVERRIDE;
254 : };
255 :
256 : PropertySetDumpHandler::PropertySetDumpHandler(TagLogger* pLogger,
257 : IdToString::Pointer_t pIdToString) :
258 : mpIdToString(pIdToString),
259 : m_pLogger(pLogger)
260 : {
261 : }
262 :
263 : PropertySetDumpHandler::~PropertySetDumpHandler()
264 : {
265 : }
266 :
267 : void PropertySetDumpHandler::resolve(
268 : writerfilter::Reference<Properties>::Pointer_t pProps)
269 : {
270 : if (pProps.get() != nullptr)
271 : pProps->resolve( *this );
272 : }
273 :
274 : void PropertySetDumpHandler::attribute(Id name, Value & val)
275 : {
276 : m_pLogger->startElement( "attribute" );
277 :
278 : m_pLogger->attribute("name", (*QNameToString::Instance())(name));
279 : m_pLogger->attribute("value", val.toString());
280 :
281 : resolve(val.getProperties());
282 :
283 : m_pLogger->endElement();
284 : }
285 :
286 : void PropertySetDumpHandler::sprm(Sprm & rSprm)
287 : {
288 : m_pLogger->startElement( "sprm" );
289 :
290 : std::string sName;
291 :
292 : if (mpIdToString != IdToString::Pointer_t())
293 : sName = mpIdToString->toString(rSprm.getId());
294 :
295 : m_pLogger->attribute( "name", sName );
296 :
297 : m_pLogger->attribute( "id", rSprm.getId() );
298 : m_pLogger->attribute( "value", rSprm.getValue()->toString() );
299 :
300 : resolve( rSprm.getProps() );
301 :
302 : m_pLogger->endElement();
303 : }
304 :
305 : void TagLogger::propertySet(writerfilter::Reference<Properties>::Pointer_t props,
306 : IdToString::Pointer_t pIdToString)
307 : {
308 : startElement( "propertySet" );
309 :
310 : PropertySetDumpHandler handler( this, pIdToString );
311 : handler.resolve( props );
312 :
313 : endElement( );
314 : }
315 : #endif
316 :
317 114 : }
318 :
319 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|