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/Writer.hpp>
26 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
27 #include <com/sun/star/lang/IllegalArgumentException.hpp>
29 #include <comphelper/attributelist.hxx>
31 #include "doctemplateslocal.hxx"
33 using namespace ::com::sun::star
;
36 std::vector
< beans::StringPair
> DocTemplLocaleHelper::ReadGroupLocalizationSequence( const uno::Reference
< io::XInputStream
>& xInStream
, const uno::Reference
< uno::XComponentContext
>& xContext
)
37 throw( uno::Exception
)
39 OUString aStringID
= "groupuinames.xml";
40 return ReadLocalizationSequence_Impl( xInStream
, aStringID
, xContext
);
44 void SAL_CALL
DocTemplLocaleHelper::WriteGroupLocalizationSequence( const uno::Reference
< io::XOutputStream
>& xOutStream
, const std::vector
< beans::StringPair
>& aSequence
, const uno::Reference
< uno::XComponentContext
>& xContext
)
45 throw( uno::Exception
)
47 if ( !xOutStream
.is() )
48 throw uno::RuntimeException();
50 uno::Reference
< xml::sax::XWriter
> xWriterHandler(
51 xml::sax::Writer::create(xContext
) );
53 xWriterHandler
->setOutputStream( xOutStream
);
55 OUString
aGroupListElement( "groupuinames:template-group-list" );
56 OUString
aGroupElement( "groupuinames:template-group" );
57 OUString
aNameAttr( "groupuinames:name" );
58 OUString
aUINameAttr( "groupuinames:default-ui-name" );
59 OUString
aCDATAString( "CDATA" );
60 OUString
aWhiteSpace( " " );
62 // write the namespace
63 ::comphelper::AttributeList
* pRootAttrList
= new ::comphelper::AttributeList
;
64 uno::Reference
< xml::sax::XAttributeList
> xRootAttrList( pRootAttrList
);
65 pRootAttrList
->AddAttribute(
68 "http://openoffice.org/2006/groupuinames" );
70 xWriterHandler
->startDocument();
71 xWriterHandler
->startElement( aGroupListElement
, xRootAttrList
);
73 for (const auto & i
: aSequence
)
75 ::comphelper::AttributeList
*pAttrList
= new ::comphelper::AttributeList
;
76 uno::Reference
< xml::sax::XAttributeList
> xAttrList( pAttrList
);
77 pAttrList
->AddAttribute( aNameAttr
, aCDATAString
, i
.First
);
78 pAttrList
->AddAttribute( aUINameAttr
, aCDATAString
, i
.Second
);
80 xWriterHandler
->startElement( aGroupElement
, xAttrList
);
81 xWriterHandler
->ignorableWhitespace( aWhiteSpace
);
82 xWriterHandler
->endElement( aGroupElement
);
85 xWriterHandler
->ignorableWhitespace( aWhiteSpace
);
86 xWriterHandler
->endElement( aGroupListElement
);
87 xWriterHandler
->endDocument();
91 std::vector
< beans::StringPair
> SAL_CALL
DocTemplLocaleHelper::ReadLocalizationSequence_Impl( const uno::Reference
< io::XInputStream
>& xInStream
, const OUString
& aStringID
, const uno::Reference
< uno::XComponentContext
>& xContext
)
92 throw( uno::Exception
)
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()
113 : m_aGroupListElement( "groupuinames:template-group-list" )
114 , m_aGroupElement( "groupuinames:template-group" )
115 , m_aNameAttr( "groupuinames:name" )
116 , m_aUINameAttr( "groupuinames:default-ui-name" )
121 DocTemplLocaleHelper::~DocTemplLocaleHelper()
126 std::vector
< beans::StringPair
> const & DocTemplLocaleHelper::GetParsingResult()
128 if ( !m_aElementsSeq
.empty() )
129 throw uno::RuntimeException(); // the parsing has still not finished!
135 void SAL_CALL
DocTemplLocaleHelper::startDocument()
136 throw(xml::sax::SAXException
, uno::RuntimeException
, std::exception
)
141 void SAL_CALL
DocTemplLocaleHelper::endDocument()
142 throw(xml::sax::SAXException
, uno::RuntimeException
, std::exception
)
147 void SAL_CALL
DocTemplLocaleHelper::startElement( const OUString
& aName
, const uno::Reference
< xml::sax::XAttributeList
>& xAttribs
)
148 throw( xml::sax::SAXException
, uno::RuntimeException
, std::exception
)
150 if ( aName
== m_aGroupListElement
)
152 if ( m_aElementsSeq
.size() != 0 )
153 throw xml::sax::SAXException(); // TODO: this element must be the first level element
155 m_aElementsSeq
.push_back( aName
);
157 return; // nothing to do
159 else if ( aName
== m_aGroupElement
)
161 if ( m_aElementsSeq
.size() != 1 )
162 throw xml::sax::SAXException(); // TODO: this element must be the second level element
164 m_aElementsSeq
.push_back( aName
);
166 sal_Int32 nNewEntryNum
= m_aResultSeq
.size() + 1;
167 m_aResultSeq
.resize( nNewEntryNum
);
169 OUString aNameValue
= xAttribs
->getValueByName( m_aNameAttr
);
170 if ( aNameValue
.isEmpty() )
171 throw xml::sax::SAXException(); // TODO: the ID value must present
173 OUString aUINameValue
= xAttribs
->getValueByName( m_aUINameAttr
);
174 if ( aUINameValue
.isEmpty() )
175 throw xml::sax::SAXException(); // TODO: the ID value must present
177 m_aResultSeq
[nNewEntryNum
-1].First
= aNameValue
;
178 m_aResultSeq
[nNewEntryNum
-1].Second
= aUINameValue
;
182 // accept future extensions
183 if ( m_aElementsSeq
.empty() )
184 throw xml::sax::SAXException(); // TODO: the extension element must not be the first level element
186 m_aElementsSeq
.push_back( aName
);
191 void SAL_CALL
DocTemplLocaleHelper::endElement( const OUString
& aName
)
192 throw( xml::sax::SAXException
, uno::RuntimeException
, std::exception
)
194 if ( m_aElementsSeq
.empty() )
195 throw xml::sax::SAXException(); // TODO: no other end elements expected!
197 if ( m_aElementsSeq
.back() != aName
)
198 throw xml::sax::SAXException(); // TODO: unexpected element ended
200 m_aElementsSeq
.pop_back();
204 void SAL_CALL
DocTemplLocaleHelper::characters( const OUString
& /*aChars*/ )
205 throw(xml::sax::SAXException
, uno::RuntimeException
, std::exception
)
210 void SAL_CALL
DocTemplLocaleHelper::ignorableWhitespace( const OUString
& /*aWhitespaces*/ )
211 throw(xml::sax::SAXException
, uno::RuntimeException
, std::exception
)
216 void SAL_CALL
DocTemplLocaleHelper::processingInstruction( const OUString
& /*aTarget*/, const OUString
& /*aData*/ )
217 throw(xml::sax::SAXException
, uno::RuntimeException
, std::exception
)
222 void SAL_CALL
DocTemplLocaleHelper::setDocumentLocator( const uno::Reference
< xml::sax::XLocator
>& /*xLocator*/ )
223 throw(xml::sax::SAXException
, uno::RuntimeException
, std::exception
)
227 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */