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 : OUString aStringID = "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 : OUString aGroupListElement( "groupuinames:template-group-list" );
56 0 : OUString aGroupElement( "groupuinames:template-group" );
57 0 : OUString aNameAttr( "groupuinames:name" );
58 0 : OUString aUINameAttr( "groupuinames:default-ui-name" );
59 0 : OUString aCDATAString( "CDATA" );
60 0 : OUString aWhiteSpace( " " );
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 : OUString( "xmlns" ),
67 : aCDATAString,
68 0 : OUString( "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 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::Reference< xml::sax::XParser > xParser = xml::sax::Parser::create( xContext );
100 :
101 0 : DocTemplLocaleHelper* pHelper = new DocTemplLocaleHelper();
102 0 : uno::Reference< xml::sax::XDocumentHandler > xHelper( static_cast< xml::sax::XDocumentHandler* >( pHelper ) );
103 0 : xml::sax::InputSource aParserInput;
104 0 : aParserInput.aInputStream = xInStream;
105 0 : aParserInput.sSystemId = aStringID;
106 0 : xParser->setDocumentHandler( xHelper );
107 0 : xParser->parseStream( aParserInput );
108 0 : xParser->setDocumentHandler( uno::Reference < xml::sax::XDocumentHandler > () );
109 :
110 0 : return pHelper->GetParsingResult();
111 : }
112 :
113 :
114 0 : DocTemplLocaleHelper::DocTemplLocaleHelper()
115 : : m_aGroupListElement( "groupuinames:template-group-list" )
116 : , m_aGroupElement( "groupuinames:template-group" )
117 : , m_aNameAttr( "groupuinames:name" )
118 0 : , m_aUINameAttr( "groupuinames:default-ui-name" )
119 : {
120 0 : }
121 :
122 :
123 0 : DocTemplLocaleHelper::~DocTemplLocaleHelper()
124 : {
125 0 : }
126 :
127 :
128 0 : uno::Sequence< beans::StringPair > DocTemplLocaleHelper::GetParsingResult()
129 : {
130 0 : if ( m_aElementsSeq.getLength() )
131 0 : throw uno::RuntimeException(); // the parsing has still not finished!
132 :
133 0 : return m_aResultSeq;
134 : }
135 :
136 :
137 0 : void SAL_CALL DocTemplLocaleHelper::startDocument()
138 : throw(xml::sax::SAXException, uno::RuntimeException, std::exception)
139 : {
140 0 : }
141 :
142 :
143 0 : void SAL_CALL DocTemplLocaleHelper::endDocument()
144 : throw(xml::sax::SAXException, uno::RuntimeException, std::exception)
145 : {
146 0 : }
147 :
148 :
149 0 : void SAL_CALL DocTemplLocaleHelper::startElement( const OUString& aName, const uno::Reference< xml::sax::XAttributeList >& xAttribs )
150 : throw( xml::sax::SAXException, uno::RuntimeException, std::exception )
151 : {
152 0 : if ( aName == m_aGroupListElement )
153 : {
154 0 : sal_Int32 nNewLength = m_aElementsSeq.getLength() + 1;
155 :
156 0 : if ( nNewLength != 1 )
157 0 : throw xml::sax::SAXException(); // TODO: this element must be the first level element
158 :
159 0 : m_aElementsSeq.realloc( nNewLength );
160 0 : m_aElementsSeq[nNewLength-1] = aName;
161 :
162 0 : return; // nothing to do
163 : }
164 0 : else if ( aName == m_aGroupElement )
165 : {
166 0 : sal_Int32 nNewLength = m_aElementsSeq.getLength() + 1;
167 0 : if ( nNewLength != 2 )
168 0 : throw xml::sax::SAXException(); // TODO: this element must be the second level element
169 :
170 0 : m_aElementsSeq.realloc( nNewLength );
171 0 : m_aElementsSeq[nNewLength-1] = aName;
172 :
173 0 : sal_Int32 nNewEntryNum = m_aResultSeq.getLength() + 1;
174 0 : m_aResultSeq.realloc( nNewEntryNum );
175 :
176 0 : OUString aNameValue = xAttribs->getValueByName( m_aNameAttr );
177 0 : if ( aNameValue.isEmpty() )
178 0 : throw xml::sax::SAXException(); // TODO: the ID value must present
179 :
180 0 : OUString aUINameValue = xAttribs->getValueByName( m_aUINameAttr );
181 0 : if ( aUINameValue.isEmpty() )
182 0 : throw xml::sax::SAXException(); // TODO: the ID value must present
183 :
184 0 : m_aResultSeq[nNewEntryNum-1].First = aNameValue;
185 0 : m_aResultSeq[nNewEntryNum-1].Second = aUINameValue;
186 : }
187 : else
188 : {
189 : // accept future extensions
190 0 : sal_Int32 nNewLength = m_aElementsSeq.getLength() + 1;
191 :
192 0 : if ( !nNewLength )
193 0 : throw xml::sax::SAXException(); // TODO: the extension element must not be the first level element
194 :
195 0 : m_aElementsSeq.realloc( nNewLength );
196 0 : m_aElementsSeq[nNewLength-1] = aName;
197 : }
198 : }
199 :
200 :
201 0 : void SAL_CALL DocTemplLocaleHelper::endElement( const OUString& aName )
202 : throw( xml::sax::SAXException, uno::RuntimeException, std::exception )
203 : {
204 0 : sal_Int32 nLength = m_aElementsSeq.getLength();
205 0 : if ( nLength <= 0 )
206 0 : throw xml::sax::SAXException(); // TODO: no other end elements expected!
207 :
208 0 : if ( !m_aElementsSeq[nLength-1].equals( aName ) )
209 0 : throw xml::sax::SAXException(); // TODO: unexpected element ended
210 :
211 0 : m_aElementsSeq.realloc( nLength - 1 );
212 0 : }
213 :
214 :
215 0 : void SAL_CALL DocTemplLocaleHelper::characters( const OUString& /*aChars*/ )
216 : throw(xml::sax::SAXException, uno::RuntimeException, std::exception)
217 : {
218 0 : }
219 :
220 :
221 0 : void SAL_CALL DocTemplLocaleHelper::ignorableWhitespace( const OUString& /*aWhitespaces*/ )
222 : throw(xml::sax::SAXException, uno::RuntimeException, std::exception)
223 : {
224 0 : }
225 :
226 :
227 0 : void SAL_CALL DocTemplLocaleHelper::processingInstruction( const OUString& /*aTarget*/, const OUString& /*aData*/ )
228 : throw(xml::sax::SAXException, uno::RuntimeException, std::exception)
229 : {
230 0 : }
231 :
232 :
233 0 : void SAL_CALL DocTemplLocaleHelper::setDocumentLocator( const uno::Reference< xml::sax::XLocator >& /*xLocator*/ )
234 : throw(xml::sax::SAXException, uno::RuntimeException, std::exception)
235 : {
236 0 : }
237 :
238 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|