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 <comphelper/processfactory.hxx>
32 #include <drawinglayer/geometry/viewinformation2d.hxx>
33 
34 //////////////////////////////////////////////////////////////////////////////
35 
36 using namespace ::com::sun::star;
37 
38 //////////////////////////////////////////////////////////////////////////////
39 
40 namespace svgio
41 {
42     namespace svgreader
43     {
44         class XSvgParser : public ::cppu::WeakAggImplHelper2< graphic::XSvgParser, lang::XServiceInfo >
45         {
46         private:
47             XSvgParser(const XSvgParser&);
48             XSvgParser& operator=(const XSvgParser&);
49 
50         protected:
51         public:
52             XSvgParser();
53             virtual ~XSvgParser();
54 
55             // XSvgParser
56             virtual uno::Sequence< uno::Reference< ::graphic::XPrimitive2D > > SAL_CALL getDecomposition(
57                 const uno::Reference< ::io::XInputStream >& xSVGStream,
58                 const ::rtl::OUString& aAbsolutePath) throw (uno::RuntimeException);
59 
60             // XServiceInfo
61             virtual rtl::OUString SAL_CALL getImplementationName() throw(uno::RuntimeException);
62             virtual ::sal_Bool SAL_CALL supportsService(const rtl::OUString&) throw(uno::RuntimeException);
63             virtual uno::Sequence< rtl::OUString > SAL_CALL getSupportedServiceNames() throw(uno::RuntimeException);
64         };
65 	} // end of namespace svgreader
66 } // end of namespace svgio
67 
68 //////////////////////////////////////////////////////////////////////////////
69 // uno functions
70 
71 namespace svgio
72 {
73     namespace svgreader
74     {
XSvgParser_getSupportedServiceNames()75         uno::Sequence< rtl::OUString > XSvgParser_getSupportedServiceNames()
76         {
77             static rtl::OUString aServiceName(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.graphic.SvgTools" ) );
78             static uno::Sequence< rtl::OUString > aServiceNames( &aServiceName, 1 );
79 
80             return( aServiceNames );
81         }
82 
XSvgParser_getImplementationName()83         rtl::OUString XSvgParser_getImplementationName()
84         {
85             return rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "svgio::svgreader::XSvgParser" ) );
86         }
87 
XSvgParser_createInstance(const uno::Reference<lang::XMultiServiceFactory> &)88         uno::Reference< uno::XInterface > SAL_CALL XSvgParser_createInstance(const uno::Reference< lang::XMultiServiceFactory >&)
89         {
90             return static_cast< ::cppu::OWeakObject* >(new XSvgParser);
91         }
92     } // end of namespace svgreader
93 } // end of namespace svgio
94 
95 //////////////////////////////////////////////////////////////////////////////
96 
97 namespace svgio
98 {
99     namespace svgreader
100     {
XSvgParser()101         XSvgParser::XSvgParser()
102         {
103         }
104 
~XSvgParser()105         XSvgParser::~XSvgParser()
106         {
107         }
108 
getDecomposition(const uno::Reference<::io::XInputStream> & xSVGStream,const::rtl::OUString & aAbsolutePath)109         uno::Sequence< uno::Reference< ::graphic::XPrimitive2D > > XSvgParser::getDecomposition(
110             const uno::Reference< ::io::XInputStream >& xSVGStream,
111             const ::rtl::OUString& aAbsolutePath ) throw (uno::RuntimeException)
112         {
113             drawinglayer::primitive2d::Primitive2DSequence aRetval;
114 
115             if(xSVGStream.is())
116             {
117                 // local document handler
118                 SvgDocHdl* pSvgDocHdl = new SvgDocHdl(aAbsolutePath);
119                 uno::Reference< xml::sax::XDocumentHandler > xSvgDocHdl(pSvgDocHdl);
120 
121                 try
122                 {
123                     // prepare ParserInputSrouce
124                     xml::sax::InputSource myInputSource;
125                     myInputSource.aInputStream = xSVGStream;
126 
127                     // get parser
128                     uno::Reference< xml::sax::XParser > xParser(
129                         comphelper::getProcessServiceFactory()->createInstance(
130                             rtl::OUString::createFromAscii("com.sun.star.xml.sax.Parser") ),
131                         uno::UNO_QUERY_THROW );
132 
133                     // connect parser and filter
134                     xParser->setDocumentHandler(xSvgDocHdl);
135 
136                     // finally, parse the stream to a hierarchy of
137                     // SVGGraphicPrimitive2D which will be embedded to the
138                     // primitive sequence. Their decompositions will in the
139                     // end create local low-level primitives, thus SVG will
140                     // be processable from all our processors
141                     xParser->parseStream(myInputSource);
142                 }
143                 catch(uno::Exception&)
144                 {
145                     OSL_ENSURE(false, "Parse error (!)");
146                 }
147 
148                 // decompose to primitives
149                 const SvgNodeVector& rResults = pSvgDocHdl->getSvgDocument().getSvgNodeVector();
150                 const sal_uInt32 nCount(rResults.size());
151 
152                 for(sal_uInt32 a(0); a < nCount; a++)
153                 {
154                     SvgNode* pCandidate = rResults[a];
155 
156                     if(Display_none != pCandidate->getDisplay())
157                     {
158                         pCandidate->decomposeSvgNode(aRetval, false);
159                     }
160                 }
161             }
162             else
163             {
164                 OSL_ENSURE(false, "Invalid stream (!)");
165             }
166 
167             return aRetval;
168         }
169 
getImplementationName()170         rtl::OUString SAL_CALL XSvgParser::getImplementationName() throw(uno::RuntimeException)
171         {
172             return(XSvgParser_getImplementationName());
173         }
174 
supportsService(const rtl::OUString & rServiceName)175         sal_Bool SAL_CALL XSvgParser::supportsService(const rtl::OUString& rServiceName) throw(uno::RuntimeException)
176         {
177             const uno::Sequence< rtl::OUString > aServices(XSvgParser_getSupportedServiceNames());
178 
179             for(sal_Int32 nService(0); nService < aServices.getLength(); nService++)
180             {
181                 if(rServiceName == aServices[nService])
182                 {
183                     return sal_True;
184                 }
185             }
186 
187             return sal_False;
188         }
189 
getSupportedServiceNames()190         uno::Sequence< rtl::OUString > SAL_CALL XSvgParser::getSupportedServiceNames() throw(uno::RuntimeException)
191         {
192             return XSvgParser_getSupportedServiceNames();
193         }
194 
195     } // end of namespace svgreader
196 } // end of namespace svgio
197 
198 //////////////////////////////////////////////////////////////////////////////
199 // eof
200