1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: doctemplateslocal.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sfx2.hxx"
34 #ifndef _COM_SUN_STAR_BEANS_PROPERTYVALUE_HPP_
35 #include <com/sun/star/beans/StringPair.hpp>
37 #ifndef _COM_SUN_STAR_LANG_XMULTISERVICEFACTORY_HPP
38 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
40 #ifndef _COM_SUN_STAR_IO_XACTIVEDATASOURCE_HPP
41 #include <com/sun/star/io/XActiveDataSource.hpp>
43 #ifndef _COM_SUN_STAR_XML_SAX_XPARSER_HPP
44 #include <com/sun/star/xml/sax/XParser.hpp>
46 #ifndef _COM_SUN_STAR_XML_SAX_XDOCUMENTHANDLER_HPP
47 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
49 #ifndef _COM_SUN_STAR_LANG_ILLEGALARGUMENTEXCEPTION_HPP
50 #include <com/sun/star/lang/IllegalArgumentException.hpp>
53 #include <comphelper/attributelist.hxx>
55 #include "doctemplateslocal.hxx"
57 using namespace ::com::sun::star
;
59 // -----------------------------------
60 uno::Sequence
< beans::StringPair
> DocTemplLocaleHelper::ReadGroupLocalizationSequence( const uno::Reference
< io::XInputStream
>& xInStream
, const uno::Reference
< lang::XMultiServiceFactory
> xFactory
)
61 throw( uno::Exception
)
63 ::rtl::OUString aStringID
= ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "groupuinames.xml" ) );
64 return ReadLocalizationSequence_Impl( xInStream
, aStringID
, xFactory
);
67 // -----------------------------------
68 void SAL_CALL
DocTemplLocaleHelper::WriteGroupLocalizationSequence( const uno::Reference
< io::XOutputStream
>& xOutStream
, const uno::Sequence
< beans::StringPair
>& aSequence
, const uno::Reference
< lang::XMultiServiceFactory
> xFactory
)
69 throw( uno::Exception
)
71 if ( !xOutStream
.is() )
72 throw uno::RuntimeException();
74 uno::Reference
< io::XActiveDataSource
> xWriterSource(
75 xFactory
->createInstance(
76 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.sax.Writer" ) ) ),
77 uno::UNO_QUERY_THROW
);
78 uno::Reference
< xml::sax::XDocumentHandler
> xWriterHandler( xWriterSource
, uno::UNO_QUERY_THROW
);
80 xWriterSource
->setOutputStream( xOutStream
);
82 ::rtl::OUString
aGroupListElement( RTL_CONSTASCII_USTRINGPARAM( "groupuinames:template-group-list" ) );
83 ::rtl::OUString
aGroupElement( RTL_CONSTASCII_USTRINGPARAM( "groupuinames:template-group" ) );
84 ::rtl::OUString
aNameAttr( RTL_CONSTASCII_USTRINGPARAM( "groupuinames:name" ) );
85 ::rtl::OUString
aUINameAttr( RTL_CONSTASCII_USTRINGPARAM( "groupuinames:default-ui-name" ) );
86 ::rtl::OUString
aCDATAString( RTL_CONSTASCII_USTRINGPARAM ( "CDATA" ) );
87 ::rtl::OUString
aWhiteSpace( RTL_CONSTASCII_USTRINGPARAM ( " " ) );
89 // write the namespace
90 ::comphelper::AttributeList
* pRootAttrList
= new ::comphelper::AttributeList
;
91 uno::Reference
< xml::sax::XAttributeList
> xRootAttrList( pRootAttrList
);
92 pRootAttrList
->AddAttribute(
93 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "xmlns" ) ),
95 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "http://openoffice.org/2006/groupuinames" ) ) );
97 xWriterHandler
->startDocument();
98 xWriterHandler
->startElement( aGroupListElement
, xRootAttrList
);
100 for ( sal_Int32 nInd
= 0; nInd
< aSequence
.getLength(); nInd
++ )
102 ::comphelper::AttributeList
*pAttrList
= new ::comphelper::AttributeList
;
103 uno::Reference
< xml::sax::XAttributeList
> xAttrList( pAttrList
);
104 pAttrList
->AddAttribute( aNameAttr
, aCDATAString
, aSequence
[nInd
].First
);
105 pAttrList
->AddAttribute( aUINameAttr
, aCDATAString
, aSequence
[nInd
].Second
);
107 xWriterHandler
->startElement( aGroupElement
, xAttrList
);
108 xWriterHandler
->ignorableWhitespace( aWhiteSpace
);
109 xWriterHandler
->endElement( aGroupElement
);
112 xWriterHandler
->ignorableWhitespace( aWhiteSpace
);
113 xWriterHandler
->endElement( aGroupListElement
);
114 xWriterHandler
->endDocument();
117 // ==================================================================================
119 // -----------------------------------
120 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
)
121 throw( uno::Exception
)
123 if ( !xFactory
.is() || !xInStream
.is() )
124 throw uno::RuntimeException();
126 uno::Sequence
< beans::StringPair
> aResult
;
128 uno::Reference
< xml::sax::XParser
> xParser( xFactory
->createInstance( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.sax.Parser" ) ) ), uno::UNO_QUERY_THROW
);
130 DocTemplLocaleHelper
* pHelper
= new DocTemplLocaleHelper();
131 uno::Reference
< xml::sax::XDocumentHandler
> xHelper( static_cast< xml::sax::XDocumentHandler
* >( pHelper
) );
132 xml::sax::InputSource aParserInput
;
133 aParserInput
.aInputStream
= xInStream
;
134 aParserInput
.sSystemId
= aStringID
;
135 xParser
->setDocumentHandler( xHelper
);
136 xParser
->parseStream( aParserInput
);
137 xParser
->setDocumentHandler( uno::Reference
< xml::sax::XDocumentHandler
> () );
139 return pHelper
->GetParsingResult();
142 // -----------------------------------
143 DocTemplLocaleHelper::DocTemplLocaleHelper()
144 : m_aGroupListElement( RTL_CONSTASCII_USTRINGPARAM( "groupuinames:template-group-list" ) )
145 , m_aGroupElement( RTL_CONSTASCII_USTRINGPARAM( "groupuinames:template-group" ) )
146 , m_aNameAttr( RTL_CONSTASCII_USTRINGPARAM( "groupuinames:name" ) )
147 , m_aUINameAttr( RTL_CONSTASCII_USTRINGPARAM( "groupuinames:default-ui-name" ) )
151 // -----------------------------------
152 DocTemplLocaleHelper::~DocTemplLocaleHelper()
156 // -----------------------------------
157 uno::Sequence
< beans::StringPair
> DocTemplLocaleHelper::GetParsingResult()
159 if ( m_aElementsSeq
.getLength() )
160 throw uno::RuntimeException(); // the parsing has still not finished!
165 // -----------------------------------
166 void SAL_CALL
DocTemplLocaleHelper::startDocument()
167 throw(xml::sax::SAXException
, uno::RuntimeException
)
171 // -----------------------------------
172 void SAL_CALL
DocTemplLocaleHelper::endDocument()
173 throw(xml::sax::SAXException
, uno::RuntimeException
)
177 // -----------------------------------
178 void SAL_CALL
DocTemplLocaleHelper::startElement( const ::rtl::OUString
& aName
, const uno::Reference
< xml::sax::XAttributeList
>& xAttribs
)
179 throw( xml::sax::SAXException
, uno::RuntimeException
)
181 if ( aName
== m_aGroupListElement
)
183 sal_Int32 nNewLength
= m_aElementsSeq
.getLength() + 1;
185 if ( nNewLength
!= 1 )
186 throw xml::sax::SAXException(); // TODO: this element must be the first level element
188 m_aElementsSeq
.realloc( nNewLength
);
189 m_aElementsSeq
[nNewLength
-1] = aName
;
191 return; // nothing to do
193 else if ( aName
== m_aGroupElement
)
195 sal_Int32 nNewLength
= m_aElementsSeq
.getLength() + 1;
196 if ( nNewLength
!= 2 )
197 throw xml::sax::SAXException(); // TODO: this element must be the second level element
199 m_aElementsSeq
.realloc( nNewLength
);
200 m_aElementsSeq
[nNewLength
-1] = aName
;
202 sal_Int32 nNewEntryNum
= m_aResultSeq
.getLength() + 1;
203 m_aResultSeq
.realloc( nNewEntryNum
);
205 ::rtl::OUString aNameValue
= xAttribs
->getValueByName( m_aNameAttr
);
206 if ( !aNameValue
.getLength() )
207 throw xml::sax::SAXException(); // TODO: the ID value must present
209 ::rtl::OUString aUINameValue
= xAttribs
->getValueByName( m_aUINameAttr
);
210 if ( !aUINameValue
.getLength() )
211 throw xml::sax::SAXException(); // TODO: the ID value must present
213 m_aResultSeq
[nNewEntryNum
-1].First
= aNameValue
;
214 m_aResultSeq
[nNewEntryNum
-1].Second
= aUINameValue
;
218 // accept future extensions
219 sal_Int32 nNewLength
= m_aElementsSeq
.getLength() + 1;
222 throw xml::sax::SAXException(); // TODO: the extension element must not be the first level element
224 m_aElementsSeq
.realloc( nNewLength
);
225 m_aElementsSeq
[nNewLength
-1] = aName
;
229 // -----------------------------------
230 void SAL_CALL
DocTemplLocaleHelper::endElement( const ::rtl::OUString
& aName
)
231 throw( xml::sax::SAXException
, uno::RuntimeException
)
233 sal_Int32 nLength
= m_aElementsSeq
.getLength();
235 throw xml::sax::SAXException(); // TODO: no other end elements expected!
237 if ( !m_aElementsSeq
[nLength
-1].equals( aName
) )
238 throw xml::sax::SAXException(); // TODO: unexpected element ended
240 m_aElementsSeq
.realloc( nLength
- 1 );
243 // -----------------------------------
244 void SAL_CALL
DocTemplLocaleHelper::characters( const ::rtl::OUString
& /*aChars*/ )
245 throw(xml::sax::SAXException
, uno::RuntimeException
)
249 // -----------------------------------
250 void SAL_CALL
DocTemplLocaleHelper::ignorableWhitespace( const ::rtl::OUString
& /*aWhitespaces*/ )
251 throw(xml::sax::SAXException
, uno::RuntimeException
)
255 // -----------------------------------
256 void SAL_CALL
DocTemplLocaleHelper::processingInstruction( const ::rtl::OUString
& /*aTarget*/, const ::rtl::OUString
& /*aData*/ )
257 throw(xml::sax::SAXException
, uno::RuntimeException
)
261 // -----------------------------------
262 void SAL_CALL
DocTemplLocaleHelper::setDocumentLocator( const uno::Reference
< xml::sax::XLocator
>& /*xLocator*/ )
263 throw(xml::sax::SAXException
, uno::RuntimeException
)