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