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 <tools/debug.hxx>
21 : #include <i18nlangtag/mslangid.hxx>
22 : #include <rtl/ustrbuf.hxx>
23 :
24 : #include <xmloff/xmlmetae.hxx>
25 : #include <xmloff/xmlexp.hxx>
26 : #include <xmloff/nmspmap.hxx>
27 : #include "xmloff/xmlnmspe.hxx"
28 :
29 : #include <com/sun/star/beans/XPropertyAccess.hpp>
30 : #include <com/sun/star/beans/StringPair.hpp>
31 : #include <com/sun/star/util/Duration.hpp>
32 : #include <com/sun/star/xml/dom/XDocument.hpp>
33 : #include <com/sun/star/xml/sax/XSAXSerializable.hpp>
34 :
35 : #include <sax/tools/converter.hxx>
36 :
37 : #include <comphelper/sequenceasvector.hxx>
38 : #include <unotools/docinfohelper.hxx>
39 :
40 : #include <string.h>
41 :
42 :
43 : using namespace com::sun::star;
44 : using namespace ::xmloff::token;
45 :
46 :
47 : //-------------------------------------------------------------------------
48 :
49 0 : static void lcl_AddTwoDigits( OUStringBuffer& rStr, sal_Int32 nVal )
50 : {
51 0 : if ( nVal < 10 )
52 0 : rStr.append( sal_Unicode('0') );
53 0 : rStr.append( nVal );
54 0 : }
55 :
56 : OUString
57 0 : SvXMLMetaExport::GetISODateTimeString( const util::DateTime& rDateTime )
58 : {
59 : // return ISO date string "YYYY-MM-DDThh:mm:ss"
60 :
61 0 : OUStringBuffer sTmp;
62 0 : sTmp.append( (sal_Int32) rDateTime.Year );
63 0 : sTmp.append( sal_Unicode('-') );
64 0 : lcl_AddTwoDigits( sTmp, rDateTime.Month );
65 0 : sTmp.append( sal_Unicode('-') );
66 0 : lcl_AddTwoDigits( sTmp, rDateTime.Day );
67 0 : sTmp.append( sal_Unicode('T') );
68 0 : lcl_AddTwoDigits( sTmp, rDateTime.Hours );
69 0 : sTmp.append( sal_Unicode(':') );
70 0 : lcl_AddTwoDigits( sTmp, rDateTime.Minutes );
71 0 : sTmp.append( sal_Unicode(':') );
72 0 : lcl_AddTwoDigits( sTmp, rDateTime.Seconds );
73 :
74 0 : return sTmp.makeStringAndClear();
75 : }
76 :
77 : //-------------------------------------------------------------------------
78 :
79 0 : void SvXMLMetaExport::SimpleStringElement( const OUString& rText,
80 : sal_uInt16 nNamespace, enum XMLTokenEnum eElementName )
81 : {
82 0 : if ( !rText.isEmpty() ) {
83 : SvXMLElementExport aElem( mrExport, nNamespace, eElementName,
84 0 : sal_True, sal_False );
85 0 : mrExport.Characters( rText );
86 : }
87 0 : }
88 :
89 0 : void SvXMLMetaExport::SimpleDateTimeElement( const util::DateTime & rDate,
90 : sal_uInt16 nNamespace, enum XMLTokenEnum eElementName )
91 : {
92 0 : if (rDate.Month != 0) { // invalid dates are 0-0-0
93 0 : OUString sValue = GetISODateTimeString( rDate );
94 0 : if ( !sValue.isEmpty() ) {
95 : SvXMLElementExport aElem( mrExport, nNamespace, eElementName,
96 0 : sal_True, sal_False );
97 0 : mrExport.Characters( sValue );
98 0 : }
99 : }
100 0 : }
101 :
102 0 : void SvXMLMetaExport::_MExport()
103 : {
104 : // generator
105 : {
106 : SvXMLElementExport aElem( mrExport, XML_NAMESPACE_META, XML_GENERATOR,
107 0 : sal_True, sal_True );
108 0 : mrExport.Characters( ::utl::DocInfoHelper::GetGeneratorString() );
109 : }
110 :
111 : // document title
112 0 : SimpleStringElement ( mxDocProps->getTitle(),
113 0 : XML_NAMESPACE_DC, XML_TITLE );
114 :
115 : // description
116 0 : SimpleStringElement ( mxDocProps->getDescription(),
117 0 : XML_NAMESPACE_DC, XML_DESCRIPTION );
118 :
119 : // subject
120 0 : SimpleStringElement ( mxDocProps->getSubject(),
121 0 : XML_NAMESPACE_DC, XML_SUBJECT );
122 :
123 : // created...
124 0 : SimpleStringElement ( mxDocProps->getAuthor(),
125 0 : XML_NAMESPACE_META, XML_INITIAL_CREATOR );
126 0 : SimpleDateTimeElement( mxDocProps->getCreationDate(),
127 0 : XML_NAMESPACE_META, XML_CREATION_DATE );
128 :
129 : // modified...
130 0 : SimpleStringElement ( mxDocProps->getModifiedBy(),
131 0 : XML_NAMESPACE_DC, XML_CREATOR );
132 0 : SimpleDateTimeElement( mxDocProps->getModificationDate(),
133 0 : XML_NAMESPACE_DC, XML_DATE );
134 :
135 : // printed...
136 0 : SimpleStringElement ( mxDocProps->getPrintedBy(),
137 0 : XML_NAMESPACE_META, XML_PRINTED_BY );
138 0 : SimpleDateTimeElement( mxDocProps->getPrintDate(),
139 0 : XML_NAMESPACE_META, XML_PRINT_DATE );
140 :
141 : // keywords
142 0 : const uno::Sequence< OUString > keywords = mxDocProps->getKeywords();
143 0 : for (sal_Int32 i = 0; i < keywords.getLength(); ++i) {
144 : SvXMLElementExport aKwElem( mrExport, XML_NAMESPACE_META, XML_KEYWORD,
145 0 : sal_True, sal_False );
146 0 : mrExport.Characters( keywords[i] );
147 0 : }
148 :
149 : // document language
150 : {
151 0 : const lang::Locale aLocale = mxDocProps->getLanguage();
152 0 : OUString sValue = aLocale.Language;
153 0 : if (!sValue.isEmpty()) {
154 0 : if ( !aLocale.Country.isEmpty() )
155 : {
156 0 : sValue += OUString::valueOf((sal_Unicode)'-');
157 0 : sValue += aLocale.Country;
158 : }
159 : SvXMLElementExport aElem( mrExport, XML_NAMESPACE_DC, XML_LANGUAGE,
160 0 : sal_True, sal_False );
161 0 : mrExport.Characters( sValue );
162 0 : }
163 : }
164 :
165 : // editing cycles
166 : {
167 : SvXMLElementExport aElem( mrExport,
168 : XML_NAMESPACE_META, XML_EDITING_CYCLES,
169 0 : sal_True, sal_False );
170 : mrExport.Characters( OUString::valueOf(
171 0 : static_cast<sal_Int32>(mxDocProps->getEditingCycles()) ) );
172 : }
173 :
174 : // editing duration
175 : // property is a int32 (seconds)
176 : {
177 0 : sal_Int32 secs = mxDocProps->getEditingDuration();
178 : SvXMLElementExport aElem( mrExport,
179 : XML_NAMESPACE_META, XML_EDITING_DURATION,
180 0 : sal_True, sal_False );
181 0 : OUStringBuffer buf;
182 : ::sax::Converter::convertDuration(buf, util::Duration(
183 0 : false, 0, 0, 0, secs/3600, (secs%3600)/60, secs%60, 0));
184 0 : mrExport.Characters(buf.makeStringAndClear());
185 : }
186 :
187 : // default target
188 0 : const OUString sDefTarget = mxDocProps->getDefaultTarget();
189 0 : if ( !sDefTarget.isEmpty() )
190 : {
191 : mrExport.AddAttribute( XML_NAMESPACE_OFFICE, XML_TARGET_FRAME_NAME,
192 0 : sDefTarget );
193 :
194 : //! define strings for xlink:show values
195 0 : const XMLTokenEnum eShow = sDefTarget == "_blank" ? XML_NEW : XML_REPLACE;
196 0 : mrExport.AddAttribute( XML_NAMESPACE_XLINK, XML_SHOW, eShow );
197 :
198 : SvXMLElementExport aElem( mrExport,
199 : XML_NAMESPACE_META,XML_HYPERLINK_BEHAVIOUR,
200 0 : sal_True, sal_False );
201 : }
202 :
203 : // auto-reload
204 0 : const OUString sReloadURL = mxDocProps->getAutoloadURL();
205 0 : const sal_Int32 sReloadDelay = mxDocProps->getAutoloadSecs();
206 0 : if (sReloadDelay != 0 || !sReloadURL.isEmpty())
207 : {
208 : mrExport.AddAttribute( XML_NAMESPACE_XLINK, XML_HREF,
209 0 : mrExport.GetRelativeReference( sReloadURL ) );
210 :
211 0 : OUStringBuffer buf;
212 : ::sax::Converter::convertDuration(buf, util::Duration(false, 0, 0, 0,
213 0 : sReloadDelay/3600, (sReloadDelay%3600)/60, sReloadDelay%60, 0));
214 : mrExport.AddAttribute( XML_NAMESPACE_META, XML_DELAY,
215 0 : buf.makeStringAndClear());
216 :
217 : SvXMLElementExport aElem( mrExport, XML_NAMESPACE_META, XML_AUTO_RELOAD,
218 0 : sal_True, sal_False );
219 : }
220 :
221 : // template
222 0 : const OUString sTplPath = mxDocProps->getTemplateURL();
223 0 : if ( !sTplPath.isEmpty() )
224 : {
225 0 : mrExport.AddAttribute( XML_NAMESPACE_XLINK, XML_TYPE, XML_SIMPLE );
226 0 : mrExport.AddAttribute( XML_NAMESPACE_XLINK, XML_ACTUATE, XML_ONREQUEST );
227 :
228 : // template URL
229 : mrExport.AddAttribute( XML_NAMESPACE_XLINK, XML_HREF,
230 0 : mrExport.GetRelativeReference(sTplPath) );
231 :
232 : // template name
233 : mrExport.AddAttribute( XML_NAMESPACE_XLINK, XML_TITLE,
234 0 : mxDocProps->getTemplateName() );
235 :
236 : // template date
237 : mrExport.AddAttribute( XML_NAMESPACE_META, XML_DATE,
238 0 : GetISODateTimeString( mxDocProps->getTemplateDate() ) );
239 :
240 : SvXMLElementExport aElem( mrExport, XML_NAMESPACE_META, XML_TEMPLATE,
241 0 : sal_True, sal_False );
242 : }
243 :
244 : // user defined fields
245 : uno::Reference< beans::XPropertyAccess > xUserDefined(
246 0 : mxDocProps->getUserDefinedProperties(), uno::UNO_QUERY_THROW);
247 : const uno::Sequence< beans::PropertyValue > props =
248 0 : xUserDefined->getPropertyValues();
249 0 : for (sal_Int32 i = 0; i < props.getLength(); ++i) {
250 0 : OUStringBuffer sValueBuffer;
251 0 : OUStringBuffer sType;
252 0 : if (!::sax::Converter::convertAny(sValueBuffer, sType, props[i].Value))
253 : {
254 0 : continue;
255 : }
256 0 : mrExport.AddAttribute( XML_NAMESPACE_META, XML_NAME, props[i].Name );
257 : mrExport.AddAttribute( XML_NAMESPACE_META, XML_VALUE_TYPE,
258 0 : sType.makeStringAndClear() );
259 : SvXMLElementExport aElem( mrExport, XML_NAMESPACE_META,
260 0 : XML_USER_DEFINED, sal_True, sal_False );
261 0 : mrExport.Characters( sValueBuffer.makeStringAndClear() );
262 0 : }
263 :
264 : const uno::Sequence< beans::NamedValue > aDocStatistic =
265 0 : mxDocProps->getDocumentStatistics();
266 : // write document statistic if there is any provided
267 0 : if ( aDocStatistic.getLength() )
268 : {
269 0 : for ( sal_Int32 nInd = 0; nInd < aDocStatistic.getLength(); nInd++ )
270 : {
271 0 : sal_Int32 nValue = 0;
272 0 : if ( aDocStatistic[nInd].Value >>= nValue )
273 : {
274 0 : OUString aValue = OUString::valueOf( nValue );
275 0 : if ( aDocStatistic[nInd].Name == "TableCount" )
276 : mrExport.AddAttribute(
277 0 : XML_NAMESPACE_META, XML_TABLE_COUNT, aValue );
278 0 : else if ( aDocStatistic[nInd].Name == "ObjectCount" )
279 : mrExport.AddAttribute(
280 0 : XML_NAMESPACE_META, XML_OBJECT_COUNT, aValue );
281 0 : else if ( aDocStatistic[nInd].Name == "ImageCount" )
282 : mrExport.AddAttribute(
283 0 : XML_NAMESPACE_META, XML_IMAGE_COUNT, aValue );
284 0 : else if ( aDocStatistic[nInd].Name == "PageCount" )
285 : mrExport.AddAttribute(
286 0 : XML_NAMESPACE_META, XML_PAGE_COUNT, aValue );
287 0 : else if ( aDocStatistic[nInd].Name == "ParagraphCount" )
288 : mrExport.AddAttribute(
289 0 : XML_NAMESPACE_META, XML_PARAGRAPH_COUNT, aValue );
290 0 : else if ( aDocStatistic[nInd].Name == "WordCount" )
291 : mrExport.AddAttribute(
292 0 : XML_NAMESPACE_META, XML_WORD_COUNT, aValue );
293 0 : else if ( aDocStatistic[nInd].Name == "CharacterCount" )
294 : mrExport.AddAttribute(
295 0 : XML_NAMESPACE_META, XML_CHARACTER_COUNT, aValue );
296 0 : else if ( aDocStatistic[nInd].Name == "CellCount" )
297 : mrExport.AddAttribute(
298 0 : XML_NAMESPACE_META, XML_CELL_COUNT, aValue );
299 : else
300 : {
301 : DBG_ASSERT( sal_False, "Unknown statistic value!\n" );
302 0 : }
303 : }
304 : }
305 : SvXMLElementExport aElem( mrExport,
306 0 : XML_NAMESPACE_META, XML_DOCUMENT_STATISTIC, sal_True, sal_True );
307 0 : }
308 0 : }
309 :
310 : //-------------------------------------------------------------------------
311 :
312 : static const char *s_xmlns = "xmlns";
313 : static const char *s_xmlns2 = "xmlns:";
314 : static const char *s_meta = "meta:";
315 : static const char *s_href = "xlink:href";
316 :
317 33 : SvXMLMetaExport::SvXMLMetaExport(
318 : SvXMLExport& i_rExp,
319 : const uno::Reference<document::XDocumentProperties>& i_rDocProps ) :
320 : mrExport( i_rExp ),
321 : mxDocProps( i_rDocProps ),
322 : m_level( 0 ),
323 33 : m_preservedNSs()
324 : {
325 : DBG_ASSERT( mxDocProps.is(), "no document properties" );
326 33 : }
327 :
328 66 : SvXMLMetaExport::~SvXMLMetaExport()
329 : {
330 66 : }
331 :
332 33 : void SvXMLMetaExport::Export()
333 : {
334 : uno::Reference< xml::sax::XSAXSerializable> xSAXable(mxDocProps,
335 33 : uno::UNO_QUERY);
336 33 : if (xSAXable.is()) {
337 33 : ::comphelper::SequenceAsVector< beans::StringPair > namespaces;
338 33 : const SvXMLNamespaceMap & rNsMap(mrExport.GetNamespaceMap());
339 316 : for (sal_uInt16 key = rNsMap.GetFirstKey();
340 283 : key != USHRT_MAX; key = rNsMap.GetNextKey(key)) {
341 283 : beans::StringPair ns;
342 566 : const OUString attrname = rNsMap.GetAttrNameByKey(key);
343 283 : if (attrname.matchAsciiL(s_xmlns2, strlen(s_xmlns2))) {
344 283 : ns.First = attrname.copy(strlen(s_xmlns2));
345 0 : } else if (attrname.equalsAsciiL(s_xmlns, strlen(s_xmlns))) {
346 : // default initialized empty string
347 : } else {
348 : OSL_FAIL("namespace attribute not starting with xmlns unexpected");
349 : }
350 283 : ns.Second = rNsMap.GetNameByKey(key);
351 283 : namespaces.push_back(ns);
352 283 : }
353 33 : xSAXable->serialize(this, namespaces.getAsConstList());
354 : } else {
355 : // office:meta
356 : SvXMLElementExport aElem( mrExport, XML_NAMESPACE_OFFICE, XML_META,
357 0 : sal_True, sal_True );
358 : // fall back to using public interface of XDocumentProperties
359 0 : _MExport();
360 33 : }
361 33 : }
362 :
363 : // ::com::sun::star::xml::sax::XDocumentHandler:
364 : void SAL_CALL
365 33 : SvXMLMetaExport::startDocument()
366 : throw (uno::RuntimeException, xml::sax::SAXException)
367 : {
368 : // ignore: has already been done by SvXMLExport::exportDoc
369 : DBG_ASSERT( m_level == 0, "SvXMLMetaExport: level error" );
370 33 : }
371 :
372 : void SAL_CALL
373 33 : SvXMLMetaExport::endDocument()
374 : throw (uno::RuntimeException, xml::sax::SAXException)
375 : {
376 : // ignore: will be done by SvXMLExport::exportDoc
377 : DBG_ASSERT( m_level == 0, "SvXMLMetaExport: level error" );
378 33 : }
379 :
380 : // unfortunately, this method contains far too much ugly namespace mangling.
381 : void SAL_CALL
382 282 : SvXMLMetaExport::startElement(const OUString & i_rName,
383 : const uno::Reference< xml::sax::XAttributeList > & i_xAttribs)
384 : throw (uno::RuntimeException, xml::sax::SAXException)
385 : {
386 :
387 282 : if (m_level == 0) {
388 : // namepace decls: default ones have been written at the root element
389 : // non-default ones must be preserved here
390 33 : const sal_Int16 nCount = i_xAttribs->getLength();
391 351 : for (sal_Int16 i = 0; i < nCount; ++i) {
392 318 : const OUString name(i_xAttribs->getNameByIndex(i));
393 318 : if (name.matchAsciiL(s_xmlns, strlen(s_xmlns))) {
394 283 : bool found(false);
395 283 : const SvXMLNamespaceMap & rNsMap(mrExport.GetNamespaceMap());
396 2304 : for (sal_uInt16 key = rNsMap.GetFirstKey();
397 2021 : key != USHRT_MAX; key = rNsMap.GetNextKey(key)) {
398 2304 : if (name.equals(rNsMap.GetAttrNameByKey(key))) {
399 283 : found = true;
400 283 : break;
401 : }
402 : }
403 283 : if (!found) {
404 : m_preservedNSs.push_back(beans::StringPair(name,
405 0 : i_xAttribs->getValueByIndex(i)));
406 : }
407 : }
408 318 : }
409 : // ignore the root: it has been written already
410 33 : ++m_level;
411 315 : return;
412 : }
413 :
414 249 : if (m_level == 1) {
415 : // attach preserved namespace decls from root node here
416 99 : for (std::vector<beans::StringPair>::const_iterator iter =
417 99 : m_preservedNSs.begin(); iter != m_preservedNSs.end(); ++iter) {
418 0 : const OUString ns(iter->First);
419 0 : bool found(false);
420 : // but only if it is not already there
421 0 : const sal_Int16 nCount = i_xAttribs->getLength();
422 0 : for (sal_Int16 i = 0; i < nCount; ++i) {
423 0 : const OUString name(i_xAttribs->getNameByIndex(i));
424 0 : if (ns.equals(name)) {
425 0 : found = true;
426 0 : break;
427 : }
428 0 : }
429 0 : if (!found) {
430 0 : mrExport.AddAttribute(ns, iter->Second);
431 : }
432 0 : }
433 : }
434 :
435 : // attach the attributes
436 249 : if (i_rName.matchAsciiL(s_meta, strlen(s_meta))) {
437 : // special handling for all elements that may have
438 : // xlink:href attributes; these must be made relative
439 181 : const sal_Int16 nLength = i_xAttribs->getLength();
440 428 : for (sal_Int16 i = 0; i < nLength; ++i) {
441 247 : const OUString name (i_xAttribs->getNameByIndex (i));
442 494 : OUString value(i_xAttribs->getValueByIndex(i));
443 247 : if (name.matchAsciiL(s_href, strlen(s_href))) {
444 7 : value = mrExport.GetRelativeReference(value);
445 : }
446 247 : mrExport.AddAttribute(name, value);
447 247 : }
448 : } else {
449 68 : const sal_Int16 nLength = i_xAttribs->getLength();
450 71 : for (sal_Int16 i = 0; i < nLength; ++i) {
451 3 : const OUString name (i_xAttribs->getNameByIndex(i));
452 6 : const OUString value (i_xAttribs->getValueByIndex(i));
453 3 : mrExport.AddAttribute(name, value);
454 3 : }
455 : }
456 :
457 : // finally, start the element
458 : // #i107240# no whitespace here, because the DOM may already contain
459 : // whitespace, which is not cleared when loading and thus accumulates.
460 249 : mrExport.StartElement(i_rName, (m_level > 1) ? sal_False : sal_True);
461 249 : ++m_level;
462 : }
463 :
464 : void SAL_CALL
465 282 : SvXMLMetaExport::endElement(const OUString & i_rName)
466 : throw (uno::RuntimeException, xml::sax::SAXException)
467 : {
468 282 : --m_level;
469 282 : if (m_level == 0) {
470 : // ignore the root; see startElement
471 315 : return;
472 : }
473 : DBG_ASSERT( m_level >= 0, "SvXMLMetaExport: level error" );
474 249 : mrExport.EndElement(i_rName, sal_False);
475 : }
476 :
477 : void SAL_CALL
478 175 : SvXMLMetaExport::characters(const OUString & i_rChars)
479 : throw (uno::RuntimeException, xml::sax::SAXException)
480 : {
481 175 : mrExport.Characters(i_rChars);
482 175 : }
483 :
484 : void SAL_CALL
485 0 : SvXMLMetaExport::ignorableWhitespace(const OUString & /*i_rWhitespaces*/)
486 : throw (uno::RuntimeException, xml::sax::SAXException)
487 : {
488 0 : mrExport.IgnorableWhitespace(/*i_rWhitespaces*/);
489 0 : }
490 :
491 : void SAL_CALL
492 0 : SvXMLMetaExport::processingInstruction(const OUString & i_rTarget,
493 : const OUString & i_rData)
494 : throw (uno::RuntimeException, xml::sax::SAXException)
495 : {
496 : // ignore; the exporter cannot handle these
497 : (void) i_rTarget;
498 : (void) i_rData;
499 0 : }
500 :
501 : void SAL_CALL
502 0 : SvXMLMetaExport::setDocumentLocator(const uno::Reference<xml::sax::XLocator>&)
503 : throw (uno::RuntimeException, xml::sax::SAXException)
504 : {
505 : // nothing to do here, move along...
506 0 : }
507 :
508 :
509 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|