1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 // MARKER(update_precomp.py): autogen include statement, do not remove 29 #include "precompiled_sfx2.hxx" 30 31 #ifndef _COM_SUN_STAR_BEANS_PROPERTYVALUE_HPP_ 32 #include <com/sun/star/beans/StringPair.hpp> 33 #endif 34 #ifndef _COM_SUN_STAR_LANG_XMULTISERVICEFACTORY_HPP 35 #include <com/sun/star/lang/XMultiServiceFactory.hpp> 36 #endif 37 #ifndef _COM_SUN_STAR_IO_XACTIVEDATASOURCE_HPP 38 #include <com/sun/star/io/XActiveDataSource.hpp> 39 #endif 40 #ifndef _COM_SUN_STAR_XML_SAX_XPARSER_HPP 41 #include <com/sun/star/xml/sax/XParser.hpp> 42 #endif 43 #ifndef _COM_SUN_STAR_XML_SAX_XDOCUMENTHANDLER_HPP 44 #include <com/sun/star/xml/sax/XDocumentHandler.hpp> 45 #endif 46 #ifndef _COM_SUN_STAR_LANG_ILLEGALARGUMENTEXCEPTION_HPP 47 #include <com/sun/star/lang/IllegalArgumentException.hpp> 48 #endif 49 50 #include <comphelper/attributelist.hxx> 51 52 #include "doctemplateslocal.hxx" 53 54 using namespace ::com::sun::star; 55 56 // ----------------------------------- 57 uno::Sequence< beans::StringPair > DocTemplLocaleHelper::ReadGroupLocalizationSequence( const uno::Reference< io::XInputStream >& xInStream, const uno::Reference< lang::XMultiServiceFactory > xFactory ) 58 throw( uno::Exception ) 59 { 60 ::rtl::OUString aStringID = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "groupuinames.xml" ) ); 61 return ReadLocalizationSequence_Impl( xInStream, aStringID, xFactory ); 62 } 63 64 // ----------------------------------- 65 void SAL_CALL DocTemplLocaleHelper::WriteGroupLocalizationSequence( const uno::Reference< io::XOutputStream >& xOutStream, const uno::Sequence< beans::StringPair >& aSequence, const uno::Reference< lang::XMultiServiceFactory > xFactory ) 66 throw( uno::Exception ) 67 { 68 if ( !xOutStream.is() ) 69 throw uno::RuntimeException(); 70 71 uno::Reference< io::XActiveDataSource > xWriterSource( 72 xFactory->createInstance( 73 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.sax.Writer" ) ) ), 74 uno::UNO_QUERY_THROW ); 75 uno::Reference< xml::sax::XDocumentHandler > xWriterHandler( xWriterSource, uno::UNO_QUERY_THROW ); 76 77 xWriterSource->setOutputStream( xOutStream ); 78 79 ::rtl::OUString aGroupListElement( RTL_CONSTASCII_USTRINGPARAM( "groupuinames:template-group-list" ) ); 80 ::rtl::OUString aGroupElement( RTL_CONSTASCII_USTRINGPARAM( "groupuinames:template-group" ) ); 81 ::rtl::OUString aNameAttr( RTL_CONSTASCII_USTRINGPARAM( "groupuinames:name" ) ); 82 ::rtl::OUString aUINameAttr( RTL_CONSTASCII_USTRINGPARAM( "groupuinames:default-ui-name" ) ); 83 ::rtl::OUString aCDATAString( RTL_CONSTASCII_USTRINGPARAM ( "CDATA" ) ); 84 ::rtl::OUString aWhiteSpace( RTL_CONSTASCII_USTRINGPARAM ( " " ) ); 85 86 // write the namespace 87 ::comphelper::AttributeList* pRootAttrList = new ::comphelper::AttributeList; 88 uno::Reference< xml::sax::XAttributeList > xRootAttrList( pRootAttrList ); 89 pRootAttrList->AddAttribute( 90 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "xmlns" ) ), 91 aCDATAString, 92 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "http://openoffice.org/2006/groupuinames" ) ) ); 93 94 xWriterHandler->startDocument(); 95 xWriterHandler->startElement( aGroupListElement, xRootAttrList ); 96 97 for ( sal_Int32 nInd = 0; nInd < aSequence.getLength(); nInd++ ) 98 { 99 ::comphelper::AttributeList *pAttrList = new ::comphelper::AttributeList; 100 uno::Reference< xml::sax::XAttributeList > xAttrList( pAttrList ); 101 pAttrList->AddAttribute( aNameAttr, aCDATAString, aSequence[nInd].First ); 102 pAttrList->AddAttribute( aUINameAttr, aCDATAString, aSequence[nInd].Second ); 103 104 xWriterHandler->startElement( aGroupElement, xAttrList ); 105 xWriterHandler->ignorableWhitespace( aWhiteSpace ); 106 xWriterHandler->endElement( aGroupElement ); 107 } 108 109 xWriterHandler->ignorableWhitespace( aWhiteSpace ); 110 xWriterHandler->endElement( aGroupListElement ); 111 xWriterHandler->endDocument(); 112 } 113 114 // ================================================================================== 115 116 // ----------------------------------- 117 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 ) 118 throw( uno::Exception ) 119 { 120 if ( !xFactory.is() || !xInStream.is() ) 121 throw uno::RuntimeException(); 122 123 uno::Sequence< beans::StringPair > aResult; 124 125 uno::Reference< xml::sax::XParser > xParser( xFactory->createInstance( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.sax.Parser" ) ) ), uno::UNO_QUERY_THROW ); 126 127 DocTemplLocaleHelper* pHelper = new DocTemplLocaleHelper(); 128 uno::Reference< xml::sax::XDocumentHandler > xHelper( static_cast< xml::sax::XDocumentHandler* >( pHelper ) ); 129 xml::sax::InputSource aParserInput; 130 aParserInput.aInputStream = xInStream; 131 aParserInput.sSystemId = aStringID; 132 xParser->setDocumentHandler( xHelper ); 133 xParser->parseStream( aParserInput ); 134 xParser->setDocumentHandler( uno::Reference < xml::sax::XDocumentHandler > () ); 135 136 return pHelper->GetParsingResult(); 137 } 138 139 // ----------------------------------- 140 DocTemplLocaleHelper::DocTemplLocaleHelper() 141 : m_aGroupListElement( RTL_CONSTASCII_USTRINGPARAM( "groupuinames:template-group-list" ) ) 142 , m_aGroupElement( RTL_CONSTASCII_USTRINGPARAM( "groupuinames:template-group" ) ) 143 , m_aNameAttr( RTL_CONSTASCII_USTRINGPARAM( "groupuinames:name" ) ) 144 , m_aUINameAttr( RTL_CONSTASCII_USTRINGPARAM( "groupuinames:default-ui-name" ) ) 145 { 146 } 147 148 // ----------------------------------- 149 DocTemplLocaleHelper::~DocTemplLocaleHelper() 150 { 151 } 152 153 // ----------------------------------- 154 uno::Sequence< beans::StringPair > DocTemplLocaleHelper::GetParsingResult() 155 { 156 if ( m_aElementsSeq.getLength() ) 157 throw uno::RuntimeException(); // the parsing has still not finished! 158 159 return m_aResultSeq; 160 } 161 162 // ----------------------------------- 163 void SAL_CALL DocTemplLocaleHelper::startDocument() 164 throw(xml::sax::SAXException, uno::RuntimeException) 165 { 166 } 167 168 // ----------------------------------- 169 void SAL_CALL DocTemplLocaleHelper::endDocument() 170 throw(xml::sax::SAXException, uno::RuntimeException) 171 { 172 } 173 174 // ----------------------------------- 175 void SAL_CALL DocTemplLocaleHelper::startElement( const ::rtl::OUString& aName, const uno::Reference< xml::sax::XAttributeList >& xAttribs ) 176 throw( xml::sax::SAXException, uno::RuntimeException ) 177 { 178 if ( aName == m_aGroupListElement ) 179 { 180 sal_Int32 nNewLength = m_aElementsSeq.getLength() + 1; 181 182 if ( nNewLength != 1 ) 183 throw xml::sax::SAXException(); // TODO: this element must be the first level element 184 185 m_aElementsSeq.realloc( nNewLength ); 186 m_aElementsSeq[nNewLength-1] = aName; 187 188 return; // nothing to do 189 } 190 else if ( aName == m_aGroupElement ) 191 { 192 sal_Int32 nNewLength = m_aElementsSeq.getLength() + 1; 193 if ( nNewLength != 2 ) 194 throw xml::sax::SAXException(); // TODO: this element must be the second level element 195 196 m_aElementsSeq.realloc( nNewLength ); 197 m_aElementsSeq[nNewLength-1] = aName; 198 199 sal_Int32 nNewEntryNum = m_aResultSeq.getLength() + 1; 200 m_aResultSeq.realloc( nNewEntryNum ); 201 202 ::rtl::OUString aNameValue = xAttribs->getValueByName( m_aNameAttr ); 203 if ( !aNameValue.getLength() ) 204 throw xml::sax::SAXException(); // TODO: the ID value must present 205 206 ::rtl::OUString aUINameValue = xAttribs->getValueByName( m_aUINameAttr ); 207 if ( !aUINameValue.getLength() ) 208 throw xml::sax::SAXException(); // TODO: the ID value must present 209 210 m_aResultSeq[nNewEntryNum-1].First = aNameValue; 211 m_aResultSeq[nNewEntryNum-1].Second = aUINameValue; 212 } 213 else 214 { 215 // accept future extensions 216 sal_Int32 nNewLength = m_aElementsSeq.getLength() + 1; 217 218 if ( !nNewLength ) 219 throw xml::sax::SAXException(); // TODO: the extension element must not be the first level element 220 221 m_aElementsSeq.realloc( nNewLength ); 222 m_aElementsSeq[nNewLength-1] = aName; 223 } 224 } 225 226 // ----------------------------------- 227 void SAL_CALL DocTemplLocaleHelper::endElement( const ::rtl::OUString& aName ) 228 throw( xml::sax::SAXException, uno::RuntimeException ) 229 { 230 sal_Int32 nLength = m_aElementsSeq.getLength(); 231 if ( nLength <= 0 ) 232 throw xml::sax::SAXException(); // TODO: no other end elements expected! 233 234 if ( !m_aElementsSeq[nLength-1].equals( aName ) ) 235 throw xml::sax::SAXException(); // TODO: unexpected element ended 236 237 m_aElementsSeq.realloc( nLength - 1 ); 238 } 239 240 // ----------------------------------- 241 void SAL_CALL DocTemplLocaleHelper::characters( const ::rtl::OUString& /*aChars*/ ) 242 throw(xml::sax::SAXException, uno::RuntimeException) 243 { 244 } 245 246 // ----------------------------------- 247 void SAL_CALL DocTemplLocaleHelper::ignorableWhitespace( const ::rtl::OUString& /*aWhitespaces*/ ) 248 throw(xml::sax::SAXException, uno::RuntimeException) 249 { 250 } 251 252 // ----------------------------------- 253 void SAL_CALL DocTemplLocaleHelper::processingInstruction( const ::rtl::OUString& /*aTarget*/, const ::rtl::OUString& /*aData*/ ) 254 throw(xml::sax::SAXException, uno::RuntimeException) 255 { 256 } 257 258 // ----------------------------------- 259 void SAL_CALL DocTemplLocaleHelper::setDocumentLocator( const uno::Reference< xml::sax::XLocator >& /*xLocator*/ ) 260 throw(xml::sax::SAXException, uno::RuntimeException) 261 { 262 } 263 264