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 .
20 #include "sal/config.h"
22 #include <com/sun/star/graphic/XSvgParser.hpp>
23 #include <com/sun/star/lang/XServiceInfo.hpp>
24 #include <cppuhelper/implbase2.hxx>
25 #include <svgio/svgreader/svgdocumenthandler.hxx>
26 #include <com/sun/star/xml/sax/XParser.hpp>
27 #include <com/sun/star/xml/sax/Parser.hpp>
28 #include <com/sun/star/xml/sax/InputSource.hpp>
29 #include <drawinglayer/geometry/viewinformation2d.hxx>
31 #include "xsvgparser.hxx"
33 //////////////////////////////////////////////////////////////////////////////
35 using namespace ::com::sun::star
;
37 //////////////////////////////////////////////////////////////////////////////
43 class XSvgParser
: public ::cppu::WeakAggImplHelper2
< graphic::XSvgParser
, lang::XServiceInfo
>
46 XSvgParser(const XSvgParser
&);
47 XSvgParser
& operator=(const XSvgParser
&);
49 uno::Reference
< uno::XComponentContext
> context_
;
54 uno::Reference
< uno::XComponentContext
> const & context
);
55 virtual ~XSvgParser();
58 virtual uno::Sequence
< uno::Reference
< ::graphic::XPrimitive2D
> > SAL_CALL
getDecomposition(
59 const uno::Reference
< ::io::XInputStream
>& xSVGStream
,
60 const ::rtl::OUString
& aAbsolutePath
) throw (uno::RuntimeException
);
63 virtual rtl::OUString SAL_CALL
getImplementationName() throw(uno::RuntimeException
);
64 virtual ::sal_Bool SAL_CALL
supportsService(const rtl::OUString
&) throw(uno::RuntimeException
);
65 virtual uno::Sequence
< rtl::OUString
> SAL_CALL
getSupportedServiceNames() throw(uno::RuntimeException
);
67 } // end of namespace svgreader
68 } // end of namespace svgio
70 //////////////////////////////////////////////////////////////////////////////
77 uno::Sequence
< rtl::OUString
> XSvgParser_getSupportedServiceNames()
79 static rtl::OUString
aServiceName(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.graphic.SvgTools" ) );
80 static uno::Sequence
< rtl::OUString
> aServiceNames( &aServiceName
, 1 );
82 return( aServiceNames
);
85 rtl::OUString
XSvgParser_getImplementationName()
87 return rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "svgio::svgreader::XSvgParser" ) );
90 uno::Reference
< uno::XInterface
> SAL_CALL
XSvgParser_createInstance(const uno::Reference
< uno::XComponentContext
>& context
)
92 return static_cast< ::cppu::OWeakObject
* >(new XSvgParser(context
));
94 } // end of namespace svgreader
95 } // end of namespace svgio
97 //////////////////////////////////////////////////////////////////////////////
103 XSvgParser::XSvgParser(
104 uno::Reference
< uno::XComponentContext
> const & context
):
109 XSvgParser::~XSvgParser()
113 uno::Sequence
< uno::Reference
< ::graphic::XPrimitive2D
> > XSvgParser::getDecomposition(
114 const uno::Reference
< ::io::XInputStream
>& xSVGStream
,
115 const ::rtl::OUString
& aAbsolutePath
) throw (uno::RuntimeException
)
117 drawinglayer::primitive2d::Primitive2DSequence aRetval
;
121 // local document handler
122 SvgDocHdl
* pSvgDocHdl
= new SvgDocHdl(aAbsolutePath
);
123 uno::Reference
< xml::sax::XDocumentHandler
> xSvgDocHdl(pSvgDocHdl
);
127 // prepare ParserInputSrouce
128 xml::sax::InputSource myInputSource
;
129 myInputSource
.aInputStream
= xSVGStream
;
132 uno::Reference
< xml::sax::XParser
> xParser(
133 xml::sax::Parser::create(context_
));
135 // connect parser and filter
136 xParser
->setDocumentHandler(xSvgDocHdl
);
138 // finally, parse the stream to a hierarchy of
139 // SVGGraphicPrimitive2D which will be embedded to the
140 // primitive sequence. Their decompositions will in the
141 // end create local low-level primitives, thus SVG will
142 // be processable from all our processors
143 xParser
->parseStream(myInputSource
);
145 catch(uno::Exception
&)
147 OSL_ENSURE(false, "Parse error (!)");
150 // decompose to primitives
151 const SvgNodeVector
& rResults
= pSvgDocHdl
->getSvgDocument().getSvgNodeVector();
152 const sal_uInt32
nCount(rResults
.size());
154 for(sal_uInt32
a(0); a
< nCount
; a
++)
156 rResults
[a
]->decomposeSvgNode(aRetval
, false);
161 OSL_ENSURE(false, "Invalid stream (!)");
167 rtl::OUString SAL_CALL
XSvgParser::getImplementationName() throw(uno::RuntimeException
)
169 return(XSvgParser_getImplementationName());
172 sal_Bool SAL_CALL
XSvgParser::supportsService(const rtl::OUString
& rServiceName
) throw(uno::RuntimeException
)
174 const uno::Sequence
< rtl::OUString
> aServices(XSvgParser_getSupportedServiceNames());
176 for(sal_Int32
nService(0); nService
< aServices
.getLength(); nService
++)
178 if(rServiceName
== aServices
[nService
])
187 uno::Sequence
< rtl::OUString
> SAL_CALL
XSvgParser::getSupportedServiceNames() throw(uno::RuntimeException
)
189 return XSvgParser_getSupportedServiceNames();
192 } // end of namespace svgreader
193 } // end of namespace svgio
195 //////////////////////////////////////////////////////////////////////////////
198 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */