/src/libreoffice/sfx2/source/doc/doctemplateslocal.cxx
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 | | |
21 | | #include <com/sun/star/beans/StringPair.hpp> |
22 | | #include <com/sun/star/xml/sax/Parser.hpp> |
23 | | #include <com/sun/star/xml/sax/SAXException.hpp> |
24 | | #include <com/sun/star/xml/sax/Writer.hpp> |
25 | | #include <com/sun/star/xml/sax/XDocumentHandler.hpp> |
26 | | |
27 | | #include <comphelper/attributelist.hxx> |
28 | | #include <rtl/ref.hxx> |
29 | | |
30 | | #include "doctemplateslocal.hxx" |
31 | | |
32 | | using namespace ::com::sun::star; |
33 | | |
34 | | namespace |
35 | | { |
36 | | |
37 | | // Relations info related strings |
38 | | constexpr OUString g_sGroupListElement(u"groupuinames:template-group-list"_ustr); |
39 | | constexpr OUString g_sGroupElement(u"groupuinames:template-group"_ustr); |
40 | | constexpr OUString g_sNameAttr(u"groupuinames:name"_ustr); |
41 | | constexpr OUString g_sUINameAttr(u"groupuinames:default-ui-name"_ustr); |
42 | | |
43 | | } |
44 | | |
45 | | std::vector< beans::StringPair > DocTemplLocaleHelper::ReadGroupLocalizationSequence( const uno::Reference< io::XInputStream >& xInStream, const uno::Reference< uno::XComponentContext >& xContext ) |
46 | 0 | { |
47 | 0 | return ReadLocalizationSequence_Impl( xInStream, u"groupuinames.xml"_ustr, xContext ); |
48 | 0 | } |
49 | | |
50 | | |
51 | | void DocTemplLocaleHelper::WriteGroupLocalizationSequence( const uno::Reference< io::XOutputStream >& xOutStream, const std::vector< beans::StringPair >& aSequence, const uno::Reference< uno::XComponentContext >& xContext ) |
52 | 0 | { |
53 | 0 | if ( !xOutStream.is() ) |
54 | 0 | throw uno::RuntimeException(); |
55 | | |
56 | 0 | uno::Reference< xml::sax::XWriter > xWriterHandler( |
57 | 0 | xml::sax::Writer::create(xContext) ); |
58 | |
|
59 | 0 | xWriterHandler->setOutputStream( xOutStream ); |
60 | |
|
61 | 0 | static constexpr OUString aWhiteSpace( u" "_ustr ); |
62 | | |
63 | | // write the namespace |
64 | 0 | rtl::Reference<::comphelper::AttributeList> pRootAttrList = new ::comphelper::AttributeList; |
65 | 0 | pRootAttrList->AddAttribute( |
66 | 0 | u"xmlns:groupuinames"_ustr, |
67 | 0 | u"http://openoffice.org/2006/groupuinames"_ustr ); |
68 | |
|
69 | 0 | xWriterHandler->startDocument(); |
70 | 0 | xWriterHandler->startElement( g_sGroupListElement, pRootAttrList ); |
71 | |
|
72 | 0 | for (const auto & i : aSequence) |
73 | 0 | { |
74 | 0 | rtl::Reference<::comphelper::AttributeList> pAttrList = new ::comphelper::AttributeList; |
75 | 0 | pAttrList->AddAttribute( g_sNameAttr, i.First ); |
76 | 0 | pAttrList->AddAttribute( g_sUINameAttr, i.Second ); |
77 | |
|
78 | 0 | xWriterHandler->startElement( g_sGroupElement, pAttrList ); |
79 | 0 | xWriterHandler->ignorableWhitespace( aWhiteSpace ); |
80 | 0 | xWriterHandler->endElement( g_sGroupElement ); |
81 | 0 | } |
82 | |
|
83 | 0 | xWriterHandler->ignorableWhitespace( aWhiteSpace ); |
84 | 0 | xWriterHandler->endElement( g_sGroupListElement ); |
85 | 0 | xWriterHandler->endDocument(); |
86 | 0 | } |
87 | | |
88 | | |
89 | | std::vector< beans::StringPair > DocTemplLocaleHelper::ReadLocalizationSequence_Impl( const uno::Reference< io::XInputStream >& xInStream, const OUString& aStringID, const uno::Reference< uno::XComponentContext >& xContext ) |
90 | 0 | { |
91 | 0 | if ( !xContext.is() || !xInStream.is() ) |
92 | 0 | throw uno::RuntimeException(); |
93 | | |
94 | 0 | uno::Reference< xml::sax::XParser > xParser = xml::sax::Parser::create( xContext ); |
95 | |
|
96 | 0 | rtl::Reference<DocTemplLocaleHelper> pHelper = new DocTemplLocaleHelper(); |
97 | 0 | xml::sax::InputSource aParserInput; |
98 | 0 | aParserInput.aInputStream = xInStream; |
99 | 0 | aParserInput.sSystemId = aStringID; |
100 | 0 | xParser->setDocumentHandler( pHelper ); |
101 | 0 | xParser->parseStream( aParserInput ); |
102 | 0 | xParser->setDocumentHandler( uno::Reference < xml::sax::XDocumentHandler > () ); |
103 | |
|
104 | 0 | return pHelper->GetParsingResult(); |
105 | 0 | } |
106 | | |
107 | | |
108 | | DocTemplLocaleHelper::DocTemplLocaleHelper() |
109 | 0 | { |
110 | 0 | } |
111 | | |
112 | | |
113 | | DocTemplLocaleHelper::~DocTemplLocaleHelper() |
114 | 0 | { |
115 | 0 | } |
116 | | |
117 | | |
118 | | std::vector< beans::StringPair > const & DocTemplLocaleHelper::GetParsingResult() const |
119 | 0 | { |
120 | 0 | if ( !m_aElementsSeq.empty() ) |
121 | 0 | throw uno::RuntimeException(u"The parsing has still not finished!"_ustr); |
122 | | |
123 | 0 | return m_aResultSeq; |
124 | 0 | } |
125 | | |
126 | | |
127 | | void SAL_CALL DocTemplLocaleHelper::startDocument() |
128 | 0 | { |
129 | 0 | } |
130 | | |
131 | | |
132 | | void SAL_CALL DocTemplLocaleHelper::endDocument() |
133 | 0 | { |
134 | 0 | } |
135 | | |
136 | | |
137 | | void SAL_CALL DocTemplLocaleHelper::startElement( const OUString& aName, const uno::Reference< xml::sax::XAttributeList >& xAttribs ) |
138 | 0 | { |
139 | 0 | if ( aName == g_sGroupListElement ) |
140 | 0 | { |
141 | 0 | if ( !m_aElementsSeq.empty() ) |
142 | 0 | throw xml::sax::SAXException(); // TODO: this element must be the first level element |
143 | | |
144 | 0 | m_aElementsSeq.push_back( aName ); |
145 | |
|
146 | 0 | return; // nothing to do |
147 | 0 | } |
148 | 0 | else if ( aName == g_sGroupElement ) |
149 | 0 | { |
150 | 0 | if ( m_aElementsSeq.size() != 1 ) |
151 | 0 | throw xml::sax::SAXException(); // TODO: this element must be the second level element |
152 | | |
153 | 0 | m_aElementsSeq.push_back( aName ); |
154 | |
|
155 | 0 | const auto nNewEntryNum = m_aResultSeq.size(); |
156 | 0 | m_aResultSeq.resize( nNewEntryNum+1 ); |
157 | |
|
158 | 0 | const OUString aNameValue = xAttribs->getValueByName( g_sNameAttr ); |
159 | 0 | if ( aNameValue.isEmpty() ) |
160 | 0 | throw xml::sax::SAXException(); // TODO: the ID value must present |
161 | | |
162 | 0 | const OUString aUINameValue = xAttribs->getValueByName( g_sUINameAttr ); |
163 | 0 | if ( aUINameValue.isEmpty() ) |
164 | 0 | throw xml::sax::SAXException(); // TODO: the ID value must present |
165 | | |
166 | 0 | m_aResultSeq[nNewEntryNum].First = aNameValue; |
167 | 0 | m_aResultSeq[nNewEntryNum].Second = aUINameValue; |
168 | 0 | } |
169 | 0 | else |
170 | 0 | { |
171 | | // accept future extensions |
172 | 0 | if ( m_aElementsSeq.empty() ) |
173 | 0 | throw xml::sax::SAXException(); // TODO: the extension element must not be the first level element |
174 | | |
175 | 0 | m_aElementsSeq.push_back( aName ); |
176 | 0 | } |
177 | 0 | } |
178 | | |
179 | | |
180 | | void SAL_CALL DocTemplLocaleHelper::endElement( const OUString& aName ) |
181 | 0 | { |
182 | 0 | if ( m_aElementsSeq.empty() ) |
183 | 0 | throw xml::sax::SAXException(); // TODO: no other end elements expected! |
184 | | |
185 | 0 | if ( m_aElementsSeq.back() != aName ) |
186 | 0 | throw xml::sax::SAXException(); // TODO: unexpected element ended |
187 | | |
188 | 0 | m_aElementsSeq.pop_back(); |
189 | 0 | } |
190 | | |
191 | | |
192 | | void SAL_CALL DocTemplLocaleHelper::characters( const OUString& /*aChars*/ ) |
193 | 0 | { |
194 | 0 | } |
195 | | |
196 | | |
197 | | void SAL_CALL DocTemplLocaleHelper::ignorableWhitespace( const OUString& /*aWhitespaces*/ ) |
198 | 0 | { |
199 | 0 | } |
200 | | |
201 | | |
202 | | void SAL_CALL DocTemplLocaleHelper::processingInstruction( const OUString& /*aTarget*/, const OUString& /*aData*/ ) |
203 | 0 | { |
204 | 0 | } |
205 | | |
206 | | |
207 | | void SAL_CALL DocTemplLocaleHelper::setDocumentLocator( const uno::Reference< xml::sax::XLocator >& /*xLocator*/ ) |
208 | 0 | { |
209 | 0 | } |
210 | | |
211 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |