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