xref: /AOO41X/main/sfx2/source/doc/doctemplateslocal.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_sfx2.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #ifndef _COM_SUN_STAR_BEANS_PROPERTYVALUE_HPP_
32*cdf0e10cSrcweir #include <com/sun/star/beans/StringPair.hpp>
33*cdf0e10cSrcweir #endif
34*cdf0e10cSrcweir #ifndef _COM_SUN_STAR_LANG_XMULTISERVICEFACTORY_HPP
35*cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
36*cdf0e10cSrcweir #endif
37*cdf0e10cSrcweir #ifndef _COM_SUN_STAR_IO_XACTIVEDATASOURCE_HPP
38*cdf0e10cSrcweir #include <com/sun/star/io/XActiveDataSource.hpp>
39*cdf0e10cSrcweir #endif
40*cdf0e10cSrcweir #ifndef _COM_SUN_STAR_XML_SAX_XPARSER_HPP
41*cdf0e10cSrcweir #include <com/sun/star/xml/sax/XParser.hpp>
42*cdf0e10cSrcweir #endif
43*cdf0e10cSrcweir #ifndef _COM_SUN_STAR_XML_SAX_XDOCUMENTHANDLER_HPP
44*cdf0e10cSrcweir #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
45*cdf0e10cSrcweir #endif
46*cdf0e10cSrcweir #ifndef _COM_SUN_STAR_LANG_ILLEGALARGUMENTEXCEPTION_HPP
47*cdf0e10cSrcweir #include <com/sun/star/lang/IllegalArgumentException.hpp>
48*cdf0e10cSrcweir #endif
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir #include <comphelper/attributelist.hxx>
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir #include "doctemplateslocal.hxx"
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir using namespace ::com::sun::star;
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir // -----------------------------------
57*cdf0e10cSrcweir uno::Sequence< beans::StringPair > DocTemplLocaleHelper::ReadGroupLocalizationSequence( const uno::Reference< io::XInputStream >& xInStream, const uno::Reference< lang::XMultiServiceFactory > xFactory )
58*cdf0e10cSrcweir 	throw( uno::Exception )
59*cdf0e10cSrcweir {
60*cdf0e10cSrcweir 	::rtl::OUString aStringID = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "groupuinames.xml" ) );
61*cdf0e10cSrcweir 	return ReadLocalizationSequence_Impl( xInStream, aStringID, xFactory );
62*cdf0e10cSrcweir }
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir // -----------------------------------
65*cdf0e10cSrcweir void SAL_CALL DocTemplLocaleHelper::WriteGroupLocalizationSequence( const uno::Reference< io::XOutputStream >& xOutStream, const uno::Sequence< beans::StringPair >& aSequence, const uno::Reference< lang::XMultiServiceFactory > xFactory )
66*cdf0e10cSrcweir 	throw( uno::Exception )
67*cdf0e10cSrcweir {
68*cdf0e10cSrcweir 	if ( !xOutStream.is() )
69*cdf0e10cSrcweir 		throw uno::RuntimeException();
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir 	uno::Reference< io::XActiveDataSource > xWriterSource(
72*cdf0e10cSrcweir 		xFactory->createInstance(
73*cdf0e10cSrcweir 			::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.sax.Writer" ) ) ),
74*cdf0e10cSrcweir 		uno::UNO_QUERY_THROW );
75*cdf0e10cSrcweir 	uno::Reference< xml::sax::XDocumentHandler > xWriterHandler( xWriterSource, uno::UNO_QUERY_THROW );
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir 	xWriterSource->setOutputStream( xOutStream );
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir 	::rtl::OUString aGroupListElement( RTL_CONSTASCII_USTRINGPARAM( "groupuinames:template-group-list" ) );
80*cdf0e10cSrcweir 	::rtl::OUString aGroupElement( RTL_CONSTASCII_USTRINGPARAM( "groupuinames:template-group" ) );
81*cdf0e10cSrcweir 	::rtl::OUString aNameAttr( RTL_CONSTASCII_USTRINGPARAM( "groupuinames:name" ) );
82*cdf0e10cSrcweir 	::rtl::OUString aUINameAttr( RTL_CONSTASCII_USTRINGPARAM( "groupuinames:default-ui-name" ) );
83*cdf0e10cSrcweir 	::rtl::OUString aCDATAString( RTL_CONSTASCII_USTRINGPARAM ( "CDATA" ) );
84*cdf0e10cSrcweir 	::rtl::OUString aWhiteSpace( RTL_CONSTASCII_USTRINGPARAM ( " " ) );
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir 	// write the namespace
87*cdf0e10cSrcweir 	::comphelper::AttributeList* pRootAttrList = new ::comphelper::AttributeList;
88*cdf0e10cSrcweir 	uno::Reference< xml::sax::XAttributeList > xRootAttrList( pRootAttrList );
89*cdf0e10cSrcweir 	pRootAttrList->AddAttribute(
90*cdf0e10cSrcweir 		::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "xmlns" ) ),
91*cdf0e10cSrcweir 		aCDATAString,
92*cdf0e10cSrcweir 		::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "http://openoffice.org/2006/groupuinames" ) ) );
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir 	xWriterHandler->startDocument();
95*cdf0e10cSrcweir 	xWriterHandler->startElement( aGroupListElement, xRootAttrList );
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir 	for ( sal_Int32 nInd = 0; nInd < aSequence.getLength(); nInd++ )
98*cdf0e10cSrcweir 	{
99*cdf0e10cSrcweir 		::comphelper::AttributeList *pAttrList = new ::comphelper::AttributeList;
100*cdf0e10cSrcweir 		uno::Reference< xml::sax::XAttributeList > xAttrList( pAttrList );
101*cdf0e10cSrcweir 		pAttrList->AddAttribute( aNameAttr, aCDATAString, aSequence[nInd].First );
102*cdf0e10cSrcweir 		pAttrList->AddAttribute( aUINameAttr, aCDATAString, aSequence[nInd].Second );
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir 		xWriterHandler->startElement( aGroupElement, xAttrList );
105*cdf0e10cSrcweir 		xWriterHandler->ignorableWhitespace( aWhiteSpace );
106*cdf0e10cSrcweir 		xWriterHandler->endElement( aGroupElement );
107*cdf0e10cSrcweir 	}
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir 	xWriterHandler->ignorableWhitespace( aWhiteSpace );
110*cdf0e10cSrcweir 	xWriterHandler->endElement( aGroupListElement );
111*cdf0e10cSrcweir 	xWriterHandler->endDocument();
112*cdf0e10cSrcweir }
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir // ==================================================================================
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir // -----------------------------------
117*cdf0e10cSrcweir 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 )
118*cdf0e10cSrcweir 	throw( uno::Exception )
119*cdf0e10cSrcweir {
120*cdf0e10cSrcweir 	if ( !xFactory.is() || !xInStream.is() )
121*cdf0e10cSrcweir 		throw uno::RuntimeException();
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir 	uno::Sequence< beans::StringPair > aResult;
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir 	uno::Reference< xml::sax::XParser > xParser( xFactory->createInstance( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.sax.Parser" ) ) ), uno::UNO_QUERY_THROW );
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir 	DocTemplLocaleHelper* pHelper = new DocTemplLocaleHelper();
128*cdf0e10cSrcweir 	uno::Reference< xml::sax::XDocumentHandler > xHelper( static_cast< xml::sax::XDocumentHandler* >( pHelper ) );
129*cdf0e10cSrcweir 	xml::sax::InputSource aParserInput;
130*cdf0e10cSrcweir 	aParserInput.aInputStream = xInStream;
131*cdf0e10cSrcweir 	aParserInput.sSystemId = aStringID;
132*cdf0e10cSrcweir 	xParser->setDocumentHandler( xHelper );
133*cdf0e10cSrcweir 	xParser->parseStream( aParserInput );
134*cdf0e10cSrcweir 	xParser->setDocumentHandler( uno::Reference < xml::sax::XDocumentHandler > () );
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir 	return pHelper->GetParsingResult();
137*cdf0e10cSrcweir }
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir // -----------------------------------
140*cdf0e10cSrcweir DocTemplLocaleHelper::DocTemplLocaleHelper()
141*cdf0e10cSrcweir : m_aGroupListElement( RTL_CONSTASCII_USTRINGPARAM( "groupuinames:template-group-list" ) )
142*cdf0e10cSrcweir , m_aGroupElement( RTL_CONSTASCII_USTRINGPARAM( "groupuinames:template-group" ) )
143*cdf0e10cSrcweir , m_aNameAttr( RTL_CONSTASCII_USTRINGPARAM( "groupuinames:name" ) )
144*cdf0e10cSrcweir , m_aUINameAttr( RTL_CONSTASCII_USTRINGPARAM( "groupuinames:default-ui-name" ) )
145*cdf0e10cSrcweir {
146*cdf0e10cSrcweir }
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir // -----------------------------------
149*cdf0e10cSrcweir DocTemplLocaleHelper::~DocTemplLocaleHelper()
150*cdf0e10cSrcweir {
151*cdf0e10cSrcweir }
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir // -----------------------------------
154*cdf0e10cSrcweir uno::Sequence< beans::StringPair > DocTemplLocaleHelper::GetParsingResult()
155*cdf0e10cSrcweir {
156*cdf0e10cSrcweir 	if ( m_aElementsSeq.getLength() )
157*cdf0e10cSrcweir 		throw uno::RuntimeException(); // the parsing has still not finished!
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir 	return m_aResultSeq;
160*cdf0e10cSrcweir }
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir // -----------------------------------
163*cdf0e10cSrcweir void SAL_CALL DocTemplLocaleHelper::startDocument()
164*cdf0e10cSrcweir 		throw(xml::sax::SAXException, uno::RuntimeException)
165*cdf0e10cSrcweir {
166*cdf0e10cSrcweir }
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir // -----------------------------------
169*cdf0e10cSrcweir void SAL_CALL DocTemplLocaleHelper::endDocument()
170*cdf0e10cSrcweir 		throw(xml::sax::SAXException, uno::RuntimeException)
171*cdf0e10cSrcweir {
172*cdf0e10cSrcweir }
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir // -----------------------------------
175*cdf0e10cSrcweir void SAL_CALL DocTemplLocaleHelper::startElement( const ::rtl::OUString& aName, const uno::Reference< xml::sax::XAttributeList >& xAttribs )
176*cdf0e10cSrcweir 		throw( xml::sax::SAXException, uno::RuntimeException )
177*cdf0e10cSrcweir {
178*cdf0e10cSrcweir 	if ( aName == m_aGroupListElement )
179*cdf0e10cSrcweir 	{
180*cdf0e10cSrcweir 		sal_Int32 nNewLength = m_aElementsSeq.getLength() + 1;
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir 		if ( nNewLength != 1 )
183*cdf0e10cSrcweir 			throw xml::sax::SAXException(); // TODO: this element must be the first level element
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir 		m_aElementsSeq.realloc( nNewLength );
186*cdf0e10cSrcweir 		m_aElementsSeq[nNewLength-1] = aName;
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir 		return; // nothing to do
189*cdf0e10cSrcweir 	}
190*cdf0e10cSrcweir 	else if ( aName == m_aGroupElement )
191*cdf0e10cSrcweir 	{
192*cdf0e10cSrcweir 		sal_Int32 nNewLength = m_aElementsSeq.getLength() + 1;
193*cdf0e10cSrcweir 		if ( nNewLength != 2 )
194*cdf0e10cSrcweir 			throw xml::sax::SAXException(); // TODO: this element must be the second level element
195*cdf0e10cSrcweir 
196*cdf0e10cSrcweir 		m_aElementsSeq.realloc( nNewLength );
197*cdf0e10cSrcweir 		m_aElementsSeq[nNewLength-1] = aName;
198*cdf0e10cSrcweir 
199*cdf0e10cSrcweir 		sal_Int32 nNewEntryNum = m_aResultSeq.getLength() + 1;
200*cdf0e10cSrcweir 		m_aResultSeq.realloc( nNewEntryNum );
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir 		::rtl::OUString aNameValue = xAttribs->getValueByName( m_aNameAttr );
203*cdf0e10cSrcweir 		if ( !aNameValue.getLength() )
204*cdf0e10cSrcweir 			throw xml::sax::SAXException(); // TODO: the ID value must present
205*cdf0e10cSrcweir 
206*cdf0e10cSrcweir 		::rtl::OUString aUINameValue = xAttribs->getValueByName( m_aUINameAttr );
207*cdf0e10cSrcweir 		if ( !aUINameValue.getLength() )
208*cdf0e10cSrcweir 			throw xml::sax::SAXException(); // TODO: the ID value must present
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir 		m_aResultSeq[nNewEntryNum-1].First = aNameValue;
211*cdf0e10cSrcweir 		m_aResultSeq[nNewEntryNum-1].Second = aUINameValue;
212*cdf0e10cSrcweir 	}
213*cdf0e10cSrcweir 	else
214*cdf0e10cSrcweir 	{
215*cdf0e10cSrcweir 		// accept future extensions
216*cdf0e10cSrcweir 		sal_Int32 nNewLength = m_aElementsSeq.getLength() + 1;
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir 		if ( !nNewLength )
219*cdf0e10cSrcweir 			throw xml::sax::SAXException(); // TODO: the extension element must not be the first level element
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir 		m_aElementsSeq.realloc( nNewLength );
222*cdf0e10cSrcweir 		m_aElementsSeq[nNewLength-1] = aName;
223*cdf0e10cSrcweir 	}
224*cdf0e10cSrcweir }
225*cdf0e10cSrcweir 
226*cdf0e10cSrcweir // -----------------------------------
227*cdf0e10cSrcweir void SAL_CALL DocTemplLocaleHelper::endElement( const ::rtl::OUString& aName )
228*cdf0e10cSrcweir 	throw( xml::sax::SAXException, uno::RuntimeException )
229*cdf0e10cSrcweir {
230*cdf0e10cSrcweir 	sal_Int32 nLength = m_aElementsSeq.getLength();
231*cdf0e10cSrcweir 	if ( nLength <= 0 )
232*cdf0e10cSrcweir 		throw xml::sax::SAXException(); // TODO: no other end elements expected!
233*cdf0e10cSrcweir 
234*cdf0e10cSrcweir 	if ( !m_aElementsSeq[nLength-1].equals( aName ) )
235*cdf0e10cSrcweir 		throw xml::sax::SAXException(); // TODO: unexpected element ended
236*cdf0e10cSrcweir 
237*cdf0e10cSrcweir 	m_aElementsSeq.realloc( nLength - 1 );
238*cdf0e10cSrcweir }
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir // -----------------------------------
241*cdf0e10cSrcweir void SAL_CALL DocTemplLocaleHelper::characters( const ::rtl::OUString& /*aChars*/ )
242*cdf0e10cSrcweir 		throw(xml::sax::SAXException, uno::RuntimeException)
243*cdf0e10cSrcweir {
244*cdf0e10cSrcweir }
245*cdf0e10cSrcweir 
246*cdf0e10cSrcweir // -----------------------------------
247*cdf0e10cSrcweir void SAL_CALL DocTemplLocaleHelper::ignorableWhitespace( const ::rtl::OUString& /*aWhitespaces*/ )
248*cdf0e10cSrcweir 		throw(xml::sax::SAXException, uno::RuntimeException)
249*cdf0e10cSrcweir {
250*cdf0e10cSrcweir }
251*cdf0e10cSrcweir 
252*cdf0e10cSrcweir // -----------------------------------
253*cdf0e10cSrcweir void SAL_CALL DocTemplLocaleHelper::processingInstruction( const ::rtl::OUString& /*aTarget*/, const ::rtl::OUString& /*aData*/ )
254*cdf0e10cSrcweir 		throw(xml::sax::SAXException, uno::RuntimeException)
255*cdf0e10cSrcweir {
256*cdf0e10cSrcweir }
257*cdf0e10cSrcweir 
258*cdf0e10cSrcweir // -----------------------------------
259*cdf0e10cSrcweir void SAL_CALL DocTemplLocaleHelper::setDocumentLocator( const uno::Reference< xml::sax::XLocator >& /*xLocator*/ )
260*cdf0e10cSrcweir 		throw(xml::sax::SAXException, uno::RuntimeException)
261*cdf0e10cSrcweir {
262*cdf0e10cSrcweir }
263*cdf0e10cSrcweir 
264