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