1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
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>
27 #include <comphelper/attributelist.hxx>
28 #include <rtl/ref.hxx>
30 #include "doctemplateslocal.hxx"
32 using namespace ::com::sun::star
;
37 // Relations info related strings
38 constexpr OUStringLiteral
g_sGroupListElement(u
"groupuinames:template-group-list");
39 constexpr OUStringLiteral
g_sGroupElement(u
"groupuinames:template-group");
40 constexpr OUStringLiteral
g_sNameAttr(u
"groupuinames:name");
41 constexpr OUStringLiteral
g_sUINameAttr(u
"groupuinames:default-ui-name");
45 std::vector
< beans::StringPair
> DocTemplLocaleHelper::ReadGroupLocalizationSequence( const uno::Reference
< io::XInputStream
>& xInStream
, const uno::Reference
< uno::XComponentContext
>& xContext
)
47 return ReadLocalizationSequence_Impl( xInStream
, "groupuinames.xml", xContext
);
51 void DocTemplLocaleHelper::WriteGroupLocalizationSequence( const uno::Reference
< io::XOutputStream
>& xOutStream
, const std::vector
< beans::StringPair
>& aSequence
, const uno::Reference
< uno::XComponentContext
>& xContext
)
53 if ( !xOutStream
.is() )
54 throw uno::RuntimeException();
56 uno::Reference
< xml::sax::XWriter
> xWriterHandler(
57 xml::sax::Writer::create(xContext
) );
59 xWriterHandler
->setOutputStream( xOutStream
);
61 static const OUStringLiteral
aCDATAString( u
"CDATA" );
62 static const OUStringLiteral
aWhiteSpace( u
" " );
64 // write the namespace
65 rtl::Reference
<::comphelper::AttributeList
> pRootAttrList
= new ::comphelper::AttributeList
;
66 pRootAttrList
->AddAttribute(
69 "http://openoffice.org/2006/groupuinames" );
71 xWriterHandler
->startDocument();
72 xWriterHandler
->startElement( g_sGroupListElement
, pRootAttrList
);
74 for (const auto & i
: aSequence
)
76 rtl::Reference
<::comphelper::AttributeList
> pAttrList
= new ::comphelper::AttributeList
;
77 pAttrList
->AddAttribute( g_sNameAttr
, aCDATAString
, i
.First
);
78 pAttrList
->AddAttribute( g_sUINameAttr
, aCDATAString
, i
.Second
);
80 xWriterHandler
->startElement( g_sGroupElement
, pAttrList
);
81 xWriterHandler
->ignorableWhitespace( aWhiteSpace
);
82 xWriterHandler
->endElement( g_sGroupElement
);
85 xWriterHandler
->ignorableWhitespace( aWhiteSpace
);
86 xWriterHandler
->endElement( g_sGroupListElement
);
87 xWriterHandler
->endDocument();
91 std::vector
< beans::StringPair
> DocTemplLocaleHelper::ReadLocalizationSequence_Impl( const uno::Reference
< io::XInputStream
>& xInStream
, const OUString
& aStringID
, const uno::Reference
< uno::XComponentContext
>& xContext
)
93 if ( !xContext
.is() || !xInStream
.is() )
94 throw uno::RuntimeException();
96 uno::Reference
< xml::sax::XParser
> xParser
= xml::sax::Parser::create( xContext
);
98 rtl::Reference
<DocTemplLocaleHelper
> pHelper
= new DocTemplLocaleHelper();
99 xml::sax::InputSource aParserInput
;
100 aParserInput
.aInputStream
= xInStream
;
101 aParserInput
.sSystemId
= aStringID
;
102 xParser
->setDocumentHandler( pHelper
);
103 xParser
->parseStream( aParserInput
);
104 xParser
->setDocumentHandler( uno::Reference
< xml::sax::XDocumentHandler
> () );
106 return pHelper
->GetParsingResult();
110 DocTemplLocaleHelper::DocTemplLocaleHelper()
115 DocTemplLocaleHelper::~DocTemplLocaleHelper()
120 std::vector
< beans::StringPair
> const & DocTemplLocaleHelper::GetParsingResult() const
122 if ( !m_aElementsSeq
.empty() )
123 throw uno::RuntimeException("The parsing has still not finished!");
129 void SAL_CALL
DocTemplLocaleHelper::startDocument()
134 void SAL_CALL
DocTemplLocaleHelper::endDocument()
139 void SAL_CALL
DocTemplLocaleHelper::startElement( const OUString
& aName
, const uno::Reference
< xml::sax::XAttributeList
>& xAttribs
)
141 if ( aName
== g_sGroupListElement
)
143 if ( !m_aElementsSeq
.empty() )
144 throw xml::sax::SAXException(); // TODO: this element must be the first level element
146 m_aElementsSeq
.push_back( aName
);
148 return; // nothing to do
150 else if ( aName
== g_sGroupElement
)
152 if ( m_aElementsSeq
.size() != 1 )
153 throw xml::sax::SAXException(); // TODO: this element must be the second level element
155 m_aElementsSeq
.push_back( aName
);
157 const auto nNewEntryNum
= m_aResultSeq
.size();
158 m_aResultSeq
.resize( nNewEntryNum
+1 );
160 const OUString aNameValue
= xAttribs
->getValueByName( g_sNameAttr
);
161 if ( aNameValue
.isEmpty() )
162 throw xml::sax::SAXException(); // TODO: the ID value must present
164 const OUString aUINameValue
= xAttribs
->getValueByName( g_sUINameAttr
);
165 if ( aUINameValue
.isEmpty() )
166 throw xml::sax::SAXException(); // TODO: the ID value must present
168 m_aResultSeq
[nNewEntryNum
].First
= aNameValue
;
169 m_aResultSeq
[nNewEntryNum
].Second
= aUINameValue
;
173 // accept future extensions
174 if ( m_aElementsSeq
.empty() )
175 throw xml::sax::SAXException(); // TODO: the extension element must not be the first level element
177 m_aElementsSeq
.push_back( aName
);
182 void SAL_CALL
DocTemplLocaleHelper::endElement( const OUString
& aName
)
184 if ( m_aElementsSeq
.empty() )
185 throw xml::sax::SAXException(); // TODO: no other end elements expected!
187 if ( m_aElementsSeq
.back() != aName
)
188 throw xml::sax::SAXException(); // TODO: unexpected element ended
190 m_aElementsSeq
.pop_back();
194 void SAL_CALL
DocTemplLocaleHelper::characters( const OUString
& /*aChars*/ )
199 void SAL_CALL
DocTemplLocaleHelper::ignorableWhitespace( const OUString
& /*aWhitespaces*/ )
204 void SAL_CALL
DocTemplLocaleHelper::processingInstruction( const OUString
& /*aTarget*/, const OUString
& /*aData*/ )
209 void SAL_CALL
DocTemplLocaleHelper::setDocumentLocator( const uno::Reference
< xml::sax::XLocator
>& /*xLocator*/ )
213 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */