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
;
35 // -----------------------------------
36 uno::Sequence
< beans::StringPair
> DocTemplLocaleHelper::ReadGroupLocalizationSequence( const uno::Reference
< io::XInputStream
>& xInStream
, const uno::Reference
< uno::XComponentContext
> xContext
)
37 throw( uno::Exception
)
39 OUString aStringID
= OUString( "groupuinames.xml" );
40 return ReadLocalizationSequence_Impl( xInStream
, aStringID
, xContext
);
43 // -----------------------------------
44 void SAL_CALL
DocTemplLocaleHelper::WriteGroupLocalizationSequence( const uno::Reference
< io::XOutputStream
>& xOutStream
, const uno::Sequence
< 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 OUString( "http://openoffice.org/2006/groupuinames" ) );
70 xWriterHandler
->startDocument();
71 xWriterHandler
->startElement( aGroupListElement
, xRootAttrList
);
73 for ( sal_Int32 nInd
= 0; nInd
< aSequence
.getLength(); nInd
++ )
75 ::comphelper::AttributeList
*pAttrList
= new ::comphelper::AttributeList
;
76 uno::Reference
< xml::sax::XAttributeList
> xAttrList( pAttrList
);
77 pAttrList
->AddAttribute( aNameAttr
, aCDATAString
, aSequence
[nInd
].First
);
78 pAttrList
->AddAttribute( aUINameAttr
, aCDATAString
, aSequence
[nInd
].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();
90 // ==================================================================================
92 // -----------------------------------
93 uno::Sequence
< beans::StringPair
> SAL_CALL
DocTemplLocaleHelper::ReadLocalizationSequence_Impl( const uno::Reference
< io::XInputStream
>& xInStream
, const OUString
& aStringID
, const uno::Reference
< uno::XComponentContext
> xContext
)
94 throw( uno::Exception
)
96 if ( !xContext
.is() || !xInStream
.is() )
97 throw uno::RuntimeException();
99 uno::Sequence
< beans::StringPair
> aResult
;
101 uno::Reference
< xml::sax::XParser
> xParser
= xml::sax::Parser::create( xContext
);
103 DocTemplLocaleHelper
* pHelper
= new DocTemplLocaleHelper();
104 uno::Reference
< xml::sax::XDocumentHandler
> xHelper( static_cast< xml::sax::XDocumentHandler
* >( pHelper
) );
105 xml::sax::InputSource aParserInput
;
106 aParserInput
.aInputStream
= xInStream
;
107 aParserInput
.sSystemId
= aStringID
;
108 xParser
->setDocumentHandler( xHelper
);
109 xParser
->parseStream( aParserInput
);
110 xParser
->setDocumentHandler( uno::Reference
< xml::sax::XDocumentHandler
> () );
112 return pHelper
->GetParsingResult();
115 // -----------------------------------
116 DocTemplLocaleHelper::DocTemplLocaleHelper()
117 : m_aGroupListElement( "groupuinames:template-group-list" )
118 , m_aGroupElement( "groupuinames:template-group" )
119 , m_aNameAttr( "groupuinames:name" )
120 , m_aUINameAttr( "groupuinames:default-ui-name" )
124 // -----------------------------------
125 DocTemplLocaleHelper::~DocTemplLocaleHelper()
129 // -----------------------------------
130 uno::Sequence
< beans::StringPair
> DocTemplLocaleHelper::GetParsingResult()
132 if ( m_aElementsSeq
.getLength() )
133 throw uno::RuntimeException(); // the parsing has still not finished!
138 // -----------------------------------
139 void SAL_CALL
DocTemplLocaleHelper::startDocument()
140 throw(xml::sax::SAXException
, uno::RuntimeException
)
144 // -----------------------------------
145 void SAL_CALL
DocTemplLocaleHelper::endDocument()
146 throw(xml::sax::SAXException
, uno::RuntimeException
)
150 // -----------------------------------
151 void SAL_CALL
DocTemplLocaleHelper::startElement( const OUString
& aName
, const uno::Reference
< xml::sax::XAttributeList
>& xAttribs
)
152 throw( xml::sax::SAXException
, uno::RuntimeException
)
154 if ( aName
== m_aGroupListElement
)
156 sal_Int32 nNewLength
= m_aElementsSeq
.getLength() + 1;
158 if ( nNewLength
!= 1 )
159 throw xml::sax::SAXException(); // TODO: this element must be the first level element
161 m_aElementsSeq
.realloc( nNewLength
);
162 m_aElementsSeq
[nNewLength
-1] = aName
;
164 return; // nothing to do
166 else if ( aName
== m_aGroupElement
)
168 sal_Int32 nNewLength
= m_aElementsSeq
.getLength() + 1;
169 if ( nNewLength
!= 2 )
170 throw xml::sax::SAXException(); // TODO: this element must be the second level element
172 m_aElementsSeq
.realloc( nNewLength
);
173 m_aElementsSeq
[nNewLength
-1] = aName
;
175 sal_Int32 nNewEntryNum
= m_aResultSeq
.getLength() + 1;
176 m_aResultSeq
.realloc( nNewEntryNum
);
178 OUString aNameValue
= xAttribs
->getValueByName( m_aNameAttr
);
179 if ( aNameValue
.isEmpty() )
180 throw xml::sax::SAXException(); // TODO: the ID value must present
182 OUString aUINameValue
= xAttribs
->getValueByName( m_aUINameAttr
);
183 if ( aUINameValue
.isEmpty() )
184 throw xml::sax::SAXException(); // TODO: the ID value must present
186 m_aResultSeq
[nNewEntryNum
-1].First
= aNameValue
;
187 m_aResultSeq
[nNewEntryNum
-1].Second
= aUINameValue
;
191 // accept future extensions
192 sal_Int32 nNewLength
= m_aElementsSeq
.getLength() + 1;
195 throw xml::sax::SAXException(); // TODO: the extension element must not be the first level element
197 m_aElementsSeq
.realloc( nNewLength
);
198 m_aElementsSeq
[nNewLength
-1] = aName
;
202 // -----------------------------------
203 void SAL_CALL
DocTemplLocaleHelper::endElement( const OUString
& aName
)
204 throw( xml::sax::SAXException
, uno::RuntimeException
)
206 sal_Int32 nLength
= m_aElementsSeq
.getLength();
208 throw xml::sax::SAXException(); // TODO: no other end elements expected!
210 if ( !m_aElementsSeq
[nLength
-1].equals( aName
) )
211 throw xml::sax::SAXException(); // TODO: unexpected element ended
213 m_aElementsSeq
.realloc( nLength
- 1 );
216 // -----------------------------------
217 void SAL_CALL
DocTemplLocaleHelper::characters( const OUString
& /*aChars*/ )
218 throw(xml::sax::SAXException
, uno::RuntimeException
)
222 // -----------------------------------
223 void SAL_CALL
DocTemplLocaleHelper::ignorableWhitespace( const OUString
& /*aWhitespaces*/ )
224 throw(xml::sax::SAXException
, uno::RuntimeException
)
228 // -----------------------------------
229 void SAL_CALL
DocTemplLocaleHelper::processingInstruction( const OUString
& /*aTarget*/, const OUString
& /*aData*/ )
230 throw(xml::sax::SAXException
, uno::RuntimeException
)
234 // -----------------------------------
235 void SAL_CALL
DocTemplLocaleHelper::setDocumentLocator( const uno::Reference
< xml::sax::XLocator
>& /*xLocator*/ )
236 throw(xml::sax::SAXException
, uno::RuntimeException
)
240 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */