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