1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: XMLImageMapContext.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_xmloff.hxx"
33 #include "XMLImageMapContext.hxx"
34 #include <rtl/ustrbuf.hxx>
35 #include <com/sun/star/uno/Reference.h>
36 #include <com/sun/star/beans/XPropertySet.hpp>
37 #ifndef _COM_SUN_STAR_BEANS_XPROPERTYSETINFO_HPP
38 #include <com/sun/star/beans/XPropertySetInfo.hpp>
40 #include <com/sun/star/xml/sax/XAttributeList.hpp>
41 #include <com/sun/star/container/XIndexContainer.hpp>
42 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
43 #include <com/sun/star/drawing/PointSequenceSequence.hpp>
45 #ifndef _COM_SUN_STAR_DOCUMENT_XEVENTSSUPPLIER_HPP
46 #include <com/sun/star/document/XEventsSupplier.hpp>
48 #include <com/sun/star/awt/Rectangle.hpp>
49 #include <xmloff/xmltoken.hxx>
50 #include <xmloff/xmlimp.hxx>
51 #include <xmloff/xmltkmap.hxx>
52 #include "xmlnmspe.hxx"
53 #include <xmloff/nmspmap.hxx>
54 #include <xmloff/xmluconv.hxx>
55 #include "xexptran.hxx"
56 #include "xmlerror.hxx"
57 #include <xmloff/XMLEventsImportContext.hxx>
58 #include "XMLStringBufferImportContext.hxx"
59 #include <tools/debug.hxx>
62 using namespace ::com::sun::star
;
63 using namespace ::xmloff::token
;
65 using ::rtl::OUString
;
66 using ::rtl::OUStringBuffer
;
67 using ::com::sun::star::beans::XPropertySet
;
68 using ::com::sun::star::beans::XPropertySetInfo
;
69 using ::com::sun::star::container::XIndexContainer
;
70 using ::com::sun::star::lang::XMultiServiceFactory
;
71 using ::com::sun::star::uno::Reference
;
72 using ::com::sun::star::uno::UNO_QUERY
;
73 using ::com::sun::star::xml::sax::XAttributeList
;
74 using ::com::sun::star::uno::XInterface
;
75 using ::com::sun::star::uno::Any
;
76 using ::com::sun::star::drawing::PointSequenceSequence
;
77 using ::com::sun::star::document::XEventsSupplier
;
85 XML_TOK_IMAP_CENTER_X
,
86 XML_TOK_IMAP_CENTER_Y
,
97 static __FAR_DATA SvXMLTokenMapEntry aImageMapObjectTokenMap
[] =
99 { XML_NAMESPACE_XLINK
, XML_HREF
, XML_TOK_IMAP_URL
},
100 { XML_NAMESPACE_OFFICE
, XML_NAME
, XML_TOK_IMAP_NAME
},
101 { XML_NAMESPACE_DRAW
, XML_NOHREF
, XML_TOK_IMAP_NOHREF
},
102 { XML_NAMESPACE_SVG
, XML_X
, XML_TOK_IMAP_X
},
103 { XML_NAMESPACE_SVG
, XML_Y
, XML_TOK_IMAP_Y
},
104 { XML_NAMESPACE_SVG
, XML_CX
, XML_TOK_IMAP_CENTER_X
},
105 { XML_NAMESPACE_SVG
, XML_CY
, XML_TOK_IMAP_CENTER_Y
},
106 { XML_NAMESPACE_SVG
, XML_WIDTH
, XML_TOK_IMAP_WIDTH
},
107 { XML_NAMESPACE_SVG
, XML_HEIGHT
, XML_TOK_IMAP_HEIGTH
},
108 { XML_NAMESPACE_SVG
, XML_R
, XML_TOK_IMAP_RADIUS
},
109 { XML_NAMESPACE_SVG
, XML_VIEWBOX
, XML_TOK_IMAP_VIEWBOX
},
110 { XML_NAMESPACE_DRAW
, XML_POINTS
, XML_TOK_IMAP_POINTS
},
111 { XML_NAMESPACE_OFFICE
, XML_TARGET_FRAME_NAME
, XML_TOK_IMAP_TARGET
},
117 class XMLImageMapObjectContext
: public SvXMLImportContext
122 const ::rtl::OUString sBoundary
;
123 const ::rtl::OUString sCenter
;
124 const ::rtl::OUString sTitle
;
125 const ::rtl::OUString sDescription
;
126 const ::rtl::OUString sImageMap
;
127 const ::rtl::OUString sIsActive
;
128 const ::rtl::OUString sName
;
129 const ::rtl::OUString sPolygon
;
130 const ::rtl::OUString sRadius
;
131 const ::rtl::OUString sTarget
;
132 const ::rtl::OUString sURL
;
134 ::rtl::OUString sServiceName
;
136 Reference
<XIndexContainer
> xImageMap
; /// the image map
137 Reference
<XPropertySet
> xMapEntry
; /// one map-entry (one area)
139 ::rtl::OUString sUrl
;
140 ::rtl::OUString sTargt
;
141 ::rtl::OUStringBuffer sDescriptionBuffer
;
142 ::rtl::OUStringBuffer sTitleBuffer
;
143 ::rtl::OUString sNam
;
151 XMLImageMapObjectContext(
152 SvXMLImport
& rImport
,
154 const ::rtl::OUString
& rLocalName
,
155 ::com::sun::star::uno::Reference
<
156 ::com::sun::star::container::XIndexContainer
> xMap
,
157 const sal_Char
* pServiceName
);
160 const ::com::sun::star::uno::Reference
<
161 ::com::sun::star::xml::sax::XAttributeList
>& xAttrList
);
165 SvXMLImportContext
*CreateChildContext(
167 const ::rtl::OUString
& rLocalName
,
168 const ::com::sun::star::uno::Reference
<
169 ::com::sun::star::xml::sax::XAttributeList
> & xAttrList
);
173 virtual void ProcessAttribute(
174 enum XMLImageMapToken eToken
,
175 const ::rtl::OUString
& rValue
);
177 virtual void Prepare(
178 ::com::sun::star::uno::Reference
<
179 ::com::sun::star::beans::XPropertySet
> & rPropertySet
);
183 TYPEINIT1( XMLImageMapObjectContext
, SvXMLImportContext
);
185 XMLImageMapObjectContext::XMLImageMapObjectContext(
186 SvXMLImport
& rImport
,
188 const OUString
& rLocalName
,
189 Reference
<XIndexContainer
> xMap
,
190 const sal_Char
* pServiceName
) :
191 SvXMLImportContext(rImport
, nPrefix
, rLocalName
),
192 sBoundary(RTL_CONSTASCII_USTRINGPARAM("Boundary")),
193 sCenter(RTL_CONSTASCII_USTRINGPARAM("Center")),
194 sTitle(RTL_CONSTASCII_USTRINGPARAM("Title")),
195 sDescription(RTL_CONSTASCII_USTRINGPARAM("Description")),
196 sImageMap(RTL_CONSTASCII_USTRINGPARAM("ImageMap")),
197 sIsActive(RTL_CONSTASCII_USTRINGPARAM("IsActive")),
198 sName(RTL_CONSTASCII_USTRINGPARAM("Name")),
199 sPolygon(RTL_CONSTASCII_USTRINGPARAM("Polygon")),
200 sRadius(RTL_CONSTASCII_USTRINGPARAM("Radius")),
201 sTarget(RTL_CONSTASCII_USTRINGPARAM("Target")),
202 sURL(RTL_CONSTASCII_USTRINGPARAM("URL")),
207 DBG_ASSERT(NULL
!= pServiceName
,
208 "Please supply the image map object service name");
210 Reference
<XMultiServiceFactory
> xFactory(GetImport().GetModel(),UNO_QUERY
);
213 Reference
<XInterface
> xIfc
= xFactory
->createInstance(
214 OUString::createFromAscii(pServiceName
));
215 DBG_ASSERT(xIfc
.is(), "can't create image map object!");
218 Reference
<XPropertySet
> xPropertySet( xIfc
, UNO_QUERY
);
220 xMapEntry
= xPropertySet
;
222 // else: can't create service -> ignore
224 // else: can't even get factory -> ignore
227 void XMLImageMapObjectContext::StartElement(
228 const Reference
<XAttributeList
>& xAttrList
)
230 SvXMLTokenMap
aMap(aImageMapObjectTokenMap
);
232 sal_Int16 nLength
= xAttrList
->getLength();
233 for(sal_Int16 nAttr
= 0; nAttr
< nLength
; nAttr
++)
236 sal_uInt16 nPrefix
= GetImport().GetNamespaceMap().
237 GetKeyByAttrName( xAttrList
->getNameByIndex(nAttr
),
239 OUString sValue
= xAttrList
->getValueByIndex(nAttr
);
242 (enum XMLImageMapToken
)aMap
.Get(nPrefix
, sLocalName
), sValue
);
246 void XMLImageMapObjectContext::EndElement()
248 // only create and insert image map object if validity flag is set
249 // (and we actually have an image map)
250 if ( bValid
&& xImageMap
.is() && xMapEntry
.is() )
253 Prepare( xMapEntry
);
255 // insert into image map
258 xImageMap
->insertByIndex( xImageMap
->getCount(), aAny
);
260 // else: not valid -> don't create and insert
263 SvXMLImportContext
* XMLImageMapObjectContext::CreateChildContext(
265 const OUString
& rLocalName
,
266 const Reference
<XAttributeList
> & xAttrList
)
268 if ( (XML_NAMESPACE_OFFICE
== nPrefix
) &&
269 IsXMLToken(rLocalName
, XML_EVENT_LISTENERS
) )
271 Reference
<XEventsSupplier
> xEvents( xMapEntry
, UNO_QUERY
);
272 return new XMLEventsImportContext(
273 GetImport(), nPrefix
, rLocalName
, xEvents
);
275 else if ( (XML_NAMESPACE_SVG
== nPrefix
) &&
276 IsXMLToken(rLocalName
, XML_TITLE
) )
278 return new XMLStringBufferImportContext(
279 GetImport(), nPrefix
, rLocalName
, sTitleBuffer
);
281 else if ( (XML_NAMESPACE_SVG
== nPrefix
) &&
282 IsXMLToken(rLocalName
, XML_DESC
) )
284 return new XMLStringBufferImportContext(
285 GetImport(), nPrefix
, rLocalName
, sDescriptionBuffer
);
288 return SvXMLImportContext::CreateChildContext(nPrefix
, rLocalName
,
293 void XMLImageMapObjectContext::ProcessAttribute(
294 enum XMLImageMapToken eToken
,
295 const OUString
& rValue
)
299 case XML_TOK_IMAP_URL
:
300 sUrl
= GetImport().GetAbsoluteReference(rValue
);
303 case XML_TOK_IMAP_TARGET
:
307 case XML_TOK_IMAP_NOHREF
:
308 bIsActive
= ! IsXMLToken(rValue
, XML_NOHREF
);
311 case XML_TOK_IMAP_NAME
:
320 void XMLImageMapObjectContext::Prepare(
321 Reference
<XPropertySet
> & rPropertySet
)
323 rPropertySet
->setPropertyValue( sURL
, Any( sUrl
) );
324 rPropertySet
->setPropertyValue( sTitle
, Any( sTitleBuffer
.makeStringAndClear() ) );
325 rPropertySet
->setPropertyValue( sDescription
, Any( sDescriptionBuffer
.makeStringAndClear() ) );
326 rPropertySet
->setPropertyValue( sTarget
, Any( sTargt
) );
327 rPropertySet
->setPropertyValue( sIsActive
, Any( bIsActive
) );
328 rPropertySet
->setPropertyValue( sName
, Any( sNam
) );
333 class XMLImageMapRectangleContext
: public XMLImageMapObjectContext
335 awt::Rectangle aRectangle
;
345 XMLImageMapRectangleContext(
346 SvXMLImport
& rImport
,
348 const ::rtl::OUString
& rLocalName
,
349 ::com::sun::star::uno::Reference
<
350 ::com::sun::star::container::XIndexContainer
> xMap
);
352 virtual ~XMLImageMapRectangleContext();
355 virtual void ProcessAttribute(
356 enum XMLImageMapToken eToken
,
357 const ::rtl::OUString
& rValue
);
359 virtual void Prepare(
360 ::com::sun::star::uno::Reference
<
361 ::com::sun::star::beans::XPropertySet
> & rPropertySet
);
366 TYPEINIT1(XMLImageMapRectangleContext
, XMLImageMapObjectContext
);
368 XMLImageMapRectangleContext::XMLImageMapRectangleContext(
369 SvXMLImport
& rImport
,
371 const OUString
& rLocalName
,
372 Reference
<XIndexContainer
> xMap
) :
373 XMLImageMapObjectContext(rImport
, nPrefix
, rLocalName
, xMap
,
374 "com.sun.star.image.ImageMapRectangleObject"),
382 XMLImageMapRectangleContext::~XMLImageMapRectangleContext()
386 void XMLImageMapRectangleContext::ProcessAttribute(
387 enum XMLImageMapToken eToken
,
388 const OUString
& rValue
)
394 if (GetImport().GetMM100UnitConverter().convertMeasure(nTmp
,
402 if (GetImport().GetMM100UnitConverter().convertMeasure(nTmp
,
409 case XML_TOK_IMAP_WIDTH
:
410 if (GetImport().GetMM100UnitConverter().convertMeasure(nTmp
,
413 aRectangle
.Width
= nTmp
;
417 case XML_TOK_IMAP_HEIGTH
:
418 if (GetImport().GetMM100UnitConverter().convertMeasure(nTmp
,
421 aRectangle
.Height
= nTmp
;
422 bHeightOK
= sal_True
;
426 XMLImageMapObjectContext::ProcessAttribute(eToken
, rValue
);
429 bValid
= bHeightOK
&& bXOK
&& bYOK
&& bWidthOK
;
432 void XMLImageMapRectangleContext::Prepare(
433 Reference
<XPropertySet
> & rPropertySet
)
437 rPropertySet
->setPropertyValue( sBoundary
, aAny
);
439 // common properties handled by super class
440 XMLImageMapObjectContext::Prepare(rPropertySet
);
444 class XMLImageMapPolygonContext
: public XMLImageMapObjectContext
446 ::rtl::OUString sViewBoxString
;
447 ::rtl::OUString sPointsString
;
455 XMLImageMapPolygonContext(
456 SvXMLImport
& rImport
,
458 const ::rtl::OUString
& rLocalName
,
459 ::com::sun::star::uno::Reference
<
460 ::com::sun::star::container::XIndexContainer
> xMap
);
462 virtual ~XMLImageMapPolygonContext();
465 virtual void ProcessAttribute(
466 enum XMLImageMapToken eToken
,
467 const ::rtl::OUString
& rValue
);
469 virtual void Prepare(
470 ::com::sun::star::uno::Reference
<
471 ::com::sun::star::beans::XPropertySet
> & rPropertySet
);
476 TYPEINIT1(XMLImageMapPolygonContext
, XMLImageMapObjectContext
);
478 XMLImageMapPolygonContext::XMLImageMapPolygonContext(
479 SvXMLImport
& rImport
,
481 const OUString
& rLocalName
,
482 Reference
<XIndexContainer
> xMap
) :
483 XMLImageMapObjectContext(rImport
, nPrefix
, rLocalName
, xMap
,
484 "com.sun.star.image.ImageMapPolygonObject"),
485 bViewBoxOK(sal_False
),
490 XMLImageMapPolygonContext::~XMLImageMapPolygonContext()
494 void XMLImageMapPolygonContext::ProcessAttribute(
495 enum XMLImageMapToken eToken
,
496 const OUString
& rValue
)
500 case XML_TOK_IMAP_POINTS
:
501 sPointsString
= rValue
;
502 bPointsOK
= sal_True
;
504 case XML_TOK_IMAP_VIEWBOX
:
505 sViewBoxString
= rValue
;
506 bViewBoxOK
= sal_True
;
509 XMLImageMapObjectContext::ProcessAttribute(eToken
, rValue
);
513 bValid
= bViewBoxOK
&& bPointsOK
;
516 void XMLImageMapPolygonContext::Prepare(
517 Reference
<XPropertySet
> & rPropertySet
)
520 SdXMLImExViewBox
aViewBox(sViewBoxString
,
521 GetImport().GetMM100UnitConverter());
523 // get polygon sequence
524 awt::Point
aPoint(aViewBox
.GetX(), aViewBox
.GetY());
525 awt::Size
aSize(aViewBox
.GetWidth(), aViewBox
.GetHeight());
526 SdXMLImExPointsElement
aPoints( sPointsString
, aViewBox
, aPoint
, aSize
,
527 GetImport().GetMM100UnitConverter() );
528 PointSequenceSequence aPointSeqSeq
= aPoints
.GetPointSequenceSequence();
530 // only use first element of sequence-sequence
531 if (aPointSeqSeq
.getLength() > 0)
534 aAny
<<= aPointSeqSeq
[0];
535 rPropertySet
->setPropertyValue(sPolygon
, aAny
);
539 XMLImageMapObjectContext::Prepare(rPropertySet
);
544 class XMLImageMapCircleContext
: public XMLImageMapObjectContext
556 XMLImageMapCircleContext(
557 SvXMLImport
& rImport
,
559 const ::rtl::OUString
& rLocalName
,
560 ::com::sun::star::uno::Reference
<
561 ::com::sun::star::container::XIndexContainer
> xMap
);
563 virtual ~XMLImageMapCircleContext();
566 virtual void ProcessAttribute(
567 enum XMLImageMapToken eToken
,
568 const ::rtl::OUString
& rValue
);
570 virtual void Prepare(
571 ::com::sun::star::uno::Reference
<
572 ::com::sun::star::beans::XPropertySet
> & rPropertySet
);
575 TYPEINIT1(XMLImageMapCircleContext
, XMLImageMapObjectContext
);
577 XMLImageMapCircleContext::XMLImageMapCircleContext(
578 SvXMLImport
& rImport
,
580 const OUString
& rLocalName
,
581 Reference
<XIndexContainer
> xMap
) :
582 XMLImageMapObjectContext(rImport
, nPrefix
, rLocalName
, xMap
,
583 "com.sun.star.image.ImageMapCircleObject"),
590 XMLImageMapCircleContext::~XMLImageMapCircleContext()
594 void XMLImageMapCircleContext::ProcessAttribute(
595 enum XMLImageMapToken eToken
,
596 const OUString
& rValue
)
601 case XML_TOK_IMAP_CENTER_X
:
602 if (GetImport().GetMM100UnitConverter().convertMeasure(nTmp
,
609 case XML_TOK_IMAP_CENTER_Y
:
610 if (GetImport().GetMM100UnitConverter().convertMeasure(nTmp
,
617 case XML_TOK_IMAP_RADIUS
:
618 if (GetImport().GetMM100UnitConverter().convertMeasure(nTmp
,
622 bRadiusOK
= sal_True
;
626 XMLImageMapObjectContext::ProcessAttribute(eToken
, rValue
);
629 bValid
= bRadiusOK
&& bXOK
&& bYOK
;
632 void XMLImageMapCircleContext::Prepare(
633 Reference
<XPropertySet
> & rPropertySet
)
638 rPropertySet
->setPropertyValue( sCenter
, aAny
);
642 rPropertySet
->setPropertyValue( sRadius
, aAny
);
644 // common properties handled by super class
645 XMLImageMapObjectContext::Prepare(rPropertySet
);
657 TYPEINIT1(XMLImageMapContext
, SvXMLImportContext
);
659 XMLImageMapContext::XMLImageMapContext(
660 SvXMLImport
& rImport
,
662 const OUString
& rLocalName
,
663 Reference
<XPropertySet
> & rPropertySet
) :
664 SvXMLImportContext(rImport
, nPrefix
, rLocalName
),
665 sImageMap(RTL_CONSTASCII_USTRINGPARAM("ImageMap")),
666 xPropertySet(rPropertySet
)
671 Reference
< XPropertySetInfo
> xInfo
=
672 xPropertySet
->getPropertySetInfo();
673 if( xInfo
.is() && xInfo
->hasPropertyByName( sImageMap
) )
674 xPropertySet
->getPropertyValue(sImageMap
) >>= xImageMap
;
676 catch( com::sun::star::uno::Exception e
)
678 uno::Sequence
<OUString
> aSeq(0);
679 rImport
.SetError( XMLERROR_FLAG_WARNING
| XMLERROR_API
, aSeq
, e
.Message
, NULL
);
683 XMLImageMapContext::~XMLImageMapContext()
687 SvXMLImportContext
*XMLImageMapContext::CreateChildContext(
689 const OUString
& rLocalName
,
690 const Reference
<XAttributeList
> & xAttrList
)
692 SvXMLImportContext
* pContext
= NULL
;
694 if ( XML_NAMESPACE_DRAW
== nPrefix
)
696 if ( IsXMLToken(rLocalName
, XML_AREA_RECTANGLE
) )
698 pContext
= new XMLImageMapRectangleContext(
699 GetImport(), nPrefix
, rLocalName
, xImageMap
);
701 else if ( IsXMLToken(rLocalName
, XML_AREA_POLYGON
) )
703 pContext
= new XMLImageMapPolygonContext(
704 GetImport(), nPrefix
, rLocalName
, xImageMap
);
706 else if ( IsXMLToken(rLocalName
, XML_AREA_CIRCLE
) )
708 pContext
= new XMLImageMapCircleContext(
709 GetImport(), nPrefix
, rLocalName
, xImageMap
);
713 pContext
= SvXMLImportContext::CreateChildContext(nPrefix
, rLocalName
,
719 void XMLImageMapContext::EndElement()
721 Reference
< XPropertySetInfo
> xInfo
=
722 xPropertySet
->getPropertySetInfo();
723 if( xInfo
.is() && xInfo
->hasPropertyByName( sImageMap
) )
724 xPropertySet
->setPropertyValue(sImageMap
, uno::makeAny( xImageMap
) );