1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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"
36 #include "XMLStringBufferImportContext.hxx"
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
;
51 class SdXMLLayerContext
: public SvXMLImportContext
54 SdXMLLayerContext( SvXMLImport
& rImport
, sal_uInt16 nPrefix
, const OUString
& rLocalName
, const Reference
< XAttributeList
>& xAttrList
, const Reference
< XNameAccess
>& xLayerManager
);
55 virtual ~SdXMLLayerContext();
57 virtual SvXMLImportContext
*CreateChildContext( sal_uInt16 nPrefix
, const OUString
& rLocalName
, const Reference
< XAttributeList
>& xAttrList
) SAL_OVERRIDE
;
58 virtual void EndElement() SAL_OVERRIDE
;
61 ::com::sun::star::uno::Reference
< ::com::sun::star::container::XNameAccess
> mxLayerManager
;
63 OUStringBuffer sDescriptionBuffer
;
64 OUStringBuffer sTitleBuffer
;
67 SdXMLLayerContext::SdXMLLayerContext( SvXMLImport
& rImport
, sal_uInt16 nPrefix
, const OUString
& rLocalName
, const Reference
< XAttributeList
>& xAttrList
, const Reference
< XNameAccess
>& xLayerManager
)
68 : SvXMLImportContext(rImport
, nPrefix
, rLocalName
)
69 , mxLayerManager( xLayerManager
)
71 const sal_Int16 nAttrCount
= xAttrList
.is() ? xAttrList
->getLength() : 0;
72 for(sal_Int16 i
=0; i
< nAttrCount
; i
++)
75 if( GetImport().GetNamespaceMap().GetKeyByAttrName( xAttrList
->getNameByIndex( i
), &aLocalName
) == XML_NAMESPACE_DRAW
)
77 const OUString
sValue( xAttrList
->getValueByIndex( i
) );
79 if( IsXMLToken( aLocalName
, XML_NAME
) )
82 break; // no more attributes needed
89 SdXMLLayerContext::~SdXMLLayerContext()
93 SvXMLImportContext
* SdXMLLayerContext::CreateChildContext( sal_uInt16 nPrefix
, const OUString
& rLocalName
, const Reference
< XAttributeList
>& )
95 if( (XML_NAMESPACE_SVG
== nPrefix
) && IsXMLToken(rLocalName
, XML_TITLE
) )
97 return new XMLStringBufferImportContext( GetImport(), nPrefix
, rLocalName
, sTitleBuffer
);
99 else if( (XML_NAMESPACE_SVG
== nPrefix
) && IsXMLToken(rLocalName
, XML_DESC
) )
101 return new XMLStringBufferImportContext( GetImport(), nPrefix
, rLocalName
, sDescriptionBuffer
);
105 return new SvXMLImportContext( GetImport(), nPrefix
, rLocalName
);
109 void SdXMLLayerContext::EndElement()
111 DBG_ASSERT( !msName
.isEmpty(), "xmloff::SdXMLLayerContext::EndElement(), draw:layer element without draw:name!" );
112 if( !msName
.isEmpty() ) try
114 Reference
< XPropertySet
> xLayer
;
116 if( mxLayerManager
->hasByName( msName
) )
118 mxLayerManager
->getByName( msName
) >>= xLayer
;
119 DBG_ASSERT( xLayer
.is(), "xmloff::SdXMLLayerContext::EndElement(), failed to get existing XLayer!" );
123 Reference
< XLayerManager
> xLayerManager( mxLayerManager
, UNO_QUERY
);
124 if( xLayerManager
.is() )
125 xLayer
= Reference
< XPropertySet
>::query( xLayerManager
->insertNewByIndex( xLayerManager
->getCount() ) );
126 DBG_ASSERT( xLayer
.is(), "xmloff::SdXMLLayerContext::EndElement(), failed to create new XLayer!" );
129 xLayer
->setPropertyValue("Name", Any( msName
) );
134 xLayer
->setPropertyValue("Title", Any( sTitleBuffer
.makeStringAndClear() ) );
135 xLayer
->setPropertyValue("Description", Any( sDescriptionBuffer
.makeStringAndClear() ) );
140 OSL_FAIL("SdXMLLayerContext::EndElement(), exception caught!");
145 TYPEINIT1( SdXMLLayerSetContext
, SvXMLImportContext
);
147 SdXMLLayerSetContext::SdXMLLayerSetContext( SvXMLImport
& rImport
, sal_uInt16 nPrfx
, const OUString
& rLocalName
,
148 const com::sun::star::uno::Reference
< com::sun::star::xml::sax::XAttributeList
>&)
149 : SvXMLImportContext(rImport
, nPrfx
, rLocalName
)
151 Reference
< XLayerSupplier
> xLayerSupplier( rImport
.GetModel(), UNO_QUERY
);
152 DBG_ASSERT( xLayerSupplier
.is(), "xmloff::SdXMLLayerSetContext::SdXMLLayerSetContext(), XModel is not supporting XLayerSupplier!" );
153 if( xLayerSupplier
.is() )
154 mxLayerManager
= xLayerSupplier
->getLayerManager();
157 SdXMLLayerSetContext::~SdXMLLayerSetContext()
161 SvXMLImportContext
* SdXMLLayerSetContext::CreateChildContext( sal_uInt16 nPrefix
, const OUString
& rLocalName
,
162 const com::sun::star::uno::Reference
< com::sun::star::xml::sax::XAttributeList
>& xAttrList
)
164 return new SdXMLLayerContext( GetImport(), nPrefix
, rLocalName
, xAttrList
, mxLayerManager
);
167 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */