Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 :
21 : #include <tools/debug.hxx>
22 : #include <com/sun/star/drawing/XLayerManager.hpp>
23 : #include <com/sun/star/beans/XPropertySet.hpp>
24 : #include <com/sun/star/xml/sax/XAttributeList.hpp>
25 : #include <com/sun/star/drawing/XLayerSupplier.hpp>
26 : #include <comphelper/extract.hxx>
27 : #include <xmloff/xmltoken.hxx>
28 : #include <xmloff/xmlimp.hxx>
29 : #include "xmloff/xmlnmspe.hxx"
30 : #include <xmloff/xmluconv.hxx>
31 : #include <xmloff/nmspmap.hxx>
32 : #include "layerimp.hxx"
33 :
34 : using ::rtl::OUString;
35 : using ::rtl::OUStringBuffer;
36 :
37 : #include "XMLStringBufferImportContext.hxx"
38 :
39 : using namespace ::std;
40 : using namespace ::cppu;
41 : using namespace ::xmloff::token;
42 : using namespace ::com::sun::star;
43 : using namespace ::com::sun::star::xml;
44 : using namespace ::com::sun::star::xml::sax;
45 : using namespace ::com::sun::star::uno;
46 : using namespace ::com::sun::star::drawing;
47 : using namespace ::com::sun::star::beans;
48 : using namespace ::com::sun::star::lang;
49 : using namespace ::com::sun::star::container;
50 : using ::xmloff::token::IsXMLToken;
51 :
52 : class SdXMLLayerContext : public SvXMLImportContext
53 : {
54 : public:
55 : SdXMLLayerContext( SvXMLImport& rImport, sal_uInt16 nPrefix, const OUString& rLocalName, const Reference< XAttributeList >& xAttrList, const Reference< XNameAccess >& xLayerManager );
56 : virtual ~SdXMLLayerContext();
57 :
58 : virtual SvXMLImportContext *CreateChildContext( sal_uInt16 nPrefix, const ::rtl::OUString& rLocalName, const Reference< XAttributeList >& xAttrList );
59 : virtual void EndElement();
60 :
61 : private:
62 : ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess > mxLayerManager;
63 : ::rtl::OUString msName;
64 : ::rtl::OUStringBuffer sDescriptionBuffer;
65 : ::rtl::OUStringBuffer sTitleBuffer;
66 : };
67 :
68 10 : SdXMLLayerContext::SdXMLLayerContext( SvXMLImport& rImport, sal_uInt16 nPrefix, const OUString& rLocalName, const Reference< XAttributeList >& xAttrList, const Reference< XNameAccess >& xLayerManager )
69 : : SvXMLImportContext(rImport, nPrefix, rLocalName)
70 10 : , mxLayerManager( xLayerManager )
71 : {
72 10 : const sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
73 10 : for(sal_Int16 i=0; i < nAttrCount; i++)
74 : {
75 10 : OUString aLocalName;
76 10 : if( GetImport().GetNamespaceMap().GetKeyByAttrName( xAttrList->getNameByIndex( i ), &aLocalName ) == XML_NAMESPACE_DRAW )
77 : {
78 10 : const OUString sValue( xAttrList->getValueByIndex( i ) );
79 :
80 10 : if( IsXMLToken( aLocalName, XML_NAME ) )
81 : {
82 10 : msName = sValue;
83 : break; // no more attributes needed
84 10 : }
85 : }
86 10 : }
87 :
88 10 : }
89 :
90 20 : SdXMLLayerContext::~SdXMLLayerContext()
91 : {
92 20 : }
93 :
94 0 : SvXMLImportContext * SdXMLLayerContext::CreateChildContext( sal_uInt16 nPrefix, const ::rtl::OUString& rLocalName, const Reference< XAttributeList >& )
95 : {
96 0 : if( (XML_NAMESPACE_SVG == nPrefix) && IsXMLToken(rLocalName, XML_TITLE) )
97 : {
98 0 : return new XMLStringBufferImportContext( GetImport(), nPrefix, rLocalName, sTitleBuffer);
99 : }
100 0 : else if( (XML_NAMESPACE_SVG == nPrefix) && IsXMLToken(rLocalName, XML_DESC) )
101 : {
102 0 : return new XMLStringBufferImportContext( GetImport(), nPrefix, rLocalName, sDescriptionBuffer);
103 : }
104 : else
105 : {
106 0 : return new SvXMLImportContext( GetImport(), nPrefix, rLocalName );
107 : }
108 : }
109 :
110 10 : void SdXMLLayerContext::EndElement()
111 : {
112 : DBG_ASSERT( !msName.isEmpty(), "xmloff::SdXMLLayerContext::EndElement(), draw:layer element without draw:name!" );
113 10 : if( !msName.isEmpty() ) try
114 : {
115 10 : Reference< XPropertySet > xLayer;
116 :
117 10 : if( mxLayerManager->hasByName( msName ) )
118 : {
119 10 : mxLayerManager->getByName( msName ) >>= xLayer;
120 : DBG_ASSERT( xLayer.is(), "xmloff::SdXMLLayerContext::EndElement(), failed to get existing XLayer!" );
121 : }
122 : else
123 : {
124 0 : Reference< XLayerManager > xLayerManager( mxLayerManager, UNO_QUERY );
125 0 : if( xLayerManager.is() )
126 0 : xLayer = Reference< XPropertySet >::query( xLayerManager->insertNewByIndex( xLayerManager->getCount() ) );
127 : DBG_ASSERT( xLayer.is(), "xmloff::SdXMLLayerContext::EndElement(), failed to create new XLayer!" );
128 :
129 0 : if( xLayer.is() )
130 0 : xLayer->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "Name" ) ), Any( msName ) );
131 : }
132 :
133 10 : if( xLayer.is() )
134 : {
135 10 : xLayer->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("Title") ), Any( sTitleBuffer.makeStringAndClear() ) );
136 10 : xLayer->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("Description") ), Any( sDescriptionBuffer.makeStringAndClear() ) );
137 10 : }
138 : }
139 0 : catch( Exception& )
140 : {
141 : OSL_FAIL("SdXMLLayerContext::EndElement(), exception caught!");
142 : }
143 10 : }
144 :
145 :
146 0 : TYPEINIT1( SdXMLLayerSetContext, SvXMLImportContext );
147 :
148 2 : SdXMLLayerSetContext::SdXMLLayerSetContext( SvXMLImport& rImport, sal_uInt16 nPrfx, const rtl::OUString& rLocalName,
149 : const com::sun::star::uno::Reference< com::sun::star::xml::sax::XAttributeList>&)
150 2 : : SvXMLImportContext(rImport, nPrfx, rLocalName)
151 : {
152 2 : Reference< XLayerSupplier > xLayerSupplier( rImport.GetModel(), UNO_QUERY );
153 : DBG_ASSERT( xLayerSupplier.is(), "xmloff::SdXMLLayerSetContext::SdXMLLayerSetContext(), XModel is not supporting XLayerSupplier!" );
154 2 : if( xLayerSupplier.is() )
155 2 : mxLayerManager = xLayerSupplier->getLayerManager();
156 2 : }
157 :
158 4 : SdXMLLayerSetContext::~SdXMLLayerSetContext()
159 : {
160 4 : }
161 :
162 10 : SvXMLImportContext * SdXMLLayerSetContext::CreateChildContext( sal_uInt16 nPrefix, const ::rtl::OUString& rLocalName,
163 : const com::sun::star::uno::Reference< com::sun::star::xml::sax::XAttributeList>& xAttrList )
164 : {
165 10 : return new SdXMLLayerContext( GetImport(), nPrefix, rLocalName, xAttrList, mxLayerManager );
166 : }
167 :
168 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|