Files
libreoffice/xmlhelp/source/treeview/tvread.cxx

770 lines
19 KiB
C++
Raw Normal View History

2001-07-11 14:35:25 +00:00
#include <string.h>
2001-07-12 12:32:24 +00:00
#ifndef _VOS_DIAGNOSE_HXX_
#include <vos/diagnose.hxx>
#endif
#ifndef _TREEVIEW_TVREAD_HXX_
#include "tvread.hxx"
#endif
2001-07-11 14:35:25 +00:00
#ifndef XmlParse_INCLUDED
#include <expat/xmlparse.h>
#endif
#ifndef _OSL_FILE_HXX_
#include <osl/file.hxx>
#endif
2001-07-12 12:32:24 +00:00
#ifndef _COM_SUN_STAR_FRAME_XCONFIGMANAGER_HPP_
#include <com/sun/star/frame/XConfigManager.hpp>
#endif
#ifndef _COM_SUN_STAR_BEANS_PROPERTYVALUE_HPP_
#include <com/sun/star/beans/PropertyValue.hpp>
#endif
#ifndef _COM_SUN_STAR_BEANS_PROPERTYSTATE_HPP_
#include <com/sun/star/beans/PropertyState.hpp>
#endif
namespace treeview {
class TVDom
{
friend class TVChildTarget;
friend class TVRead;
public:
TVDom( TVDom* arent = 0 )
: parent( arent ),
2001-07-12 14:33:36 +00:00
childs( 0 ),
kind( other )
2001-07-12 12:32:24 +00:00
{
}
~TVDom()
{
for( unsigned i = 0; i < childs.size(); ++i )
delete childs[i];
}
TVDom* newChild()
{
childs.push_back( new TVDom( this ) );
return childs.back();
}
TVDom* getParent() const
{
return parent;
}
enum Kind {
tree_view,
tree_node,
tree_leaf,
other
};
bool isLeaf() const { return kind == TVDom::tree_leaf; }
void setKind( Kind ind ) { kind = ind; }
Kind getKind( ) const { return kind; }
void setApplication( const char* appl )
{
application = rtl::OUString( (sal_Char*)(appl),
strlen( appl ),
RTL_TEXTENCODING_UTF8 );
}
void setTitle( const char* itle )
{
title = rtl::OUString( (sal_Char*)(itle),
strlen( itle ),
RTL_TEXTENCODING_UTF8 );
}
void setTitle( const XML_Char* itle,int len )
{
title = rtl::OUString( (sal_Char*)(itle),
len,
RTL_TEXTENCODING_UTF8 );
}
void setId( const char* d )
{
id = rtl::OUString( (sal_Char*)(d),
strlen( d ),
RTL_TEXTENCODING_UTF8 );
}
void setAnchor( const char* nchor )
{
anchor = rtl::OUString( (sal_Char*)(nchor),
strlen( nchor ),
RTL_TEXTENCODING_UTF8 );
}
rtl::OUString getTargetURL()
{
if( ! targetURL.getLength() )
{
const TVDom* p = this;
while( ! p->application.getLength() )
p = p->parent;
targetURL = ( rtl::OUString::createFromAscii( "vnd.sun.star.help://" ) +
p->application +
rtl::OUString::createFromAscii( "/" ) +
id );
}
return targetURL;
}
private:
Kind kind;
rtl::OUString application;
rtl::OUString title;
rtl::OUString id;
rtl::OUString anchor;
rtl::OUString targetURL;
TVDom *parent;
std::vector< TVDom* > childs;
};
}
2001-07-11 14:35:25 +00:00
using namespace treeview;
using namespace com::sun::star;
using namespace com::sun::star::uno;
2001-07-12 12:32:24 +00:00
using namespace com::sun::star::beans;
using namespace com::sun::star::lang;
using namespace com::sun::star::util;
2001-07-12 12:32:24 +00:00
using namespace com::sun::star::frame;
using namespace com::sun::star::container;
2001-07-12 12:32:24 +00:00
TVRead::TVRead()
{
}
2001-07-12 12:32:24 +00:00
TVRead::TVRead( const ConfigData& configData,TVDom* tvDom )
{
2001-07-12 12:32:24 +00:00
if( ! tvDom )
return;
Title = tvDom->title;
if( tvDom->isLeaf() )
{
TargetURL = ( tvDom->getTargetURL() + configData.appendix );
if( tvDom->anchor.getLength() )
TargetURL += ( rtl::OUString::createFromAscii( "#" ) +
tvDom->anchor );
}
else
Children = new TVChildTarget( configData,tvDom );
}
TVRead::~TVRead()
{
}
//////////////////////////////////////////////////////////////////////////
// XInterface
//////////////////////////////////////////////////////////////////////////
void SAL_CALL
TVRead::acquire(
void )
throw( RuntimeException )
{
OWeakObject::acquire();
}
void SAL_CALL
TVRead::release(
void )
throw( RuntimeException )
{
OWeakObject::release();
}
Any SAL_CALL
TVRead::queryInterface(
const Type& rType )
throw( RuntimeException )
{
Any aRet = cppu::queryInterface( rType,
SAL_STATIC_CAST( XTypeProvider*, this ),
SAL_STATIC_CAST( XNameAccess*, this ),
SAL_STATIC_CAST( XHierarchicalNameAccess*, this ),
SAL_STATIC_CAST( XChangesNotifier*, this ),
SAL_STATIC_CAST( XComponent*, this ) );
return aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType );
}
////////////////////////////////////////////////////////////////////////////////
//
// XTypeProvider methods.
XTYPEPROVIDER_IMPL_5( TVRead,
XTypeProvider,
XNameAccess,
XHierarchicalNameAccess,
XChangesNotifier,
XComponent );
// XNameAccess
Any SAL_CALL
TVRead::getByName( const rtl::OUString& aName )
throw( NoSuchElementException,
WrappedTargetException,
2001-07-11 14:35:25 +00:00
RuntimeException )
{
2001-07-11 14:35:25 +00:00
bool found( true );
Any aAny;
if( aName.compareToAscii( "Title" ) == 0 )
aAny <<= Title;
else if( aName.compareToAscii( "TargetURL" ) == 0 )
aAny <<= TargetURL;
2001-07-11 14:35:25 +00:00
else if( aName.compareToAscii( "Children" ) == 0 )
{
2001-07-11 14:35:25 +00:00
cppu::OWeakObject* p = Children.get();
aAny <<= Reference< XInterface >( p );
}
2001-07-11 14:35:25 +00:00
else
found = false;
2001-07-11 14:35:25 +00:00
if( found )
return aAny;
throw NoSuchElementException();
}
Sequence< rtl::OUString > SAL_CALL
TVRead::getElementNames( )
throw( RuntimeException )
{
2001-07-11 14:35:25 +00:00
Sequence< rtl::OUString > seq( 3 );
seq[0] = rtl::OUString::createFromAscii( "Title" );
seq[1] = rtl::OUString::createFromAscii( "TargetURL" );
seq[2] = rtl::OUString::createFromAscii( "Children" );
return seq;
}
sal_Bool SAL_CALL
TVRead::hasByName( const rtl::OUString& aName )
throw( RuntimeException )
{
if( aName.compareToAscii( "Title" ) == 0 ||
2001-07-11 14:35:25 +00:00
aName.compareToAscii( "TargetURL" ) == 0 ||
aName.compareToAscii( "Children" ) == 0 )
return true;
return false;
}
// XHierarchicalNameAccess
Any SAL_CALL
TVRead::getByHierarchicalName( const rtl::OUString& aName )
throw( NoSuchElementException,
RuntimeException )
{
sal_Int32 idx;
rtl::OUString name( aName );
2001-07-11 14:35:25 +00:00
if( ( idx = name.indexOf( sal_Unicode( '/' ) ) ) != -1 &&
name.copy( 0,idx ).compareToAscii( "Children" ) == 0 )
return Children->getByHierarchicalName( name.copy( 1 + idx ) );
return getByName( name );
}
sal_Bool SAL_CALL
TVRead::hasByHierarchicalName( const rtl::OUString& aName )
throw( RuntimeException )
{
sal_Int32 idx;
rtl::OUString name( aName );
if( ( idx = name.indexOf( sal_Unicode( '/' ) ) ) != -1 &&
name.copy( 0,idx ).compareToAscii( "Children" ) == 0 )
return Children->hasByHierarchicalName( name.copy( 1 + idx ) );
return hasByName( name );
}
/**************************************************************************/
/* */
/* TVChildTarget */
/* */
/**************************************************************************/
void start_handler(void *userData,
const XML_Char *name,
const XML_Char **atts)
{
2001-07-12 12:32:24 +00:00
TVDom::Kind kind;
2001-07-11 14:35:25 +00:00
2001-07-12 12:32:24 +00:00
if( strcmp( name,"help_section" ) == 0 ||
strcmp( name,"node" ) == 0 )
kind = TVDom::tree_node;
else if( strcmp( name,"topic" ) == 0 )
kind = TVDom::tree_leaf;
else
return;
2001-07-11 14:35:25 +00:00
2001-07-12 12:32:24 +00:00
TVDom **tvDom = static_cast< TVDom** >( userData );
TVDom *p;
p = *tvDom;
2001-07-11 14:35:25 +00:00
2001-07-12 13:35:29 +00:00
*tvDom = p->newChild();
p = *tvDom;
2001-07-11 14:35:25 +00:00
2001-07-12 12:32:24 +00:00
p->setKind( kind );
while( *atts )
{
if( strcmp( *atts,"application" ) == 0 )
p->setApplication( *(atts+1) );
else if( strcmp( *atts,"title" ) == 0 )
p->setTitle( *(atts+1) );
else if( strcmp( *atts,"id" ) == 0 )
p->setId( *(atts+1) );
else if( strcmp( *atts,"anchor" ) == 0 )
p->setAnchor( *(atts+1) );
atts+=2;
2001-07-11 14:35:25 +00:00
}
}
void end_handler(void *userData,
const XML_Char *name )
{
2001-07-12 12:32:24 +00:00
TVDom **tvDom = static_cast< TVDom** >( userData );
2001-07-12 13:35:29 +00:00
*tvDom = (*tvDom)->getParent();
2001-07-11 14:35:25 +00:00
}
void data_handler( void *userData,
const XML_Char *s,
int len)
{
2001-07-12 12:32:24 +00:00
TVDom **tvDom = static_cast< TVDom** >( userData );
if( (*tvDom)->isLeaf() )
(*tvDom)->setTitle( s,len );
2001-07-11 14:35:25 +00:00
}
2001-07-12 12:32:24 +00:00
TVChildTarget::TVChildTarget( const ConfigData& configData,TVDom* tvDom )
2001-07-11 14:35:25 +00:00
{
2001-07-12 12:32:24 +00:00
Elements.resize( tvDom->childs.size() );
for( unsigned i = 0; i < Elements.size(); ++i )
Elements[i] = new TVRead( configData,tvDom->childs[i] );
2001-07-11 14:35:25 +00:00
}
2001-07-12 12:32:24 +00:00
TVChildTarget::TVChildTarget( const Reference< XMultiServiceFactory >& xMSF )
2001-07-11 14:35:25 +00:00
{
2001-07-12 12:32:24 +00:00
ConfigData configData = init( xMSF );
2001-07-11 14:35:25 +00:00
2001-07-12 12:32:24 +00:00
if( ! configData.fileurl.getLength() ||
! configData.locale.getLength() ||
! configData.system.getLength() ||
! configData.filelen )
return;
2001-07-11 14:35:25 +00:00
2001-07-12 12:32:24 +00:00
osl::File aFile(
configData.fileurl );
2001-07-11 14:35:25 +00:00
2001-07-12 12:32:24 +00:00
configData.appendix =
rtl::OUString::createFromAscii( "?Language=" ) +
configData.locale +
rtl::OUString::createFromAscii( "&System=" ) +
configData.system;
sal_uInt64 ret,len = configData.filelen;
if( ! len ) return;
2001-07-11 14:35:25 +00:00
char* s = new char[ int(len) ];
int isFinal = true;
XML_Parser parser = XML_ParserCreate( 0 );
aFile.open( OpenFlag_Read );
aFile.read( s,len,ret );
2001-07-12 12:32:24 +00:00
aFile.close();
2001-07-11 14:35:25 +00:00
XML_SetElementHandler( parser,
start_handler,
end_handler );
XML_SetCharacterDataHandler( parser,
data_handler);
2001-07-12 12:32:24 +00:00
TVDom tvDom;
TVDom* pTVDom = &tvDom;
XML_SetUserData( parser,&pTVDom );
2001-07-11 14:35:25 +00:00
int parsed = XML_Parse( parser,s,int( len ),isFinal );
delete[] s;
XML_ParserFree( parser );
2001-07-12 12:32:24 +00:00
// now TVDom holds the relevant information
Elements.resize( tvDom.childs.size() );
for( unsigned i = 0; i < Elements.size(); ++i )
Elements[i] = new TVRead( configData,tvDom.childs[i] );
2001-07-11 14:35:25 +00:00
}
TVChildTarget::~TVChildTarget()
{
}
Any SAL_CALL
TVChildTarget::getByName( const rtl::OUString& aName )
throw( NoSuchElementException,
WrappedTargetException,
RuntimeException )
{
2001-07-12 14:33:36 +00:00
rtl::OUString num( aName.getStr()+2,aName.getLength()-4 );
sal_Int32 idx = num.toInt32() - 1;
2001-07-11 14:35:25 +00:00
if( idx < 0 || Elements.size() <= sal_uInt32( idx ) )
throw NoSuchElementException();
Any aAny;
cppu::OWeakObject* p = Elements[idx].get();
aAny <<= Reference< XInterface >( p );
return aAny;
}
Sequence< rtl::OUString > SAL_CALL
TVChildTarget::getElementNames( )
throw( RuntimeException )
{
Sequence< rtl::OUString > seq( Elements.size() );
for( unsigned i = 0; i < Elements.size(); ++i )
seq[i] = rtl::OUString::valueOf( sal_Int32( 1+i ) );
return seq;
}
sal_Bool SAL_CALL
TVChildTarget::hasByName( const rtl::OUString& aName )
throw( RuntimeException )
{
2001-07-12 14:33:36 +00:00
rtl::OUString num( aName.getStr()+2,aName.getLength()-4 );
sal_Int32 idx = num.toInt32() - 1;
2001-07-11 14:35:25 +00:00
if( idx < 0 || Elements.size() <= sal_uInt32( idx ) )
return false;
return true;
}
// XHierarchicalNameAccess
Any SAL_CALL
TVChildTarget::getByHierarchicalName( const rtl::OUString& aName )
throw( NoSuchElementException,
RuntimeException )
{
sal_Int32 idx;
rtl::OUString name( aName );
if( ( idx = name.indexOf( sal_Unicode( '/' ) ) ) != -1 )
{
2001-07-12 14:33:36 +00:00
rtl::OUString num( name.getStr()+2,idx-4 );
sal_Int32 pref = num.toInt32() - 1;
2001-07-11 14:35:25 +00:00
if( pref < 0 || Elements.size() <= sal_uInt32( pref ) )
throw NoSuchElementException();
2001-07-11 14:35:25 +00:00
return Elements[pref]->getByHierarchicalName( name.copy( 1 + idx ) );
}
else
return getByName( name );
}
sal_Bool SAL_CALL
2001-07-11 14:35:25 +00:00
TVChildTarget::hasByHierarchicalName( const rtl::OUString& aName )
throw( RuntimeException )
{
sal_Int32 idx;
rtl::OUString name( aName );
2001-07-11 14:35:25 +00:00
if( ( idx = name.indexOf( sal_Unicode( '/' ) ) ) != -1 )
{
2001-07-12 14:33:36 +00:00
rtl::OUString num( name.getStr()+2,idx-4 );
sal_Int32 pref = num.toInt32() - 1;
2001-07-11 14:35:25 +00:00
if( pref < 0 || Elements.size() <= sal_uInt32( pref ) )
return false;
2001-07-11 14:35:25 +00:00
return Elements[pref]->hasByHierarchicalName( name.copy( 1 + idx ) );
}
else
return hasByName( name );
}
2001-07-12 12:32:24 +00:00
ConfigData TVChildTarget::init( const Reference< XMultiServiceFactory >& xSMgr )
{
ConfigData configData;
rtl::OUString sProviderService =
rtl::OUString::createFromAscii( "com.sun.star.configuration.ConfigurationProvider" );
Any aAny;
aAny <<= rtl::OUString::createFromAscii( "local" );
PropertyValue aProp( rtl::OUString::createFromAscii( "servertype" ),
-1,
aAny,
PropertyState_DIRECT_VALUE );
Sequence< Any > seq(1);
seq[0] <<= aProp;
Reference< XMultiServiceFactory > sProvider;
try
{
sProvider =
Reference< XMultiServiceFactory >(
xSMgr->createInstanceWithArguments( sProviderService,seq ),
UNO_QUERY );
}
catch( const com::sun::star::uno::Exception& )
{
VOS_ENSHURE( sProvider.is()," cant instantiate the multiservicefactory " );
}
if( ! sProvider.is() )
return configData;
rtl::OUString sReaderService =
rtl::OUString::createFromAscii( "com.sun.star.configuration.ConfigurationAccess" );
seq[0] <<= rtl::OUString::createFromAscii( "org.openoffice.Office.Common" );
Reference< XHierarchicalNameAccess > xHierAccess;
try
{
xHierAccess =
Reference< XHierarchicalNameAccess >
( sProvider->createInstanceWithArguments( sReaderService,seq ),
UNO_QUERY );
}
catch( const com::sun::star::uno::Exception& )
{
VOS_ENSHURE( xHierAccess.is()," cant instantiate the reader service " );
}
if( ! xHierAccess.is() )
return configData;
try
{
aAny =
xHierAccess->getByHierarchicalName( rtl::OUString::createFromAscii("Path/Current/Help") );
}
catch( const com::sun::star::container::NoSuchElementException& )
{
VOS_ENSHURE( false," path to help files could not be determined " );
return configData;
}
rtl::OUString instPath;
bool err = ! ( aAny >>= instPath );
if( err )
{
VOS_ENSHURE( false," path to help files could not be determined " );
return configData;
}
Reference< XConfigManager > xCfgMgr;
try
{
xCfgMgr =
Reference< XConfigManager >(
xSMgr->createInstance( rtl::OUString::createFromAscii( "com.sun.star.config.SpecialConfigManager" ) ),
UNO_QUERY );
}
catch( const com::sun::star::uno::Exception& )
{
VOS_ENSHURE( xCfgMgr.is()," cant instantiate the special config manager " );
}
if( ! xCfgMgr.is() )
return configData;
instPath = xCfgMgr->substituteVariables( instPath );
rtl::OUString url;
osl::FileBase::RC errFile = osl::FileBase::getFileURLFromSystemPath( instPath,url );
if( errFile != osl::FileBase::E_None )
return configData;
Any aAny1;
try
{
aAny1 =
xHierAccess->getByHierarchicalName( rtl::OUString::createFromAscii("Help/System") );
}
catch( const com::sun::star::container::NoSuchElementException& )
{
VOS_ENSHURE( false," " );
return configData;
}
rtl::OUString system;
err = ! ( aAny1 >>= system );
if( err )
{
VOS_ENSHURE( false," path to help files could not be determined " );
return configData;
}
// Reading Locale
seq[0] <<= rtl::OUString::createFromAscii( "org.openoffice.UserProfile" );
try
{
xHierAccess =
Reference< XHierarchicalNameAccess >
( sProvider->createInstanceWithArguments( sReaderService,seq ),
UNO_QUERY );
}
catch( const com::sun::star::uno::Exception& )
{
VOS_ENSHURE( xHierAccess.is()," cant instantiate the reader service " );
}
if( ! xHierAccess.is() )
return configData;
Any aAny2;
try
{
aAny2 =
xHierAccess->getByHierarchicalName( rtl::OUString::createFromAscii("International/Locale") );
}
catch( const com::sun::star::container::NoSuchElementException& )
{
VOS_ENSHURE( false," path to help files could not be determined " );
return configData;
}
rtl::OUString locale;
err = ! ( aAny2 >>= locale );
if( err )
{
VOS_ENSHURE( false," ");
return configData;
}
// Determine fileurl from url and locale
if( url.lastIndexOf( sal_Unicode( '/' ) ) != url.getLength() - 1 )
url += rtl::OUString::createFromAscii( "/" );
rtl::OUString ret;
sal_Int32 idx;
osl::DirectoryItem aDirItem;
if( osl::FileBase::E_None == osl::DirectoryItem::get( url + locale,aDirItem ) )
ret = locale;
else if( ( ( idx = locale.indexOf( '-' ) ) != -1 ||
( idx = locale.indexOf( '_' ) ) != -1 ) &&
osl::FileBase::E_None == osl::DirectoryItem::get( url + locale.copy( 0,idx ),
aDirItem ) )
ret = locale.copy( 0,idx );
url = url + ret + rtl::OUString::createFromAscii( "/treeview.xml" );
// Determine the filelen
sal_uInt64 filelen = 0;
osl::FileStatus aStatus( FileStatusMask_FileSize );
if( osl::FileBase::E_None == osl::DirectoryItem::get( url,aDirItem ) &&
osl::FileBase::E_None == aDirItem.getFileStatus( aStatus ) &&
aStatus.isValid( FileStatusMask_FileSize ) )
filelen = aStatus.getFileSize();
configData.fileurl = url;
configData.system = system;
configData.locale = locale;
configData.filelen = filelen;
return configData;
}