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