1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include <com/sun/star/beans/StringPair.hpp>
31 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
32 #include <com/sun/star/io/XActiveDataSource.hpp>
33 #include <com/sun/star/xml/sax/XParser.hpp>
34 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
35 #include <com/sun/star/lang/IllegalArgumentException.hpp>
37 #include <comphelper/attributelist.hxx>
39 #include "doctemplateslocal.hxx"
41 using namespace ::com::sun::star
;
43 // -----------------------------------
44 uno::Sequence
< beans::StringPair
> DocTemplLocaleHelper::ReadGroupLocalizationSequence( const uno::Reference
< io::XInputStream
>& xInStream
, const uno::Reference
< lang::XMultiServiceFactory
> xFactory
)
45 throw( uno::Exception
)
47 ::rtl::OUString aStringID
= ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "groupuinames.xml" ) );
48 return ReadLocalizationSequence_Impl( xInStream
, aStringID
, xFactory
);
51 // -----------------------------------
52 void SAL_CALL
DocTemplLocaleHelper::WriteGroupLocalizationSequence( const uno::Reference
< io::XOutputStream
>& xOutStream
, const uno::Sequence
< beans::StringPair
>& aSequence
, const uno::Reference
< lang::XMultiServiceFactory
> xFactory
)
53 throw( uno::Exception
)
55 if ( !xOutStream
.is() )
56 throw uno::RuntimeException();
58 uno::Reference
< io::XActiveDataSource
> xWriterSource(
59 xFactory
->createInstance(
60 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.sax.Writer" ) ) ),
61 uno::UNO_QUERY_THROW
);
62 uno::Reference
< xml::sax::XDocumentHandler
> xWriterHandler( xWriterSource
, uno::UNO_QUERY_THROW
);
64 xWriterSource
->setOutputStream( xOutStream
);
66 ::rtl::OUString
aGroupListElement( RTL_CONSTASCII_USTRINGPARAM( "groupuinames:template-group-list" ) );
67 ::rtl::OUString
aGroupElement( RTL_CONSTASCII_USTRINGPARAM( "groupuinames:template-group" ) );
68 ::rtl::OUString
aNameAttr( RTL_CONSTASCII_USTRINGPARAM( "groupuinames:name" ) );
69 ::rtl::OUString
aUINameAttr( RTL_CONSTASCII_USTRINGPARAM( "groupuinames:default-ui-name" ) );
70 ::rtl::OUString
aCDATAString( RTL_CONSTASCII_USTRINGPARAM ( "CDATA" ) );
71 ::rtl::OUString
aWhiteSpace( RTL_CONSTASCII_USTRINGPARAM ( " " ) );
73 // write the namespace
74 ::comphelper::AttributeList
* pRootAttrList
= new ::comphelper::AttributeList
;
75 uno::Reference
< xml::sax::XAttributeList
> xRootAttrList( pRootAttrList
);
76 pRootAttrList
->AddAttribute(
77 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "xmlns" ) ),
79 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "http://openoffice.org/2006/groupuinames" ) ) );
81 xWriterHandler
->startDocument();
82 xWriterHandler
->startElement( aGroupListElement
, xRootAttrList
);
84 for ( sal_Int32 nInd
= 0; nInd
< aSequence
.getLength(); nInd
++ )
86 ::comphelper::AttributeList
*pAttrList
= new ::comphelper::AttributeList
;
87 uno::Reference
< xml::sax::XAttributeList
> xAttrList( pAttrList
);
88 pAttrList
->AddAttribute( aNameAttr
, aCDATAString
, aSequence
[nInd
].First
);
89 pAttrList
->AddAttribute( aUINameAttr
, aCDATAString
, aSequence
[nInd
].Second
);
91 xWriterHandler
->startElement( aGroupElement
, xAttrList
);
92 xWriterHandler
->ignorableWhitespace( aWhiteSpace
);
93 xWriterHandler
->endElement( aGroupElement
);
96 xWriterHandler
->ignorableWhitespace( aWhiteSpace
);
97 xWriterHandler
->endElement( aGroupListElement
);
98 xWriterHandler
->endDocument();
101 // ==================================================================================
103 // -----------------------------------
104 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
)
105 throw( uno::Exception
)
107 if ( !xFactory
.is() || !xInStream
.is() )
108 throw uno::RuntimeException();
110 uno::Sequence
< beans::StringPair
> aResult
;
112 uno::Reference
< xml::sax::XParser
> xParser( xFactory
->createInstance( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.sax.Parser" ) ) ), uno::UNO_QUERY_THROW
);
114 DocTemplLocaleHelper
* pHelper
= new DocTemplLocaleHelper();
115 uno::Reference
< xml::sax::XDocumentHandler
> xHelper( static_cast< xml::sax::XDocumentHandler
* >( pHelper
) );
116 xml::sax::InputSource aParserInput
;
117 aParserInput
.aInputStream
= xInStream
;
118 aParserInput
.sSystemId
= aStringID
;
119 xParser
->setDocumentHandler( xHelper
);
120 xParser
->parseStream( aParserInput
);
121 xParser
->setDocumentHandler( uno::Reference
< xml::sax::XDocumentHandler
> () );
123 return pHelper
->GetParsingResult();
126 // -----------------------------------
127 DocTemplLocaleHelper::DocTemplLocaleHelper()
128 : m_aGroupListElement( RTL_CONSTASCII_USTRINGPARAM( "groupuinames:template-group-list" ) )
129 , m_aGroupElement( RTL_CONSTASCII_USTRINGPARAM( "groupuinames:template-group" ) )
130 , m_aNameAttr( RTL_CONSTASCII_USTRINGPARAM( "groupuinames:name" ) )
131 , m_aUINameAttr( RTL_CONSTASCII_USTRINGPARAM( "groupuinames:default-ui-name" ) )
135 // -----------------------------------
136 DocTemplLocaleHelper::~DocTemplLocaleHelper()
140 // -----------------------------------
141 uno::Sequence
< beans::StringPair
> DocTemplLocaleHelper::GetParsingResult()
143 if ( m_aElementsSeq
.getLength() )
144 throw uno::RuntimeException(); // the parsing has still not finished!
149 // -----------------------------------
150 void SAL_CALL
DocTemplLocaleHelper::startDocument()
151 throw(xml::sax::SAXException
, uno::RuntimeException
)
155 // -----------------------------------
156 void SAL_CALL
DocTemplLocaleHelper::endDocument()
157 throw(xml::sax::SAXException
, uno::RuntimeException
)
161 // -----------------------------------
162 void SAL_CALL
DocTemplLocaleHelper::startElement( const ::rtl::OUString
& aName
, const uno::Reference
< xml::sax::XAttributeList
>& xAttribs
)
163 throw( xml::sax::SAXException
, uno::RuntimeException
)
165 if ( aName
== m_aGroupListElement
)
167 sal_Int32 nNewLength
= m_aElementsSeq
.getLength() + 1;
169 if ( nNewLength
!= 1 )
170 throw xml::sax::SAXException(); // TODO: this element must be the first level element
172 m_aElementsSeq
.realloc( nNewLength
);
173 m_aElementsSeq
[nNewLength
-1] = aName
;
175 return; // nothing to do
177 else if ( aName
== m_aGroupElement
)
179 sal_Int32 nNewLength
= m_aElementsSeq
.getLength() + 1;
180 if ( nNewLength
!= 2 )
181 throw xml::sax::SAXException(); // TODO: this element must be the second level element
183 m_aElementsSeq
.realloc( nNewLength
);
184 m_aElementsSeq
[nNewLength
-1] = aName
;
186 sal_Int32 nNewEntryNum
= m_aResultSeq
.getLength() + 1;
187 m_aResultSeq
.realloc( nNewEntryNum
);
189 ::rtl::OUString aNameValue
= xAttribs
->getValueByName( m_aNameAttr
);
190 if ( aNameValue
.isEmpty() )
191 throw xml::sax::SAXException(); // TODO: the ID value must present
193 ::rtl::OUString aUINameValue
= xAttribs
->getValueByName( m_aUINameAttr
);
194 if ( aUINameValue
.isEmpty() )
195 throw xml::sax::SAXException(); // TODO: the ID value must present
197 m_aResultSeq
[nNewEntryNum
-1].First
= aNameValue
;
198 m_aResultSeq
[nNewEntryNum
-1].Second
= aUINameValue
;
202 // accept future extensions
203 sal_Int32 nNewLength
= m_aElementsSeq
.getLength() + 1;
206 throw xml::sax::SAXException(); // TODO: the extension element must not be the first level element
208 m_aElementsSeq
.realloc( nNewLength
);
209 m_aElementsSeq
[nNewLength
-1] = aName
;
213 // -----------------------------------
214 void SAL_CALL
DocTemplLocaleHelper::endElement( const ::rtl::OUString
& aName
)
215 throw( xml::sax::SAXException
, uno::RuntimeException
)
217 sal_Int32 nLength
= m_aElementsSeq
.getLength();
219 throw xml::sax::SAXException(); // TODO: no other end elements expected!
221 if ( !m_aElementsSeq
[nLength
-1].equals( aName
) )
222 throw xml::sax::SAXException(); // TODO: unexpected element ended
224 m_aElementsSeq
.realloc( nLength
- 1 );
227 // -----------------------------------
228 void SAL_CALL
DocTemplLocaleHelper::characters( const ::rtl::OUString
& /*aChars*/ )
229 throw(xml::sax::SAXException
, uno::RuntimeException
)
233 // -----------------------------------
234 void SAL_CALL
DocTemplLocaleHelper::ignorableWhitespace( const ::rtl::OUString
& /*aWhitespaces*/ )
235 throw(xml::sax::SAXException
, uno::RuntimeException
)
239 // -----------------------------------
240 void SAL_CALL
DocTemplLocaleHelper::processingInstruction( const ::rtl::OUString
& /*aTarget*/, const ::rtl::OUString
& /*aData*/ )
241 throw(xml::sax::SAXException
, uno::RuntimeException
)
245 // -----------------------------------
246 void SAL_CALL
DocTemplLocaleHelper::setDocumentLocator( const uno::Reference
< xml::sax::XLocator
>& /*xLocator*/ )
247 throw(xml::sax::SAXException
, uno::RuntimeException
)
251 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */