Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / svgio / source / svguno / xsvgparser.cxx
blob5a9bd9b7551ed1cb7072aec5905192eaed583ca6
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sal/config.h>
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 <comphelper/sequence.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 <svgdocumenthandler.hxx>
34 #include "xsvgparser.hxx"
36 using namespace ::com::sun::star;
38 namespace svgio
40 namespace svgreader
42 class XSvgParser : public ::cppu::WeakAggImplHelper2< graphic::XSvgParser, lang::XServiceInfo >
44 private:
45 uno::Reference< uno::XComponentContext > context_;
47 protected:
48 public:
49 explicit XSvgParser(
50 uno::Reference< uno::XComponentContext > const & context);
51 XSvgParser(const XSvgParser&) = delete;
52 XSvgParser& operator=(const XSvgParser&) = delete;
54 // XSvgParser
55 virtual uno::Sequence< uno::Reference< ::graphic::XPrimitive2D > > SAL_CALL getDecomposition(
56 const uno::Reference< ::io::XInputStream >& xSVGStream,
57 const OUString& aAbsolutePath) override;
59 // XServiceInfo
60 virtual OUString SAL_CALL getImplementationName() override;
61 virtual sal_Bool SAL_CALL supportsService(const OUString&) override;
62 virtual uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
64 } // end of namespace svgreader
65 } // end of namespace svgio
67 // uno functions
68 namespace svgio
70 namespace svgreader
72 uno::Sequence< OUString > XSvgParser_getSupportedServiceNames()
74 return uno::Sequence< OUString > { "com.sun.star.graphic.SvgTools" };
77 OUString XSvgParser_getImplementationName()
79 return OUString( "svgio::svgreader::XSvgParser" );
82 uno::Reference< uno::XInterface > XSvgParser_createInstance(const uno::Reference< uno::XComponentContext >& context)
84 return static_cast< ::cppu::OWeakObject* >(new XSvgParser(context));
86 } // end of namespace svgreader
87 } // end of namespace svgio
89 namespace svgio
91 namespace svgreader
93 XSvgParser::XSvgParser(
94 uno::Reference< uno::XComponentContext > const & context):
95 context_(context)
99 uno::Sequence< uno::Reference< ::graphic::XPrimitive2D > > XSvgParser::getDecomposition(
100 const uno::Reference< ::io::XInputStream >& xSVGStream,
101 const OUString& aAbsolutePath )
103 drawinglayer::primitive2d::Primitive2DContainer aRetval;
105 if(xSVGStream.is())
107 // local document handler
108 SvgDocHdl* pSvgDocHdl = new SvgDocHdl(aAbsolutePath);
109 uno::Reference< xml::sax::XDocumentHandler > xSvgDocHdl(pSvgDocHdl);
113 // prepare ParserInputSrouce
114 xml::sax::InputSource myInputSource;
115 myInputSource.aInputStream = xSVGStream;
117 // get parser
118 uno::Reference< xml::sax::XParser > xParser(
119 xml::sax::Parser::create(context_));
120 // fdo#60471 need to enable internal entities because
121 // certain ... popular proprietary products write SVG files
122 // that use entities to define XML namespaces.
123 uno::Reference<lang::XInitialization> const xInit(xParser,
124 uno::UNO_QUERY_THROW);
125 uno::Sequence<uno::Any> args(1);
126 args[0] <<= OUString("DoSmeplease");
127 xInit->initialize(args);
129 // connect parser and filter
130 xParser->setDocumentHandler(xSvgDocHdl);
132 // finally, parse the stream to a hierarchy of
133 // SVGGraphicPrimitive2D which will be embedded to the
134 // primitive sequence. Their decompositions will in the
135 // end create local low-level primitives, thus SVG will
136 // be processable from all our processors
137 xParser->parseStream(myInputSource);
139 catch(const uno::Exception& e)
141 SAL_WARN( "svg", "Parse error! : " << e);
144 // decompose to primitives
145 const SvgNodeVector& rResults = pSvgDocHdl->getSvgDocument().getSvgNodeVector();
146 const sal_uInt32 nCount(rResults.size());
148 for(sal_uInt32 a(0); a < nCount; a++)
150 SvgNode* pCandidate = rResults[a];
152 if(Display_none != pCandidate->getDisplay())
154 pCandidate->decomposeSvgNode(aRetval, false);
158 else
160 OSL_ENSURE(false, "Invalid stream (!)");
163 return comphelper::containerToSequence(aRetval);
166 OUString SAL_CALL XSvgParser::getImplementationName()
168 return XSvgParser_getImplementationName();
171 sal_Bool SAL_CALL XSvgParser::supportsService(const OUString& rServiceName)
173 return cppu::supportsService(this, rServiceName);
176 uno::Sequence< OUString > SAL_CALL XSvgParser::getSupportedServiceNames()
178 return XSvgParser_getSupportedServiceNames();
181 } // end of namespace svgreader
182 } // end of namespace svgio
184 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */