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