1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: xmlmetai.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_xmloff.hxx"
34 #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
35 #include <com/sun/star/xml/dom/XSAXDocumentBuilder.hpp>
36 #include <com/sun/star/beans/XPropertySet.hpp>
37 #include <com/sun/star/beans/XPropertySetInfo.hpp>
39 #include <tools/debug.hxx>
41 #include <xmloff/xmlmetai.hxx>
42 #include <xmloff/xmlimp.hxx>
43 #include <xmloff/nmspmap.hxx>
44 #include <xmloff/xmltoken.hxx>
45 #include "xmlnmspe.hxx"
48 using ::rtl::OUString
;
49 using ::rtl::OUStringBuffer
;
50 using namespace com::sun::star
;
51 using namespace ::xmloff::token
;
54 //===========================================================================
56 /// builds a DOM tree from SAX events, by forwarding to SAXDocumentBuilder
57 class XMLDocumentBuilderContext
: public SvXMLImportContext
60 ::com::sun::star::uno::Reference
<
61 ::com::sun::star::xml::sax::XDocumentHandler
> mxDocBuilder
;
64 XMLDocumentBuilderContext(SvXMLImport
& rImport
, USHORT nPrfx
,
65 const ::rtl::OUString
& rLName
,
66 const ::com::sun::star::uno::Reference
<
67 ::com::sun::star::xml::sax::XAttributeList
>& xAttrList
,
68 const ::com::sun::star::uno::Reference
<
69 ::com::sun::star::xml::sax::XDocumentHandler
>& rDocBuilder
);
71 virtual ~XMLDocumentBuilderContext();
73 virtual SvXMLImportContext
*CreateChildContext( USHORT nPrefix
,
74 const rtl::OUString
& rLocalName
,
75 const ::com::sun::star::uno::Reference
<
76 ::com::sun::star::xml::sax::XAttributeList
>& xAttrList
);
78 virtual void StartElement( const ::com::sun::star::uno::Reference
<
79 ::com::sun::star::xml::sax::XAttributeList
>& xAttrList
);
81 virtual void Characters( const ::rtl::OUString
& rChars
);
83 virtual void EndElement();
86 XMLDocumentBuilderContext::XMLDocumentBuilderContext(SvXMLImport
& rImport
,
87 USHORT nPrfx
, const ::rtl::OUString
& rLName
,
88 const uno::Reference
<xml::sax::XAttributeList
>&,
89 const uno::Reference
<xml::sax::XDocumentHandler
>& rDocBuilder
) :
90 SvXMLImportContext( rImport
, nPrfx
, rLName
),
91 mxDocBuilder(rDocBuilder
)
95 XMLDocumentBuilderContext::~XMLDocumentBuilderContext()
100 XMLDocumentBuilderContext::CreateChildContext( USHORT nPrefix
,
101 const rtl::OUString
& rLocalName
,
102 const uno::Reference
< xml::sax::XAttributeList
>& rAttrs
)
104 return new XMLDocumentBuilderContext(
105 GetImport(), nPrefix
, rLocalName
, rAttrs
, mxDocBuilder
);
108 void XMLDocumentBuilderContext::StartElement(
109 const uno::Reference
< xml::sax::XAttributeList
>& xAttrList
)
111 mxDocBuilder
->startElement(
112 GetImport().GetNamespaceMap().GetQNameByKey(GetPrefix(), GetLocalName()),
116 void XMLDocumentBuilderContext::Characters( const ::rtl::OUString
& rChars
)
118 mxDocBuilder
->characters(rChars
);
121 void XMLDocumentBuilderContext::EndElement()
123 mxDocBuilder
->endElement(
124 GetImport().GetNamespaceMap().GetQNameByKey(GetPrefix(), GetLocalName()));
128 //===========================================================================
130 SvXMLMetaDocumentContext::SvXMLMetaDocumentContext(SvXMLImport
& rImport
,
131 USHORT nPrfx
, const rtl::OUString
& rLName
,
132 const uno::Reference
<document::XDocumentProperties
>& xDocProps
,
133 const uno::Reference
<xml::sax::XDocumentHandler
>& xDocBuilder
) :
134 SvXMLImportContext( rImport
, nPrfx
, rLName
),
135 mxDocProps(xDocProps
),
136 mxDocBuilder(xDocBuilder
)
138 DBG_ASSERT(xDocProps
.is(), "SvXMLMetaDocumentContext: no document props");
139 DBG_ASSERT(xDocBuilder
.is(), "SvXMLMetaDocumentContext: no document hdlr");
140 // here are no attributes
143 SvXMLMetaDocumentContext::~SvXMLMetaDocumentContext()
147 SvXMLImportContext
*SvXMLMetaDocumentContext::CreateChildContext(
148 USHORT nPrefix
, const rtl::OUString
& rLocalName
,
149 const uno::Reference
<xml::sax::XAttributeList
>& rAttrs
)
151 if ( (XML_NAMESPACE_OFFICE
== nPrefix
) &&
152 IsXMLToken(rLocalName
, XML_META
) )
154 return new XMLDocumentBuilderContext(
155 GetImport(), nPrefix
, rLocalName
, rAttrs
, mxDocBuilder
);
159 return new SvXMLImportContext( GetImport(), nPrefix
, rLocalName
);
164 void SvXMLMetaDocumentContext::StartElement(
165 const uno::Reference
< xml::sax::XAttributeList
>& xAttrList
)
167 mxDocBuilder
->startDocument();
168 // hardcode office:document-meta (necessary in case of flat file ODF)
169 mxDocBuilder
->startElement(
170 GetImport().GetNamespaceMap().GetQNameByKey(GetPrefix(),
171 GetXMLToken(XML_DOCUMENT_META
)), xAttrList
);
174 void SvXMLMetaDocumentContext::EndElement()
176 // hardcode office:document-meta (necessary in case of flat file ODF)
177 mxDocBuilder
->endElement(
178 GetImport().GetNamespaceMap().GetQNameByKey(GetPrefix(),
179 GetXMLToken(XML_DOCUMENT_META
)));
180 mxDocBuilder
->endDocument();
181 initDocumentProperties();
184 void SvXMLMetaDocumentContext::initDocumentProperties()
186 uno::Sequence
< uno::Any
> aSeq(1);
187 uno::Reference
< xml::dom::XSAXDocumentBuilder
> xDB (mxDocBuilder
,
188 uno::UNO_QUERY_THROW
);
189 aSeq
[0] <<= xDB
->getDocument();
190 uno::Reference
< lang::XInitialization
> xInit(mxDocProps
,
191 uno::UNO_QUERY_THROW
);
193 xInit
->initialize(aSeq
);
194 GetImport().SetStatistics(mxDocProps
->getDocumentStatistics());
195 // convert all URLs from relative to absolute
196 mxDocProps
->setTemplateURL(GetImport().GetAbsoluteReference(
197 mxDocProps
->getTemplateURL()));
198 mxDocProps
->setAutoloadURL(GetImport().GetAbsoluteReference(
199 mxDocProps
->getAutoloadURL()));
200 setBuildId(mxDocProps
->getGenerator());
201 } catch (uno::RuntimeException
) {
203 } catch (uno::Exception
& e
) {
204 throw lang::WrappedTargetRuntimeException(
205 ::rtl::OUString::createFromAscii(
206 "SvXMLMetaDocumentContext::initDocumentProperties: "
207 "properties init exception"),
208 GetImport(), makeAny(e
));
212 void SvXMLMetaDocumentContext::setBuildId(const ::rtl::OUString
& i_rBuildId
)
214 SvXMLMetaDocumentContext::setBuildId( i_rBuildId
, GetImport().getImportInfo() );
218 void SvXMLMetaDocumentContext::setBuildId(::rtl::OUString
const& i_rBuildId
, const uno::Reference
<beans::XPropertySet
>& xImportInfo
)
221 // skip to second product
222 sal_Int32 nBegin
= i_rBuildId
.indexOf( ' ' );
225 // skip to build information
226 nBegin
= i_rBuildId
.indexOf( '/', nBegin
);
229 sal_Int32 nEnd
= i_rBuildId
.indexOf( 'm', nBegin
);
232 OUStringBuffer
sBuffer(
233 i_rBuildId
.copy( nBegin
+1, nEnd
-nBegin
-1 ) );
234 const OUString
sBuildCompare(
235 RTL_CONSTASCII_USTRINGPARAM( "$Build-" ) );
236 nBegin
= i_rBuildId
.indexOf( sBuildCompare
, nEnd
);
239 sBuffer
.append( (sal_Unicode
)'$' );
240 sBuffer
.append( i_rBuildId
.copy(
241 nBegin
+ sBuildCompare
.getLength() ) );
242 sBuildId
= sBuffer
.makeStringAndClear();
248 if ( sBuildId
.getLength() == 0 )
250 if ((i_rBuildId
.compareToAscii(
251 RTL_CONSTASCII_STRINGPARAM("StarOffice 7") ) == 0) ||
252 (i_rBuildId
.compareToAscii(
253 RTL_CONSTASCII_STRINGPARAM("StarSuite 7") ) == 0) ||
254 (i_rBuildId
.compareToAscii(
255 RTL_CONSTASCII_STRINGPARAM("OpenOffice.org 1") ) == 0))
257 sBuildId
= OUString::createFromAscii( "645$8687" );
259 if ((i_rBuildId
.compareToAscii( RTL_CONSTASCII_STRINGPARAM("NeoOffice/2") ) == 0) )
261 sBuildId
= OUString::createFromAscii( "680$9134" ); // fake NeoOffice as OpenOffice.org 2.2 release
265 if ( sBuildId
.getLength() ) try
267 if( xImportInfo
.is() )
269 const OUString
aPropName(RTL_CONSTASCII_USTRINGPARAM("BuildId"));
270 uno::Reference
< beans::XPropertySetInfo
> xSetInfo(
271 xImportInfo
->getPropertySetInfo());
272 if( xSetInfo
.is() && xSetInfo
->hasPropertyByName( aPropName
) )
273 xImportInfo
->setPropertyValue( aPropName
, uno::makeAny( sBuildId
) );
276 catch( uno::Exception
& )