xref: /AOO41X/main/sfx2/source/doc/doctemplateslocal.cxx (revision d119d52d53d0b2180f2ae51341d882123be2af2b)
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     throw( uno::Exception )
55 {
56     ::rtl::OUString aStringID = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "groupuinames.xml" ) );
57     return ReadLocalizationSequence_Impl( xInStream, aStringID, xFactory );
58 }
59 
60 // -----------------------------------
WriteGroupLocalizationSequence(const uno::Reference<io::XOutputStream> & xOutStream,const uno::Sequence<beans::StringPair> & aSequence,const uno::Reference<lang::XMultiServiceFactory> xFactory)61 void SAL_CALL DocTemplLocaleHelper::WriteGroupLocalizationSequence( const uno::Reference< io::XOutputStream >& xOutStream, const uno::Sequence< beans::StringPair >& aSequence, const uno::Reference< lang::XMultiServiceFactory > xFactory )
62     throw( uno::Exception )
63 {
64     if ( !xOutStream.is() )
65         throw uno::RuntimeException();
66 
67     uno::Reference< io::XActiveDataSource > xWriterSource(
68         xFactory->createInstance(
69             ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.sax.Writer" ) ) ),
70         uno::UNO_QUERY_THROW );
71     uno::Reference< xml::sax::XDocumentHandler > xWriterHandler( xWriterSource, uno::UNO_QUERY_THROW );
72 
73     xWriterSource->setOutputStream( xOutStream );
74 
75     ::rtl::OUString aGroupListElement( RTL_CONSTASCII_USTRINGPARAM( "groupuinames:template-group-list" ) );
76     ::rtl::OUString aGroupElement( RTL_CONSTASCII_USTRINGPARAM( "groupuinames:template-group" ) );
77     ::rtl::OUString aNameAttr( RTL_CONSTASCII_USTRINGPARAM( "groupuinames:name" ) );
78     ::rtl::OUString aUINameAttr( RTL_CONSTASCII_USTRINGPARAM( "groupuinames:default-ui-name" ) );
79     ::rtl::OUString aCDATAString( RTL_CONSTASCII_USTRINGPARAM ( "CDATA" ) );
80     ::rtl::OUString aWhiteSpace( RTL_CONSTASCII_USTRINGPARAM ( " " ) );
81 
82     // write the namespace
83     ::comphelper::AttributeList* pRootAttrList = new ::comphelper::AttributeList;
84     uno::Reference< xml::sax::XAttributeList > xRootAttrList( pRootAttrList );
85     pRootAttrList->AddAttribute(
86         ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "xmlns" ) ),
87         aCDATAString,
88         ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "http://openoffice.org/2006/groupuinames" ) ) );
89 
90     xWriterHandler->startDocument();
91     xWriterHandler->startElement( aGroupListElement, xRootAttrList );
92 
93     for ( sal_Int32 nInd = 0; nInd < aSequence.getLength(); nInd++ )
94     {
95         ::comphelper::AttributeList *pAttrList = new ::comphelper::AttributeList;
96         uno::Reference< xml::sax::XAttributeList > xAttrList( pAttrList );
97         pAttrList->AddAttribute( aNameAttr, aCDATAString, aSequence[nInd].First );
98         pAttrList->AddAttribute( aUINameAttr, aCDATAString, aSequence[nInd].Second );
99 
100         xWriterHandler->startElement( aGroupElement, xAttrList );
101         xWriterHandler->ignorableWhitespace( aWhiteSpace );
102         xWriterHandler->endElement( aGroupElement );
103     }
104 
105     xWriterHandler->ignorableWhitespace( aWhiteSpace );
106     xWriterHandler->endElement( aGroupListElement );
107     xWriterHandler->endDocument();
108 }
109 
110 // ==================================================================================
111 
112 // -----------------------------------
ReadLocalizationSequence_Impl(const uno::Reference<io::XInputStream> & xInStream,const::rtl::OUString & aStringID,const uno::Reference<lang::XMultiServiceFactory> xFactory)113 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 )
114     throw( uno::Exception )
115 {
116     if ( !xFactory.is() || !xInStream.is() )
117         throw uno::RuntimeException();
118 
119     uno::Sequence< beans::StringPair > aResult;
120 
121     uno::Reference< xml::sax::XParser > xParser( xFactory->createInstance( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.sax.Parser" ) ) ), uno::UNO_QUERY_THROW );
122 
123     DocTemplLocaleHelper* pHelper = new DocTemplLocaleHelper();
124     uno::Reference< xml::sax::XDocumentHandler > xHelper( static_cast< xml::sax::XDocumentHandler* >( pHelper ) );
125     xml::sax::InputSource aParserInput;
126     aParserInput.aInputStream = xInStream;
127     aParserInput.sSystemId = aStringID;
128     xParser->setDocumentHandler( xHelper );
129     xParser->parseStream( aParserInput );
130     xParser->setDocumentHandler( uno::Reference < xml::sax::XDocumentHandler > () );
131 
132     return pHelper->GetParsingResult();
133 }
134 
135 // -----------------------------------
DocTemplLocaleHelper()136 DocTemplLocaleHelper::DocTemplLocaleHelper()
137 : m_aGroupListElement( RTL_CONSTASCII_USTRINGPARAM( "groupuinames:template-group-list" ) )
138 , m_aGroupElement( RTL_CONSTASCII_USTRINGPARAM( "groupuinames:template-group" ) )
139 , m_aNameAttr( RTL_CONSTASCII_USTRINGPARAM( "groupuinames:name" ) )
140 , m_aUINameAttr( RTL_CONSTASCII_USTRINGPARAM( "groupuinames:default-ui-name" ) )
141 {
142 }
143 
144 // -----------------------------------
~DocTemplLocaleHelper()145 DocTemplLocaleHelper::~DocTemplLocaleHelper()
146 {
147 }
148 
149 // -----------------------------------
GetParsingResult()150 uno::Sequence< beans::StringPair > DocTemplLocaleHelper::GetParsingResult()
151 {
152     if ( m_aElementsSeq.getLength() )
153         throw uno::RuntimeException(); // the parsing has still not finished!
154 
155     return m_aResultSeq;
156 }
157 
158 // -----------------------------------
startDocument()159 void SAL_CALL DocTemplLocaleHelper::startDocument()
160         throw(xml::sax::SAXException, uno::RuntimeException)
161 {
162 }
163 
164 // -----------------------------------
endDocument()165 void SAL_CALL DocTemplLocaleHelper::endDocument()
166         throw(xml::sax::SAXException, uno::RuntimeException)
167 {
168 }
169 
170 // -----------------------------------
startElement(const::rtl::OUString & aName,const uno::Reference<xml::sax::XAttributeList> & xAttribs)171 void SAL_CALL DocTemplLocaleHelper::startElement( const ::rtl::OUString& aName, const uno::Reference< xml::sax::XAttributeList >& xAttribs )
172         throw( xml::sax::SAXException, uno::RuntimeException )
173 {
174     if ( aName == m_aGroupListElement )
175     {
176         sal_Int32 nNewLength = m_aElementsSeq.getLength() + 1;
177 
178         if ( nNewLength != 1 )
179             throw xml::sax::SAXException(); // TODO: this element must be the first level element
180 
181         m_aElementsSeq.realloc( nNewLength );
182         m_aElementsSeq[nNewLength-1] = aName;
183 
184         return; // nothing to do
185     }
186     else if ( aName == m_aGroupElement )
187     {
188         sal_Int32 nNewLength = m_aElementsSeq.getLength() + 1;
189         if ( nNewLength != 2 )
190             throw xml::sax::SAXException(); // TODO: this element must be the second level element
191 
192         m_aElementsSeq.realloc( nNewLength );
193         m_aElementsSeq[nNewLength-1] = aName;
194 
195         sal_Int32 nNewEntryNum = m_aResultSeq.getLength() + 1;
196         m_aResultSeq.realloc( nNewEntryNum );
197 
198         ::rtl::OUString aNameValue = xAttribs->getValueByName( m_aNameAttr );
199         if ( !aNameValue.getLength() )
200             throw xml::sax::SAXException(); // TODO: the ID value must present
201 
202         ::rtl::OUString aUINameValue = xAttribs->getValueByName( m_aUINameAttr );
203         if ( !aUINameValue.getLength() )
204             throw xml::sax::SAXException(); // TODO: the ID value must present
205 
206         m_aResultSeq[nNewEntryNum-1].First = aNameValue;
207         m_aResultSeq[nNewEntryNum-1].Second = aUINameValue;
208     }
209     else
210     {
211         // accept future extensions
212         sal_Int32 nNewLength = m_aElementsSeq.getLength() + 1;
213 
214         if ( !nNewLength )
215             throw xml::sax::SAXException(); // TODO: the extension element must not be the first level element
216 
217         m_aElementsSeq.realloc( nNewLength );
218         m_aElementsSeq[nNewLength-1] = aName;
219     }
220 }
221 
222 // -----------------------------------
endElement(const::rtl::OUString & aName)223 void SAL_CALL DocTemplLocaleHelper::endElement( const ::rtl::OUString& aName )
224     throw( xml::sax::SAXException, uno::RuntimeException )
225 {
226     sal_Int32 nLength = m_aElementsSeq.getLength();
227     if ( nLength <= 0 )
228         throw xml::sax::SAXException(); // TODO: no other end elements expected!
229 
230     if ( !m_aElementsSeq[nLength-1].equals( aName ) )
231         throw xml::sax::SAXException(); // TODO: unexpected element ended
232 
233     m_aElementsSeq.realloc( nLength - 1 );
234 }
235 
236 // -----------------------------------
characters(const::rtl::OUString &)237 void SAL_CALL DocTemplLocaleHelper::characters( const ::rtl::OUString& /*aChars*/ )
238         throw(xml::sax::SAXException, uno::RuntimeException)
239 {
240 }
241 
242 // -----------------------------------
ignorableWhitespace(const::rtl::OUString &)243 void SAL_CALL DocTemplLocaleHelper::ignorableWhitespace( const ::rtl::OUString& /*aWhitespaces*/ )
244         throw(xml::sax::SAXException, uno::RuntimeException)
245 {
246 }
247 
248 // -----------------------------------
processingInstruction(const::rtl::OUString &,const::rtl::OUString &)249 void SAL_CALL DocTemplLocaleHelper::processingInstruction( const ::rtl::OUString& /*aTarget*/, const ::rtl::OUString& /*aData*/ )
250         throw(xml::sax::SAXException, uno::RuntimeException)
251 {
252 }
253 
254 // -----------------------------------
setDocumentLocator(const uno::Reference<xml::sax::XLocator> &)255 void SAL_CALL DocTemplLocaleHelper::setDocumentLocator( const uno::Reference< xml::sax::XLocator >& /*xLocator*/ )
256         throw(xml::sax::SAXException, uno::RuntimeException)
257 {
258 }
259 
260