1*d119d52dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*d119d52dSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*d119d52dSAndrew Rist * or more contributor license agreements. See the NOTICE file
5*d119d52dSAndrew Rist * distributed with this work for additional information
6*d119d52dSAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*d119d52dSAndrew Rist * to you under the Apache License, Version 2.0 (the
8*d119d52dSAndrew Rist * "License"); you may not use this file except in compliance
9*d119d52dSAndrew Rist * with the License. You may obtain a copy of the License at
10*d119d52dSAndrew Rist *
11*d119d52dSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*d119d52dSAndrew Rist *
13*d119d52dSAndrew Rist * Unless required by applicable law or agreed to in writing,
14*d119d52dSAndrew Rist * software distributed under the License is distributed on an
15*d119d52dSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*d119d52dSAndrew Rist * KIND, either express or implied. See the License for the
17*d119d52dSAndrew Rist * specific language governing permissions and limitations
18*d119d52dSAndrew Rist * under the License.
19*d119d52dSAndrew Rist *
20*d119d52dSAndrew Rist *************************************************************/
21*d119d52dSAndrew Rist
22*d119d52dSAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sfx2.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir #ifndef _COM_SUN_STAR_BEANS_PROPERTYVALUE_HPP_
28cdf0e10cSrcweir #include <com/sun/star/beans/StringPair.hpp>
29cdf0e10cSrcweir #endif
30cdf0e10cSrcweir #ifndef _COM_SUN_STAR_LANG_XMULTISERVICEFACTORY_HPP
31cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
32cdf0e10cSrcweir #endif
33cdf0e10cSrcweir #ifndef _COM_SUN_STAR_IO_XACTIVEDATASOURCE_HPP
34cdf0e10cSrcweir #include <com/sun/star/io/XActiveDataSource.hpp>
35cdf0e10cSrcweir #endif
36cdf0e10cSrcweir #ifndef _COM_SUN_STAR_XML_SAX_XPARSER_HPP
37cdf0e10cSrcweir #include <com/sun/star/xml/sax/XParser.hpp>
38cdf0e10cSrcweir #endif
39cdf0e10cSrcweir #ifndef _COM_SUN_STAR_XML_SAX_XDOCUMENTHANDLER_HPP
40cdf0e10cSrcweir #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
41cdf0e10cSrcweir #endif
42cdf0e10cSrcweir #ifndef _COM_SUN_STAR_LANG_ILLEGALARGUMENTEXCEPTION_HPP
43cdf0e10cSrcweir #include <com/sun/star/lang/IllegalArgumentException.hpp>
44cdf0e10cSrcweir #endif
45cdf0e10cSrcweir
46cdf0e10cSrcweir #include <comphelper/attributelist.hxx>
47cdf0e10cSrcweir
48cdf0e10cSrcweir #include "doctemplateslocal.hxx"
49cdf0e10cSrcweir
50cdf0e10cSrcweir using namespace ::com::sun::star;
51cdf0e10cSrcweir
52cdf0e10cSrcweir // -----------------------------------
ReadGroupLocalizationSequence(const uno::Reference<io::XInputStream> & xInStream,const uno::Reference<lang::XMultiServiceFactory> xFactory)53cdf0e10cSrcweir uno::Sequence< beans::StringPair > DocTemplLocaleHelper::ReadGroupLocalizationSequence( const uno::Reference< io::XInputStream >& xInStream, const uno::Reference< lang::XMultiServiceFactory > xFactory )
54cdf0e10cSrcweir throw( uno::Exception )
55cdf0e10cSrcweir {
56cdf0e10cSrcweir ::rtl::OUString aStringID = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "groupuinames.xml" ) );
57cdf0e10cSrcweir return ReadLocalizationSequence_Impl( xInStream, aStringID, xFactory );
58cdf0e10cSrcweir }
59cdf0e10cSrcweir
60cdf0e10cSrcweir // -----------------------------------
WriteGroupLocalizationSequence(const uno::Reference<io::XOutputStream> & xOutStream,const uno::Sequence<beans::StringPair> & aSequence,const uno::Reference<lang::XMultiServiceFactory> xFactory)61cdf0e10cSrcweir void SAL_CALL DocTemplLocaleHelper::WriteGroupLocalizationSequence( const uno::Reference< io::XOutputStream >& xOutStream, const uno::Sequence< beans::StringPair >& aSequence, const uno::Reference< lang::XMultiServiceFactory > xFactory )
62cdf0e10cSrcweir throw( uno::Exception )
63cdf0e10cSrcweir {
64cdf0e10cSrcweir if ( !xOutStream.is() )
65cdf0e10cSrcweir throw uno::RuntimeException();
66cdf0e10cSrcweir
67cdf0e10cSrcweir uno::Reference< io::XActiveDataSource > xWriterSource(
68cdf0e10cSrcweir xFactory->createInstance(
69cdf0e10cSrcweir ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.sax.Writer" ) ) ),
70cdf0e10cSrcweir uno::UNO_QUERY_THROW );
71cdf0e10cSrcweir uno::Reference< xml::sax::XDocumentHandler > xWriterHandler( xWriterSource, uno::UNO_QUERY_THROW );
72cdf0e10cSrcweir
73cdf0e10cSrcweir xWriterSource->setOutputStream( xOutStream );
74cdf0e10cSrcweir
75cdf0e10cSrcweir ::rtl::OUString aGroupListElement( RTL_CONSTASCII_USTRINGPARAM( "groupuinames:template-group-list" ) );
76cdf0e10cSrcweir ::rtl::OUString aGroupElement( RTL_CONSTASCII_USTRINGPARAM( "groupuinames:template-group" ) );
77cdf0e10cSrcweir ::rtl::OUString aNameAttr( RTL_CONSTASCII_USTRINGPARAM( "groupuinames:name" ) );
78cdf0e10cSrcweir ::rtl::OUString aUINameAttr( RTL_CONSTASCII_USTRINGPARAM( "groupuinames:default-ui-name" ) );
79cdf0e10cSrcweir ::rtl::OUString aCDATAString( RTL_CONSTASCII_USTRINGPARAM ( "CDATA" ) );
80cdf0e10cSrcweir ::rtl::OUString aWhiteSpace( RTL_CONSTASCII_USTRINGPARAM ( " " ) );
81cdf0e10cSrcweir
82cdf0e10cSrcweir // write the namespace
83cdf0e10cSrcweir ::comphelper::AttributeList* pRootAttrList = new ::comphelper::AttributeList;
84cdf0e10cSrcweir uno::Reference< xml::sax::XAttributeList > xRootAttrList( pRootAttrList );
85cdf0e10cSrcweir pRootAttrList->AddAttribute(
86cdf0e10cSrcweir ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "xmlns" ) ),
87cdf0e10cSrcweir aCDATAString,
88cdf0e10cSrcweir ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "http://openoffice.org/2006/groupuinames" ) ) );
89cdf0e10cSrcweir
90cdf0e10cSrcweir xWriterHandler->startDocument();
91cdf0e10cSrcweir xWriterHandler->startElement( aGroupListElement, xRootAttrList );
92cdf0e10cSrcweir
93cdf0e10cSrcweir for ( sal_Int32 nInd = 0; nInd < aSequence.getLength(); nInd++ )
94cdf0e10cSrcweir {
95cdf0e10cSrcweir ::comphelper::AttributeList *pAttrList = new ::comphelper::AttributeList;
96cdf0e10cSrcweir uno::Reference< xml::sax::XAttributeList > xAttrList( pAttrList );
97cdf0e10cSrcweir pAttrList->AddAttribute( aNameAttr, aCDATAString, aSequence[nInd].First );
98cdf0e10cSrcweir pAttrList->AddAttribute( aUINameAttr, aCDATAString, aSequence[nInd].Second );
99cdf0e10cSrcweir
100cdf0e10cSrcweir xWriterHandler->startElement( aGroupElement, xAttrList );
101cdf0e10cSrcweir xWriterHandler->ignorableWhitespace( aWhiteSpace );
102cdf0e10cSrcweir xWriterHandler->endElement( aGroupElement );
103cdf0e10cSrcweir }
104cdf0e10cSrcweir
105cdf0e10cSrcweir xWriterHandler->ignorableWhitespace( aWhiteSpace );
106cdf0e10cSrcweir xWriterHandler->endElement( aGroupListElement );
107cdf0e10cSrcweir xWriterHandler->endDocument();
108cdf0e10cSrcweir }
109cdf0e10cSrcweir
110cdf0e10cSrcweir // ==================================================================================
111cdf0e10cSrcweir
112cdf0e10cSrcweir // -----------------------------------
ReadLocalizationSequence_Impl(const uno::Reference<io::XInputStream> & xInStream,const::rtl::OUString & aStringID,const uno::Reference<lang::XMultiServiceFactory> xFactory)113cdf0e10cSrcweir uno::Sequence< beans::StringPair > SAL_CALL DocTemplLocaleHelper::ReadLocalizationSequence_Impl( const uno::Reference< io::XInputStream >& xInStream, const ::rtl::OUString& aStringID, const uno::Reference< lang::XMultiServiceFactory > xFactory )
114cdf0e10cSrcweir throw( uno::Exception )
115cdf0e10cSrcweir {
116cdf0e10cSrcweir if ( !xFactory.is() || !xInStream.is() )
117cdf0e10cSrcweir throw uno::RuntimeException();
118cdf0e10cSrcweir
119cdf0e10cSrcweir uno::Sequence< beans::StringPair > aResult;
120cdf0e10cSrcweir
121cdf0e10cSrcweir uno::Reference< xml::sax::XParser > xParser( xFactory->createInstance( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.sax.Parser" ) ) ), uno::UNO_QUERY_THROW );
122cdf0e10cSrcweir
123cdf0e10cSrcweir DocTemplLocaleHelper* pHelper = new DocTemplLocaleHelper();
124cdf0e10cSrcweir uno::Reference< xml::sax::XDocumentHandler > xHelper( static_cast< xml::sax::XDocumentHandler* >( pHelper ) );
125cdf0e10cSrcweir xml::sax::InputSource aParserInput;
126cdf0e10cSrcweir aParserInput.aInputStream = xInStream;
127cdf0e10cSrcweir aParserInput.sSystemId = aStringID;
128cdf0e10cSrcweir xParser->setDocumentHandler( xHelper );
129cdf0e10cSrcweir xParser->parseStream( aParserInput );
130cdf0e10cSrcweir xParser->setDocumentHandler( uno::Reference < xml::sax::XDocumentHandler > () );
131cdf0e10cSrcweir
132cdf0e10cSrcweir return pHelper->GetParsingResult();
133cdf0e10cSrcweir }
134cdf0e10cSrcweir
135cdf0e10cSrcweir // -----------------------------------
DocTemplLocaleHelper()136cdf0e10cSrcweir DocTemplLocaleHelper::DocTemplLocaleHelper()
137cdf0e10cSrcweir : m_aGroupListElement( RTL_CONSTASCII_USTRINGPARAM( "groupuinames:template-group-list" ) )
138cdf0e10cSrcweir , m_aGroupElement( RTL_CONSTASCII_USTRINGPARAM( "groupuinames:template-group" ) )
139cdf0e10cSrcweir , m_aNameAttr( RTL_CONSTASCII_USTRINGPARAM( "groupuinames:name" ) )
140cdf0e10cSrcweir , m_aUINameAttr( RTL_CONSTASCII_USTRINGPARAM( "groupuinames:default-ui-name" ) )
141cdf0e10cSrcweir {
142cdf0e10cSrcweir }
143cdf0e10cSrcweir
144cdf0e10cSrcweir // -----------------------------------
~DocTemplLocaleHelper()145cdf0e10cSrcweir DocTemplLocaleHelper::~DocTemplLocaleHelper()
146cdf0e10cSrcweir {
147cdf0e10cSrcweir }
148cdf0e10cSrcweir
149cdf0e10cSrcweir // -----------------------------------
GetParsingResult()150cdf0e10cSrcweir uno::Sequence< beans::StringPair > DocTemplLocaleHelper::GetParsingResult()
151cdf0e10cSrcweir {
152cdf0e10cSrcweir if ( m_aElementsSeq.getLength() )
153cdf0e10cSrcweir throw uno::RuntimeException(); // the parsing has still not finished!
154cdf0e10cSrcweir
155cdf0e10cSrcweir return m_aResultSeq;
156cdf0e10cSrcweir }
157cdf0e10cSrcweir
158cdf0e10cSrcweir // -----------------------------------
startDocument()159cdf0e10cSrcweir void SAL_CALL DocTemplLocaleHelper::startDocument()
160cdf0e10cSrcweir throw(xml::sax::SAXException, uno::RuntimeException)
161cdf0e10cSrcweir {
162cdf0e10cSrcweir }
163cdf0e10cSrcweir
164cdf0e10cSrcweir // -----------------------------------
endDocument()165cdf0e10cSrcweir void SAL_CALL DocTemplLocaleHelper::endDocument()
166cdf0e10cSrcweir throw(xml::sax::SAXException, uno::RuntimeException)
167cdf0e10cSrcweir {
168cdf0e10cSrcweir }
169cdf0e10cSrcweir
170cdf0e10cSrcweir // -----------------------------------
startElement(const::rtl::OUString & aName,const uno::Reference<xml::sax::XAttributeList> & xAttribs)171cdf0e10cSrcweir void SAL_CALL DocTemplLocaleHelper::startElement( const ::rtl::OUString& aName, const uno::Reference< xml::sax::XAttributeList >& xAttribs )
172cdf0e10cSrcweir throw( xml::sax::SAXException, uno::RuntimeException )
173cdf0e10cSrcweir {
174cdf0e10cSrcweir if ( aName == m_aGroupListElement )
175cdf0e10cSrcweir {
176cdf0e10cSrcweir sal_Int32 nNewLength = m_aElementsSeq.getLength() + 1;
177cdf0e10cSrcweir
178cdf0e10cSrcweir if ( nNewLength != 1 )
179cdf0e10cSrcweir throw xml::sax::SAXException(); // TODO: this element must be the first level element
180cdf0e10cSrcweir
181cdf0e10cSrcweir m_aElementsSeq.realloc( nNewLength );
182cdf0e10cSrcweir m_aElementsSeq[nNewLength-1] = aName;
183cdf0e10cSrcweir
184cdf0e10cSrcweir return; // nothing to do
185cdf0e10cSrcweir }
186cdf0e10cSrcweir else if ( aName == m_aGroupElement )
187cdf0e10cSrcweir {
188cdf0e10cSrcweir sal_Int32 nNewLength = m_aElementsSeq.getLength() + 1;
189cdf0e10cSrcweir if ( nNewLength != 2 )
190cdf0e10cSrcweir throw xml::sax::SAXException(); // TODO: this element must be the second level element
191cdf0e10cSrcweir
192cdf0e10cSrcweir m_aElementsSeq.realloc( nNewLength );
193cdf0e10cSrcweir m_aElementsSeq[nNewLength-1] = aName;
194cdf0e10cSrcweir
195cdf0e10cSrcweir sal_Int32 nNewEntryNum = m_aResultSeq.getLength() + 1;
196cdf0e10cSrcweir m_aResultSeq.realloc( nNewEntryNum );
197cdf0e10cSrcweir
198cdf0e10cSrcweir ::rtl::OUString aNameValue = xAttribs->getValueByName( m_aNameAttr );
199cdf0e10cSrcweir if ( !aNameValue.getLength() )
200cdf0e10cSrcweir throw xml::sax::SAXException(); // TODO: the ID value must present
201cdf0e10cSrcweir
202cdf0e10cSrcweir ::rtl::OUString aUINameValue = xAttribs->getValueByName( m_aUINameAttr );
203cdf0e10cSrcweir if ( !aUINameValue.getLength() )
204cdf0e10cSrcweir throw xml::sax::SAXException(); // TODO: the ID value must present
205cdf0e10cSrcweir
206cdf0e10cSrcweir m_aResultSeq[nNewEntryNum-1].First = aNameValue;
207cdf0e10cSrcweir m_aResultSeq[nNewEntryNum-1].Second = aUINameValue;
208cdf0e10cSrcweir }
209cdf0e10cSrcweir else
210cdf0e10cSrcweir {
211cdf0e10cSrcweir // accept future extensions
212cdf0e10cSrcweir sal_Int32 nNewLength = m_aElementsSeq.getLength() + 1;
213cdf0e10cSrcweir
214cdf0e10cSrcweir if ( !nNewLength )
215cdf0e10cSrcweir throw xml::sax::SAXException(); // TODO: the extension element must not be the first level element
216cdf0e10cSrcweir
217cdf0e10cSrcweir m_aElementsSeq.realloc( nNewLength );
218cdf0e10cSrcweir m_aElementsSeq[nNewLength-1] = aName;
219cdf0e10cSrcweir }
220cdf0e10cSrcweir }
221cdf0e10cSrcweir
222cdf0e10cSrcweir // -----------------------------------
endElement(const::rtl::OUString & aName)223cdf0e10cSrcweir void SAL_CALL DocTemplLocaleHelper::endElement( const ::rtl::OUString& aName )
224cdf0e10cSrcweir throw( xml::sax::SAXException, uno::RuntimeException )
225cdf0e10cSrcweir {
226cdf0e10cSrcweir sal_Int32 nLength = m_aElementsSeq.getLength();
227cdf0e10cSrcweir if ( nLength <= 0 )
228cdf0e10cSrcweir throw xml::sax::SAXException(); // TODO: no other end elements expected!
229cdf0e10cSrcweir
230cdf0e10cSrcweir if ( !m_aElementsSeq[nLength-1].equals( aName ) )
231cdf0e10cSrcweir throw xml::sax::SAXException(); // TODO: unexpected element ended
232cdf0e10cSrcweir
233cdf0e10cSrcweir m_aElementsSeq.realloc( nLength - 1 );
234cdf0e10cSrcweir }
235cdf0e10cSrcweir
236cdf0e10cSrcweir // -----------------------------------
characters(const::rtl::OUString &)237cdf0e10cSrcweir void SAL_CALL DocTemplLocaleHelper::characters( const ::rtl::OUString& /*aChars*/ )
238cdf0e10cSrcweir throw(xml::sax::SAXException, uno::RuntimeException)
239cdf0e10cSrcweir {
240cdf0e10cSrcweir }
241cdf0e10cSrcweir
242cdf0e10cSrcweir // -----------------------------------
ignorableWhitespace(const::rtl::OUString &)243cdf0e10cSrcweir void SAL_CALL DocTemplLocaleHelper::ignorableWhitespace( const ::rtl::OUString& /*aWhitespaces*/ )
244cdf0e10cSrcweir throw(xml::sax::SAXException, uno::RuntimeException)
245cdf0e10cSrcweir {
246cdf0e10cSrcweir }
247cdf0e10cSrcweir
248cdf0e10cSrcweir // -----------------------------------
processingInstruction(const::rtl::OUString &,const::rtl::OUString &)249cdf0e10cSrcweir void SAL_CALL DocTemplLocaleHelper::processingInstruction( const ::rtl::OUString& /*aTarget*/, const ::rtl::OUString& /*aData*/ )
250cdf0e10cSrcweir throw(xml::sax::SAXException, uno::RuntimeException)
251cdf0e10cSrcweir {
252cdf0e10cSrcweir }
253cdf0e10cSrcweir
254cdf0e10cSrcweir // -----------------------------------
setDocumentLocator(const uno::Reference<xml::sax::XLocator> &)255cdf0e10cSrcweir void SAL_CALL DocTemplLocaleHelper::setDocumentLocator( const uno::Reference< xml::sax::XLocator >& /*xLocator*/ )
256cdf0e10cSrcweir throw(xml::sax::SAXException, uno::RuntimeException)
257cdf0e10cSrcweir {
258cdf0e10cSrcweir }
259cdf0e10cSrcweir
260