/src/libreoffice/xmlscript/source/xmllib_imexp/imp_share.hxx
Line | Count | Source |
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 | | #pragma once |
21 | | |
22 | | #include <xmlscript/xmllib_imexp.hxx> |
23 | | |
24 | | #include <cppuhelper/implbase.hxx> |
25 | | |
26 | | #include <com/sun/star/xml/input/XRoot.hpp> |
27 | | #include <com/sun/star/xml/sax/SAXException.hpp> |
28 | | #include <rtl/ref.hxx> |
29 | | #include <o3tl/string_view.hxx> |
30 | | |
31 | | #include <vector> |
32 | | |
33 | | namespace xmlscript |
34 | | { |
35 | | inline sal_Int32 toInt32( std::u16string_view rStr ) |
36 | | { |
37 | | sal_Int32 nVal; |
38 | | if (rStr.size() > 2 && rStr[ 0 ] == '0' && rStr[ 1 ] == 'x') |
39 | | { |
40 | | nVal = o3tl::toUInt32(rStr.substr( 2 ), 16); |
41 | | } |
42 | | else |
43 | | { |
44 | | nVal = o3tl::toInt32(rStr); |
45 | | } |
46 | | return nVal; |
47 | | } |
48 | | inline bool getBoolAttr( |
49 | | bool * pRet, OUString const & rAttrName, |
50 | | css::uno::Reference< css::xml::input::XAttributes > const & xAttributes, sal_Int32 uid ) |
51 | 0 | { |
52 | 0 | OUString aValue( |
53 | 0 | xAttributes->getValueByUidName( uid, rAttrName ) ); |
54 | 0 | if (!aValue.isEmpty()) |
55 | 0 | { |
56 | 0 | if ( aValue == "true" ) |
57 | 0 | { |
58 | 0 | *pRet = true; |
59 | 0 | return true; |
60 | 0 | } |
61 | 0 | else if ( aValue == "false" ) |
62 | 0 | { |
63 | 0 | *pRet = false; |
64 | 0 | return true; |
65 | 0 | } |
66 | 0 | else |
67 | 0 | { |
68 | 0 | throw css::xml::sax::SAXException(rAttrName + ": no boolean value (true|false)!", css::uno::Reference< css::uno::XInterface >(), css::uno::Any() ); |
69 | 0 | } |
70 | 0 | } |
71 | 0 | return false; |
72 | 0 | } |
73 | | |
74 | | inline bool getStringAttr( |
75 | | OUString * pRet, OUString const & rAttrName, |
76 | | css::uno::Reference< css::xml::input::XAttributes > const & xAttributes, sal_Int32 uid ) |
77 | | { |
78 | | *pRet = xAttributes->getValueByUidName( uid, rAttrName ); |
79 | | return (!pRet->isEmpty()); |
80 | | } |
81 | | |
82 | | inline bool getLongAttr( |
83 | | sal_Int32 * pRet, OUString const & rAttrName, |
84 | | css::uno::Reference< css::xml::input::XAttributes > const & xAttributes, |
85 | | sal_Int32 uid ) |
86 | | { |
87 | | OUString aValue( |
88 | | xAttributes->getValueByUidName( uid, rAttrName ) ); |
89 | | if (!aValue.isEmpty()) |
90 | | { |
91 | | *pRet = toInt32( aValue ); |
92 | | return true; |
93 | | } |
94 | | return false; |
95 | | } |
96 | | |
97 | | // Library import |
98 | | |
99 | | struct LibraryImport |
100 | | : public ::cppu::WeakImplHelper< css::xml::input::XRoot > |
101 | | { |
102 | | friend class LibrariesElement; |
103 | | friend class LibraryElement; |
104 | | |
105 | | LibDescriptorArray* mpLibArray; |
106 | | LibDescriptor* const mpLibDesc; // Single library mode |
107 | | |
108 | | sal_Int32 XMLNS_LIBRARY_UID; |
109 | | sal_Int32 XMLNS_XLINK_UID; |
110 | | |
111 | | public: |
112 | | explicit LibraryImport( LibDescriptorArray* pLibArray ) |
113 | 0 | : mpLibArray(pLibArray) |
114 | 0 | , mpLibDesc(nullptr) |
115 | 0 | , XMLNS_LIBRARY_UID(0) |
116 | 0 | , XMLNS_XLINK_UID(0) |
117 | 0 | { |
118 | 0 | } |
119 | | |
120 | | // Single library mode |
121 | | explicit LibraryImport(LibDescriptor* pLibDesc) |
122 | 0 | : mpLibArray(nullptr) |
123 | 0 | , mpLibDesc(pLibDesc) |
124 | 0 | , XMLNS_LIBRARY_UID(0) |
125 | 0 | , XMLNS_XLINK_UID(0) |
126 | 0 | { |
127 | 0 | } |
128 | | |
129 | | virtual ~LibraryImport() override; |
130 | | |
131 | | // XRoot |
132 | | virtual void SAL_CALL startDocument( |
133 | | css::uno::Reference< css::xml::input::XNamespaceMapping > const & xNamespaceMapping ) override; |
134 | | virtual void SAL_CALL endDocument() override; |
135 | | virtual void SAL_CALL processingInstruction( |
136 | | OUString const & rTarget, OUString const & rData ) override; |
137 | | virtual void SAL_CALL setDocumentLocator( |
138 | | css::uno::Reference< css::xml::sax::XLocator > const & xLocator ) override; |
139 | | virtual css::uno::Reference< css::xml::input::XElement > SAL_CALL startRootElement( |
140 | | sal_Int32 nUid, OUString const & rLocalName, |
141 | | css::uno::Reference< css::xml::input::XAttributes > const & xAttributes ) override; |
142 | | }; |
143 | | |
144 | | class LibElementBase |
145 | | : public ::cppu::WeakImplHelper< css::xml::input::XElement > |
146 | | { |
147 | | protected: |
148 | | rtl::Reference<LibraryImport> mxImport; |
149 | | rtl::Reference<LibElementBase> mxParent; |
150 | | private: |
151 | | OUString const _aLocalName; |
152 | | css::uno::Reference< css::xml::input::XAttributes > _xAttributes; |
153 | | |
154 | | public: |
155 | | LibElementBase( |
156 | | OUString aLocalName, |
157 | | css::uno::Reference< css::xml::input::XAttributes > const & xAttributes, |
158 | | LibElementBase * pParent, LibraryImport * pImport ); |
159 | | virtual ~LibElementBase() override; |
160 | | |
161 | | // XElement |
162 | | virtual css::uno::Reference< css::xml::input::XElement > SAL_CALL getParent() override; |
163 | | virtual OUString SAL_CALL getLocalName() override; |
164 | | virtual sal_Int32 SAL_CALL getUid() override; |
165 | | virtual css::uno::Reference< css::xml::input::XAttributes > SAL_CALL getAttributes() override; |
166 | | virtual void SAL_CALL ignorableWhitespace( |
167 | | OUString const & rWhitespaces ) override; |
168 | | virtual void SAL_CALL characters( OUString const & rChars ) override; |
169 | | virtual void SAL_CALL processingInstruction( |
170 | | OUString const & rTarget, OUString const & rData ) override; |
171 | | virtual void SAL_CALL endElement() override; |
172 | | virtual css::uno::Reference< css::xml::input::XElement > SAL_CALL startChildElement( |
173 | | sal_Int32 nUid, OUString const & rLocalName, |
174 | | css::uno::Reference< css::xml::input::XAttributes > const & xAttributes ) override; |
175 | | }; |
176 | | |
177 | | class LibrariesElement : public LibElementBase |
178 | | { |
179 | | friend class LibraryElement; |
180 | | |
181 | | std::vector< LibDescriptor > mLibDescriptors; |
182 | | |
183 | | public: |
184 | | virtual css::uno::Reference< css::xml::input::XElement > SAL_CALL startChildElement( |
185 | | sal_Int32 nUid, OUString const & rLocalName, |
186 | | css::uno::Reference< css::xml::input::XAttributes > const & xAttributes ) override; |
187 | | virtual void SAL_CALL endElement() override; |
188 | | |
189 | | LibrariesElement( |
190 | | OUString const & rLocalName, |
191 | | css::uno::Reference< css::xml::input::XAttributes > const & xAttributes, |
192 | | LibraryImport * pImport ) |
193 | 0 | : LibElementBase( rLocalName, xAttributes, nullptr, pImport ) |
194 | 0 | {} |
195 | | }; |
196 | | |
197 | | class LibraryElement : public LibElementBase |
198 | | { |
199 | | std::vector< OUString > mElements; |
200 | | |
201 | | public: |
202 | | |
203 | | virtual css::uno::Reference< css::xml::input::XElement > SAL_CALL startChildElement( |
204 | | sal_Int32 nUid, OUString const & rLocalName, |
205 | | css::uno::Reference< css::xml::input::XAttributes > const & xAttributes ) override; |
206 | | virtual void SAL_CALL endElement() override; |
207 | | |
208 | | LibraryElement( |
209 | | OUString const & rLocalName, |
210 | | css::uno::Reference< css::xml::input::XAttributes > const & xAttributes, |
211 | | LibElementBase * pParent, LibraryImport * pImport ) |
212 | 0 | : LibElementBase( rLocalName, xAttributes, pParent, pImport ) |
213 | 0 | {} |
214 | | }; |
215 | | |
216 | | } |
217 | | |
218 | | |
219 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |