Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <sal/config.h>
21 :
22 : #include <boost/noncopyable.hpp>
23 : #include <com/sun/star/graphic/XSvgParser.hpp>
24 : #include <com/sun/star/lang/XServiceInfo.hpp>
25 : #include <com/sun/star/lang/XInitialization.hpp>
26 : #include <cppuhelper/implbase2.hxx>
27 : #include <cppuhelper/supportsservice.hxx>
28 : #include <com/sun/star/xml/sax/XParser.hpp>
29 : #include <com/sun/star/xml/sax/Parser.hpp>
30 : #include <com/sun/star/xml/sax/InputSource.hpp>
31 : #include <drawinglayer/geometry/viewinformation2d.hxx>
32 : #include <svgio/svgreader/svgdocumenthandler.hxx>
33 :
34 : #include "xsvgparser.hxx"
35 :
36 : using namespace ::com::sun::star;
37 :
38 : namespace svgio
39 : {
40 : namespace svgreader
41 : {
42 : class XSvgParser : public ::cppu::WeakAggImplHelper2< graphic::XSvgParser, lang::XServiceInfo >, private boost::noncopyable
43 : {
44 : private:
45 : uno::Reference< uno::XComponentContext > context_;
46 :
47 : protected:
48 : public:
49 : XSvgParser(
50 : uno::Reference< uno::XComponentContext > const & context);
51 : virtual ~XSvgParser();
52 :
53 : // XSvgParser
54 : virtual uno::Sequence< uno::Reference< ::graphic::XPrimitive2D > > SAL_CALL getDecomposition(
55 : const uno::Reference< ::io::XInputStream >& xSVGStream,
56 : const OUString& aAbsolutePath) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE;
57 :
58 : // XServiceInfo
59 : virtual OUString SAL_CALL getImplementationName() throw(uno::RuntimeException, std::exception) SAL_OVERRIDE;
60 : virtual sal_Bool SAL_CALL supportsService(const OUString&) throw(uno::RuntimeException, std::exception) SAL_OVERRIDE;
61 : virtual uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() throw(uno::RuntimeException, std::exception) SAL_OVERRIDE;
62 : };
63 : } // end of namespace svgreader
64 : } // end of namespace svgio
65 :
66 : // uno functions
67 : namespace svgio
68 : {
69 : namespace svgreader
70 : {
71 8 : uno::Sequence< OUString > XSvgParser_getSupportedServiceNames()
72 : {
73 8 : static OUString aServiceName("com.sun.star.graphic.SvgTools" );
74 8 : static uno::Sequence< OUString > aServiceNames( &aServiceName, 1 );
75 :
76 8 : return( aServiceNames );
77 : }
78 :
79 8 : OUString XSvgParser_getImplementationName()
80 : {
81 8 : return OUString( "svgio::svgreader::XSvgParser" );
82 : }
83 :
84 302 : uno::Reference< uno::XInterface > SAL_CALL XSvgParser_createInstance(const uno::Reference< uno::XComponentContext >& context)
85 : {
86 302 : return static_cast< ::cppu::OWeakObject* >(new XSvgParser(context));
87 : }
88 : } // end of namespace svgreader
89 : } // end of namespace svgio
90 :
91 : namespace svgio
92 : {
93 : namespace svgreader
94 : {
95 302 : XSvgParser::XSvgParser(
96 : uno::Reference< uno::XComponentContext > const & context):
97 302 : context_(context)
98 : {
99 302 : }
100 :
101 604 : XSvgParser::~XSvgParser()
102 : {
103 604 : }
104 :
105 300 : uno::Sequence< uno::Reference< ::graphic::XPrimitive2D > > XSvgParser::getDecomposition(
106 : const uno::Reference< ::io::XInputStream >& xSVGStream,
107 : const OUString& aAbsolutePath ) throw (uno::RuntimeException, std::exception)
108 : {
109 300 : drawinglayer::primitive2d::Primitive2DSequence aRetval;
110 :
111 300 : if(xSVGStream.is())
112 : {
113 : // local document handler
114 300 : SvgDocHdl* pSvgDocHdl = new SvgDocHdl(aAbsolutePath);
115 300 : uno::Reference< xml::sax::XDocumentHandler > xSvgDocHdl(pSvgDocHdl);
116 :
117 : try
118 : {
119 : // prepare ParserInputSrouce
120 300 : xml::sax::InputSource myInputSource;
121 300 : myInputSource.aInputStream = xSVGStream;
122 :
123 : // get parser
124 : uno::Reference< xml::sax::XParser > xParser(
125 600 : xml::sax::Parser::create(context_));
126 : // fdo#60471 need to enable internal entities because
127 : // certain ... popular proprietary products write SVG files
128 : // that use entities to define XML namespaces.
129 : uno::Reference<lang::XInitialization> const xInit(xParser,
130 600 : uno::UNO_QUERY_THROW);
131 600 : uno::Sequence<uno::Any> args(1);
132 300 : args[0] <<= OUString("DoSmeplease");
133 300 : xInit->initialize(args);
134 :
135 : // connect parser and filter
136 300 : xParser->setDocumentHandler(xSvgDocHdl);
137 :
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 600 : xParser->parseStream(myInputSource);
144 : }
145 8 : catch(const uno::Exception& e)
146 : {
147 : SAL_WARN( "svg", "Parse error! : " << e.Message);
148 : }
149 :
150 : // decompose to primitives
151 300 : const SvgNodeVector& rResults = pSvgDocHdl->getSvgDocument().getSvgNodeVector();
152 300 : const sal_uInt32 nCount(rResults.size());
153 :
154 600 : for(sal_uInt32 a(0); a < nCount; a++)
155 : {
156 300 : SvgNode* pCandidate = rResults[a];
157 :
158 300 : if(Display_none != pCandidate->getDisplay())
159 : {
160 300 : pCandidate->decomposeSvgNode(aRetval, false);
161 : }
162 300 : }
163 : }
164 : else
165 : {
166 : OSL_ENSURE(false, "Invalid stream (!)");
167 : }
168 :
169 300 : return aRetval;
170 : }
171 :
172 0 : OUString SAL_CALL XSvgParser::getImplementationName() throw(uno::RuntimeException, std::exception)
173 : {
174 0 : return(XSvgParser_getImplementationName());
175 : }
176 :
177 0 : sal_Bool SAL_CALL XSvgParser::supportsService(const OUString& rServiceName) throw(uno::RuntimeException, std::exception)
178 : {
179 0 : return cppu::supportsService(this, rServiceName);
180 : }
181 :
182 0 : uno::Sequence< OUString > SAL_CALL XSvgParser::getSupportedServiceNames() throw(uno::RuntimeException, std::exception)
183 : {
184 0 : return XSvgParser_getSupportedServiceNames();
185 : }
186 :
187 : } // end of namespace svgreader
188 : } // end of namespace svgio
189 :
190 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|