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_framework.hxx" 26 27 #include <framework/fwedllapi.h> 28 #include <stdio.h> 29 30 //_________________________________________________________________________________________________________________ 31 // my own includes 32 //_________________________________________________________________________________________________________________ 33 34 #include <threadhelp/resetableguard.hxx> 35 #include <xml/eventsdocumenthandler.hxx> 36 #include <macros/debug.hxx> 37 38 //_________________________________________________________________________________________________________________ 39 // interface includes 40 //_________________________________________________________________________________________________________________ 41 42 #ifndef __COM_SUN_STAR_XML_SAX_XEXTENDEDDOCUMENTHANDLER_HPP_ 43 #include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp> 44 #endif 45 46 //_________________________________________________________________________________________________________________ 47 // other includes 48 //_________________________________________________________________________________________________________________ 49 50 #include <sal/config.h> 51 #include <vcl/svapp.hxx> 52 #include <vcl/toolbox.hxx> 53 54 #include <comphelper/attributelist.hxx> 55 56 //_________________________________________________________________________________________________________________ 57 // namespace 58 //_________________________________________________________________________________________________________________ 59 60 using namespace ::com::sun::star::uno; 61 using namespace ::com::sun::star::beans; 62 using namespace ::com::sun::star::xml::sax; 63 64 65 #define XMLNS_EVENT "http://openoffice.org/2001/event" 66 #define XMLNS_XLINK "http://www.w3.org/1999/xlink" 67 #define XMLNS_EVENT_PREFIX "event:" 68 #define XMLNS_XLINK_PREFIX "xlink:" 69 70 #define ATTRIBUTE_XMLNS_EVENT "xmlns:event" 71 #define ATTRIBUTE_XMLNS_XLINK "xmlns:xlink" 72 73 #define XMLNS_FILTER_SEPARATOR "^" 74 75 #define ELEMENT_EVENTS "events" 76 #define ELEMENT_EVENT "event" 77 78 #define ATTRIBUTE_LANGUAGE "language" 79 #define ATTRIBUTE_LIBRARY "library" 80 #define ATTRIBUTE_NAME "name" 81 #define ATTRIBUTE_HREF "href" 82 #define ATTRIBUTE_TYPE "type" 83 #define ATTRIBUTE_MACRONAME "macro-name" 84 85 #define ELEMENT_NS_EVENTS "event:events" 86 #define ELEMENT_NS_EVENT "event:event" 87 88 #define ATTRIBUTE_TYPE_CDATA "CDATA" 89 90 #define EVENTS_DOCTYPE "<!DOCTYPE event:events PUBLIC \"-//OpenOffice.org//DTD OfficeDocument 1.0//EN\" \"event.dtd\">" 91 92 // Property names for events 93 #define PROP_EVENT_TYPE "EventType" 94 #define PROP_LIBRARY "Library" 95 #define PROP_SCRIPT "Script" 96 #define PROP_MACRO_NAME "MacroName" 97 #define STAR_BASIC "StarBasic" 98 #define JAVA_SCRIPT "JavaScript" 99 100 101 namespace framework 102 { 103 104 struct EventEntryProperty 105 { 106 OReadEventsDocumentHandler::Event_XML_Namespace nNamespace; 107 char aEntryName[20]; 108 }; 109 110 static EventEntryProperty EventEntries[OReadEventsDocumentHandler::EV_XML_ENTRY_COUNT] = 111 { 112 { OReadEventsDocumentHandler::EV_NS_EVENT, ELEMENT_EVENTS }, 113 { OReadEventsDocumentHandler::EV_NS_EVENT, ELEMENT_EVENT }, 114 { OReadEventsDocumentHandler::EV_NS_EVENT, ATTRIBUTE_LANGUAGE }, 115 { OReadEventsDocumentHandler::EV_NS_EVENT, ATTRIBUTE_NAME }, 116 { OReadEventsDocumentHandler::EV_NS_XLINK, ATTRIBUTE_HREF }, 117 { OReadEventsDocumentHandler::EV_NS_XLINK, ATTRIBUTE_TYPE }, 118 { OReadEventsDocumentHandler::EV_NS_EVENT, ATTRIBUTE_MACRONAME }, 119 { OReadEventsDocumentHandler::EV_NS_EVENT, ATTRIBUTE_LIBRARY } 120 }; 121 122 123 OReadEventsDocumentHandler::OReadEventsDocumentHandler( EventsConfig& aItems ) : 124 ThreadHelpBase( &Application::GetSolarMutex() ), 125 m_aEventItems( aItems ) 126 { 127 ::rtl::OUString aNamespaceEvent( RTL_CONSTASCII_USTRINGPARAM( XMLNS_EVENT )); 128 ::rtl::OUString aNamespaceXLink( RTL_CONSTASCII_USTRINGPARAM( XMLNS_XLINK )); 129 ::rtl::OUString aSeparator( RTL_CONSTASCII_USTRINGPARAM( XMLNS_FILTER_SEPARATOR )); 130 131 // create hash map 132 for ( int i = 0; i < (int)EV_XML_ENTRY_COUNT; i++ ) 133 { 134 if ( EventEntries[i].nNamespace == EV_NS_EVENT ) 135 { 136 ::rtl::OUString temp( aNamespaceEvent ); 137 temp += aSeparator; 138 temp += ::rtl::OUString::createFromAscii( EventEntries[i].aEntryName ); 139 m_aEventsMap.insert( EventsHashMap::value_type( temp, (Events_XML_Entry)i ) ); 140 } 141 else 142 { 143 ::rtl::OUString temp( aNamespaceXLink ); 144 temp += aSeparator; 145 temp += ::rtl::OUString::createFromAscii( EventEntries[i].aEntryName ); 146 m_aEventsMap.insert( EventsHashMap::value_type( temp, (Events_XML_Entry)i ) ); 147 } 148 } 149 150 m_bEventsStartFound = sal_False; 151 m_bEventsEndFound = sal_False; 152 m_bEventStartFound = sal_False; 153 } 154 155 OReadEventsDocumentHandler::~OReadEventsDocumentHandler() 156 { 157 } 158 159 // XDocumentHandler 160 void SAL_CALL OReadEventsDocumentHandler::startDocument(void) 161 { 162 } 163 164 void SAL_CALL OReadEventsDocumentHandler::endDocument(void) 165 { 166 ResetableGuard aGuard( m_aLock ); 167 168 if (( m_bEventsStartFound && !m_bEventsEndFound ) || 169 ( !m_bEventsStartFound && m_bEventsEndFound ) ) 170 { 171 ::rtl::OUString aErrorMessage = getErrorLineString(); 172 aErrorMessage += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "No matching start or end element 'event:events' found!" )); 173 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() ); 174 } 175 } 176 177 void SAL_CALL OReadEventsDocumentHandler::startElement( 178 const ::rtl::OUString& aName, const Reference< XAttributeList > &xAttribs ) 179 { 180 ResetableGuard aGuard( m_aLock ); 181 182 EventsHashMap::const_iterator pEventEntry = m_aEventsMap.find( aName ); 183 if ( pEventEntry != m_aEventsMap.end() ) 184 { 185 switch ( pEventEntry->second ) 186 { 187 case EV_ELEMENT_EVENTS: 188 { 189 if ( m_bEventsStartFound ) 190 { 191 ::rtl::OUString aErrorMessage = getErrorLineString(); 192 aErrorMessage += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Element 'event:events' cannot be embedded into 'event:events'!" )); 193 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() ); 194 } 195 196 m_bEventsStartFound = sal_True; 197 } 198 break; 199 200 case EV_ELEMENT_EVENT: 201 { 202 if ( !m_bEventsStartFound ) 203 { 204 ::rtl::OUString aErrorMessage = getErrorLineString(); 205 aErrorMessage += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Element 'event:event' must be embedded into element 'event:events'!" )); 206 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() ); 207 } 208 209 if ( m_bEventStartFound ) 210 { 211 ::rtl::OUString aErrorMessage = getErrorLineString(); 212 aErrorMessage += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Element event:event is not a container!" )); 213 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() ); 214 } 215 216 ::rtl::OUString aLanguage; 217 ::rtl::OUString aURL; 218 ::rtl::OUString aMacroName; 219 ::rtl::OUString aLibrary; 220 ::rtl::OUString aEventName; 221 222 m_bEventStartFound = sal_True; 223 224 long nIndex = m_aEventItems.aEventNames.getLength(); 225 long nPropCount = 2; // every event config entry needs at least 2 properties 226 Sequence< PropertyValue > aEventProperties( nPropCount ); 227 228 m_aEventItems.aEventNames.realloc( nIndex + 1 ); 229 230 for ( sal_Int16 n = 0; n < xAttribs->getLength(); n++ ) 231 { 232 pEventEntry = m_aEventsMap.find( xAttribs->getNameByIndex( n ) ); 233 if ( pEventEntry != m_aEventsMap.end() ) 234 { 235 switch ( pEventEntry->second ) 236 { 237 case EV_ATTRIBUTE_TYPE: 238 { 239 aLanguage = xAttribs->getValueByIndex( n ); 240 } 241 break; 242 243 case EV_ATTRIBUTE_NAME: 244 { 245 aEventName = xAttribs->getValueByIndex( n ); 246 } 247 break; 248 249 case XL_ATTRIBUTE_HREF: 250 { 251 aURL = xAttribs->getValueByIndex( n ); 252 } 253 break; 254 255 case EV_ATTRIBUTE_MACRONAME: 256 { 257 aMacroName = xAttribs->getValueByIndex( n ); 258 } 259 break; 260 261 case EV_ATTRIBUTE_LIBRARY: 262 { 263 aLibrary = xAttribs->getValueByIndex( n ); 264 } 265 break; 266 267 default: 268 break; // nothing to do 269 } 270 } 271 } // for 272 273 ::rtl::OUString aRequiredAttributeName; 274 if ( aLanguage.getLength() == 0 ) 275 aRequiredAttributeName = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ATTRIBUTE_TYPE )); 276 else if ( aEventName.getLength() == 0 ) 277 aRequiredAttributeName = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ATTRIBUTE_NAME )); 278 279 // check for missing attribute values 280 if ( aRequiredAttributeName.getLength() > 0 ) 281 { 282 ::rtl::OUString aErrorMessage = getErrorLineString(); 283 aErrorMessage += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Required attribute ")); 284 aErrorMessage += aRequiredAttributeName; 285 aErrorMessage += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( " must have a value!" )); 286 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() ); 287 } 288 289 Any a; 290 291 // set properties 292 a <<= aLanguage; 293 aEventProperties[0].Value <<= a; 294 aEventProperties[0].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( PROP_EVENT_TYPE )); 295 296 a <<= aMacroName; 297 aEventProperties[1].Value <<= a; 298 aEventProperties[1].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( PROP_MACRO_NAME )); 299 300 if ( aLibrary.getLength() > 0 ) 301 { 302 ++nPropCount; 303 aEventProperties.realloc( nPropCount ); 304 a <<= aLibrary; 305 aEventProperties[nPropCount-1].Value <<= a; 306 aEventProperties[nPropCount-1].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( PROP_LIBRARY )); 307 } 308 309 if ( aURL.getLength() > 0 ) 310 { 311 ++nPropCount; 312 aEventProperties.realloc( nPropCount ); 313 a <<= aURL; 314 aEventProperties[nPropCount-1].Value <<= a; 315 aEventProperties[nPropCount-1].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( PROP_SCRIPT )); 316 } 317 318 // set event name 319 m_aEventItems.aEventNames[ nIndex ] = aEventName; 320 321 m_aEventItems.aEventsProperties.realloc( nIndex + 1 ); 322 a <<= aEventProperties; 323 m_aEventItems.aEventsProperties[ nIndex ] = a; 324 } 325 break; 326 327 default: 328 break; 329 } 330 } 331 } 332 333 void SAL_CALL OReadEventsDocumentHandler::endElement(const ::rtl::OUString& aName) 334 { 335 ResetableGuard aGuard( m_aLock ); 336 337 EventsHashMap::const_iterator pEventEntry = m_aEventsMap.find( aName ); 338 if ( pEventEntry != m_aEventsMap.end() ) 339 { 340 switch ( pEventEntry->second ) 341 { 342 case EV_ELEMENT_EVENTS: 343 { 344 if ( !m_bEventsStartFound ) 345 { 346 ::rtl::OUString aErrorMessage = getErrorLineString(); 347 aErrorMessage += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "End element 'event:events' found, but no start element" )); 348 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() ); 349 } 350 351 m_bEventsStartFound = sal_False; 352 } 353 break; 354 355 case EV_ELEMENT_EVENT: 356 { 357 if ( !m_bEventStartFound ) 358 { 359 ::rtl::OUString aErrorMessage = getErrorLineString(); 360 aErrorMessage += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "End element 'event:event' found, but no start element" )); 361 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() ); 362 } 363 364 m_bEventStartFound = sal_False; 365 } 366 break; 367 368 default: 369 break; // impossible case 370 } 371 } 372 } 373 374 void SAL_CALL OReadEventsDocumentHandler::characters(const ::rtl::OUString&) 375 { 376 } 377 378 void SAL_CALL OReadEventsDocumentHandler::ignorableWhitespace(const ::rtl::OUString&) 379 { 380 } 381 382 void SAL_CALL OReadEventsDocumentHandler::processingInstruction( 383 const ::rtl::OUString& /*aTarget*/, const ::rtl::OUString& /*aData*/ ) 384 { 385 } 386 387 void SAL_CALL OReadEventsDocumentHandler::setDocumentLocator( 388 const Reference< XLocator > &xLocator) 389 { 390 ResetableGuard aGuard( m_aLock ); 391 392 m_xLocator = xLocator; 393 } 394 395 ::rtl::OUString OReadEventsDocumentHandler::getErrorLineString() 396 { 397 ResetableGuard aGuard( m_aLock ); 398 399 char buffer[32]; 400 401 if ( m_xLocator.is() ) 402 { 403 snprintf( buffer, sizeof(buffer), "Line: %ld - ", static_cast<long>(m_xLocator->getLineNumber() )); 404 return ::rtl::OUString::createFromAscii( buffer ); 405 } 406 else 407 return ::rtl::OUString(); 408 } 409 410 411 //_________________________________________________________________________________________________________________ 412 // OWriteEventsDocumentHandler 413 //_________________________________________________________________________________________________________________ 414 415 OWriteEventsDocumentHandler::OWriteEventsDocumentHandler( 416 const EventsConfig& aItems, 417 Reference< XDocumentHandler > rWriteDocumentHandler ) : 418 ThreadHelpBase( &Application::GetSolarMutex() ), 419 m_aItems( aItems ), 420 m_xWriteDocumentHandler( rWriteDocumentHandler ) 421 { 422 ::comphelper::AttributeList* pList = new ::comphelper::AttributeList; 423 m_xEmptyList = Reference< XAttributeList >( (XAttributeList *) pList, UNO_QUERY ); 424 m_aAttributeType = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ATTRIBUTE_TYPE_CDATA )); 425 m_aXMLXlinkNS = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( XMLNS_XLINK_PREFIX )); 426 m_aXMLEventNS = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( XMLNS_EVENT_PREFIX )); 427 } 428 429 OWriteEventsDocumentHandler::~OWriteEventsDocumentHandler() 430 { 431 } 432 433 void OWriteEventsDocumentHandler::WriteEventsDocument() 434 { 435 ResetableGuard aGuard( m_aLock ); 436 437 m_xWriteDocumentHandler->startDocument(); 438 439 // write DOCTYPE line! 440 Reference< XExtendedDocumentHandler > xExtendedDocHandler( m_xWriteDocumentHandler, UNO_QUERY ); 441 if ( xExtendedDocHandler.is() ) 442 { 443 xExtendedDocHandler->unknown( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( EVENTS_DOCTYPE )) ); 444 m_xWriteDocumentHandler->ignorableWhitespace( ::rtl::OUString() ); 445 } 446 447 ::comphelper::AttributeList* pList = new ::comphelper::AttributeList; 448 Reference< XAttributeList > xList( (XAttributeList *) pList , UNO_QUERY ); 449 450 pList->AddAttribute( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ATTRIBUTE_XMLNS_EVENT )), 451 m_aAttributeType, 452 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( XMLNS_EVENT )) ); 453 pList->AddAttribute( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ATTRIBUTE_XMLNS_XLINK )), 454 m_aAttributeType, 455 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( XMLNS_XLINK )) ); 456 457 m_xWriteDocumentHandler->startElement( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ELEMENT_NS_EVENTS )), pList ); 458 m_xWriteDocumentHandler->ignorableWhitespace( ::rtl::OUString() ); 459 460 Sequence< PropertyValue > aEventProperties; 461 462 for ( int i = 0; i < m_aItems.aEventNames.getLength(); i++ ) 463 { 464 if ( m_aItems.aEventsProperties[i] >>= aEventProperties ) 465 WriteEvent( m_aItems.aEventNames[i], aEventProperties ); 466 } 467 468 m_xWriteDocumentHandler->ignorableWhitespace( ::rtl::OUString() ); 469 m_xWriteDocumentHandler->endElement( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ELEMENT_NS_EVENTS )) ); 470 471 m_xWriteDocumentHandler->ignorableWhitespace( ::rtl::OUString() ); 472 m_xWriteDocumentHandler->endDocument(); 473 } 474 475 //_________________________________________________________________________________________________________________ 476 // protected member functions 477 //_________________________________________________________________________________________________________________ 478 479 void OWriteEventsDocumentHandler::WriteEvent( const ::rtl::OUString& aEventName, const Sequence< PropertyValue >& aPropertyValues ) 480 { 481 if ( aPropertyValues.getLength() > 0 ) 482 { 483 ::comphelper::AttributeList* pList = new ::comphelper::AttributeList; 484 Reference< XAttributeList > xList( (XAttributeList *) pList , UNO_QUERY ); 485 486 if ( m_aAttributeURL.getLength() == 0 ) 487 { 488 m_aAttributeURL = m_aXMLXlinkNS; 489 m_aAttributeURL += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ATTRIBUTE_HREF )); 490 m_aAttributeLinkType = m_aXMLXlinkNS; 491 m_aAttributeLinkType += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ATTRIBUTE_TYPE )); 492 m_aAttributeLanguage = m_aXMLEventNS; 493 m_aAttributeLanguage += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ATTRIBUTE_LANGUAGE )); 494 m_aAttributeMacroName = m_aXMLEventNS; 495 m_aAttributeMacroName += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ATTRIBUTE_MACRONAME )); 496 m_aAttributeLibrary = m_aXMLEventNS; 497 m_aAttributeLibrary += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ATTRIBUTE_LIBRARY )); 498 m_aAttributeName = m_aXMLEventNS; 499 m_aAttributeName += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ATTRIBUTE_NAME )); 500 } 501 502 pList->AddAttribute( m_aAttributeName, m_aAttributeType, aEventName ); 503 504 sal_Bool bURLSet = sal_False; 505 ::rtl::OUString aValue; 506 ::rtl::OUString aName; 507 508 // save attributes 509 for ( int i = 0; i < aPropertyValues.getLength(); i++ ) 510 { 511 aPropertyValues[i].Value >>= aValue; 512 if ( aPropertyValues[i].Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( PROP_EVENT_TYPE ))) 513 pList->AddAttribute( m_aAttributeLanguage, m_aAttributeType, aValue ); 514 else if ( aPropertyValues[i].Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( PROP_MACRO_NAME )) && 515 aValue.getLength() > 0 ) 516 pList->AddAttribute( m_aAttributeMacroName, m_aAttributeType, aValue ); 517 else if ( aPropertyValues[i].Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( PROP_LIBRARY )) && 518 aValue.getLength() > 0 ) 519 pList->AddAttribute( m_aAttributeLibrary, m_aAttributeType, aValue ); 520 else if ( aPropertyValues[i].Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( PROP_SCRIPT ))) 521 { 522 pList->AddAttribute( m_aAttributeURL, m_aAttributeType, aValue ); 523 bURLSet = sal_True; 524 } 525 } 526 527 if ( bURLSet ) 528 pList->AddAttribute( m_aAttributeLinkType, m_aAttributeType, ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "simple" )) ); 529 530 m_xWriteDocumentHandler->startElement( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ELEMENT_NS_EVENT )), xList ); 531 m_xWriteDocumentHandler->ignorableWhitespace( ::rtl::OUString() ); 532 533 m_xWriteDocumentHandler->endElement( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ELEMENT_NS_EVENT )) ); 534 m_xWriteDocumentHandler->ignorableWhitespace( ::rtl::OUString() ); 535 } 536 } 537 538 } // namespace framework 539