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 9 : uno::Sequence< OUString > XSvgParser_getSupportedServiceNames()
72 : {
73 9 : OUString aServiceName("com.sun.star.graphic.SvgTools" );
74 9 : uno::Sequence< OUString > aServiceNames( &aServiceName, 1 );
75 9 : return aServiceNames;
76 : }
77 :
78 9 : OUString XSvgParser_getImplementationName()
79 : {
80 9 : return OUString( "svgio::svgreader::XSvgParser" );
81 : }
82 :
83 300 : uno::Reference< uno::XInterface > SAL_CALL XSvgParser_createInstance(const uno::Reference< uno::XComponentContext >& context)
84 : {
85 300 : return static_cast< ::cppu::OWeakObject* >(new XSvgParser(context));
86 : }
87 : } // end of namespace svgreader
88 : } // end of namespace svgio
89 :
90 : namespace svgio
91 : {
92 : namespace svgreader
93 : {
94 300 : XSvgParser::XSvgParser(
95 : uno::Reference< uno::XComponentContext > const & context):
96 300 : context_(context)
97 : {
98 300 : }
99 :
100 600 : XSvgParser::~XSvgParser()
101 : {
102 600 : }
103 :
104 299 : uno::Sequence< uno::Reference< ::graphic::XPrimitive2D > > XSvgParser::getDecomposition(
105 : const uno::Reference< ::io::XInputStream >& xSVGStream,
106 : const OUString& aAbsolutePath ) throw (uno::RuntimeException, std::exception)
107 : {
108 299 : drawinglayer::primitive2d::Primitive2DSequence aRetval;
109 :
110 299 : if(xSVGStream.is())
111 : {
112 : // local document handler
113 299 : SvgDocHdl* pSvgDocHdl = new SvgDocHdl(aAbsolutePath);
114 299 : uno::Reference< xml::sax::XDocumentHandler > xSvgDocHdl(pSvgDocHdl);
115 :
116 : try
117 : {
118 : // prepare ParserInputSrouce
119 299 : xml::sax::InputSource myInputSource;
120 299 : myInputSource.aInputStream = xSVGStream;
121 :
122 : // get parser
123 : uno::Reference< xml::sax::XParser > xParser(
124 598 : xml::sax::Parser::create(context_));
125 : // fdo#60471 need to enable internal entities because
126 : // certain ... popular proprietary products write SVG files
127 : // that use entities to define XML namespaces.
128 : uno::Reference<lang::XInitialization> const xInit(xParser,
129 598 : uno::UNO_QUERY_THROW);
130 598 : uno::Sequence<uno::Any> args(1);
131 299 : args[0] <<= OUString("DoSmeplease");
132 299 : xInit->initialize(args);
133 :
134 : // connect parser and filter
135 299 : 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 598 : xParser->parseStream(myInputSource);
143 : }
144 4 : catch(const uno::Exception& e)
145 : {
146 : SAL_WARN( "svg", "Parse error! : " << e.Message);
147 : }
148 :
149 : // decompose to primitives
150 299 : const SvgNodeVector& rResults = pSvgDocHdl->getSvgDocument().getSvgNodeVector();
151 299 : const sal_uInt32 nCount(rResults.size());
152 :
153 598 : for(sal_uInt32 a(0); a < nCount; a++)
154 : {
155 299 : SvgNode* pCandidate = rResults[a];
156 :
157 299 : if(Display_none != pCandidate->getDisplay())
158 : {
159 299 : pCandidate->decomposeSvgNode(aRetval, false);
160 : }
161 299 : }
162 : }
163 : else
164 : {
165 : OSL_ENSURE(false, "Invalid stream (!)");
166 : }
167 :
168 299 : return aRetval;
169 : }
170 :
171 1 : OUString SAL_CALL XSvgParser::getImplementationName() throw(uno::RuntimeException, std::exception)
172 : {
173 1 : return(XSvgParser_getImplementationName());
174 : }
175 :
176 0 : sal_Bool SAL_CALL XSvgParser::supportsService(const OUString& rServiceName) throw(uno::RuntimeException, std::exception)
177 : {
178 0 : return cppu::supportsService(this, rServiceName);
179 : }
180 :
181 1 : uno::Sequence< OUString > SAL_CALL XSvgParser::getSupportedServiceNames() throw(uno::RuntimeException, std::exception)
182 : {
183 1 : return XSvgParser_getSupportedServiceNames();
184 : }
185 :
186 : } // end of namespace svgreader
187 : } // end of namespace svgio
188 :
189 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|