xref: /AOO41X/main/xmloff/source/draw/layerimp.cxx (revision 63bba73cc51e0afb45f8a8d578158724bb5afee8)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_xmloff.hxx"
26 
27 #include <tools/debug.hxx>
28 #include <com/sun/star/drawing/XLayerManager.hpp>
29 #include <com/sun/star/beans/XPropertySet.hpp>
30 #include <com/sun/star/xml/sax/XAttributeList.hpp>
31 #include <com/sun/star/drawing/XLayerSupplier.hpp>
32 #include <comphelper/extract.hxx>
33 #include <xmloff/xmltoken.hxx>
34 #include <xmloff/xmlimp.hxx>
35 #include "xmloff/xmlnmspe.hxx"
36 #include <xmloff/xmluconv.hxx>
37 #include <xmloff/nmspmap.hxx>
38 #include "layerimp.hxx"
39 
40 using ::rtl::OUString;
41 using ::rtl::OUStringBuffer;
42 
43 #include "XMLStringBufferImportContext.hxx"
44 
45 using namespace ::std;
46 using namespace ::cppu;
47 using namespace ::xmloff::token;
48 using namespace ::com::sun::star;
49 using namespace ::com::sun::star::xml;
50 using namespace ::com::sun::star::xml::sax;
51 using namespace ::com::sun::star::uno;
52 using namespace ::com::sun::star::drawing;
53 using namespace ::com::sun::star::beans;
54 using namespace ::com::sun::star::lang;
55 using namespace ::com::sun::star::container;
56 using ::xmloff::token::IsXMLToken;
57 
58 class SdXMLLayerContext : public SvXMLImportContext
59 {
60 public:
61     SdXMLLayerContext( SvXMLImport& rImport, sal_uInt16 nPrefix, const OUString& rLocalName, const Reference< XAttributeList >& xAttrList, const Reference< XNameAccess >& xLayerManager );
62     virtual ~SdXMLLayerContext();
63 
64     virtual SvXMLImportContext *CreateChildContext( sal_uInt16 nPrefix, const ::rtl::OUString& rLocalName, const Reference< XAttributeList >& xAttrList );
65     virtual void EndElement();
66 
67 private:
68     ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess > mxLayerManager;
69     ::rtl::OUString msName;
70     ::rtl::OUStringBuffer sDescriptionBuffer;
71     ::rtl::OUStringBuffer sTitleBuffer;
72 };
73 
SdXMLLayerContext(SvXMLImport & rImport,sal_uInt16 nPrefix,const OUString & rLocalName,const Reference<XAttributeList> & xAttrList,const Reference<XNameAccess> & xLayerManager)74 SdXMLLayerContext::SdXMLLayerContext( SvXMLImport& rImport, sal_uInt16 nPrefix, const OUString& rLocalName, const Reference< XAttributeList >& xAttrList, const Reference< XNameAccess >& xLayerManager )
75 : SvXMLImportContext(rImport, nPrefix, rLocalName)
76 , mxLayerManager( xLayerManager )
77 {
78     const OUString strName( RTL_CONSTASCII_USTRINGPARAM( "Name" ) );
79 
80     OUString aName;
81 
82     const sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
83     for(sal_Int16 i=0; i < nAttrCount; i++)
84     {
85         OUString aLocalName;
86         if( GetImport().GetNamespaceMap().GetKeyByAttrName( xAttrList->getNameByIndex( i ), &aLocalName ) == XML_NAMESPACE_DRAW )
87         {
88             const OUString sValue( xAttrList->getValueByIndex( i ) );
89 
90             if( IsXMLToken( aLocalName, XML_NAME ) )
91             {
92                 msName = sValue;
93                 break; // no more attributes needed
94             }
95         }
96     }
97 
98 }
99 
~SdXMLLayerContext()100 SdXMLLayerContext::~SdXMLLayerContext()
101 {
102 }
103 
CreateChildContext(sal_uInt16 nPrefix,const::rtl::OUString & rLocalName,const Reference<XAttributeList> &)104 SvXMLImportContext * SdXMLLayerContext::CreateChildContext( sal_uInt16 nPrefix, const ::rtl::OUString& rLocalName, const Reference< XAttributeList >& )
105 {
106     if( (XML_NAMESPACE_SVG == nPrefix) && IsXMLToken(rLocalName, XML_TITLE) )
107     {
108         return new XMLStringBufferImportContext( GetImport(), nPrefix, rLocalName, sTitleBuffer);
109     }
110     else if( (XML_NAMESPACE_SVG == nPrefix) && IsXMLToken(rLocalName, XML_DESC) )
111     {
112         return new XMLStringBufferImportContext( GetImport(), nPrefix, rLocalName, sDescriptionBuffer);
113     }
114     else
115     {
116         return new SvXMLImportContext( GetImport(), nPrefix, rLocalName );
117     }
118 }
119 
EndElement()120 void SdXMLLayerContext::EndElement()
121 {
122     DBG_ASSERT( msName.getLength(), "xmloff::SdXMLLayerContext::EndElement(), draw:layer element without draw:name!" );
123     if( msName.getLength() ) try
124     {
125         Reference< XPropertySet > xLayer;
126 
127         if( mxLayerManager->hasByName( msName ) )
128         {
129             mxLayerManager->getByName( msName ) >>= xLayer;
130             DBG_ASSERT( xLayer.is(), "xmloff::SdXMLLayerContext::EndElement(), failed to get existing XLayer!" );
131         }
132         else
133         {
134             Reference< XLayerManager > xLayerManager( mxLayerManager, UNO_QUERY );
135             if( xLayerManager.is() )
136                 xLayer = Reference< XPropertySet >::query( xLayerManager->insertNewByIndex( xLayerManager->getCount() ) );
137             DBG_ASSERT( xLayer.is(), "xmloff::SdXMLLayerContext::EndElement(), failed to create new XLayer!" );
138 
139             if( xLayer.is() )
140                 xLayer->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "Name" ) ), Any( msName ) );
141         }
142 
143         if( xLayer.is() )
144         {
145             xLayer->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("Title") ), Any( sTitleBuffer.makeStringAndClear() ) );
146             xLayer->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("Description") ), Any( sDescriptionBuffer.makeStringAndClear() ) );
147         }
148     }
149     catch( Exception& )
150     {
151         DBG_ERROR("SdXMLLayerContext::EndElement(), exception caught!");
152     }
153 }
154 
155 
156 TYPEINIT1( SdXMLLayerSetContext, SvXMLImportContext );
157 
SdXMLLayerSetContext(SvXMLImport & rImport,sal_uInt16 nPrfx,const rtl::OUString & rLocalName,const com::sun::star::uno::Reference<com::sun::star::xml::sax::XAttributeList> &)158 SdXMLLayerSetContext::SdXMLLayerSetContext( SvXMLImport& rImport, sal_uInt16 nPrfx, const rtl::OUString& rLocalName,
159         const com::sun::star::uno::Reference< com::sun::star::xml::sax::XAttributeList>&)
160 : SvXMLImportContext(rImport, nPrfx, rLocalName)
161 {
162     Reference< XLayerSupplier > xLayerSupplier( rImport.GetModel(), UNO_QUERY );
163     DBG_ASSERT( xLayerSupplier.is(), "xmloff::SdXMLLayerSetContext::SdXMLLayerSetContext(), XModel is not supporting XLayerSupplier!" );
164     if( xLayerSupplier.is() )
165         mxLayerManager = xLayerSupplier->getLayerManager();
166 }
167 
~SdXMLLayerSetContext()168 SdXMLLayerSetContext::~SdXMLLayerSetContext()
169 {
170 }
171 
CreateChildContext(sal_uInt16 nPrefix,const::rtl::OUString & rLocalName,const com::sun::star::uno::Reference<com::sun::star::xml::sax::XAttributeList> & xAttrList)172 SvXMLImportContext * SdXMLLayerSetContext::CreateChildContext( sal_uInt16 nPrefix, const ::rtl::OUString& rLocalName,
173         const com::sun::star::uno::Reference< com::sun::star::xml::sax::XAttributeList>& xAttrList )
174 {
175     return new SdXMLLayerContext( GetImport(), nPrefix, rLocalName, xAttrList, mxLayerManager );
176 }
177