1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 // MARKER(update_precomp.py): autogen include statement, do not remove 29 #include "precompiled_xmloff.hxx" 30 31 #include <tools/debug.hxx> 32 #include <com/sun/star/drawing/XLayerManager.hpp> 33 #include <com/sun/star/beans/XPropertySet.hpp> 34 #include <com/sun/star/xml/sax/XAttributeList.hpp> 35 #include <com/sun/star/drawing/XLayerSupplier.hpp> 36 #include <comphelper/extract.hxx> 37 #include <xmloff/xmltoken.hxx> 38 #include <xmloff/xmlimp.hxx> 39 #include "xmloff/xmlnmspe.hxx" 40 #include <xmloff/xmluconv.hxx> 41 #include <xmloff/nmspmap.hxx> 42 #include "layerimp.hxx" 43 44 using ::rtl::OUString; 45 using ::rtl::OUStringBuffer; 46 47 #include "XMLStringBufferImportContext.hxx" 48 49 using namespace ::std; 50 using namespace ::cppu; 51 using namespace ::xmloff::token; 52 using namespace ::com::sun::star; 53 using namespace ::com::sun::star::xml; 54 using namespace ::com::sun::star::xml::sax; 55 using namespace ::com::sun::star::uno; 56 using namespace ::com::sun::star::drawing; 57 using namespace ::com::sun::star::beans; 58 using namespace ::com::sun::star::lang; 59 using namespace ::com::sun::star::container; 60 using ::xmloff::token::IsXMLToken; 61 62 class SdXMLLayerContext : public SvXMLImportContext 63 { 64 public: 65 SdXMLLayerContext( SvXMLImport& rImport, sal_uInt16 nPrefix, const OUString& rLocalName, const Reference< XAttributeList >& xAttrList, const Reference< XNameAccess >& xLayerManager ); 66 virtual ~SdXMLLayerContext(); 67 68 virtual SvXMLImportContext *CreateChildContext( sal_uInt16 nPrefix, const ::rtl::OUString& rLocalName, const Reference< XAttributeList >& xAttrList ); 69 virtual void EndElement(); 70 71 private: 72 ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess > mxLayerManager; 73 ::rtl::OUString msName; 74 ::rtl::OUStringBuffer sDescriptionBuffer; 75 ::rtl::OUStringBuffer sTitleBuffer; 76 }; 77 78 SdXMLLayerContext::SdXMLLayerContext( SvXMLImport& rImport, sal_uInt16 nPrefix, const OUString& rLocalName, const Reference< XAttributeList >& xAttrList, const Reference< XNameAccess >& xLayerManager ) 79 : SvXMLImportContext(rImport, nPrefix, rLocalName) 80 , mxLayerManager( xLayerManager ) 81 { 82 const OUString strName( RTL_CONSTASCII_USTRINGPARAM( "Name" ) ); 83 84 OUString aName; 85 86 const sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0; 87 for(sal_Int16 i=0; i < nAttrCount; i++) 88 { 89 OUString aLocalName; 90 if( GetImport().GetNamespaceMap().GetKeyByAttrName( xAttrList->getNameByIndex( i ), &aLocalName ) == XML_NAMESPACE_DRAW ) 91 { 92 const OUString sValue( xAttrList->getValueByIndex( i ) ); 93 94 if( IsXMLToken( aLocalName, XML_NAME ) ) 95 { 96 msName = sValue; 97 break; // no more attributes needed 98 } 99 } 100 } 101 102 } 103 104 SdXMLLayerContext::~SdXMLLayerContext() 105 { 106 } 107 108 SvXMLImportContext * SdXMLLayerContext::CreateChildContext( sal_uInt16 nPrefix, const ::rtl::OUString& rLocalName, const Reference< XAttributeList >& ) 109 { 110 if( (XML_NAMESPACE_SVG == nPrefix) && IsXMLToken(rLocalName, XML_TITLE) ) 111 { 112 return new XMLStringBufferImportContext( GetImport(), nPrefix, rLocalName, sTitleBuffer); 113 } 114 else if( (XML_NAMESPACE_SVG == nPrefix) && IsXMLToken(rLocalName, XML_DESC) ) 115 { 116 return new XMLStringBufferImportContext( GetImport(), nPrefix, rLocalName, sDescriptionBuffer); 117 } 118 else 119 { 120 return new SvXMLImportContext( GetImport(), nPrefix, rLocalName ); 121 } 122 } 123 124 void SdXMLLayerContext::EndElement() 125 { 126 DBG_ASSERT( msName.getLength(), "xmloff::SdXMLLayerContext::EndElement(), draw:layer element without draw:name!" ); 127 if( msName.getLength() ) try 128 { 129 Reference< XPropertySet > xLayer; 130 131 if( mxLayerManager->hasByName( msName ) ) 132 { 133 mxLayerManager->getByName( msName ) >>= xLayer; 134 DBG_ASSERT( xLayer.is(), "xmloff::SdXMLLayerContext::EndElement(), failed to get existing XLayer!" ); 135 } 136 else 137 { 138 Reference< XLayerManager > xLayerManager( mxLayerManager, UNO_QUERY ); 139 if( xLayerManager.is() ) 140 xLayer = Reference< XPropertySet >::query( xLayerManager->insertNewByIndex( xLayerManager->getCount() ) ); 141 DBG_ASSERT( xLayer.is(), "xmloff::SdXMLLayerContext::EndElement(), failed to create new XLayer!" ); 142 143 if( xLayer.is() ) 144 xLayer->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "Name" ) ), Any( msName ) ); 145 } 146 147 if( xLayer.is() ) 148 { 149 xLayer->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("Title") ), Any( sTitleBuffer.makeStringAndClear() ) ); 150 xLayer->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("Description") ), Any( sDescriptionBuffer.makeStringAndClear() ) ); 151 } 152 } 153 catch( Exception& ) 154 { 155 DBG_ERROR("SdXMLLayerContext::EndElement(), exception caught!"); 156 } 157 } 158 159 160 TYPEINIT1( SdXMLLayerSetContext, SvXMLImportContext ); 161 162 SdXMLLayerSetContext::SdXMLLayerSetContext( SvXMLImport& rImport, sal_uInt16 nPrfx, const rtl::OUString& rLocalName, 163 const com::sun::star::uno::Reference< com::sun::star::xml::sax::XAttributeList>&) 164 : SvXMLImportContext(rImport, nPrfx, rLocalName) 165 { 166 Reference< XLayerSupplier > xLayerSupplier( rImport.GetModel(), UNO_QUERY ); 167 DBG_ASSERT( xLayerSupplier.is(), "xmloff::SdXMLLayerSetContext::SdXMLLayerSetContext(), XModel is not supporting XLayerSupplier!" ); 168 if( xLayerSupplier.is() ) 169 mxLayerManager = xLayerSupplier->getLayerManager(); 170 } 171 172 SdXMLLayerSetContext::~SdXMLLayerSetContext() 173 { 174 } 175 176 SvXMLImportContext * SdXMLLayerSetContext::CreateChildContext( sal_uInt16 nPrefix, const ::rtl::OUString& rLocalName, 177 const com::sun::star::uno::Reference< com::sun::star::xml::sax::XAttributeList>& xAttrList ) 178 { 179 return new SdXMLLayerContext( GetImport(), nPrefix, rLocalName, xAttrList, mxLayerManager ); 180 } 181