Files
libreoffice/xmloff/source/meta/xmlmetai.cxx

278 lines
9.5 KiB
C++
Raw Normal View History

2000-09-18 16:07:07 +00:00
/*************************************************************************
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2000-09-18 16:07:07 +00:00
*
* Copyright 2000, 2010 Oracle and/or its affiliates.
2000-09-18 16:07:07 +00:00
*
* OpenOffice.org - a multi-platform office productivity suite
2000-09-18 16:07:07 +00:00
*
* This file is part of OpenOffice.org.
2000-09-18 16:07:07 +00:00
*
* OpenOffice.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
2000-09-18 16:07:07 +00:00
*
* OpenOffice.org is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 3 for more details
* (a copy is included in the LICENSE file that accompanied this code).
2000-09-18 16:07:07 +00:00
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with OpenOffice.org. If not, see
* <http://www.openoffice.org/license.html>
* for a copy of the LGPLv3 License.
2000-09-18 16:07:07 +00:00
*
************************************************************************/
// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_xmloff.hxx"
#include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
#include <com/sun/star/xml/dom/XSAXDocumentBuilder.hpp>
#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/beans/XPropertySetInfo.hpp>
#include <tools/debug.hxx>
#include <xmloff/xmlmetai.hxx>
#include <xmloff/xmlimp.hxx>
#include <xmloff/nmspmap.hxx>
#include <xmloff/xmltoken.hxx>
#include "xmlnmspe.hxx"
using ::rtl::OUString;
using ::rtl::OUStringBuffer;
2000-09-18 16:07:07 +00:00
using namespace com::sun::star;
using namespace ::xmloff::token;
2000-09-18 16:07:07 +00:00
//===========================================================================
/// builds a DOM tree from SAX events, by forwarding to SAXDocumentBuilder
class XMLDocumentBuilderContext : public SvXMLImportContext
2000-09-18 16:07:07 +00:00
{
private:
::com::sun::star::uno::Reference<
::com::sun::star::xml::sax::XDocumentHandler> mxDocBuilder;
2000-09-18 16:07:07 +00:00
public:
XMLDocumentBuilderContext(SvXMLImport& rImport, USHORT nPrfx,
const ::rtl::OUString& rLName,
const ::com::sun::star::uno::Reference<
::com::sun::star::xml::sax::XAttributeList>& xAttrList,
const ::com::sun::star::uno::Reference<
::com::sun::star::xml::sax::XDocumentHandler>& rDocBuilder);
2000-09-18 16:07:07 +00:00
virtual ~XMLDocumentBuilderContext();
2000-09-18 16:07:07 +00:00
virtual SvXMLImportContext *CreateChildContext( USHORT nPrefix,
const rtl::OUString& rLocalName,
const ::com::sun::star::uno::Reference<
::com::sun::star::xml::sax::XAttributeList>& xAttrList );
2000-09-18 16:07:07 +00:00
virtual void StartElement( const ::com::sun::star::uno::Reference<
::com::sun::star::xml::sax::XAttributeList >& xAttrList );
2000-09-18 16:07:07 +00:00
virtual void Characters( const ::rtl::OUString& rChars );
2000-09-18 16:07:07 +00:00
virtual void EndElement();
2000-09-18 16:07:07 +00:00
};
XMLDocumentBuilderContext::XMLDocumentBuilderContext(SvXMLImport& rImport,
USHORT nPrfx, const ::rtl::OUString& rLName,
const uno::Reference<xml::sax::XAttributeList>&,
const uno::Reference<xml::sax::XDocumentHandler>& rDocBuilder) :
SvXMLImportContext( rImport, nPrfx, rLName ),
mxDocBuilder(rDocBuilder)
2000-09-18 16:07:07 +00:00
{
}
XMLDocumentBuilderContext::~XMLDocumentBuilderContext()
2000-09-18 16:07:07 +00:00
{
}
SvXMLImportContext *
XMLDocumentBuilderContext::CreateChildContext( USHORT nPrefix,
const rtl::OUString& rLocalName,
const uno::Reference< xml::sax::XAttributeList>& rAttrs)
2000-09-18 16:07:07 +00:00
{
return new XMLDocumentBuilderContext(
GetImport(), nPrefix, rLocalName, rAttrs, mxDocBuilder);
2000-09-18 16:07:07 +00:00
}
void XMLDocumentBuilderContext::StartElement(
const uno::Reference< xml::sax::XAttributeList >& xAttrList )
2000-09-18 16:07:07 +00:00
{
mxDocBuilder->startElement(
GetImport().GetNamespaceMap().GetQNameByKey(GetPrefix(), GetLocalName()),
xAttrList);
2000-09-18 16:07:07 +00:00
}
void XMLDocumentBuilderContext::Characters( const ::rtl::OUString& rChars )
2000-09-18 16:07:07 +00:00
{
mxDocBuilder->characters(rChars);
2000-09-18 16:07:07 +00:00
}
void XMLDocumentBuilderContext::EndElement()
2000-09-18 16:07:07 +00:00
{
mxDocBuilder->endElement(
GetImport().GetNamespaceMap().GetQNameByKey(GetPrefix(), GetLocalName()));
}
//===========================================================================
SvXMLMetaDocumentContext::SvXMLMetaDocumentContext(SvXMLImport& rImport,
USHORT nPrfx, const rtl::OUString& rLName,
const uno::Reference<document::XDocumentProperties>& xDocProps,
const uno::Reference<xml::sax::XDocumentHandler>& xDocBuilder) :
SvXMLImportContext( rImport, nPrfx, rLName ),
mxDocProps(xDocProps),
mxDocBuilder(xDocBuilder)
{
DBG_ASSERT(xDocProps.is(), "SvXMLMetaDocumentContext: no document props");
DBG_ASSERT(xDocBuilder.is(), "SvXMLMetaDocumentContext: no document hdlr");
// here are no attributes
2000-09-18 16:07:07 +00:00
}
SvXMLMetaDocumentContext::~SvXMLMetaDocumentContext()
2000-09-18 16:07:07 +00:00
{
}
SvXMLImportContext *SvXMLMetaDocumentContext::CreateChildContext(
USHORT nPrefix, const rtl::OUString& rLocalName,
const uno::Reference<xml::sax::XAttributeList>& rAttrs)
2000-09-18 16:07:07 +00:00
{
if ( (XML_NAMESPACE_OFFICE == nPrefix) &&
IsXMLToken(rLocalName, XML_META) )
2000-09-18 16:07:07 +00:00
{
return new XMLDocumentBuilderContext(
GetImport(), nPrefix, rLocalName, rAttrs, mxDocBuilder);
}
else
{
return new SvXMLImportContext( GetImport(), nPrefix, rLocalName );
2000-09-18 16:07:07 +00:00
}
}
void SvXMLMetaDocumentContext::StartElement(
const uno::Reference< xml::sax::XAttributeList >& xAttrList )
{
mxDocBuilder->startDocument();
// hardcode office:document-meta (necessary in case of flat file ODF)
mxDocBuilder->startElement(
GetImport().GetNamespaceMap().GetQNameByKey(GetPrefix(),
GetXMLToken(XML_DOCUMENT_META)), xAttrList);
}
void SvXMLMetaDocumentContext::EndElement()
2000-09-18 16:07:07 +00:00
{
// hardcode office:document-meta (necessary in case of flat file ODF)
mxDocBuilder->endElement(
GetImport().GetNamespaceMap().GetQNameByKey(GetPrefix(),
GetXMLToken(XML_DOCUMENT_META)));
mxDocBuilder->endDocument();
initDocumentProperties();
2000-09-18 16:07:07 +00:00
}
void SvXMLMetaDocumentContext::initDocumentProperties()
2000-09-18 16:07:07 +00:00
{
uno::Sequence< uno::Any > aSeq(1);
uno::Reference< xml::dom::XSAXDocumentBuilder > xDB (mxDocBuilder,
uno::UNO_QUERY_THROW);
aSeq[0] <<= xDB->getDocument();
uno::Reference< lang::XInitialization > xInit(mxDocProps,
uno::UNO_QUERY_THROW);
try {
xInit->initialize(aSeq);
GetImport().SetStatistics(mxDocProps->getDocumentStatistics());
// convert all URLs from relative to absolute
mxDocProps->setTemplateURL(GetImport().GetAbsoluteReference(
mxDocProps->getTemplateURL()));
mxDocProps->setAutoloadURL(GetImport().GetAbsoluteReference(
mxDocProps->getAutoloadURL()));
setBuildId(mxDocProps->getGenerator());
} catch (uno::RuntimeException) {
throw;
} catch (uno::Exception & e) {
throw lang::WrappedTargetRuntimeException(
::rtl::OUString::createFromAscii(
"SvXMLMetaDocumentContext::initDocumentProperties: "
"properties init exception"),
GetImport(), makeAny(e));
2000-09-18 16:07:07 +00:00
}
}
void SvXMLMetaDocumentContext::setBuildId(const ::rtl::OUString & i_rBuildId)
{
SvXMLMetaDocumentContext::setBuildId( i_rBuildId, GetImport().getImportInfo() );
}
//static
void SvXMLMetaDocumentContext::setBuildId(::rtl::OUString const& i_rBuildId, const uno::Reference<beans::XPropertySet>& xImportInfo )
2000-09-18 16:07:07 +00:00
{
OUString sBuildId;
// skip to second product
sal_Int32 nBegin = i_rBuildId.indexOf( ' ' );
if ( nBegin != -1 )
2000-09-18 16:07:07 +00:00
{
// skip to build information
nBegin = i_rBuildId.indexOf( '/', nBegin );
if ( nBegin != -1 )
{
sal_Int32 nEnd = i_rBuildId.indexOf( 'm', nBegin );
if ( nEnd != -1 )
{
OUStringBuffer sBuffer(
i_rBuildId.copy( nBegin+1, nEnd-nBegin-1 ) );
const OUString sBuildCompare(
RTL_CONSTASCII_USTRINGPARAM( "$Build-" ) );
nBegin = i_rBuildId.indexOf( sBuildCompare, nEnd );
if ( nBegin != -1 )
{
sBuffer.append( (sal_Unicode)'$' );
sBuffer.append( i_rBuildId.copy(
nBegin + sBuildCompare.getLength() ) );
sBuildId = sBuffer.makeStringAndClear();
}
}
}
2000-09-18 16:07:07 +00:00
}
if ( sBuildId.getLength() == 0 )
2000-09-18 16:07:07 +00:00
{
if ((i_rBuildId.compareToAscii(
RTL_CONSTASCII_STRINGPARAM("StarOffice 7") ) == 0) ||
(i_rBuildId.compareToAscii(
RTL_CONSTASCII_STRINGPARAM("StarSuite 7") ) == 0) ||
(i_rBuildId.compareToAscii(
RTL_CONSTASCII_STRINGPARAM("OpenOffice.org 1") ) == 0))
{
sBuildId = OUString::createFromAscii( "645$8687" );
}
if ((i_rBuildId.compareToAscii( RTL_CONSTASCII_STRINGPARAM("NeoOffice/2") ) == 0) )
{
sBuildId = OUString::createFromAscii( "680$9134" ); // fake NeoOffice as OpenOffice.org 2.2 release
}
2000-09-18 16:07:07 +00:00
}
if ( sBuildId.getLength() ) try
2000-09-18 16:07:07 +00:00
{
if( xImportInfo.is() )
{
const OUString aPropName(RTL_CONSTASCII_USTRINGPARAM("BuildId"));
uno::Reference< beans::XPropertySetInfo > xSetInfo(
xImportInfo->getPropertySetInfo());
if( xSetInfo.is() && xSetInfo->hasPropertyByName( aPropName ) )
xImportInfo->setPropertyValue( aPropName, uno::makeAny( sBuildId ) );
}
2000-09-18 16:07:07 +00:00
}
catch( uno::Exception& )
{
}
}
2000-09-18 16:07:07 +00:00