xref: /trunk/main/svgio/source/svguno/xsvgparser.cxx (revision cf6516809c57e1bb0a940545cca99cdad54d4ce2)
1ddde725dSArmin Le Grand /**************************************************************
2ddde725dSArmin Le Grand  *
3ddde725dSArmin Le Grand  * Licensed to the Apache Software Foundation (ASF) under one
4ddde725dSArmin Le Grand  * or more contributor license agreements.  See the NOTICE file
5ddde725dSArmin Le Grand  * distributed with this work for additional information
6ddde725dSArmin Le Grand  * regarding copyright ownership.  The ASF licenses this file
7ddde725dSArmin Le Grand  * to you under the Apache License, Version 2.0 (the
8ddde725dSArmin Le Grand  * "License"); you may not use this file except in compliance
9ddde725dSArmin Le Grand  * with the License.  You may obtain a copy of the License at
10ddde725dSArmin Le Grand  *
11ddde725dSArmin Le Grand  *   http://www.apache.org/licenses/LICENSE-2.0
12ddde725dSArmin Le Grand  *
13ddde725dSArmin Le Grand  * Unless required by applicable law or agreed to in writing,
14ddde725dSArmin Le Grand  * software distributed under the License is distributed on an
15ddde725dSArmin Le Grand  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16ddde725dSArmin Le Grand  * KIND, either express or implied.  See the License for the
17ddde725dSArmin Le Grand  * specific language governing permissions and limitations
18ddde725dSArmin Le Grand  * under the License.
19ddde725dSArmin Le Grand  *
20ddde725dSArmin Le Grand  *************************************************************/
21ddde725dSArmin Le Grand 
22ddde725dSArmin Le Grand // MARKER(update_precomp.py): autogen include statement, do not remove
23ddde725dSArmin Le Grand #include "precompiled_svgio.hxx"
24ddde725dSArmin Le Grand 
25ddde725dSArmin Le Grand #include <com/sun/star/graphic/XSvgParser.hpp>
26ddde725dSArmin Le Grand #include <com/sun/star/lang/XServiceInfo.hpp>
27ddde725dSArmin Le Grand #include <cppuhelper/implbase2.hxx>
28ddde725dSArmin Le Grand #include <svgio/svgreader/svgdocumenthandler.hxx>
29ddde725dSArmin Le Grand #include <com/sun/star/xml/sax/XParser.hpp>
30ddde725dSArmin Le Grand #include <com/sun/star/xml/sax/InputSource.hpp>
31*c058b6cbSDamjan Jovanovic #include <com/sun/star/uno/XComponentContext.hpp>
32ddde725dSArmin Le Grand #include <comphelper/processfactory.hxx>
33ddde725dSArmin Le Grand #include <drawinglayer/geometry/viewinformation2d.hxx>
34ddde725dSArmin Le Grand 
35ddde725dSArmin Le Grand //////////////////////////////////////////////////////////////////////////////
36ddde725dSArmin Le Grand 
37ddde725dSArmin Le Grand using namespace ::com::sun::star;
38ddde725dSArmin Le Grand 
39ddde725dSArmin Le Grand //////////////////////////////////////////////////////////////////////////////
40ddde725dSArmin Le Grand 
41ddde725dSArmin Le Grand namespace svgio
42ddde725dSArmin Le Grand {
43ddde725dSArmin Le Grand     namespace svgreader
44ddde725dSArmin Le Grand     {
45ddde725dSArmin Le Grand         class XSvgParser : public ::cppu::WeakAggImplHelper2< graphic::XSvgParser, lang::XServiceInfo >
46ddde725dSArmin Le Grand         {
47ddde725dSArmin Le Grand         private:
48ddde725dSArmin Le Grand             XSvgParser(const XSvgParser&);
49ddde725dSArmin Le Grand             XSvgParser& operator=(const XSvgParser&);
50ddde725dSArmin Le Grand 
51ddde725dSArmin Le Grand         protected:
52ddde725dSArmin Le Grand         public:
53ddde725dSArmin Le Grand             XSvgParser();
54ddde725dSArmin Le Grand             virtual ~XSvgParser();
55ddde725dSArmin Le Grand 
56ddde725dSArmin Le Grand             // XSvgParser
57ddde725dSArmin Le Grand             virtual uno::Sequence< uno::Reference< ::graphic::XPrimitive2D > > SAL_CALL getDecomposition(
58ddde725dSArmin Le Grand                 const uno::Reference< ::io::XInputStream >& xSVGStream,
59ddde725dSArmin Le Grand                 const ::rtl::OUString& aAbsolutePath) throw (uno::RuntimeException);
60ddde725dSArmin Le Grand 
61ddde725dSArmin Le Grand             // XServiceInfo
62ddde725dSArmin Le Grand             virtual rtl::OUString SAL_CALL getImplementationName() throw(uno::RuntimeException);
63ddde725dSArmin Le Grand             virtual ::sal_Bool SAL_CALL supportsService(const rtl::OUString&) throw(uno::RuntimeException);
64ddde725dSArmin Le Grand             virtual uno::Sequence< rtl::OUString > SAL_CALL getSupportedServiceNames() throw(uno::RuntimeException);
65ddde725dSArmin Le Grand         };
66ddde725dSArmin Le Grand     } // end of namespace svgreader
67ddde725dSArmin Le Grand } // end of namespace svgio
68ddde725dSArmin Le Grand 
69ddde725dSArmin Le Grand //////////////////////////////////////////////////////////////////////////////
70ddde725dSArmin Le Grand // uno functions
71ddde725dSArmin Le Grand 
72ddde725dSArmin Le Grand namespace svgio
73ddde725dSArmin Le Grand {
74ddde725dSArmin Le Grand     namespace svgreader
75ddde725dSArmin Le Grand     {
XSvgParser_getSupportedServiceNames()76ddde725dSArmin Le Grand         uno::Sequence< rtl::OUString > XSvgParser_getSupportedServiceNames()
77ddde725dSArmin Le Grand         {
78ddde725dSArmin Le Grand             static rtl::OUString aServiceName(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.graphic.SvgTools" ) );
79ddde725dSArmin Le Grand             static uno::Sequence< rtl::OUString > aServiceNames( &aServiceName, 1 );
80ddde725dSArmin Le Grand 
81ddde725dSArmin Le Grand             return( aServiceNames );
82ddde725dSArmin Le Grand         }
83ddde725dSArmin Le Grand 
XSvgParser_getImplementationName()84ddde725dSArmin Le Grand         rtl::OUString XSvgParser_getImplementationName()
85ddde725dSArmin Le Grand         {
86ddde725dSArmin Le Grand             return rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "svgio::svgreader::XSvgParser" ) );
87ddde725dSArmin Le Grand         }
88ddde725dSArmin Le Grand 
XSvgParser_createInstance(const uno::Reference<uno::XComponentContext> &)89*c058b6cbSDamjan Jovanovic         uno::Reference< uno::XInterface > SAL_CALL XSvgParser_createInstance(const uno::Reference< uno::XComponentContext >&)
90ddde725dSArmin Le Grand         {
91ddde725dSArmin Le Grand             return static_cast< ::cppu::OWeakObject* >(new XSvgParser);
92ddde725dSArmin Le Grand         }
93ddde725dSArmin Le Grand     } // end of namespace svgreader
94ddde725dSArmin Le Grand } // end of namespace svgio
95ddde725dSArmin Le Grand 
96ddde725dSArmin Le Grand //////////////////////////////////////////////////////////////////////////////
97ddde725dSArmin Le Grand 
98ddde725dSArmin Le Grand namespace svgio
99ddde725dSArmin Le Grand {
100ddde725dSArmin Le Grand     namespace svgreader
101ddde725dSArmin Le Grand     {
XSvgParser()102ddde725dSArmin Le Grand         XSvgParser::XSvgParser()
103ddde725dSArmin Le Grand         {
104ddde725dSArmin Le Grand         }
105ddde725dSArmin Le Grand 
~XSvgParser()106ddde725dSArmin Le Grand         XSvgParser::~XSvgParser()
107ddde725dSArmin Le Grand         {
108ddde725dSArmin Le Grand         }
109ddde725dSArmin Le Grand 
getDecomposition(const uno::Reference<::io::XInputStream> & xSVGStream,const::rtl::OUString & aAbsolutePath)110ddde725dSArmin Le Grand         uno::Sequence< uno::Reference< ::graphic::XPrimitive2D > > XSvgParser::getDecomposition(
111ddde725dSArmin Le Grand             const uno::Reference< ::io::XInputStream >& xSVGStream,
112ddde725dSArmin Le Grand             const ::rtl::OUString& aAbsolutePath ) throw (uno::RuntimeException)
113ddde725dSArmin Le Grand         {
114ddde725dSArmin Le Grand             drawinglayer::primitive2d::Primitive2DSequence aRetval;
115ddde725dSArmin Le Grand 
116ddde725dSArmin Le Grand             if(xSVGStream.is())
117ddde725dSArmin Le Grand             {
118ddde725dSArmin Le Grand                 // local document handler
119ddde725dSArmin Le Grand                 SvgDocHdl* pSvgDocHdl = new SvgDocHdl(aAbsolutePath);
120ddde725dSArmin Le Grand                 uno::Reference< xml::sax::XDocumentHandler > xSvgDocHdl(pSvgDocHdl);
121ddde725dSArmin Le Grand 
122ddde725dSArmin Le Grand                 try
123ddde725dSArmin Le Grand                 {
124ddde725dSArmin Le Grand                     // prepare ParserInputSrouce
125ddde725dSArmin Le Grand                     xml::sax::InputSource myInputSource;
126ddde725dSArmin Le Grand                     myInputSource.aInputStream = xSVGStream;
127ddde725dSArmin Le Grand 
128ddde725dSArmin Le Grand                     // get parser
129ddde725dSArmin Le Grand                     uno::Reference< xml::sax::XParser > xParser(
130ddde725dSArmin Le Grand                         comphelper::getProcessServiceFactory()->createInstance(
131ddde725dSArmin Le Grand                             rtl::OUString::createFromAscii("com.sun.star.xml.sax.Parser") ),
132ddde725dSArmin Le Grand                         uno::UNO_QUERY_THROW );
133ddde725dSArmin Le Grand 
134ddde725dSArmin Le Grand                     // connect parser and filter
135ddde725dSArmin Le Grand                     xParser->setDocumentHandler(xSvgDocHdl);
136ddde725dSArmin Le Grand 
137ddde725dSArmin Le Grand                     // finally, parse the stream to a hierarchy of
138ddde725dSArmin Le Grand                     // SVGGraphicPrimitive2D which will be embedded to the
139ddde725dSArmin Le Grand                     // primitive sequence. Their decompositions will in the
140ddde725dSArmin Le Grand                     // end create local low-level primitives, thus SVG will
141ddde725dSArmin Le Grand                     // be processable from all our processors
142ddde725dSArmin Le Grand                     xParser->parseStream(myInputSource);
143ddde725dSArmin Le Grand                 }
144ddde725dSArmin Le Grand                 catch(uno::Exception&)
145ddde725dSArmin Le Grand                 {
146ddde725dSArmin Le Grand                     OSL_ENSURE(false, "Parse error (!)");
147ddde725dSArmin Le Grand                 }
148ddde725dSArmin Le Grand 
149ddde725dSArmin Le Grand                 // decompose to primitives
150ddde725dSArmin Le Grand                 const SvgNodeVector& rResults = pSvgDocHdl->getSvgDocument().getSvgNodeVector();
151ddde725dSArmin Le Grand                 const sal_uInt32 nCount(rResults.size());
152ddde725dSArmin Le Grand 
153ddde725dSArmin Le Grand                 for(sal_uInt32 a(0); a < nCount; a++)
154ddde725dSArmin Le Grand                 {
155a275c134SArmin Le Grand                     SvgNode* pCandidate = rResults[a];
156a275c134SArmin Le Grand 
157a275c134SArmin Le Grand                     if(Display_none != pCandidate->getDisplay())
158a275c134SArmin Le Grand                     {
159a275c134SArmin Le Grand                         pCandidate->decomposeSvgNode(aRetval, false);
160a275c134SArmin Le Grand                     }
161ddde725dSArmin Le Grand                 }
162ddde725dSArmin Le Grand             }
163ddde725dSArmin Le Grand             else
164ddde725dSArmin Le Grand             {
165ddde725dSArmin Le Grand                 OSL_ENSURE(false, "Invalid stream (!)");
166ddde725dSArmin Le Grand             }
167ddde725dSArmin Le Grand 
168ddde725dSArmin Le Grand             return aRetval;
169ddde725dSArmin Le Grand         }
170ddde725dSArmin Le Grand 
getImplementationName()171ddde725dSArmin Le Grand         rtl::OUString SAL_CALL XSvgParser::getImplementationName() throw(uno::RuntimeException)
172ddde725dSArmin Le Grand         {
173ddde725dSArmin Le Grand             return(XSvgParser_getImplementationName());
174ddde725dSArmin Le Grand         }
175ddde725dSArmin Le Grand 
supportsService(const rtl::OUString & rServiceName)176ddde725dSArmin Le Grand         sal_Bool SAL_CALL XSvgParser::supportsService(const rtl::OUString& rServiceName) throw(uno::RuntimeException)
177ddde725dSArmin Le Grand         {
178ddde725dSArmin Le Grand             const uno::Sequence< rtl::OUString > aServices(XSvgParser_getSupportedServiceNames());
179ddde725dSArmin Le Grand 
180ddde725dSArmin Le Grand             for(sal_Int32 nService(0); nService < aServices.getLength(); nService++)
181ddde725dSArmin Le Grand             {
182ddde725dSArmin Le Grand                 if(rServiceName == aServices[nService])
183ddde725dSArmin Le Grand                 {
184ddde725dSArmin Le Grand                     return sal_True;
185ddde725dSArmin Le Grand                 }
186ddde725dSArmin Le Grand             }
187ddde725dSArmin Le Grand 
188ddde725dSArmin Le Grand             return sal_False;
189ddde725dSArmin Le Grand         }
190ddde725dSArmin Le Grand 
getSupportedServiceNames()191ddde725dSArmin Le Grand         uno::Sequence< rtl::OUString > SAL_CALL XSvgParser::getSupportedServiceNames() throw(uno::RuntimeException)
192ddde725dSArmin Le Grand         {
193ddde725dSArmin Le Grand             return XSvgParser_getSupportedServiceNames();
194ddde725dSArmin Le Grand         }
195ddde725dSArmin Le Grand 
196ddde725dSArmin Le Grand     } // end of namespace svgreader
197ddde725dSArmin Le Grand } // end of namespace svgio
198ddde725dSArmin Le Grand 
199ddde725dSArmin Le Grand //////////////////////////////////////////////////////////////////////////////
200ddde725dSArmin Le Grand // eof
201