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