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>
29 #include "doctemplateslocal.hxx"
31 using namespace ::com::sun::star
;
36 // Relations info related strings
37 constexpr OUStringLiteral
g_sGroupListElement(u
"groupuinames:template-group-list");
38 constexpr OUStringLiteral
g_sGroupElement(u
"groupuinames:template-group");
39 constexpr OUStringLiteral
g_sNameAttr(u
"groupuinames:name");
40 constexpr OUStringLiteral
g_sUINameAttr(u
"groupuinames:default-ui-name");
44 std::vector
< beans::StringPair
> DocTemplLocaleHelper::ReadGroupLocalizationSequence( const uno::Reference
< io::XInputStream
>& xInStream
, const uno::Reference
< uno::XComponentContext
>& xContext
)
46 return ReadLocalizationSequence_Impl( xInStream
, "groupuinames.xml", xContext
);
50 void DocTemplLocaleHelper::WriteGroupLocalizationSequence( const uno::Reference
< io::XOutputStream
>& xOutStream
, const std::vector
< beans::StringPair
>& aSequence
, const uno::Reference
< uno::XComponentContext
>& xContext
)
52 if ( !xOutStream
.is() )
53 throw uno::RuntimeException();
55 uno::Reference
< xml::sax::XWriter
> xWriterHandler(
56 xml::sax::Writer::create(xContext
) );
58 xWriterHandler
->setOutputStream( xOutStream
);
60 const OUString
aCDATAString( "CDATA" );
61 const OUString
aWhiteSpace( " " );
63 // write the namespace
64 ::comphelper::AttributeList
* pRootAttrList
= new ::comphelper::AttributeList
;
65 uno::Reference
< xml::sax::XAttributeList
> xRootAttrList( pRootAttrList
);
66 pRootAttrList
->AddAttribute(
69 "http://openoffice.org/2006/groupuinames" );
71 xWriterHandler
->startDocument();
72 xWriterHandler
->startElement( g_sGroupListElement
, xRootAttrList
);
74 for (const auto & i
: aSequence
)
76 ::comphelper::AttributeList
*pAttrList
= new ::comphelper::AttributeList
;
77 uno::Reference
< xml::sax::XAttributeList
> xAttrList( pAttrList
);
78 pAttrList
->AddAttribute( g_sNameAttr
, aCDATAString
, i
.First
);
79 pAttrList
->AddAttribute( g_sUINameAttr
, aCDATAString
, i
.Second
);
81 xWriterHandler
->startElement( g_sGroupElement
, xAttrList
);
82 xWriterHandler
->ignorableWhitespace( aWhiteSpace
);
83 xWriterHandler
->endElement( g_sGroupElement
);
86 xWriterHandler
->ignorableWhitespace( aWhiteSpace
);
87 xWriterHandler
->endElement( g_sGroupListElement
);
88 xWriterHandler
->endDocument();
92 std::vector
< beans::StringPair
> DocTemplLocaleHelper::ReadLocalizationSequence_Impl( const uno::Reference
< io::XInputStream
>& xInStream
, const OUString
& aStringID
, const uno::Reference
< uno::XComponentContext
>& xContext
)
94 if ( !xContext
.is() || !xInStream
.is() )
95 throw uno::RuntimeException();
97 uno::Reference
< xml::sax::XParser
> xParser
= xml::sax::Parser::create( xContext
);
99 DocTemplLocaleHelper
* pHelper
= new DocTemplLocaleHelper();
100 uno::Reference
< xml::sax::XDocumentHandler
> xHelper( static_cast< xml::sax::XDocumentHandler
* >( pHelper
) );
101 xml::sax::InputSource aParserInput
;
102 aParserInput
.aInputStream
= xInStream
;
103 aParserInput
.sSystemId
= aStringID
;
104 xParser
->setDocumentHandler( xHelper
);
105 xParser
->parseStream( aParserInput
);
106 xParser
->setDocumentHandler( uno::Reference
< xml::sax::XDocumentHandler
> () );
108 return pHelper
->GetParsingResult();
112 DocTemplLocaleHelper::DocTemplLocaleHelper()
117 DocTemplLocaleHelper::~DocTemplLocaleHelper()
122 std::vector
< beans::StringPair
> const & DocTemplLocaleHelper::GetParsingResult() const
124 if ( !m_aElementsSeq
.empty() )
125 throw uno::RuntimeException(); // the parsing has still not finished!
131 void SAL_CALL
DocTemplLocaleHelper::startDocument()
136 void SAL_CALL
DocTemplLocaleHelper::endDocument()
141 void SAL_CALL
DocTemplLocaleHelper::startElement( const OUString
& aName
, const uno::Reference
< xml::sax::XAttributeList
>& xAttribs
)
143 if ( aName
== g_sGroupListElement
)
145 if ( !m_aElementsSeq
.empty() )
146 throw xml::sax::SAXException(); // TODO: this element must be the first level element
148 m_aElementsSeq
.push_back( aName
);
150 return; // nothing to do
152 else if ( aName
== g_sGroupElement
)
154 if ( m_aElementsSeq
.size() != 1 )
155 throw xml::sax::SAXException(); // TODO: this element must be the second level element
157 m_aElementsSeq
.push_back( aName
);
159 const auto nNewEntryNum
= m_aResultSeq
.size();
160 m_aResultSeq
.resize( nNewEntryNum
+1 );
162 const OUString aNameValue
= xAttribs
->getValueByName( g_sNameAttr
);
163 if ( aNameValue
.isEmpty() )
164 throw xml::sax::SAXException(); // TODO: the ID value must present
166 const OUString aUINameValue
= xAttribs
->getValueByName( g_sUINameAttr
);
167 if ( aUINameValue
.isEmpty() )
168 throw xml::sax::SAXException(); // TODO: the ID value must present
170 m_aResultSeq
[nNewEntryNum
].First
= aNameValue
;
171 m_aResultSeq
[nNewEntryNum
].Second
= aUINameValue
;
175 // accept future extensions
176 if ( m_aElementsSeq
.empty() )
177 throw xml::sax::SAXException(); // TODO: the extension element must not be the first level element
179 m_aElementsSeq
.push_back( aName
);
184 void SAL_CALL
DocTemplLocaleHelper::endElement( const OUString
& aName
)
186 if ( m_aElementsSeq
.empty() )
187 throw xml::sax::SAXException(); // TODO: no other end elements expected!
189 if ( m_aElementsSeq
.back() != aName
)
190 throw xml::sax::SAXException(); // TODO: unexpected element ended
192 m_aElementsSeq
.pop_back();
196 void SAL_CALL
DocTemplLocaleHelper::characters( const OUString
& /*aChars*/ )
201 void SAL_CALL
DocTemplLocaleHelper::ignorableWhitespace( const OUString
& /*aWhitespaces*/ )
206 void SAL_CALL
DocTemplLocaleHelper::processingInstruction( const OUString
& /*aTarget*/, const OUString
& /*aData*/ )
211 void SAL_CALL
DocTemplLocaleHelper::setDocumentLocator( const uno::Reference
< xml::sax::XLocator
>& /*xLocator*/ )
215 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */