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