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