diff options
Diffstat (limited to 'sw/source/core/swg')
-rw-r--r-- | sw/source/core/swg/SwXMLBlockExport.cxx | 145 | ||||
-rw-r--r-- | sw/source/core/swg/SwXMLBlockImport.cxx | 119 | ||||
-rw-r--r-- | sw/source/core/swg/SwXMLBlockListContext.cxx | 253 | ||||
-rw-r--r-- | sw/source/core/swg/SwXMLSectionList.cxx | 157 | ||||
-rw-r--r-- | sw/source/core/swg/SwXMLTextBlocks.cxx | 644 | ||||
-rw-r--r-- | sw/source/core/swg/SwXMLTextBlocks1.cxx | 738 | ||||
-rw-r--r-- | sw/source/core/swg/makefile.mk | 62 | ||||
-rw-r--r-- | sw/source/core/swg/swblocks.cxx | 814 |
8 files changed, 2932 insertions, 0 deletions
diff --git a/sw/source/core/swg/SwXMLBlockExport.cxx b/sw/source/core/swg/SwXMLBlockExport.cxx new file mode 100644 index 000000000000..f27ccc3e9c76 --- /dev/null +++ b/sw/source/core/swg/SwXMLBlockExport.cxx @@ -0,0 +1,145 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * 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. + * + * 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). + * + * 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. + * + ************************************************************************/ + +// MARKER(update_precomp.py): autogen include statement, do not remove +#include "precompiled_sw.hxx" +#include <SwXMLBlockExport.hxx> +#include <SwXMLTextBlocks.hxx> +#include <xmloff/nmspmap.hxx> +#include <xmloff/xmlnmspe.hxx> +using namespace ::com::sun::star::uno; +using namespace ::com::sun::star; +using namespace ::xmloff::token; +using ::rtl::OUString; + +// #110680# +SwXMLBlockListExport::SwXMLBlockListExport( + const uno::Reference< lang::XMultiServiceFactory > xServiceFactory, + SwXMLTextBlocks & rBlocks, + const rtl::OUString &rFileName, + uno::Reference< xml::sax::XDocumentHandler> &rHandler) +: SvXMLExport( xServiceFactory, rFileName, rHandler ), + rBlockList(rBlocks) +{ + _GetNamespaceMap().Add( GetXMLToken ( XML_NP_BLOCK_LIST ), + GetXMLToken ( XML_N_BLOCK_LIST ), + XML_NAMESPACE_BLOCKLIST ); +} + +sal_uInt32 SwXMLBlockListExport::exportDoc(enum XMLTokenEnum ) +{ + GetDocHandler()->startDocument(); + + AddAttribute ( XML_NAMESPACE_NONE, + _GetNamespaceMap().GetAttrNameByKey ( XML_NAMESPACE_BLOCKLIST ), + _GetNamespaceMap().GetNameByKey ( XML_NAMESPACE_BLOCKLIST ) ); + AddAttribute( XML_NAMESPACE_BLOCKLIST, + XML_LIST_NAME, + OUString (rBlockList.GetName())); + { + SvXMLElementExport pRoot (*this, XML_NAMESPACE_BLOCKLIST, XML_BLOCK_LIST, sal_True, sal_True); + sal_uInt16 nBlocks= rBlockList.GetCount(); + for ( sal_uInt16 i = 0; i < nBlocks; i++) + { + AddAttribute( XML_NAMESPACE_BLOCKLIST, + XML_ABBREVIATED_NAME, + OUString(rBlockList.GetShortName(i))); + AddAttribute( XML_NAMESPACE_BLOCKLIST, + XML_PACKAGE_NAME, + OUString(rBlockList.GetPackageName(i))); + AddAttribute( XML_NAMESPACE_BLOCKLIST, + XML_NAME, + OUString(rBlockList.GetLongName(i))); + AddAttribute( XML_NAMESPACE_BLOCKLIST, + XML_UNFORMATTED_TEXT, + rBlockList.IsOnlyTextBlock(i) ? XML_TRUE : XML_FALSE ); + + SvXMLElementExport aBlock( *this, XML_NAMESPACE_BLOCKLIST, XML_BLOCK, sal_True, sal_True); + } + } + GetDocHandler()->endDocument(); + return 0; +} + +// #110680# +SwXMLTextBlockExport::SwXMLTextBlockExport( + const uno::Reference< lang::XMultiServiceFactory > xServiceFactory, + SwXMLTextBlocks & rBlocks, + const rtl::OUString &rFileName, + uno::Reference< xml::sax::XDocumentHandler> &rHandler) +: SvXMLExport( xServiceFactory, rFileName, rHandler ), + rBlockList(rBlocks) +{ + _GetNamespaceMap().Add( GetXMLToken ( XML_NP_BLOCK_LIST ), + GetXMLToken ( XML_N_BLOCK_LIST ), + XML_NAMESPACE_BLOCKLIST ); + _GetNamespaceMap().Add( GetXMLToken ( XML_NP_OFFICE ), + GetXMLToken(XML_N_OFFICE_OOO), + XML_NAMESPACE_OFFICE ); + _GetNamespaceMap().Add( GetXMLToken ( XML_NP_TEXT ), + GetXMLToken(XML_N_TEXT_OOO), + XML_NAMESPACE_TEXT ); +} + +sal_uInt32 SwXMLTextBlockExport::exportDoc(const String &rText) +{ + GetDocHandler()->startDocument(); + + AddAttribute ( XML_NAMESPACE_NONE, + _GetNamespaceMap().GetAttrNameByKey ( XML_NAMESPACE_BLOCKLIST ), + _GetNamespaceMap().GetNameByKey ( XML_NAMESPACE_BLOCKLIST ) ); + AddAttribute ( XML_NAMESPACE_NONE, + _GetNamespaceMap().GetAttrNameByKey ( XML_NAMESPACE_TEXT ), + _GetNamespaceMap().GetNameByKey ( XML_NAMESPACE_TEXT ) ); + AddAttribute ( XML_NAMESPACE_NONE, + _GetNamespaceMap().GetAttrNameByKey ( XML_NAMESPACE_OFFICE ), + _GetNamespaceMap().GetNameByKey ( XML_NAMESPACE_OFFICE ) ); + AddAttribute( XML_NAMESPACE_BLOCKLIST, + XML_LIST_NAME, + OUString (rBlockList.GetName())); + { + SvXMLElementExport aDocument (*this, XML_NAMESPACE_OFFICE, XML_DOCUMENT, sal_True, sal_True); + { + SvXMLElementExport aBody (*this, XML_NAMESPACE_OFFICE, XML_BODY, sal_True, sal_True); + { + xub_StrLen nPos = 0; + do + { + String sTemp ( rText.GetToken( 0, '\015', nPos ) ); + SvXMLElementExport aPara (*this, XML_NAMESPACE_TEXT, XML_P, sal_True, sal_False); + GetDocHandler()->characters(sTemp); + } while (STRING_NOTFOUND != nPos ); + } + + } + } + GetDocHandler()->endDocument(); + return 0; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sw/source/core/swg/SwXMLBlockImport.cxx b/sw/source/core/swg/SwXMLBlockImport.cxx new file mode 100644 index 000000000000..b3e60d2df91d --- /dev/null +++ b/sw/source/core/swg/SwXMLBlockImport.cxx @@ -0,0 +1,119 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * 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. + * + * 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). + * + * 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. + * + ************************************************************************/ + +// MARKER(update_precomp.py): autogen include statement, do not remove +#include "precompiled_sw.hxx" +#include <SwXMLBlockImport.hxx> +#include <xmloff/nmspmap.hxx> +#include <xmloff/xmlnmspe.hxx> +#include <SwXMLBlockListContext.hxx> +#include <SwXMLTextBlocks.hxx> +using namespace ::com::sun::star::uno; +using namespace ::com::sun::star; +using namespace ::xmloff::token; +using ::rtl::OUString; + +sal_Char __READONLY_DATA sXML_np__block_list[] = "_block-list"; +sal_Char __READONLY_DATA sXML_np__office[] = "_ooffice"; +sal_Char __READONLY_DATA sXML_np__text[] = "_otext"; + +// #110680# +SwXMLBlockListImport::SwXMLBlockListImport( + const uno::Reference< lang::XMultiServiceFactory > xServiceFactory, + SwXMLTextBlocks &rBlocks ) +: SvXMLImport( xServiceFactory, 0 ), + rBlockList (rBlocks) +{ + GetNamespaceMap().Add( OUString ( RTL_CONSTASCII_USTRINGPARAM ( sXML_np__block_list ) ), + GetXMLToken ( XML_N_BLOCK_LIST ), + XML_NAMESPACE_BLOCKLIST ); +} + +SwXMLBlockListImport::~SwXMLBlockListImport ( void ) + throw () +{ +} + +SvXMLImportContext *SwXMLBlockListImport::CreateContext( + sal_uInt16 nPrefix, + const OUString& rLocalName, + const uno::Reference< xml::sax::XAttributeList > & xAttrList ) +{ + SvXMLImportContext *pContext = 0; + if ( XML_NAMESPACE_BLOCKLIST == nPrefix && + IsXMLToken ( rLocalName, XML_BLOCK_LIST ) ) + pContext = new SwXMLBlockListContext( *this, nPrefix, rLocalName, + xAttrList ); + else + pContext = SvXMLImport::CreateContext( nPrefix, rLocalName, xAttrList ); + return pContext; +} + +// #110680# +SwXMLTextBlockImport::SwXMLTextBlockImport( + const uno::Reference< lang::XMultiServiceFactory > xServiceFactory, + SwXMLTextBlocks &rBlocks, + String & rNewText, + sal_Bool bNewTextOnly ) +: SvXMLImport(xServiceFactory, IMPORT_ALL ), + rBlockList ( rBlocks ), + bTextOnly ( bNewTextOnly ), + m_rText ( rNewText ) +{ + GetNamespaceMap().Add( OUString( RTL_CONSTASCII_USTRINGPARAM ( sXML_np__office ) ), + GetXMLToken(XML_N_OFFICE_OOO), + XML_NAMESPACE_OFFICE ); + GetNamespaceMap().Add( OUString( RTL_CONSTASCII_USTRINGPARAM ( sXML_np__text ) ), + GetXMLToken(XML_N_TEXT_OOO), + XML_NAMESPACE_TEXT ); +} + +SwXMLTextBlockImport::~SwXMLTextBlockImport ( void ) + throw() +{ +} + +SvXMLImportContext *SwXMLTextBlockImport::CreateContext( + sal_uInt16 nPrefix, + const OUString& rLocalName, + const uno::Reference< xml::sax::XAttributeList > & xAttrList ) +{ + SvXMLImportContext *pContext = 0; + if( XML_NAMESPACE_OFFICE == nPrefix && + IsXMLToken ( rLocalName, bTextOnly ? XML_DOCUMENT : XML_DOCUMENT_CONTENT ) ) + pContext = new SwXMLTextBlockDocumentContext( *this, nPrefix, rLocalName, xAttrList ); + else + pContext = SvXMLImport::CreateContext( nPrefix, rLocalName, xAttrList ); + return pContext; +} +void SAL_CALL SwXMLTextBlockImport::endDocument(void) + throw( xml::sax::SAXException, uno::RuntimeException ) +{ +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sw/source/core/swg/SwXMLBlockListContext.cxx b/sw/source/core/swg/SwXMLBlockListContext.cxx new file mode 100644 index 000000000000..7cc1804f52f4 --- /dev/null +++ b/sw/source/core/swg/SwXMLBlockListContext.cxx @@ -0,0 +1,253 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * 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. + * + * 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). + * + * 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. + * + ************************************************************************/ + +// MARKER(update_precomp.py): autogen include statement, do not remove +#include "precompiled_sw.hxx" +#include <SwXMLBlockListContext.hxx> +#include <SwXMLBlockImport.hxx> +#include <SwXMLTextBlocks.hxx> +#include <xmloff/nmspmap.hxx> +#include <xmloff/xmlnmspe.hxx> +#include <unotools/charclass.hxx> +#include <swtypes.hxx> + +using namespace ::com::sun::star::uno; +using namespace ::com::sun::star; +using namespace ::xmloff::token; +using ::rtl::OUString; + +SwXMLBlockListContext::SwXMLBlockListContext( + SwXMLBlockListImport& rImport, + sal_uInt16 nPrefix, + const OUString& rLocalName, + const uno::Reference< + xml::sax::XAttributeList > & xAttrList ) : + SvXMLImportContext ( rImport, nPrefix, rLocalName ), + rLocalRef (rImport) +{ + sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0; + for (sal_Int16 i=0; i < nAttrCount; i++) + { + const OUString& rAttrName = xAttrList->getNameByIndex( i ); + OUString aLocalName; + sal_uInt16 nPrefx = rImport.GetNamespaceMap().GetKeyByAttrName( rAttrName, &aLocalName); + const OUString& rAttrValue = xAttrList->getValueByIndex( i ); + if ( XML_NAMESPACE_BLOCKLIST == nPrefx ) + { + if ( IsXMLToken ( aLocalName, XML_LIST_NAME ) ) + { + rImport.getBlockList().SetName(rAttrValue); + break; + } + } + } +} + +SwXMLBlockListContext::~SwXMLBlockListContext ( void ) +{ +} + +SvXMLImportContext *SwXMLBlockListContext::CreateChildContext( + sal_uInt16 nPrefix, + const OUString& rLocalName, + const uno::Reference< xml::sax::XAttributeList > & xAttrList ) +{ + SvXMLImportContext *pContext = 0; + if (nPrefix == XML_NAMESPACE_BLOCKLIST && + IsXMLToken ( rLocalName, XML_BLOCK ) ) + pContext = new SwXMLBlockContext (rLocalRef, nPrefix, rLocalName, xAttrList); + else + pContext = new SvXMLImportContext( rLocalRef, nPrefix, rLocalName); + return pContext; +} + +SwXMLBlockContext::SwXMLBlockContext( + SwXMLBlockListImport& rImport, + sal_uInt16 nPrefix, + const OUString& rLocalName, + const uno::Reference< + xml::sax::XAttributeList > & xAttrList ) : + SvXMLImportContext ( rImport, nPrefix, rLocalName ), + rLocalRef(rImport) +{ + static const CharClass & rCC = GetAppCharClass(); + String aShort, aLong, aPackageName; + BOOL bTextOnly = FALSE; + + sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0; + for (sal_Int16 i=0; i < nAttrCount; i++) + { + const OUString& rAttrName = xAttrList->getNameByIndex( i ); + OUString aLocalName; + sal_uInt16 nPrefx = rImport.GetNamespaceMap().GetKeyByAttrName( rAttrName, &aLocalName); + const OUString& rAttrValue = xAttrList->getValueByIndex( i ); + if (XML_NAMESPACE_BLOCKLIST == nPrefx) + { + if ( IsXMLToken ( aLocalName, XML_ABBREVIATED_NAME ) ) + { + aShort = rCC.upper(rAttrValue); + } + else if ( IsXMLToken ( aLocalName, XML_NAME ) ) + { + aLong = rAttrValue; + } + else if ( IsXMLToken ( aLocalName, XML_PACKAGE_NAME ) ) + { + aPackageName = rAttrValue; + } + else if ( IsXMLToken ( aLocalName, XML_UNFORMATTED_TEXT ) ) + { + if ( IsXMLToken ( rAttrValue, XML_TRUE ) ) + bTextOnly = TRUE; + } + } + } + if (!aShort.Len() || !aLong.Len() || !aPackageName.Len()) + return; + rImport.getBlockList().AddName( aShort, aLong, aPackageName, bTextOnly); +} + +SwXMLBlockContext::~SwXMLBlockContext ( void ) +{ +} + +SwXMLTextBlockDocumentContext::SwXMLTextBlockDocumentContext( + SwXMLTextBlockImport& rImport, + sal_uInt16 nPrefix, + const OUString& rLocalName, + const uno::Reference< + xml::sax::XAttributeList > & ) : + SvXMLImportContext ( rImport, nPrefix, rLocalName ), + rLocalRef(rImport) +{ +} + +SvXMLImportContext *SwXMLTextBlockDocumentContext::CreateChildContext( + sal_uInt16 nPrefix, + const OUString& rLocalName, + const uno::Reference< xml::sax::XAttributeList > & xAttrList ) +{ + SvXMLImportContext *pContext = 0; + if (nPrefix == XML_NAMESPACE_OFFICE && + IsXMLToken ( rLocalName, XML_BODY ) ) + pContext = new SwXMLTextBlockBodyContext (rLocalRef, nPrefix, rLocalName, xAttrList); + else + pContext = new SvXMLImportContext( rLocalRef, nPrefix, rLocalName); + return pContext; +} +SwXMLTextBlockDocumentContext::~SwXMLTextBlockDocumentContext ( void ) +{ +} + + +SwXMLTextBlockTextContext::SwXMLTextBlockTextContext( + SwXMLTextBlockImport& rImport, + sal_uInt16 nPrefix, + const OUString& rLocalName, + const uno::Reference< + xml::sax::XAttributeList > & ) : + SvXMLImportContext ( rImport, nPrefix, rLocalName ), + rLocalRef(rImport) +{ +} + +SvXMLImportContext *SwXMLTextBlockTextContext::CreateChildContext( + sal_uInt16 nPrefix, + const OUString& rLocalName, + const uno::Reference< xml::sax::XAttributeList > & xAttrList ) +{ + SvXMLImportContext *pContext = 0; + if (nPrefix == XML_NAMESPACE_TEXT && + IsXMLToken ( rLocalName, XML_P ) ) + pContext = new SwXMLTextBlockParContext (rLocalRef, nPrefix, rLocalName, xAttrList); + else + pContext = new SvXMLImportContext( rLocalRef, nPrefix, rLocalName); + return pContext; +} +SwXMLTextBlockTextContext::~SwXMLTextBlockTextContext ( void ) +{ +} + + +SwXMLTextBlockBodyContext::SwXMLTextBlockBodyContext( + SwXMLTextBlockImport& rImport, + sal_uInt16 nPrefix, + const OUString& rLocalName, + const uno::Reference< + xml::sax::XAttributeList > & ) : + SvXMLImportContext ( rImport, nPrefix, rLocalName ), + rLocalRef(rImport) +{ +} + +SvXMLImportContext *SwXMLTextBlockBodyContext::CreateChildContext( + sal_uInt16 nPrefix, + const OUString& rLocalName, + const uno::Reference< xml::sax::XAttributeList > & xAttrList ) +{ + SvXMLImportContext *pContext = 0; + if (nPrefix == XML_NAMESPACE_OFFICE && + IsXMLToken ( rLocalName, XML_TEXT ) ) + pContext = new SwXMLTextBlockTextContext (rLocalRef, nPrefix, rLocalName, xAttrList); + else if (nPrefix == XML_NAMESPACE_TEXT && + IsXMLToken ( rLocalName, XML_P ) ) + pContext = new SwXMLTextBlockParContext (rLocalRef, nPrefix, rLocalName, xAttrList); + else + pContext = new SvXMLImportContext( rLocalRef, nPrefix, rLocalName); + return pContext; +} +SwXMLTextBlockBodyContext::~SwXMLTextBlockBodyContext ( void ) +{ +} +SwXMLTextBlockParContext::SwXMLTextBlockParContext( + SwXMLTextBlockImport& rImport, + sal_uInt16 nPrefix, + const OUString& rLocalName, + const uno::Reference< + xml::sax::XAttributeList > & ) : + SvXMLImportContext ( rImport, nPrefix, rLocalName ), + rLocalRef(rImport) +{ +} + +void SwXMLTextBlockParContext::Characters( const ::rtl::OUString& rChars ) +{ + rLocalRef.m_rText.Append ( rChars.getStr()); +} +SwXMLTextBlockParContext::~SwXMLTextBlockParContext ( void ) +{ + if (rLocalRef.bTextOnly) + rLocalRef.m_rText.AppendAscii( "\015" ); + else + { + if (rLocalRef.m_rText.GetChar ( rLocalRef.m_rText.Len()) != ' ' ) + rLocalRef.m_rText.AppendAscii( " " ); + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sw/source/core/swg/SwXMLSectionList.cxx b/sw/source/core/swg/SwXMLSectionList.cxx new file mode 100644 index 000000000000..d32852ff59d1 --- /dev/null +++ b/sw/source/core/swg/SwXMLSectionList.cxx @@ -0,0 +1,157 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * 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. + * + * 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). + * + * 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. + * + ************************************************************************/ + +// MARKER(update_precomp.py): autogen include statement, do not remove +#include "precompiled_sw.hxx" +#define _SVSTDARR_STRINGSDTOR +#define _SVSTDARR_STRINGS +#include <svl/svstdarr.hxx> +#include <SwXMLSectionList.hxx> +#include <xmloff/nmspmap.hxx> +#include <xmloff/xmlnmspe.hxx> + +using namespace ::com::sun::star; +using ::rtl::OUString; +using namespace ::xmloff::token; + +sal_Char __READONLY_DATA sXML_np__office[] = "_ooffice"; +sal_Char __READONLY_DATA sXML_np__text[] = "_otext"; + +// #110680# +SwXMLSectionList::SwXMLSectionList( + const uno::Reference< lang::XMultiServiceFactory > xServiceFactory, + SvStrings & rNewSectionList) +: SvXMLImport( xServiceFactory ), + rSectionList ( rNewSectionList ) +{ + GetNamespaceMap().Add( OUString( RTL_CONSTASCII_USTRINGPARAM ( sXML_np__office ) ), + GetXMLToken(XML_N_OFFICE_OOO), + XML_NAMESPACE_OFFICE ); + GetNamespaceMap().Add( OUString( RTL_CONSTASCII_USTRINGPARAM ( sXML_np__text ) ), + GetXMLToken(XML_N_TEXT_OOO), + XML_NAMESPACE_TEXT ); +} + +SwXMLSectionList::~SwXMLSectionList ( void ) + throw() +{ +} + +SvXMLImportContext *SwXMLSectionList::CreateContext( + sal_uInt16 nPrefix, + const OUString& rLocalName, + const uno::Reference< xml::sax::XAttributeList > & xAttrList ) +{ + SvXMLImportContext *pContext = 0; + + if(( nPrefix == XML_NAMESPACE_OFFICE && IsXMLToken ( rLocalName, XML_BODY )) || + ( nPrefix == XML_NAMESPACE_TEXT && + (IsXMLToken ( rLocalName, XML_P ) || + IsXMLToken ( rLocalName, XML_H ) || + IsXMLToken ( rLocalName, XML_A ) || + IsXMLToken ( rLocalName, XML_SPAN ) || + IsXMLToken ( rLocalName, XML_SECTION ) || + IsXMLToken ( rLocalName, XML_INDEX_BODY ) || + IsXMLToken ( rLocalName, XML_INDEX_TITLE )|| + IsXMLToken ( rLocalName, XML_INSERTION ) || + IsXMLToken ( rLocalName, XML_DELETION ) ) + ) + ) + { + pContext = new SvXMLSectionListContext (*this, nPrefix, rLocalName, xAttrList); + } + else + pContext = SvXMLImport::CreateContext( nPrefix, rLocalName, xAttrList ); + return pContext; +} + +SvXMLSectionListContext::SvXMLSectionListContext( + SwXMLSectionList& rImport, + sal_uInt16 nPrefix, + const OUString& rLocalName, + const uno::Reference< xml::sax::XAttributeList > & ) : + SvXMLImportContext ( rImport, nPrefix, rLocalName ), + rLocalRef(rImport) +{ +} + +SvXMLImportContext *SvXMLSectionListContext::CreateChildContext( + sal_uInt16 nPrefix, + const OUString& rLocalName, + const uno::Reference< xml::sax::XAttributeList > & xAttrList ) +{ + SvXMLImportContext *pContext = 0; + String sName; + + if (nPrefix == XML_NAMESPACE_TEXT && ( IsXMLToken ( rLocalName, XML_SECTION ) || + IsXMLToken ( rLocalName, XML_BOOKMARK) ) ) + { + sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0; + + for (sal_Int16 i=0; i < nAttrCount; i++) + { + const OUString& rAttrName = xAttrList->getNameByIndex( i ); + OUString aLocalName; + sal_uInt16 nPrefx = rLocalRef.GetNamespaceMap().GetKeyByAttrName( rAttrName, &aLocalName); + const OUString& rAttrValue = xAttrList->getValueByIndex( i ); + if (XML_NAMESPACE_TEXT == nPrefx && IsXMLToken ( aLocalName, XML_NAME ) ) + sName = rAttrValue; + } + if ( sName.Len() ) + rLocalRef.rSectionList.Insert ( new String(sName), rLocalRef.rSectionList.Count() ); + } + + pContext = new SvXMLSectionListContext (rLocalRef, nPrefix, rLocalName, xAttrList); + return pContext; +} +SvXMLSectionListContext::~SvXMLSectionListContext ( void ) +{ +} + +SvXMLIgnoreSectionListContext::SvXMLIgnoreSectionListContext( + SwXMLSectionList& rImport, + sal_uInt16 nPrefix, + const OUString& rLocalName, + const uno::Reference< xml::sax::XAttributeList > & ) : + SvXMLImportContext ( rImport, nPrefix, rLocalName ), + rLocalRef(rImport) +{ +} + +SvXMLIgnoreSectionListContext::~SvXMLIgnoreSectionListContext ( void ) +{ +} +SvXMLImportContext *SvXMLIgnoreSectionListContext::CreateChildContext( + sal_uInt16 nPrefix, + const OUString& rLocalName, + const uno::Reference< xml::sax::XAttributeList > & xAttrList ) +{ + return new SvXMLIgnoreSectionListContext (rLocalRef, nPrefix, rLocalName, xAttrList); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sw/source/core/swg/SwXMLTextBlocks.cxx b/sw/source/core/swg/SwXMLTextBlocks.cxx new file mode 100644 index 000000000000..4563b4a7056c --- /dev/null +++ b/sw/source/core/swg/SwXMLTextBlocks.cxx @@ -0,0 +1,644 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * 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. + * + * 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). + * + * 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. + * + ************************************************************************/ + +// MARKER(update_precomp.py): autogen include statement, do not remove +#include "precompiled_sw.hxx" + + +#include <com/sun/star/embed/ElementModes.hpp> +#include <com/sun/star/embed/XTransactedObject.hpp> +#include <tools/urlobj.hxx> +#include <sot/stg.hxx> +#include <sfx2/docfile.hxx> +#include <unotools/localfilehelper.hxx> +#include <unotools/ucbstreamhelper.hxx> + +#include <comphelper/storagehelper.hxx> +#include <doc.hxx> +#include <docsh.hxx> +#include <pam.hxx> +#include <swblocks.hxx> +#include <ndtxt.hxx> +#include <shellio.hxx> +#include <poolfmt.hxx> +#include <SwXMLTextBlocks.hxx> +#include <errhdl.hxx> +#include <SwXMLBlockImport.hxx> +#include <SwXMLBlockExport.hxx> +#include <swerror.h> + +#define STREAM_STGREAD ( STREAM_READ | STREAM_SHARE_DENYWRITE | STREAM_NOCREATE ) +#define STREAM_STGWRITE ( STREAM_READ | STREAM_WRITE | STREAM_SHARE_DENYWRITE ) + +using namespace ::com::sun::star; + + +void SwXMLTextBlocks::InitBlockMode ( const uno::Reference < embed::XStorage >& rStorage ) +{ + xBlkRoot = rStorage; + xRoot = 0; +} + +void SwXMLTextBlocks::ResetBlockMode ( ) +{ + xBlkRoot = 0; + xRoot = 0; +} + +SwXMLTextBlocks::SwXMLTextBlocks( const String& rFile ) +: SwImpBlocks( rFile ), bAutocorrBlock( FALSE ), nFlags ( 0 ) +{ + SwDocShell* pDocSh = new SwDocShell ( SFX_CREATE_MODE_INTERNAL ); + if( !pDocSh->DoInitNew( 0 ) ) + return; + bReadOnly = TRUE; + pDoc = pDocSh->GetDoc(); + xDocShellRef = pDocSh; + pDoc->SetOle2Link( Link() ); + pDoc->DoUndo( FALSE ); // always FALSE + pDoc->acquire(); + uno::Reference< embed::XStorage > refStg; + if( !aDateModified.GetDate() || !aTimeModified.GetTime() ) + Touch(); // falls neu angelegt -> neuen ZeitStempel besorgen + try + { + refStg = comphelper::OStorageHelper::GetStorageFromURL( rFile, embed::ElementModes::READWRITE ); + bReadOnly = FALSE; + } + catch( const uno::Exception& ) + { + //couldn't open the file - maybe it's readonly + } + if( !refStg.is()) + { + try + { + refStg = comphelper::OStorageHelper::GetStorageFromURL( rFile, embed::ElementModes::READ ); + } + catch( const uno::Exception& ) + { + DBG_ERROR("exception while creating AutoText storage"); + } + } + InitBlockMode ( refStg ); + ReadInfo(); + ResetBlockMode (); + bInfoChanged = FALSE; +} + +SwXMLTextBlocks::SwXMLTextBlocks( const uno::Reference < embed::XStorage >& rStg, const String& rName ) +: SwImpBlocks( rName ) +, bAutocorrBlock( TRUE ) +, nFlags ( 0 ) +{ + SwDocShell* pDocSh = new SwDocShell ( SFX_CREATE_MODE_INTERNAL ); + if( !pDocSh->DoInitNew( 0 ) ) + return; + bReadOnly = FALSE; + pDoc = pDocSh->GetDoc(); + xDocShellRef = pDocSh; + pDoc->SetOle2Link( Link() ); + pDoc->DoUndo( FALSE ); + pDoc->acquire(); + + InitBlockMode ( rStg ); + ReadInfo(); + bInfoChanged = FALSE; +} + +SwXMLTextBlocks::~SwXMLTextBlocks() +{ + if ( bInfoChanged ) + WriteInfo(); + ResetBlockMode (); + if(xDocShellRef.Is()) + xDocShellRef->DoClose(); + xDocShellRef = 0; + if( pDoc && !pDoc->release() ) + delete pDoc; +} + +void SwXMLTextBlocks::ClearDoc() +{ + SwDocShell * pDocShell = pDoc->GetDocShell(); + pDocShell->InvalidateModel(); + pDocShell->ReactivateModel(); + + pDoc->ClearDoc(); + pDocShell->ClearEmbeddedObjects(); +} +void SwXMLTextBlocks::AddName( const String& rShort, const String& rLong, BOOL bOnlyTxt ) +{ + USHORT nIdx = GetIndex( rShort ); + SwBlockName* pNew = NULL; + if( nIdx != (USHORT) -1 ) + aNames.DeleteAndDestroy( nIdx ); + + GeneratePackageName( rShort, aPackageName ); + pNew = new SwBlockName( rShort, rLong, aPackageName ); + + pNew->bIsOnlyTxtFlagInit = TRUE; + pNew->bIsOnlyTxt = bOnlyTxt; + aNames.C40_PTR_INSERT( SwBlockName, pNew ); + bInfoChanged = TRUE; +} +void SwXMLTextBlocks::AddName( const String& rShort, const String& rLong, + const String& rPackageName, BOOL bOnlyTxt ) +{ + USHORT nIdx = GetIndex( rShort ); + if( nIdx != (USHORT) -1 ) + aNames.DeleteAndDestroy( nIdx ); + SwBlockName* pNew = new SwBlockName( rShort, rLong, rPackageName ); + pNew->bIsOnlyTxtFlagInit = TRUE; + pNew->bIsOnlyTxt = bOnlyTxt; + aNames.C40_PTR_INSERT( SwBlockName, pNew ); + bInfoChanged = TRUE; +} + +ULONG SwXMLTextBlocks::Delete( USHORT n ) +{ + String aPckName (aNames[ n ]->aPackageName); + uno::Reference < container::XNameAccess > xAccess( xBlkRoot, uno::UNO_QUERY ); + if ( xAccess.is() && + xAccess->hasByName( aPckName ) && xBlkRoot->isStreamElement( aPckName ) ) + { + try + { + xBlkRoot->removeElement ( aPckName ); + uno::Reference < embed::XTransactedObject > xTrans( xBlkRoot, uno::UNO_QUERY ); + if ( xTrans.is() ) + xTrans->commit(); + return 0; + } + catch ( uno::Exception) + { + return ERR_SWG_WRITE_ERROR; + } + } + return 0; +} + +ULONG SwXMLTextBlocks::Rename( USHORT nIdx, const String& rNewShort, const String& ) +{ + DBG_ASSERT( xBlkRoot.is(), "No storage set" ); + if(!xBlkRoot.is()) + return 0; + String aOldName (aNames[ nIdx ]->aPackageName); + aShort = rNewShort; + GeneratePackageName( aShort, aPackageName ); + if (IsOnlyTextBlock ( nIdx ) ) + { + String sExt( String::CreateFromAscii( ".xml" )); + String aOldStreamName( aOldName ); aOldStreamName += sExt; + String aNewStreamName( aPackageName ); aNewStreamName += sExt; + + xRoot = xBlkRoot->openStorageElement( aOldName, embed::ElementModes::READWRITE ); + xRoot->renameElement ( aOldStreamName, aNewStreamName ); + uno::Reference < embed::XTransactedObject > xTrans( xRoot, uno::UNO_QUERY ); + if ( xTrans.is() ) + xTrans->commit(); + xRoot = 0; + } + + if(aOldName != aPackageName) + { + try + { + xBlkRoot->renameElement ( aOldName, aPackageName ); + } + catch( const container::ElementExistException& rEx ) + { + (void)rEx; + } + } + uno::Reference < embed::XTransactedObject > xTrans( xBlkRoot, uno::UNO_QUERY ); + if ( xTrans.is() ) + xTrans->commit(); + // No need to commit xBlkRoot here as SwTextBlocks::Rename calls + // WriteInfo which does the commit + return 0; +} + +ULONG SwXMLTextBlocks::CopyBlock( SwImpBlocks& rDestImp, String& rShort, + const String& rLong) +{ + ULONG nError = 0; + OpenFile(TRUE); + rDestImp.OpenFile(FALSE); + String aGroup( rShort ); + BOOL bTextOnly = IsOnlyTextBlock ( rShort ) ;//pImp->pBlkRoot->IsStream( aGroup ); + USHORT nIndex = GetIndex ( rShort ); + String sDestShortName( GetPackageName (nIndex) ); + USHORT nIdx = 0; + + DBG_ASSERT( xBlkRoot.is(), "No storage set" ); + if(!xBlkRoot.is()) + return ERR_SWG_WRITE_ERROR; + + uno::Reference < container::XNameAccess > xAccess( ((SwXMLTextBlocks&)rDestImp).xBlkRoot, uno::UNO_QUERY ); + while ( xAccess->hasByName( sDestShortName ) ) + { + ++nIdx; + //falls wirklich mal einer so verrueckt ist + if(USHRT_MAX == nIdx) + { + CloseFile(); + rDestImp.CloseFile(); + return ERR_SWG_WRITE_ERROR; + } + sDestShortName += String::CreateFromInt32( nIdx ); + } + + try + { + uno::Reference < embed::XStorage > rSourceRoot = xBlkRoot->openStorageElement( aGroup, embed::ElementModes::READ ); + uno::Reference < embed::XStorage > rDestRoot = ((SwXMLTextBlocks&)rDestImp).xBlkRoot->openStorageElement( sDestShortName, embed::ElementModes::READWRITE ); + //if(!rSourceRoot.Is()) + // nError = ERR_SWG_READ_ERROR; + //else + //{ + rSourceRoot->copyToStorage( rDestRoot ); + } + catch ( uno::Exception& ) + { + nError = ERR_SWG_WRITE_ERROR; + } + + /* I think this should work now that text only blocks are in sub-storages as well + else + { + SvStorageStreamRef rSourceStream = xBlkRoot->OpenStream( aGroup, STREAM_STGREAD ); + SvStorageStreamRef rDestStream = ((SwXMLTextBlocks&)rDestImp).xBlkRoot-> OpenStream( sDestShortName, STREAM_STGWRITE ); + if(!rDestStream.Is()) + nError = ERR_SWG_WRITE_ERROR; + else + { + if(!rSourceStream->CopyTo(&rDestStream)) + nError = ERR_SWG_WRITE_ERROR; + else + rDestStream->Commit(); + } + } + */ + if(!nError) + { + rShort = sDestShortName; + //((SwXMLTextBlocks&)rDestImp).xBlkRoot->Commit(); + ((SwXMLTextBlocks&)rDestImp).AddName( rShort, rLong, bTextOnly ); + ((SwXMLTextBlocks&)rDestImp).MakeBlockList(); + } + CloseFile(); + rDestImp.CloseFile(); + return nError; +} + + +ULONG SwXMLTextBlocks::StartPutBlock( const String& rShort, const String& rPackageName ) +{ + DBG_ASSERT( xBlkRoot.is(), "No storage set" ); + if(!xBlkRoot.is()) + return 0; + GetIndex ( rShort ); + /* + if( xBlkRoot->IsContained( rPackageName ) ) + { + xBlkRoot->Remove( rPackageName ); + xBlkRoot->Commit(); + } + */ + try + { + xRoot = xBlkRoot->openStorageElement( rPackageName, embed::ElementModes::READWRITE ); + + uno::Reference< beans::XPropertySet > xRootProps( xRoot, uno::UNO_QUERY_THROW ); + ::rtl::OUString aPropName( RTL_CONSTASCII_USTRINGPARAM("MediaType") ); + ::rtl::OUString aMime( SotExchange::GetFormatMimeType( SOT_FORMATSTR_ID_STARWRITER_8 ) ); + xRootProps->setPropertyValue( aPropName, uno::makeAny( aMime ) ); + } + catch (uno::Exception&) + { + } + return 0; +} +ULONG SwXMLTextBlocks::BeginPutDoc( const String& rShort, const String& rLong ) +{ + // In der Basisklasse ablegen! + aShort = rShort; + aLong = rLong; + GeneratePackageName( rShort, aPackageName ); + SetIsTextOnly( rShort, FALSE); + return StartPutBlock (rShort, aPackageName); +} + +ULONG SwXMLTextBlocks::PutBlock( SwPaM& , const String& ) +{ + ULONG nRes = 0; + USHORT nCommitFlags = nFlags & (SWXML_CONVBLOCK|SWXML_NOROOTCOMMIT); + + nFlags |= nCommitFlags; + + WriterRef xWrt; + ::GetXMLWriter ( aEmptyStr, GetBaseURL(), xWrt); + SwWriter aWriter (xRoot, *pDoc ); + + xWrt->bBlock = sal_True; + nRes = aWriter.Write ( xWrt ); + xWrt->bBlock = sal_False; + // Save OLE objects if there are some + SwDocShell *pDocSh = pDoc->GetDocShell(); + + sal_Bool bHasChildren = pDocSh && pDocSh->GetEmbeddedObjectContainer().HasEmbeddedObjects(); + if( !nRes && bHasChildren ) + { + // we have to write to the temporary storage first, since the used below functions are optimized + // TODO/LATER: it is only a temporary solution, that should be changed soon, the used methods should be + // called without optimization + + sal_Bool bOK = sal_False; + + if ( xRoot.is() ) + { + SfxMedium* pTmpMedium = NULL; + try + { + uno::Reference< embed::XStorage > xTempStorage = + ::comphelper::OStorageHelper::GetTemporaryStorage(); + + xRoot->copyToStorage( xTempStorage ); + + // TODO/LATER: no progress bar?! + // TODO/MBA: strange construct + pTmpMedium = new SfxMedium( xTempStorage, GetBaseURL() ); + sal_Bool bTmpOK = pDocSh->SaveAsChildren( *pTmpMedium ); + if( bTmpOK ) + bTmpOK = pDocSh->SaveCompletedChildren( sal_False ); + + xTempStorage->copyToStorage( xRoot ); + bOK = bTmpOK; + } + catch( uno::Exception& ) + { + } + + if ( pTmpMedium ) + DELETEZ( pTmpMedium ); + } + + if( !bOK ) + nRes = ERR_SWG_WRITE_ERROR; + } + + try + { + uno::Reference < embed::XTransactedObject > xTrans( xRoot, uno::UNO_QUERY ); + if ( xTrans.is() ) + xTrans->commit(); + xRoot = 0; + if ( !nCommitFlags ) + { + uno::Reference < embed::XTransactedObject > xTmpTrans( xBlkRoot, uno::UNO_QUERY ); + if ( xTmpTrans.is() ) + xTmpTrans->commit(); + } + } + catch (uno::Exception&) + { + } + + //TODO/LATER: error handling + /* + ULONG nErr = xBlkRoot->GetError(); + if( nErr == SVSTREAM_DISK_FULL ) + nRes = ERR_W4W_WRITE_FULL; + else if( nErr != SVSTREAM_OK ) + nRes = ERR_SWG_WRITE_ERROR; + nFlags |= nCommitFlags; + return nErr;*/ + return 0; +} + +ULONG SwXMLTextBlocks::PutDoc() +{ + SwPaM* pPaM = MakePaM(); + ULONG nErr = PutBlock(*pPaM, aLong); + delete pPaM; + return nErr; +} + +ULONG SwXMLTextBlocks::GetText( USHORT nIdx, String& rText ) +{ + return GetBlockText( aNames[ nIdx ]->aShort, rText ); +} + +ULONG SwXMLTextBlocks::GetText( const String& rShort, String& rText ) +{ + return GetBlockText( rShort, rText ); +} + + +ULONG SwXMLTextBlocks::MakeBlockList() +{ + WriteInfo(); + return 0; +} + +BOOL SwXMLTextBlocks::PutMuchEntries( BOOL bOn ) +{ + BOOL bRet = FALSE; + if( bOn ) + { + if( bInPutMuchBlocks ) + { + OSL_ENSURE( !this, "verschachtelte Aufrufe sind nicht erlaubt" ); + } + else if( !IsFileChanged() ) + { + bRet = 0 == OpenFile( FALSE ); + if( bRet ) + { + nFlags |= SWXML_NOROOTCOMMIT; + bInPutMuchBlocks = TRUE; + } + } + } + else if( bInPutMuchBlocks ) + { + nFlags &= ~SWXML_NOROOTCOMMIT; + if( xBlkRoot.is() ) + { + try + { + uno::Reference < embed::XTransactedObject > xTrans( xBlkRoot, uno::UNO_QUERY ); + if ( xTrans.is() ) + xTrans->commit(); + MakeBlockList(); + CloseFile(); + Touch(); + bInPutMuchBlocks = FALSE; + bRet = TRUE; + } + catch (uno::Exception&) + { + } + } + } + return bRet; +} + +ULONG SwXMLTextBlocks::OpenFile( BOOL bRdOnly ) +{ + if( bAutocorrBlock ) + return 0; + ULONG nRet = 0; + try + { + uno::Reference < embed::XStorage > refStg = comphelper::OStorageHelper::GetStorageFromURL( aFile, + bRdOnly ? embed::ElementModes::READ : embed::ElementModes::READWRITE ); + InitBlockMode ( refStg ); + } + catch ( uno::Exception& ) + { + //TODO/LATER: error handling + nRet = 1; + } + + return nRet; +} + +void SwXMLTextBlocks::CloseFile() +{ + if ( !bAutocorrBlock ) + { + if (bInfoChanged) + WriteInfo(); + ResetBlockMode(); + } +} + +void SwXMLTextBlocks::SetIsTextOnly( const String& rShort, BOOL bNewValue ) +{ + USHORT nIdx = GetIndex ( rShort ); + if (nIdx != (USHORT) -1 && nIdx != USHRT_MAX) + aNames[nIdx]->bIsOnlyTxt = bNewValue; +} + +void SwXMLTextBlocks::SetIsTextOnly( USHORT nIdx, BOOL bNewValue ) +{ + aNames[nIdx]->bIsOnlyTxt = bNewValue; +} + +BOOL SwXMLTextBlocks::IsOnlyTextBlock( const String& rShort ) const +{ + USHORT nIdx = GetIndex ( rShort ); + BOOL bRet = FALSE; + if (nIdx != (USHORT) -1 && nIdx != USHRT_MAX) + { + bRet = aNames[nIdx]->bIsOnlyTxt; + } + return bRet; +} +BOOL SwXMLTextBlocks::IsOnlyTextBlock( USHORT nIdx ) const +{ + return aNames[nIdx]->bIsOnlyTxt; +} + +BOOL SwXMLTextBlocks::IsFileUCBStorage( const String & rFileName) +{ + String aName( rFileName ); + INetURLObject aObj( aName ); + if ( aObj.GetProtocol() == INET_PROT_NOT_VALID ) + { + String aURL; + ::utl::LocalFileHelper::ConvertPhysicalNameToURL( aName, aURL ); + aObj.SetURL( aURL ); + aName = aObj.GetMainURL( INetURLObject::NO_DECODE ); + } + + SvStream * pStm = ::utl::UcbStreamHelper::CreateStream( aName, STREAM_STD_READ ); + BOOL bRet = UCBStorage::IsStorageFile( pStm ); + delete pStm; + return bRet; +} + + + +short SwXMLTextBlocks::GetFileType ( void ) const +{ + return SWBLK_XML; +} + +void SwXMLTextBlocks::GeneratePackageName ( const String& rShort, String& rPackageName ) +{ + rPackageName = rShort; + xub_StrLen nPos = 0; + sal_Unicode pDelims[] = { '!', '/', ':', '.', '\\', 0 }; + ByteString sByte ( rPackageName, RTL_TEXTENCODING_UTF7); + rPackageName = String (sByte, RTL_TEXTENCODING_ASCII_US); + while( STRING_NOTFOUND != ( nPos = rPackageName.SearchChar( pDelims, nPos ))) + { + rPackageName.SetChar( nPos, '_' ); + ++nPos; + } +} + +ULONG SwXMLTextBlocks::PutText( const String& rShort, const String& rName, + const String& rText ) +{ + ULONG nRes = 0; + aShort = rShort; + aLong = rName; + aCur = rText; + SetIsTextOnly( aShort, TRUE ); + GeneratePackageName( rShort, aPackageName ); + ClearDoc(); + nRes = PutBlockText( rShort, rName, rText, aPackageName ); + return nRes; +} + +void SwXMLTextBlocks::MakeBlockText( const String& rText ) +{ + SwTxtNode* pTxtNode = pDoc->GetNodes()[ pDoc->GetNodes().GetEndOfContent(). + GetIndex() - 1 ]->GetTxtNode(); + //JP 18.09.98: Bug 56706 - Standard sollte zumindest gesetzt sein! + if( pTxtNode->GetTxtColl() == pDoc->GetDfltTxtFmtColl() ) + pTxtNode->ChgFmtColl( pDoc->GetTxtCollFromPool( RES_POOLCOLL_STANDARD )); + + xub_StrLen nPos = 0; + do + { + if ( nPos ) + { + pTxtNode = (SwTxtNode*)pTxtNode->AppendNode( SwPosition( *pTxtNode ) ); + } + SwIndex aIdx( pTxtNode ); + String sTemp(rText.GetToken( 0, '\015', nPos ) ); + pTxtNode->InsertText( sTemp, aIdx ); + } while ( STRING_NOTFOUND != nPos ); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sw/source/core/swg/SwXMLTextBlocks1.cxx b/sw/source/core/swg/SwXMLTextBlocks1.cxx new file mode 100644 index 000000000000..1b50c5aa8a23 --- /dev/null +++ b/sw/source/core/swg/SwXMLTextBlocks1.cxx @@ -0,0 +1,738 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * 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. + * + * 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). + * + * 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. + * + ************************************************************************/ + +// MARKER(update_precomp.py): autogen include statement, do not remove +#include "precompiled_sw.hxx" +#include <com/sun/star/embed/ElementModes.hpp> +#include <com/sun/star/embed/XTransactedObject.hpp> +#include <svl/macitem.hxx> +#include <svtools/unoevent.hxx> +#include <sfx2/docfile.hxx> +#include <unotools/streamwrap.hxx> +#include <comphelper/processfactory.hxx> +#include <com/sun/star/xml/sax/InputSource.hpp> +#include <com/sun/star/io/XActiveDataSource.hpp> +#include <com/sun/star/xml/sax/XParser.hpp> +#include <com/sun/star/document/XStorageBasedDocument.hpp> +#include <doc.hxx> +#include <docsh.hxx> +#include <shellio.hxx> +#include <SwXMLTextBlocks.hxx> +#include <SwXMLBlockImport.hxx> +#include <SwXMLBlockExport.hxx> +#include <swevent.hxx> +#include <swerror.h> +#include <errhdl.hxx> + + +#define STREAM_STGREAD ( STREAM_READ | STREAM_SHARE_DENYWRITE | STREAM_NOCREATE ) +#define STREAM_STGWRITE ( STREAM_READ | STREAM_WRITE | STREAM_SHARE_DENYWRITE ) + +sal_Char XMLN_BLOCKLIST[] = "BlockList.xml"; + +using namespace ::com::sun::star; +using namespace ::com::sun::star::uno; +using namespace ::com::sun::star::container; +using ::rtl::OUString; + +using ::xmloff::token::XML_BLOCK_LIST; +using ::xmloff::token::XML_UNFORMATTED_TEXT; +using ::xmloff::token::GetXMLToken; + +ULONG SwXMLTextBlocks::GetDoc( USHORT nIdx ) +{ + String aFolderName ( GetPackageName ( nIdx ) ); + + if (!IsOnlyTextBlock ( nIdx ) ) + { + try + { + xRoot = xBlkRoot->openStorageElement( aFolderName, embed::ElementModes::READ ); + xMedium = new SfxMedium(xRoot, GetBaseURL()); + SwReader aReader(*xMedium,aFolderName, pDoc ); + ReadXML->SetBlockMode( sal_True ); + aReader.Read( *ReadXML ); + ReadXML->SetBlockMode( sal_False ); + // Ole objects fails to display when inserted into document + // because the ObjectReplacement folder ( and contents are missing ) + rtl::OUString sObjReplacements( RTL_CONSTASCII_USTRINGPARAM( "ObjectReplacements" ) ); + if ( xRoot->hasByName( sObjReplacements ) ) + { + uno::Reference< document::XStorageBasedDocument > xDocStor( pDoc->GetDocShell()->GetModel(), uno::UNO_QUERY_THROW ); + uno::Reference< embed::XStorage > xStr( xDocStor->getDocumentStorage() ); + if ( xStr.is() ) + { + xRoot->copyElementTo( sObjReplacements, xStr, sObjReplacements ); + uno::Reference< embed::XTransactedObject > xTrans( xStr, uno::UNO_QUERY ); + if ( xTrans.is() ) + xTrans->commit(); + } + } + } + catch( uno::Exception& ) + { + } + + xRoot = 0; + } + else + { + String aStreamName = aFolderName + (OUString) String::CreateFromAscii(".xml"); + try + { + xRoot = xBlkRoot->openStorageElement( aFolderName, embed::ElementModes::READ ); + uno::Reference < io::XStream > xStream = xRoot->openStreamElement( aStreamName, embed::ElementModes::READ ); + + uno::Reference< lang::XMultiServiceFactory > xServiceFactory = + comphelper::getProcessServiceFactory(); + OSL_ENSURE( xServiceFactory.is(), "XMLReader::Read: got no service manager" ); + if( !xServiceFactory.is() ) + { + // Throw an exception ? + } + + xml::sax::InputSource aParserInput; + aParserInput.sSystemId = aNames [ nIdx ] ->aPackageName; + + aParserInput.aInputStream = xStream->getInputStream(); + + // get parser + uno::Reference< XInterface > xXMLParser = xServiceFactory->createInstance( + OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.xml.sax.Parser"))); + OSL_ENSURE( xXMLParser.is(), + "XMLReader::Read: com.sun.star.xml.sax.Parser service missing" ); + if( !xXMLParser.is() ) + { + // Maybe throw an exception? + } + + // get filter + // #110680# + // uno::Reference< xml::sax::XDocumentHandler > xFilter = new SwXMLTextBlockImport( *this, aCur, sal_True ); + uno::Reference< xml::sax::XDocumentHandler > xFilter = new SwXMLTextBlockImport( xServiceFactory, *this, aCur, sal_True ); + + // connect parser and filter + uno::Reference< xml::sax::XParser > xParser( xXMLParser, UNO_QUERY ); + xParser->setDocumentHandler( xFilter ); + + // parse + try + { + xParser->parseStream( aParserInput ); + } + catch( xml::sax::SAXParseException& ) + { + // re throw ? + } + catch( xml::sax::SAXException& ) + { + // re throw ? + } + catch( io::IOException& ) + { + // re throw ? + } + + bInfoChanged = FALSE; + MakeBlockText(aCur); + } + catch( uno::Exception& ) + { + } + + xRoot = 0; + } + return 0; +} + +// event description for autotext events; this constant should really be +// taken from unocore/unoevents.cxx or ui/unotxt.cxx +const struct SvEventDescription aAutotextEvents[] = +{ + { SW_EVENT_START_INS_GLOSSARY, "OnInsertStart" }, + { SW_EVENT_END_INS_GLOSSARY, "OnInsertDone" }, + { 0, NULL } +}; + +ULONG SwXMLTextBlocks::GetMacroTable( USHORT nIdx, + SvxMacroTableDtor& rMacroTbl, + sal_Bool bFileAlreadyOpen ) +{ + // set current auto text + + aShort = aNames[ nIdx ]->aShort; + aLong = aNames[ nIdx ]->aLong; + aPackageName = aNames[ nIdx ]->aPackageName; + + ULONG nRet = 0; + + // open stream in proper sub-storage + if( !bFileAlreadyOpen ) + { + CloseFile(); + nRet = OpenFile ( TRUE ); + } + if ( 0 == nRet ) + { + try + { + xRoot = xBlkRoot->openStorageElement( aPackageName, embed::ElementModes::READ ); + long nTmp = SOT_FORMATSTR_ID_STARWRITER_60; + sal_Bool bOasis = ( SotStorage::GetVersion( xRoot ) > nTmp ); + + OUString sStreamName(RTL_CONSTASCII_USTRINGPARAM("atevent.xml")); + uno::Reference < io::XStream > xDocStream = xRoot->openStreamElement( + sStreamName, embed::ElementModes::READ ); + DBG_ASSERT(xDocStream.is(), "Can't create stream"); + if ( xDocStream.is() ) + { + uno::Reference<io::XInputStream> xInputStream = xDocStream->getInputStream(); + + // prepare ParserInputSrouce + xml::sax::InputSource aParserInput; + aParserInput.sSystemId = aName; + aParserInput.aInputStream = xInputStream; + + // get service factory + uno::Reference< lang::XMultiServiceFactory > xServiceFactory = + comphelper::getProcessServiceFactory(); + if ( xServiceFactory.is() ) + { + + // get parser + OUString sParserService( RTL_CONSTASCII_USTRINGPARAM( + "com.sun.star.xml.sax.Parser" ) ); + uno::Reference< xml::sax::XParser > xParser( + xServiceFactory->createInstance(sParserService), + UNO_QUERY ); + DBG_ASSERT( xParser.is(), "Can't create parser" ); + if( xParser.is() ) + { + // create descriptor and reference to it. Either + // both or neither must be kept because of the + // reference counting! + SvMacroTableEventDescriptor* pDescriptor = + new SvMacroTableEventDescriptor(aAutotextEvents); + uno::Reference<XNameReplace> xReplace = pDescriptor; + Sequence<Any> aFilterArguments( 1 ); + aFilterArguments[0] <<= xReplace; + + // get filter + OUString sFilterComponent = bOasis + ? OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.Writer.XMLOasisAutotextEventsImporter")) + : OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.Writer.XMLAutotextEventsImporter")); + uno::Reference< xml::sax::XDocumentHandler > xFilter( + xServiceFactory->createInstanceWithArguments( + sFilterComponent, aFilterArguments), + UNO_QUERY ); + DBG_ASSERT( xFilter.is(), + "can't instantiate atevents filter"); + if ( xFilter.is() ) + { + // connect parser and filter + xParser->setDocumentHandler( xFilter ); + + // connect model and filter + uno::Reference<document::XImporter> xImporter( xFilter, + UNO_QUERY ); + + // we don't need a model + // xImporter->setTargetDocument( xModelComponent ); + + // parse the stream + try + { + xParser->parseStream( aParserInput ); + } + catch( xml::sax::SAXParseException& ) + { + // workaround for #83452#: SetSize doesn't work + // nRet = ERR_SWG_READ_ERROR; + } + catch( xml::sax::SAXException& ) + { + nRet = ERR_SWG_READ_ERROR; + } + catch( io::IOException& ) + { + nRet = ERR_SWG_READ_ERROR; + } + + // and finally, copy macro into table + if (0 == nRet) + pDescriptor->copyMacrosIntoTable(rMacroTbl); + } + else + nRet = ERR_SWG_READ_ERROR; + } + else + nRet = ERR_SWG_READ_ERROR; + + } + else + nRet = ERR_SWG_READ_ERROR; + } + else + nRet = ERR_SWG_READ_ERROR; + } + catch( uno::Exception& ) + { + nRet = ERR_SWG_READ_ERROR; + } + } + else + nRet = ERR_SWG_READ_ERROR; + + // success! + return nRet; +} + + +ULONG SwXMLTextBlocks::GetBlockText( const String& rShort, String& rText ) +{ + ULONG n = 0; + sal_Bool bTextOnly = sal_True; + String aFolderName; + GeneratePackageName ( rShort, aFolderName ); + String aStreamName = aFolderName + (OUString) String::CreateFromAscii(".xml"); + rText.Erase(); + + try + { + xRoot = xBlkRoot->openStorageElement( aFolderName, embed::ElementModes::READ ); + uno::Reference < container::XNameAccess > xAccess( xRoot, uno::UNO_QUERY ); + if ( !xAccess->hasByName( aStreamName ) || !xRoot->isStreamElement( aStreamName ) ) + { + bTextOnly = sal_False; + aStreamName = String::CreateFromAscii("content.xml"); + } + + uno::Reference < io::XStream > xContents = xRoot->openStreamElement( aStreamName, embed::ElementModes::READ ); + uno::Reference< lang::XMultiServiceFactory > xServiceFactory = + comphelper::getProcessServiceFactory(); + OSL_ENSURE( xServiceFactory.is(), "XMLReader::Read: got no service manager" ); + if( !xServiceFactory.is() ) + { + // Throw an exception ? + } + + xml::sax::InputSource aParserInput; + aParserInput.sSystemId = aName; + aParserInput.aInputStream = xContents->getInputStream(); + + // get parser + uno::Reference< XInterface > xXMLParser = xServiceFactory->createInstance( + OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.xml.sax.Parser")) ); + OSL_ENSURE( xXMLParser.is(), + "XMLReader::Read: com.sun.star.xml.sax.Parser service missing" ); + if( !xXMLParser.is() ) + { + // Maybe throw an exception? + } + + // get filter + // #110680# + // uno::Reference< xml::sax::XDocumentHandler > xFilter = new SwXMLTextBlockImport( *this, rText, bTextOnly ); + uno::Reference< xml::sax::XDocumentHandler > xFilter = new SwXMLTextBlockImport( xServiceFactory, *this, rText, bTextOnly ); + + // connect parser and filter + uno::Reference< xml::sax::XParser > xParser( xXMLParser, UNO_QUERY ); + xParser->setDocumentHandler( xFilter ); + + // parse + try + { + xParser->parseStream( aParserInput ); + } + catch( xml::sax::SAXParseException& ) + { + // re throw ? + } + catch( xml::sax::SAXException& ) + { + // re throw ? + } + catch( io::IOException& ) + { + // re throw ? + } + + xRoot = 0; + } + catch ( uno::Exception& ) + { + OSL_ENSURE( sal_False, "Tried to open non-existent folder or stream!"); + } + + return n; +} + +ULONG SwXMLTextBlocks::PutBlockText( const String& rShort, const String& , + const String& rText, const String& rPackageName ) +{ + GetIndex ( rShort ); + /* + if (xBlkRoot->IsContained ( rPackageName ) ) + { + xBlkRoot->Remove ( rPackageName ); + xBlkRoot->Commit ( ); + } + */ + String aFolderName( rPackageName ); + String aStreamName = aFolderName + (OUString) String::CreateFromAscii(".xml"); + + uno::Reference< lang::XMultiServiceFactory > xServiceFactory = + comphelper::getProcessServiceFactory(); + OSL_ENSURE( xServiceFactory.is(), + "XMLReader::Read: got no service manager" ); + if( !xServiceFactory.is() ) + { + // Throw an exception ? + } + + uno::Reference < XInterface > xWriter (xServiceFactory->createInstance( + OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.xml.sax.Writer")))); + DBG_ASSERT(xWriter.is(),"com.sun.star.xml.sax.Writer service missing"); + ULONG nRes = 0; + + try + { + xRoot = xBlkRoot->openStorageElement( aFolderName, embed::ElementModes::WRITE ); + uno::Reference < io::XStream > xDocStream = xRoot->openStreamElement( aStreamName, + embed::ElementModes::WRITE | embed::ElementModes::TRUNCATE ); + + uno::Reference < beans::XPropertySet > xSet( xDocStream, uno::UNO_QUERY ); + String aPropName( String::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM("MediaType") ) ); + OUString aMime ( RTL_CONSTASCII_USTRINGPARAM ( "text/xml") ); + Any aAny; + aAny <<= aMime; + xSet->setPropertyValue( aPropName, aAny ); + uno::Reference < io::XOutputStream > xOut = xDocStream->getOutputStream(); + uno::Reference<io::XActiveDataSource> xSrc(xWriter, uno::UNO_QUERY); + xSrc->setOutputStream(xOut); + + uno::Reference<xml::sax::XDocumentHandler> xHandler(xWriter, + uno::UNO_QUERY); + + // #110680# + // SwXMLTextBlockExport aExp(*this, GetXMLToken ( XML_UNFORMATTED_TEXT ), xHandler); + SwXMLTextBlockExport aExp( xServiceFactory, *this, GetXMLToken ( XML_UNFORMATTED_TEXT ), xHandler); + + aExp.exportDoc( rText ); + + uno::Reference < embed::XTransactedObject > xTrans( xRoot, uno::UNO_QUERY ); + if ( xTrans.is() ) + xTrans->commit(); + + if (! (nFlags & SWXML_NOROOTCOMMIT) ) + { + uno::Reference < embed::XTransactedObject > xTmpTrans( xBlkRoot, uno::UNO_QUERY ); + if ( xTmpTrans.is() ) + xTmpTrans->commit(); + } + } + catch ( uno::Exception& ) + { + nRes = ERR_SWG_WRITE_ERROR; + } + + xRoot = 0; + + //TODO/LATER: error handling + /* + ULONG nErr = xBlkRoot->GetError(); + ULONG nRes = 0; + if( nErr == SVSTREAM_DISK_FULL ) + nRes = ERR_W4W_WRITE_FULL; + else if( nErr != SVSTREAM_OK ) + nRes = ERR_SWG_WRITE_ERROR; + */ + if( !nRes ) // damit ueber GetText & nCur aufs Doc zugegriffen + MakeBlockText( rText ); + + return nRes; +} + +void SwXMLTextBlocks::ReadInfo( void ) +{ + try + { + const OUString sDocName( RTL_CONSTASCII_USTRINGPARAM( XMLN_BLOCKLIST ) ); + uno::Reference < container::XNameAccess > xAccess( xBlkRoot, uno::UNO_QUERY ); + if ( xAccess.is() && xAccess->hasByName( sDocName ) && xBlkRoot->isStreamElement( sDocName ) ) + { + uno::Reference< lang::XMultiServiceFactory > xServiceFactory = + comphelper::getProcessServiceFactory(); + OSL_ENSURE( xServiceFactory.is(), + "XMLReader::Read: got no service manager" ); + if( !xServiceFactory.is() ) + { + // Throw an exception ? + } + + xml::sax::InputSource aParserInput; + aParserInput.sSystemId = sDocName; + + uno::Reference < io::XStream > xDocStream = xBlkRoot->openStreamElement( sDocName, embed::ElementModes::READ ); + aParserInput.aInputStream = xDocStream->getInputStream(); + + // get parser + uno::Reference< XInterface > xXMLParser = xServiceFactory->createInstance( + OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.xml.sax.Parser"))); + OSL_ENSURE( xXMLParser.is(), + "XMLReader::Read: com.sun.star.xml.sax.Parser service missing" ); + if( !xXMLParser.is() ) + { + // Maybe throw an exception? + } + + // get filter + // #110680# + // uno::Reference< xml::sax::XDocumentHandler > xFilter = new SwXMLBlockListImport( *this ); + uno::Reference< xml::sax::XDocumentHandler > xFilter = new SwXMLBlockListImport( xServiceFactory, *this ); + + // connect parser and filter + uno::Reference< xml::sax::XParser > xParser( xXMLParser, UNO_QUERY ); + xParser->setDocumentHandler( xFilter ); + + // parse + try + { + xParser->parseStream( aParserInput ); + } + catch( xml::sax::SAXParseException& ) + { + // re throw ? + } + catch( xml::sax::SAXException& ) + { + // re throw ? + } + catch( io::IOException& ) + { + // re throw ? + } + } + } + catch ( uno::Exception& ) + { + } +} +void SwXMLTextBlocks::WriteInfo( void ) +{ + if ( xBlkRoot.is() || 0 == OpenFile ( FALSE ) ) + { + uno::Reference< lang::XMultiServiceFactory > xServiceFactory = + comphelper::getProcessServiceFactory(); + DBG_ASSERT( xServiceFactory.is(), + "XMLReader::Read: got no service manager" ); + if( !xServiceFactory.is() ) + { + // Throw an exception ? + } + + uno::Reference < XInterface > xWriter (xServiceFactory->createInstance( + OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.xml.sax.Writer")))); + DBG_ASSERT(xWriter.is(),"com.sun.star.xml.sax.Writer service missing"); + OUString sDocName( RTL_CONSTASCII_USTRINGPARAM( XMLN_BLOCKLIST ) ); + + /* + if ( xBlkRoot->IsContained( sDocName) ) + { + xBlkRoot->Remove ( sDocName ); + xBlkRoot->Commit(); + } + */ + + try + { + uno::Reference < io::XStream > xDocStream = xBlkRoot->openStreamElement( sDocName, + embed::ElementModes::WRITE | embed::ElementModes::TRUNCATE ); + + uno::Reference < beans::XPropertySet > xSet( xDocStream, uno::UNO_QUERY ); + String aPropName( String::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM("MediaType") ) ); + OUString aMime ( RTL_CONSTASCII_USTRINGPARAM ( "text/xml") ); + Any aAny; + aAny <<= aMime; + xSet->setPropertyValue( aPropName, aAny ); + uno::Reference < io::XOutputStream > xOut = xDocStream->getOutputStream(); + uno::Reference<io::XActiveDataSource> xSrc(xWriter, uno::UNO_QUERY); + xSrc->setOutputStream(xOut); + + uno::Reference<xml::sax::XDocumentHandler> xHandler(xWriter, uno::UNO_QUERY); + + SwXMLBlockListExport aExp( xServiceFactory, *this, OUString(RTL_CONSTASCII_USTRINGPARAM(XMLN_BLOCKLIST)), xHandler); + + aExp.exportDoc( XML_BLOCK_LIST ); + + uno::Reference < embed::XTransactedObject > xTrans( xBlkRoot, uno::UNO_QUERY ); + if ( xTrans.is() ) + xTrans->commit(); + } + catch ( uno::Exception& ) + { + } + + bInfoChanged = FALSE; + return; + } +} + +ULONG SwXMLTextBlocks::SetMacroTable( + USHORT nIdx, + const SvxMacroTableDtor& rMacroTbl, + sal_Bool bFileAlreadyOpen ) +{ + // set current autotext + aShort = aNames[ nIdx ]->aShort; + aLong = aNames[ nIdx ]->aLong; + aPackageName = aNames[ nIdx ]->aPackageName; + + // start XML autotext event export + ULONG nRes = 0; + + uno::Reference< lang::XMultiServiceFactory > xServiceFactory = + comphelper::getProcessServiceFactory(); + OSL_ENSURE( xServiceFactory.is(), + "XML autotext event write:: got no service manager" ); + if( !xServiceFactory.is() ) + return ERR_SWG_WRITE_ERROR; + + // Get model + uno::Reference< lang::XComponent > xModelComp( + pDoc->GetDocShell()->GetModel(), UNO_QUERY ); + OSL_ENSURE( xModelComp.is(), "XMLWriter::Write: got no model" ); + if( !xModelComp.is() ) + return ERR_SWG_WRITE_ERROR; + + // open stream in proper sub-storage + if( !bFileAlreadyOpen ) + { + CloseFile(); // close (it may be open in read-only-mode) + nRes = OpenFile ( FALSE ); + } + + if ( 0 == nRes ) + { + try + { + xRoot = xBlkRoot->openStorageElement( aPackageName, embed::ElementModes::WRITE ); + OUString sStreamName( RTL_CONSTASCII_USTRINGPARAM("atevent.xml") ); + long nTmp = SOT_FORMATSTR_ID_STARWRITER_60; + sal_Bool bOasis = ( SotStorage::GetVersion( xRoot ) > nTmp ); + + uno::Reference < io::XStream > xDocStream = xRoot->openStreamElement( sStreamName, + embed::ElementModes::WRITE | embed::ElementModes::TRUNCATE ); + + uno::Reference < beans::XPropertySet > xSet( xDocStream, uno::UNO_QUERY ); + String aPropName( String::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM("MediaType") ) ); + OUString aMime ( RTL_CONSTASCII_USTRINGPARAM ( "text/xml") ); + Any aAny; + aAny <<= aMime; + xSet->setPropertyValue( aPropName, aAny ); + uno::Reference < io::XOutputStream > xOutputStream = xDocStream->getOutputStream(); + + // get XML writer + uno::Reference< io::XActiveDataSource > xSaxWriter( + xServiceFactory->createInstance( + OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.xml.sax.Writer")) ), + UNO_QUERY ); + OSL_ENSURE( xSaxWriter.is(), "can't instantiate XML writer" ); + if( xSaxWriter.is() ) + { + + // connect XML writer to output stream + xSaxWriter->setOutputStream( xOutputStream ); + uno::Reference<xml::sax::XDocumentHandler> xDocHandler( + xSaxWriter, UNO_QUERY); + + // construct events object + uno::Reference<XNameAccess> xEvents = + new SvMacroTableEventDescriptor(rMacroTbl,aAutotextEvents); + + // prepare arguments (prepend doc handler to given arguments) + Sequence<Any> aParams(2); + aParams[0] <<= xDocHandler; + aParams[1] <<= xEvents; + + + // get filter component + OUString sFilterComponent = bOasis + ? OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.Writer.XMLOasisAutotextEventsExporter")) + : OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.Writer.XMLAutotextEventsExporter")); + uno::Reference< document::XExporter > xExporter( + xServiceFactory->createInstanceWithArguments( + sFilterComponent, aParams), UNO_QUERY); + OSL_ENSURE( xExporter.is(), + "can't instantiate export filter component" ); + if( xExporter.is() ) + { + // connect model and filter + xExporter->setSourceDocument( xModelComp ); + + // filter! + Sequence<beans::PropertyValue> aFilterProps( 0 ); + uno::Reference < document::XFilter > xFilter( xExporter, + UNO_QUERY ); + xFilter->filter( aFilterProps ); + } + else + nRes = ERR_SWG_WRITE_ERROR; + } + else + nRes = ERR_SWG_WRITE_ERROR; + + // finally, commit stream, sub-storage and storage + uno::Reference < embed::XTransactedObject > xTmpTrans( xRoot, uno::UNO_QUERY ); + if ( xTmpTrans.is() ) + xTmpTrans->commit(); + + if ( !bFileAlreadyOpen ) + { + uno::Reference < embed::XTransactedObject > xTrans( xBlkRoot, uno::UNO_QUERY ); + if ( xTrans.is() ) + xTrans->commit(); + } + + xRoot = 0; + } + catch ( uno::Exception& ) + { + nRes = ERR_SWG_WRITE_ERROR; + } + + if( !bFileAlreadyOpen ) + CloseFile(); + } + else + nRes = ERR_SWG_WRITE_ERROR; + + return nRes; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sw/source/core/swg/makefile.mk b/sw/source/core/swg/makefile.mk new file mode 100644 index 000000000000..c7aa25ec5d24 --- /dev/null +++ b/sw/source/core/swg/makefile.mk @@ -0,0 +1,62 @@ +#************************************************************************* +# +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +# +# Copyright 2000, 2010 Oracle and/or its affiliates. +# +# OpenOffice.org - a multi-platform office productivity suite +# +# This file is part of OpenOffice.org. +# +# 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. +# +# 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). +# +# 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. +# +#************************************************************************* + +PRJ=..$/..$/.. + +PRJNAME=sw +TARGET=swg + +#ENABLE_EXCEPTIONS=TRUE + +# --- Settings ----------------------------------------------------- + +.INCLUDE : $(PRJ)$/inc$/swpre.mk +.INCLUDE : settings.mk +.INCLUDE : $(PRJ)$/inc$/sw.mk + +# --- Files -------------------------------------------------------- +SLOFILES = \ + $(SLO)$/swblocks.obj \ + $(SLO)$/SwXMLBlockImport.obj \ + $(SLO)$/SwXMLSectionList.obj \ + $(SLO)$/SwXMLBlockExport.obj \ + $(SLO)$/SwXMLBlockListContext.obj \ + $(SLO)$/SwXMLTextBlocks.obj \ + $(SLO)$/SwXMLTextBlocks1.obj + +EXCEPTIONSFILES = \ + $(SLO)$/SwXMLBlockImport.obj \ + $(SLO)$/SwXMLSectionList.obj \ + $(SLO)$/SwXMLBlockExport.obj \ + $(SLO)$/SwXMLBlockListContext.obj \ + $(SLO)$/SwXMLTextBlocks.obj \ + $(SLO)$/SwXMLTextBlocks1.obj + +# --- Tagets ------------------------------------------------------- + +.INCLUDE : target.mk + diff --git a/sw/source/core/swg/swblocks.cxx b/sw/source/core/swg/swblocks.cxx new file mode 100644 index 000000000000..4415c3bae337 --- /dev/null +++ b/sw/source/core/swg/swblocks.cxx @@ -0,0 +1,814 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * 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. + * + * 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). + * + * 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. + * + ************************************************************************/ + +// MARKER(update_precomp.py): autogen include statement, do not remove +#include "precompiled_sw.hxx" + + +#include <sfx2/docfilt.hxx> +#include <sot/storage.hxx> +#include <tools/urlobj.hxx> +#include <svl/fstathelper.hxx> +#include <svl/macitem.hxx> +#include <unotools/charclass.hxx> +#include <frmfmt.hxx> +#include <doc.hxx> +#include <docary.hxx> +#include <pam.hxx> +#include <shellio.hxx> +#include <swblocks.hxx> +#include <ndtxt.hxx> +#include <mdiexp.hxx> // Progress +#include <SwXMLTextBlocks.hxx> +#include <docsh.hxx> +#include <swunohelper.hxx> + +#include <statstr.hrc> +#include <swerror.h> + +SV_IMPL_OP_PTRARR_SORT( SwBlockNames, SwBlockName* ); + +////////////////////////////////////////////////////////////////////////// + +// Hash-Code errechnen (muss nicht eindeutig sein) + + +USHORT SwImpBlocks::Hash( const String& r ) +{ + USHORT n = 0; + xub_StrLen nLen = r.Len(); + if( nLen > 8 ) + nLen = 8; + const sal_Unicode* p = r.GetBuffer(); + while( nLen-- ) + n = ( n << 1 ) + *p++; + return n; +} + + +SwBlockName::SwBlockName( const String& rShort, const String& rLong, long n ) + : nPos( n ), aShort( rShort ), aLong( rLong ), aPackageName (rShort), + bIsOnlyTxtFlagInit( FALSE ), bIsOnlyTxt( FALSE ) +{ + nHashS = SwImpBlocks::Hash( rShort ); + nHashL = SwImpBlocks::Hash( rLong ); +} +SwBlockName::SwBlockName( const String& rShort, const String& rLong, const String& rPackageName) + : nPos( 0 ), aShort( rShort ), aLong( rLong ), aPackageName (rPackageName), + bIsOnlyTxtFlagInit( FALSE ), bIsOnlyTxt( FALSE ) +{ + nHashS = SwImpBlocks::Hash( rShort ); + nHashL = SwImpBlocks::Hash( rLong ); +} + + +// Ist die angegebene Datei ein Storage oder gibt es sie nicht? + +short SwImpBlocks::GetFileType( const String& rFile ) +{ + if( !FStatHelper::IsDocument( rFile ) ) + return SWBLK_NO_FILE; + if( SwXMLTextBlocks::IsFileUCBStorage( rFile ) ) + return SWBLK_XML; + if( SvStorage::IsStorageFile( rFile ) ) + return SWBLK_SW3; + //otherwise return NONE + return SWBLK_NONE; +} + + +SwImpBlocks::SwImpBlocks( const String& rFile, BOOL ) + : aFile( rFile ), pDoc( 0 ), nCur( (USHORT)-1 ), + bReadOnly( TRUE ), bInPutMuchBlocks( FALSE ) +{ + FStatHelper::GetModifiedDateTimeOfFile( rFile, + &aDateModified, &aTimeModified ); + INetURLObject aObj(rFile); + aObj.setExtension( aEmptyStr ); + aName = aObj.GetBase(); +} + + +SwImpBlocks::~SwImpBlocks() +{ + aNames.DeleteAndDestroy( 0, aNames.Count() ); +} + +// Loeschen des Inhaltes des Dokuments +void SwImpBlocks::ClearDoc() +{ + pDoc->ClearDoc(); +} + +ULONG SwImpBlocks::GetDocForConversion( USHORT n ) +{ + return GetDoc( n ); +} + +// Erzeugen eines PaMs, der das ganze Dokument umfasst +SwPaM* SwImpBlocks::MakePaM() +{ + SwPaM* pPam = new SwPaM( pDoc->GetNodes().GetEndOfContent() ); + pPam->Move( fnMoveBackward, fnGoDoc ); + pPam->SetMark(); + pPam->Move( fnMoveForward, fnGoDoc ); + pPam->Exchange(); + return pPam; +} + + +USHORT SwImpBlocks::GetCount() const +{ + return aNames.Count(); +} + +// Case Insensitive +USHORT SwImpBlocks::GetIndex( const String& rShort ) const +{ + String s( GetAppCharClass().upper( rShort ) ); + USHORT nHash = Hash( s ); + for( USHORT i = 0; i < aNames.Count(); i++ ) + { + SwBlockName* pName = aNames[ i ]; + if( pName->nHashS == nHash + && pName->aShort == s ) + return i; + } + return (USHORT) -1; +} + + +USHORT SwImpBlocks::GetLongIndex( const String& rLong ) const +{ + USHORT nHash = Hash( rLong ); + for( USHORT i = 0; i < aNames.Count(); i++ ) + { + SwBlockName* pName = aNames[ i ]; + if( pName->nHashL == nHash + && pName->aLong == rLong ) + return i; + } + return (USHORT) -1; +} + + +const String& SwImpBlocks::GetShortName( USHORT n ) const +{ + if( n < aNames.Count() ) + return aNames[ n ]->aShort; + return aEmptyStr; +} + + +const String& SwImpBlocks::GetLongName( USHORT n ) const +{ + if( n < aNames.Count() ) + return aNames[ n ]->aLong; + return aEmptyStr; +} + +const String& SwImpBlocks::GetPackageName( USHORT n ) const +{ + if( n < aNames.Count() ) + return aNames[ n ]->aPackageName; + return aEmptyStr; +} + +void SwImpBlocks::AddName( const String& rShort, const String& rLong, + BOOL bOnlyTxt ) +{ + USHORT nIdx = GetIndex( rShort ); + if( nIdx != (USHORT) -1 ) + aNames.DeleteAndDestroy( nIdx ); + SwBlockName* pNew = new SwBlockName( rShort, rLong, 0L ); + pNew->bIsOnlyTxtFlagInit = TRUE; + pNew->bIsOnlyTxt = bOnlyTxt; + aNames.C40_PTR_INSERT( SwBlockName, pNew ); +} + + + +BOOL SwImpBlocks::IsFileChanged() const +{ + Date aTempDateModified( aDateModified ); + Time aTempTimeModified( aTimeModified ); + return FStatHelper::GetModifiedDateTimeOfFile( aFile, + &aTempDateModified, &aTempTimeModified ) && + ( aDateModified != aTempDateModified || + aTimeModified != aTempTimeModified ); +} + + +void SwImpBlocks::Touch() +{ + FStatHelper::GetModifiedDateTimeOfFile( aFile, + &aDateModified, &aTimeModified ); +} + +BOOL SwImpBlocks::IsOnlyTextBlock( const String& ) const +{ + return FALSE; +} + +ULONG SwImpBlocks::GetMacroTable( USHORT, SvxMacroTableDtor&, sal_Bool ) +{ + return 0; +} + +ULONG SwImpBlocks::SetMacroTable( USHORT , + const SvxMacroTableDtor& , sal_Bool ) +{ + return 0; +} + +BOOL SwImpBlocks::PutMuchEntries( BOOL ) +{ + return FALSE; +} + +//////////////////////////////////////////////////////////////////////////// + + +SwTextBlocks::SwTextBlocks( const String& rFile ) + : pImp( 0 ), nErr( 0 ) +{ + INetURLObject aObj(rFile); + String sFileName = aObj.GetMainURL( INetURLObject::NO_DECODE ); + switch( SwImpBlocks::GetFileType( rFile ) ) + { + //case SWBLK_SW2: pImp = new Sw2TextBlocks( sFileName ); break; + //case SWBLK_SW3: pImp = new Sw3TextBlocks( sFileName ); break; + case SWBLK_XML: pImp = new SwXMLTextBlocks( sFileName ); break; + case SWBLK_NO_FILE: pImp = new SwXMLTextBlocks( sFileName ); break; + } + if( !pImp ) + nErr = ERR_SWG_FILE_FORMAT_ERROR; +} + +SwTextBlocks::~SwTextBlocks() +{ + delete pImp; +} + +const String& SwTextBlocks::GetName() +{ + return pImp ? pImp->aName : aEmptyStr; +} + + +void SwTextBlocks::SetName( const String& r ) +{ + if( pImp ) + pImp->SetName( r ); +} + + +BOOL SwTextBlocks::IsOld() const +{ + if (pImp) + { + short nType = pImp->GetFileType(); + if (SWBLK_SW3 == nType || SWBLK_SW2 == nType ) + return TRUE; + } + return FALSE; +} + + +/* +ULONG SwTextBlocks::ConvertToNew() +{ + // Wir nehmen die aktuelle Datei, benennen diese in .BAK um + // und kreieren den neuen Storage + if( IsOld() ) + { + // Erst mal muessen wir die Datei freigeben + short nType = pImp->GetFileType(); + Sw2TextBlocks *pTwo = NULL; + Sw3TextBlocks *pThree = NULL; + SwImpBlocks *pOld = NULL; + + pImp->nCur = (USHORT) -1; + String aName( pImp->aFile ); + delete pImp; pImp = NULL; + // Jetzt wird umbenannt + INetURLObject aOldFull( aName ); + INetURLObject aNewFull( aName ); + + aOldFull.SetExtension( String::CreateFromAscii("bak") ); + String aOld( aOldFull.GetMainURL( INetURLObject::NO_DECODE ) ); + String aNew( aNewFull.GetMainURL( INetURLObject::NO_DECODE ) ); + + BOOL bError = !SWUnoHelper::UCB_CopyFile( aNew, aOld, TRUE ); + if( bError ) + { + if (nType == SWBLK_SW2) + pImp = new Sw2TextBlocks( aOld ); + else + pImp = new Sw3TextBlocks( aOld ); + return nErr = ERR_SWG_CANNOT_WRITE; + } + + // Die Datei ist erfolgreich umbenannt. Jetzt wird der Storage + // aufgesetzt + if (nType == SWBLK_SW2) + pOld = pTwo = new Sw2TextBlocks( aOld ); + else + pOld = pThree = new Sw3TextBlocks( aOld ); + SwXMLTextBlocks* pNew = new SwXMLTextBlocks( aName ); + pNew->SetName ( pOld->GetName()); + // Wir kopieren den Doc-Ptr in das alte System + // den alten SvPersist heben wir uns aber auf, + // da dieser die ganze Zeit leben bleibt + // und lesen die Dateivorlagen erneut ein + SvPersist* pPersist2 = pOld->pDoc->GetPersist(); + if (SWBLK_SW2 == nType ) + { + delete pOld->pDoc; + pOld->pDoc = pNew->pDoc;nLinkCt + nErr = pTwo->LoadDoc(); + } + else + { + nErr = pThree->OpenFile ( TRUE ); + // Within this call, Sw3IoImp::SetDoc calls RemoveLink + // on the old document, and deletes it if the + // ref count is now zero + pThree->SetDoc ( pNew->pDoc ); + pOld->pDoc->AddLink(); + } + if( !nErr && 0 == ( nErr = pNew->OpenFile( FALSE )) ) + { + nErr = pNew->SetConvertMode( TRUE ); + // jetzt werden die Bausteine einfach umkopiert! + if( !nErr ) + { + if (SWBLK_SW2 == nType) + pTwo->StatLineStartPercent(); + USHORT nCount = pOld->GetCount(); + for( USHORT i = 0; i < nCount; i++ ) + { + pNew->ClearDoc(); + String aShort( pOld->GetShortName( i ) ); + String aLong( pOld->GetLongName( i ) ); + pNew->AddName( aShort, aLong ); + if ( SWBLK_SW3 == nType && pThree->IsOnlyTextBlock(aShort) ) + { + String sText; + pThree->GetText( aShort, sText ); + pNew->PutText( aShort, aLong, sText ); + } + else + { + if (SWBLK_SW2 == nType ) + { + // I think this is how it should work (!!!!!!) mtg + pNew->pDoc->SetPersist( pPersist2 ); + } + nErr = pOld->GetDocForConversion( i ); + if( nErr ) + break; + nErr = pNew->BeginPutDoc( aShort, aLong ); + if( nErr ) + break; + nErr = pNew->PutDoc(); + if( nErr ) + break; + } + + // convert macros, too + SvxMacroTableDtor aMacroTable; + pOld->GetMacroTable( i, aMacroTable, sal_True ); + pNew->SetMacroTable( i, aMacroTable, sal_True ); + + if (SWBLK_SW2 == nType ) + pNew->pDoc->SetPersist( 0 ); + } + if (SWBLK_SW2 == nType ) + ::EndProgress( pOld->pDoc->GetDocShell() ); + } + if( !nErr ) + nErr = pNew->SetConvertMode( FALSE ); + } + if ( SWBLK_SW3 == nType ) + { + pThree->CloseFile(); + } + else + { + // Haben wir es geschafft? + pOld->pDoc = NULL; + } + pNew->ClearDoc(); + if( !nErr ) + { + delete pOld; + pImp = pNew; + SWUnoHelper::UCB_DeleteFile( aOld ); + pNew->MakeBlockList(); + } + else + { + delete pOld; delete pNew; + SWUnoHelper::UCB_DeleteFile( aNew ); + SWUnoHelper::UCB_CopyFile( aOld, aNew, TRUE ); + if ( SWBLK_SW2 == nType ) + pImp = new Sw2TextBlocks( aOld ); + else + pImp = new Sw3TextBlocks( aOld ); + } + pNew->CloseFile(); + FStatHelper::GetModifiedDateTimeOfFile( aNew, + &pImp->aDateModified, &pImp->aTimeModified ); + } + return nErr; +} */ + + +USHORT SwTextBlocks::GetCount() const +{ + return pImp ? pImp->GetCount() : 0; +} + + +USHORT SwTextBlocks::GetIndex( const String& r ) const +{ + return pImp ? pImp->GetIndex( r ) : (USHORT) -1; +} + + +USHORT SwTextBlocks::GetLongIndex( const String& r ) const +{ + return pImp ? (USHORT)(pImp->GetLongIndex( r )) : (USHORT) -1; +} + + +const String& SwTextBlocks::GetShortName( USHORT n ) const +{ + if( pImp ) + return pImp->GetShortName( n ); + return aEmptyStr; +} + + +const String& SwTextBlocks::GetLongName( USHORT n ) const +{ + if( pImp ) + return pImp->GetLongName( n ); + return aEmptyStr; +} + + +BOOL SwTextBlocks::Delete( USHORT n ) +{ + if( pImp && !pImp->bInPutMuchBlocks ) + { + if( pImp->IsFileChanged() ) + nErr = ERR_TXTBLOCK_NEWFILE_ERROR; + else if( 0 == (nErr = pImp->OpenFile( FALSE ) )) + { + nErr = pImp->Delete( n ); + if( !nErr ) + pImp->aNames.DeleteAndDestroy( n ); + if( n == pImp->nCur ) + pImp->nCur = (USHORT) -1; + if( !nErr ) + nErr = pImp->MakeBlockList(); + } + pImp->CloseFile(); + pImp->Touch(); + + return BOOL( nErr == 0 ); + } + return FALSE; +} + + +USHORT SwTextBlocks::Rename( USHORT n, const String* s, const String* l ) +{ + USHORT nIdx = (USHORT)-1; + if( pImp && !pImp->bInPutMuchBlocks ) + { + pImp->nCur = nIdx; + String aNew, aLong; + if( s ) + aNew = aLong = *s; + if( l ) + aLong = *l; + if( !aNew.Len() ) + { + OSL_ENSURE( !this, "Kein Kurzname in Rename angegeben" ); + nErr = ERR_SWG_INTERNAL_ERROR; return (USHORT) -1; + } + + if( pImp->IsFileChanged() ) + nErr = ERR_TXTBLOCK_NEWFILE_ERROR; + else if( 0 == ( nErr = pImp->OpenFile( FALSE ))) + { + // Vorher den neuen Eintrag in die Liste setzen! + GetAppCharClass().toUpper( aNew ); + nErr = pImp->Rename( n, aNew, aLong ); + if( !nErr ) + { + BOOL bOnlyTxt = pImp->aNames[ n ]->bIsOnlyTxt; + pImp->aNames.DeleteAndDestroy( n ); + pImp->AddName( aNew, aLong, bOnlyTxt ); + nErr = pImp->MakeBlockList(); + } + } + pImp->CloseFile(); + pImp->Touch(); + if( !nErr ) + nIdx = pImp->GetIndex( aNew ); + } + return nIdx; +} + +ULONG SwTextBlocks::CopyBlock( SwTextBlocks& rSource, String& rSrcShort, + const String& rLong ) +{ + BOOL bIsOld = FALSE; + if (rSource.pImp) + { + short nType = rSource.pImp->GetFileType(); + if (SWBLK_SW2 == nType || SWBLK_SW3 == nType ) + bIsOld = TRUE; + } + if( bIsOld ) //rSource.IsOld() ) + nErr = ERR_SWG_OLD_GLOSSARY; + else if( pImp->bInPutMuchBlocks ) + nErr = ERR_SWG_INTERNAL_ERROR; + else + nErr = pImp->CopyBlock(*rSource.pImp, rSrcShort, rLong); + return nErr; +} + +BOOL SwTextBlocks::BeginGetDoc( USHORT n ) +{ + if( pImp && !pImp->bInPutMuchBlocks ) + { +// diese Optimierierung darf es nicht mehr geben. OLE-Objecte muessen auf +// ihre SubStorages zugreifem koennen! +// if( n == pImp->nCur ) +// return TRUE; + + if( pImp->IsFileChanged() ) + nErr = ERR_TXTBLOCK_NEWFILE_ERROR; + else if( 0 == ( nErr = pImp->OpenFile( TRUE ))) + { + pImp->ClearDoc(); + nErr = pImp->GetDoc( n ); + if( nErr ) + pImp->nCur = (USHORT)-1; + else + pImp->nCur = n; + } + return BOOL( nErr == 0 ); + } + return FALSE; +} + + +void SwTextBlocks::EndGetDoc() +{ + if( pImp && !pImp->bInPutMuchBlocks ) + pImp->CloseFile(); +} + + +BOOL SwTextBlocks::BeginPutDoc( const String& s, const String& l ) +{ + if( pImp ) + { + BOOL bOk = pImp->bInPutMuchBlocks; + if( !bOk ) + { + if( pImp->IsFileChanged() ) + nErr = ERR_TXTBLOCK_NEWFILE_ERROR; + else + nErr = pImp->OpenFile( FALSE ); + bOk = 0 == nErr; + } + if( bOk ) + { + String aNew( s ); + GetAppCharClass().toUpper( aNew ); + nErr = pImp->BeginPutDoc( aNew, l ); + } + if( nErr ) + pImp->CloseFile(); + } + return 0 == nErr; +} + + +USHORT SwTextBlocks::PutDoc() +{ + USHORT nIdx = (USHORT)-1; + if( pImp ) + { + nErr = pImp->PutDoc(); + if( !nErr ) + { + pImp->nCur = GetIndex( pImp->aShort ); + if( pImp->nCur != (USHORT) -1 ) + pImp->aNames[ pImp->nCur ]->aLong = pImp->aLong; + else + { + pImp->AddName( pImp->aShort, pImp->aLong ); + pImp->nCur = pImp->GetIndex( pImp->aShort ); + } + if( !pImp->bInPutMuchBlocks ) + nErr = pImp->MakeBlockList(); + } + if( !pImp->bInPutMuchBlocks ) + { + pImp->CloseFile(); + pImp->Touch(); + } + nIdx = pImp->nCur; + } + return nIdx; +} + +USHORT SwTextBlocks::PutText( const String& rShort, const String& rName, + const String& rTxt ) +{ + USHORT nIdx = (USHORT) -1; + if( pImp ) + { + BOOL bOk = pImp->bInPutMuchBlocks; + if( !bOk ) + { + if( pImp->IsFileChanged() ) + nErr = ERR_TXTBLOCK_NEWFILE_ERROR; + else + nErr = pImp->OpenFile( FALSE ); + bOk = 0 == nErr; + } + if( bOk ) + { + String aNew( rShort ); + GetAppCharClass().toUpper( aNew ); + nErr = pImp->PutText( aNew, rName, rTxt ); + pImp->nCur = (USHORT) -1; + if( !nErr ) + { + nIdx = GetIndex( pImp->aShort ); + if( nIdx != (USHORT) -1 ) + pImp->aNames[ nIdx ]->aLong = rName; + else + { + pImp->AddName( pImp->aShort, rName, TRUE ); + nIdx = pImp->GetIndex( pImp->aShort ); + } + if( !pImp->bInPutMuchBlocks ) + nErr = pImp->MakeBlockList(); + } + } + if( !pImp->bInPutMuchBlocks ) + { + pImp->CloseFile(); + pImp->Touch(); + } + } + return nIdx; +} + + +SwDoc* SwTextBlocks::GetDoc() +{ + if( pImp ) + return pImp->pDoc; + return 0; +} + + +void SwTextBlocks::ClearDoc() +{ + if( pImp ) + pImp->ClearDoc(); + pImp->nCur = (USHORT) -1; +} + + +const String& SwTextBlocks::GetFileName() const +{ + return pImp->GetFileName(); +} + + +BOOL SwTextBlocks::IsReadOnly() const +{ + return pImp->bReadOnly; +} + +BOOL SwTextBlocks::IsOnlyTextBlock( USHORT nIdx ) const +{ + BOOL bRet = FALSE; + if( pImp && !pImp->bInPutMuchBlocks ) + { + SwBlockName* pBlkNm = pImp->aNames[ nIdx ]; + if( !pBlkNm->bIsOnlyTxtFlagInit && + !pImp->IsFileChanged() && !pImp->OpenFile( TRUE ) ) + { + pBlkNm->bIsOnlyTxt = pImp->IsOnlyTextBlock( pBlkNm->aShort ); + pBlkNm->bIsOnlyTxtFlagInit = TRUE; + pImp->CloseFile(); + } + bRet = pBlkNm->bIsOnlyTxt; + } + return bRet; +} + +BOOL SwTextBlocks::IsOnlyTextBlock( const String& rShort ) const +{ + USHORT nIdx = pImp->GetIndex( rShort ); + if( USHRT_MAX != nIdx ) + { + if( pImp->aNames[ nIdx ]->bIsOnlyTxtFlagInit ) + return pImp->aNames[ nIdx ]->bIsOnlyTxt; + return IsOnlyTextBlock( nIdx ); + } + + OSL_ENSURE( !this, "ungueltiger Name" ); + return FALSE; +} + +BOOL SwTextBlocks::GetMacroTable( USHORT nIdx, SvxMacroTableDtor& rMacroTbl ) +{ + BOOL bRet = TRUE; + if ( pImp && !pImp->bInPutMuchBlocks ) + bRet = ( 0 == pImp->GetMacroTable( nIdx, rMacroTbl ) ); + return bRet; +} + +BOOL SwTextBlocks::SetMacroTable( USHORT nIdx, + const SvxMacroTableDtor& rMacroTbl ) +{ + BOOL bRet = TRUE; + if ( pImp && !pImp->bInPutMuchBlocks ) + bRet = ( 0 == pImp->SetMacroTable( nIdx, rMacroTbl ) ); + return bRet; +} + +BOOL SwTextBlocks::StartPutMuchBlockEntries() +{ + BOOL bRet = FALSE; + if( !IsOld() && pImp ) + bRet = pImp->PutMuchEntries( TRUE ); + return bRet; +} + +void SwTextBlocks::EndPutMuchBlockEntries() +{ + if( pImp ) + pImp->PutMuchEntries( FALSE ); +} + +/*-- 20.09.2004 10:25:33--------------------------------------------------- + + -----------------------------------------------------------------------*/ +String SwTextBlocks::GetBaseURL() const +{ + String sRet; + if(pImp) + sRet = pImp->GetBaseURL(); + return sRet; +} +/*-- 20.09.2004 10:25:33--------------------------------------------------- + + -----------------------------------------------------------------------*/ +void SwTextBlocks::SetBaseURL( const String& rURL ) +{ + if(pImp) + pImp->SetBaseURL(rURL); +} + + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |