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/lang/XMultiServiceFactory.hpp>
23 #include <com/sun/star/io/XActiveDataSource.hpp>
24 #include <com/sun/star/xml/sax/Parser.hpp>
25 #include <com/sun/star/xml/sax/SAXException.hpp>
26 #include <com/sun/star/xml/sax/Writer.hpp>
27 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
28 #include <com/sun/star/lang/IllegalArgumentException.hpp>
30 #include <comphelper/attributelist.hxx>
32 #include "doctemplateslocal.hxx"
34 using namespace ::com::sun::star
;
39 // Relations info related strings
40 const OUStringLiteral
g_sGroupListElement("groupuinames:template-group-list");
41 const OUStringLiteral
g_sGroupElement("groupuinames:template-group");
42 const OUStringLiteral
g_sNameAttr("groupuinames:name");
43 const OUStringLiteral
g_sUINameAttr("groupuinames:default-ui-name");
47 std::vector
< beans::StringPair
> DocTemplLocaleHelper::ReadGroupLocalizationSequence( const uno::Reference
< io::XInputStream
>& xInStream
, const uno::Reference
< uno::XComponentContext
>& xContext
)
49 return ReadLocalizationSequence_Impl( xInStream
, "groupuinames.xml", xContext
);
53 void DocTemplLocaleHelper::WriteGroupLocalizationSequence( const uno::Reference
< io::XOutputStream
>& xOutStream
, const std::vector
< beans::StringPair
>& aSequence
, const uno::Reference
< uno::XComponentContext
>& xContext
)
55 if ( !xOutStream
.is() )
56 throw uno::RuntimeException();
58 uno::Reference
< xml::sax::XWriter
> xWriterHandler(
59 xml::sax::Writer::create(xContext
) );
61 xWriterHandler
->setOutputStream( xOutStream
);
63 const OUString
aCDATAString( "CDATA" );
64 const OUString
aWhiteSpace( " " );
66 // write the namespace
67 ::comphelper::AttributeList
* pRootAttrList
= new ::comphelper::AttributeList
;
68 uno::Reference
< xml::sax::XAttributeList
> xRootAttrList( pRootAttrList
);
69 pRootAttrList
->AddAttribute(
72 "http://openoffice.org/2006/groupuinames" );
74 xWriterHandler
->startDocument();
75 xWriterHandler
->startElement( g_sGroupListElement
, xRootAttrList
);
77 for (const auto & i
: aSequence
)
79 ::comphelper::AttributeList
*pAttrList
= new ::comphelper::AttributeList
;
80 uno::Reference
< xml::sax::XAttributeList
> xAttrList( pAttrList
);
81 pAttrList
->AddAttribute( g_sNameAttr
, aCDATAString
, i
.First
);
82 pAttrList
->AddAttribute( g_sUINameAttr
, aCDATAString
, i
.Second
);
84 xWriterHandler
->startElement( g_sGroupElement
, xAttrList
);
85 xWriterHandler
->ignorableWhitespace( aWhiteSpace
);
86 xWriterHandler
->endElement( g_sGroupElement
);
89 xWriterHandler
->ignorableWhitespace( aWhiteSpace
);
90 xWriterHandler
->endElement( g_sGroupListElement
);
91 xWriterHandler
->endDocument();
95 std::vector
< beans::StringPair
> DocTemplLocaleHelper::ReadLocalizationSequence_Impl( const uno::Reference
< io::XInputStream
>& xInStream
, const OUString
& aStringID
, const uno::Reference
< uno::XComponentContext
>& xContext
)
97 if ( !xContext
.is() || !xInStream
.is() )
98 throw uno::RuntimeException();
100 uno::Reference
< xml::sax::XParser
> xParser
= xml::sax::Parser::create( xContext
);
102 DocTemplLocaleHelper
* pHelper
= new DocTemplLocaleHelper();
103 uno::Reference
< xml::sax::XDocumentHandler
> xHelper( static_cast< xml::sax::XDocumentHandler
* >( pHelper
) );
104 xml::sax::InputSource aParserInput
;
105 aParserInput
.aInputStream
= xInStream
;
106 aParserInput
.sSystemId
= aStringID
;
107 xParser
->setDocumentHandler( xHelper
);
108 xParser
->parseStream( aParserInput
);
109 xParser
->setDocumentHandler( uno::Reference
< xml::sax::XDocumentHandler
> () );
111 return pHelper
->GetParsingResult();
115 DocTemplLocaleHelper::DocTemplLocaleHelper()
120 DocTemplLocaleHelper::~DocTemplLocaleHelper()
125 std::vector
< beans::StringPair
> const & DocTemplLocaleHelper::GetParsingResult() const
127 if ( !m_aElementsSeq
.empty() )
128 throw uno::RuntimeException(); // the parsing has still not finished!
134 void SAL_CALL
DocTemplLocaleHelper::startDocument()
139 void SAL_CALL
DocTemplLocaleHelper::endDocument()
144 void SAL_CALL
DocTemplLocaleHelper::startElement( const OUString
& aName
, const uno::Reference
< xml::sax::XAttributeList
>& xAttribs
)
146 if ( aName
== g_sGroupListElement
)
148 if ( !m_aElementsSeq
.empty() )
149 throw xml::sax::SAXException(); // TODO: this element must be the first level element
151 m_aElementsSeq
.push_back( aName
);
153 return; // nothing to do
155 else if ( aName
== g_sGroupElement
)
157 if ( m_aElementsSeq
.size() != 1 )
158 throw xml::sax::SAXException(); // TODO: this element must be the second level element
160 m_aElementsSeq
.push_back( aName
);
162 const auto nNewEntryNum
= m_aResultSeq
.size();
163 m_aResultSeq
.resize( nNewEntryNum
+1 );
165 const OUString aNameValue
= xAttribs
->getValueByName( g_sNameAttr
);
166 if ( aNameValue
.isEmpty() )
167 throw xml::sax::SAXException(); // TODO: the ID value must present
169 const OUString aUINameValue
= xAttribs
->getValueByName( g_sUINameAttr
);
170 if ( aUINameValue
.isEmpty() )
171 throw xml::sax::SAXException(); // TODO: the ID value must present
173 m_aResultSeq
[nNewEntryNum
].First
= aNameValue
;
174 m_aResultSeq
[nNewEntryNum
].Second
= aUINameValue
;
178 // accept future extensions
179 if ( m_aElementsSeq
.empty() )
180 throw xml::sax::SAXException(); // TODO: the extension element must not be the first level element
182 m_aElementsSeq
.push_back( aName
);
187 void SAL_CALL
DocTemplLocaleHelper::endElement( const OUString
& aName
)
189 if ( m_aElementsSeq
.empty() )
190 throw xml::sax::SAXException(); // TODO: no other end elements expected!
192 if ( m_aElementsSeq
.back() != aName
)
193 throw xml::sax::SAXException(); // TODO: unexpected element ended
195 m_aElementsSeq
.pop_back();
199 void SAL_CALL
DocTemplLocaleHelper::characters( const OUString
& /*aChars*/ )
204 void SAL_CALL
DocTemplLocaleHelper::ignorableWhitespace( const OUString
& /*aWhitespaces*/ )
209 void SAL_CALL
DocTemplLocaleHelper::processingInstruction( const OUString
& /*aTarget*/, const OUString
& /*aData*/ )
214 void SAL_CALL
DocTemplLocaleHelper::setDocumentLocator( const uno::Reference
< xml::sax::XLocator
>& /*xLocator*/ )
218 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */