xref: /core/sfx2/source/doc/doctemplateslocal.cxx (revision ca5c9591)
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 
ReadGroupLocalizationSequence(const uno::Reference<io::XInputStream> & xInStream,const uno::Reference<uno::XComponentContext> & xContext)45 std::vector< beans::StringPair > DocTemplLocaleHelper::ReadGroupLocalizationSequence( const uno::Reference< io::XInputStream >& xInStream, const uno::Reference< uno::XComponentContext >& xContext )
46 {
47     return ReadLocalizationSequence_Impl( xInStream, u"groupuinames.xml"_ustr, xContext );
48 }
49 
50 
WriteGroupLocalizationSequence(const uno::Reference<io::XOutputStream> & xOutStream,const std::vector<beans::StringPair> & aSequence,const uno::Reference<uno::XComponentContext> & xContext)51 void 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 OUString aWhiteSpace( u" "_ustr );
62 
63     // write the namespace
64     rtl::Reference<::comphelper::AttributeList> pRootAttrList = new ::comphelper::AttributeList;
65     pRootAttrList->AddAttribute(
66         u"xmlns:groupuinames"_ustr,
67         u"http://openoffice.org/2006/groupuinames"_ustr );
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 
ReadLocalizationSequence_Impl(const uno::Reference<io::XInputStream> & xInStream,const OUString & aStringID,const uno::Reference<uno::XComponentContext> & xContext)89 std::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 
96     rtl::Reference<DocTemplLocaleHelper> pHelper = new DocTemplLocaleHelper();
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 
DocTemplLocaleHelper()108 DocTemplLocaleHelper::DocTemplLocaleHelper()
109 {
110 }
111 
112 
~DocTemplLocaleHelper()113 DocTemplLocaleHelper::~DocTemplLocaleHelper()
114 {
115 }
116 
117 
GetParsingResult() const118 std::vector< beans::StringPair > const & DocTemplLocaleHelper::GetParsingResult() const
119 {
120     if ( !m_aElementsSeq.empty() )
121         throw uno::RuntimeException(u"The parsing has still not finished!"_ustr);
122 
123     return m_aResultSeq;
124 }
125 
126 
startDocument()127 void SAL_CALL DocTemplLocaleHelper::startDocument()
128 {
129 }
130 
131 
endDocument()132 void SAL_CALL DocTemplLocaleHelper::endDocument()
133 {
134 }
135 
136 
startElement(const OUString & aName,const uno::Reference<xml::sax::XAttributeList> & xAttribs)137 void 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 
endElement(const OUString & aName)180 void 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 
characters(const OUString &)192 void SAL_CALL DocTemplLocaleHelper::characters( const OUString& /*aChars*/ )
193 {
194 }
195 
196 
ignorableWhitespace(const OUString &)197 void SAL_CALL DocTemplLocaleHelper::ignorableWhitespace( const OUString& /*aWhitespaces*/ )
198 {
199 }
200 
201 
processingInstruction(const OUString &,const OUString &)202 void SAL_CALL DocTemplLocaleHelper::processingInstruction( const OUString& /*aTarget*/, const OUString& /*aData*/ )
203 {
204 }
205 
206 
setDocumentLocator(const uno::Reference<xml::sax::XLocator> &)207 void SAL_CALL DocTemplLocaleHelper::setDocumentLocator( const uno::Reference< xml::sax::XLocator >& /*xLocator*/ )
208 {
209 }
210 
211 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
212