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