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 <osl/diagnose.h>
23 : #include <com/sun/star/drawing/XLayerManager.hpp>
24 : #include <com/sun/star/beans/XPropertySet.hpp>
25 : #include <com/sun/star/xml/sax/XAttributeList.hpp>
26 : #include <com/sun/star/drawing/XLayerSupplier.hpp>
27 : #include <comphelper/extract.hxx>
28 : #include <xmloff/xmltoken.hxx>
29 : #include <xmloff/xmlimp.hxx>
30 : #include <xmloff/xmlnmspe.hxx>
31 : #include <xmloff/xmluconv.hxx>
32 : #include <xmloff/nmspmap.hxx>
33 : #include "layerimp.hxx"
34 :
35 :
36 : #include "XMLStringBufferImportContext.hxx"
37 :
38 : using namespace ::std;
39 : using namespace ::cppu;
40 : using namespace ::xmloff::token;
41 : using namespace ::com::sun::star;
42 : using namespace ::com::sun::star::xml;
43 : using namespace ::com::sun::star::xml::sax;
44 : using namespace ::com::sun::star::uno;
45 : using namespace ::com::sun::star::drawing;
46 : using namespace ::com::sun::star::beans;
47 : using namespace ::com::sun::star::lang;
48 : using namespace ::com::sun::star::container;
49 : using ::xmloff::token::IsXMLToken;
50 :
51 : class SdXMLLayerContext : public SvXMLImportContext
52 : {
53 : public:
54 : SdXMLLayerContext( SvXMLImport& rImport, sal_uInt16 nPrefix, const OUString& rLocalName, const Reference< XAttributeList >& xAttrList, const Reference< XNameAccess >& xLayerManager );
55 : virtual ~SdXMLLayerContext();
56 :
57 : virtual SvXMLImportContext *CreateChildContext( sal_uInt16 nPrefix, const OUString& rLocalName, const Reference< XAttributeList >& xAttrList ) SAL_OVERRIDE;
58 : virtual void EndElement() SAL_OVERRIDE;
59 :
60 : private:
61 : ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess > mxLayerManager;
62 : OUString msName;
63 : OUStringBuffer sDescriptionBuffer;
64 : OUStringBuffer sTitleBuffer;
65 : };
66 :
67 233 : SdXMLLayerContext::SdXMLLayerContext( SvXMLImport& rImport, sal_uInt16 nPrefix, const OUString& rLocalName, const Reference< XAttributeList >& xAttrList, const Reference< XNameAccess >& xLayerManager )
68 : : SvXMLImportContext(rImport, nPrefix, rLocalName)
69 233 : , mxLayerManager( xLayerManager )
70 : {
71 233 : const sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
72 233 : for(sal_Int16 i=0; i < nAttrCount; i++)
73 : {
74 233 : OUString aLocalName;
75 233 : if( GetImport().GetNamespaceMap().GetKeyByAttrName( xAttrList->getNameByIndex( i ), &aLocalName ) == XML_NAMESPACE_DRAW )
76 : {
77 233 : const OUString sValue( xAttrList->getValueByIndex( i ) );
78 :
79 233 : if( IsXMLToken( aLocalName, XML_NAME ) )
80 : {
81 233 : msName = sValue;
82 233 : break; // no more attributes needed
83 0 : }
84 : }
85 0 : }
86 :
87 233 : }
88 :
89 466 : SdXMLLayerContext::~SdXMLLayerContext()
90 : {
91 466 : }
92 :
93 0 : SvXMLImportContext * SdXMLLayerContext::CreateChildContext( sal_uInt16 nPrefix, const OUString& rLocalName, const Reference< XAttributeList >& )
94 : {
95 0 : if( (XML_NAMESPACE_SVG == nPrefix) && IsXMLToken(rLocalName, XML_TITLE) )
96 : {
97 0 : return new XMLStringBufferImportContext( GetImport(), nPrefix, rLocalName, sTitleBuffer);
98 : }
99 0 : else if( (XML_NAMESPACE_SVG == nPrefix) && IsXMLToken(rLocalName, XML_DESC) )
100 : {
101 0 : return new XMLStringBufferImportContext( GetImport(), nPrefix, rLocalName, sDescriptionBuffer);
102 : }
103 : else
104 : {
105 0 : return new SvXMLImportContext( GetImport(), nPrefix, rLocalName );
106 : }
107 : }
108 :
109 233 : void SdXMLLayerContext::EndElement()
110 : {
111 : DBG_ASSERT( !msName.isEmpty(), "xmloff::SdXMLLayerContext::EndElement(), draw:layer element without draw:name!" );
112 233 : if( !msName.isEmpty() ) try
113 : {
114 233 : Reference< XPropertySet > xLayer;
115 :
116 233 : if( mxLayerManager->hasByName( msName ) )
117 : {
118 230 : mxLayerManager->getByName( msName ) >>= xLayer;
119 : DBG_ASSERT( xLayer.is(), "xmloff::SdXMLLayerContext::EndElement(), failed to get existing XLayer!" );
120 : }
121 : else
122 : {
123 3 : Reference< XLayerManager > xLayerManager( mxLayerManager, UNO_QUERY );
124 3 : if( xLayerManager.is() )
125 3 : xLayer = Reference< XPropertySet >::query( xLayerManager->insertNewByIndex( xLayerManager->getCount() ) );
126 : DBG_ASSERT( xLayer.is(), "xmloff::SdXMLLayerContext::EndElement(), failed to create new XLayer!" );
127 :
128 3 : if( xLayer.is() )
129 3 : xLayer->setPropertyValue("Name", Any( msName ) );
130 : }
131 :
132 233 : if( xLayer.is() )
133 : {
134 233 : xLayer->setPropertyValue("Title", Any( sTitleBuffer.makeStringAndClear() ) );
135 233 : xLayer->setPropertyValue("Description", Any( sDescriptionBuffer.makeStringAndClear() ) );
136 233 : }
137 : }
138 0 : catch( Exception& )
139 : {
140 : OSL_FAIL("SdXMLLayerContext::EndElement(), exception caught!");
141 : }
142 233 : }
143 :
144 :
145 0 : TYPEINIT1( SdXMLLayerSetContext, SvXMLImportContext );
146 :
147 46 : SdXMLLayerSetContext::SdXMLLayerSetContext( SvXMLImport& rImport, sal_uInt16 nPrfx, const OUString& rLocalName,
148 : const com::sun::star::uno::Reference< com::sun::star::xml::sax::XAttributeList>&)
149 46 : : SvXMLImportContext(rImport, nPrfx, rLocalName)
150 : {
151 46 : Reference< XLayerSupplier > xLayerSupplier( rImport.GetModel(), UNO_QUERY );
152 : DBG_ASSERT( xLayerSupplier.is(), "xmloff::SdXMLLayerSetContext::SdXMLLayerSetContext(), XModel is not supporting XLayerSupplier!" );
153 46 : if( xLayerSupplier.is() )
154 46 : mxLayerManager = xLayerSupplier->getLayerManager();
155 46 : }
156 :
157 92 : SdXMLLayerSetContext::~SdXMLLayerSetContext()
158 : {
159 92 : }
160 :
161 233 : SvXMLImportContext * SdXMLLayerSetContext::CreateChildContext( sal_uInt16 nPrefix, const OUString& rLocalName,
162 : const com::sun::star::uno::Reference< com::sun::star::xml::sax::XAttributeList>& xAttrList )
163 : {
164 233 : return new SdXMLLayerContext( GetImport(), nPrefix, rLocalName, xAttrList, mxLayerManager );
165 : }
166 :
167 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|