Difference between revisions of "User:Arielch/Internship/XML Reader and Writer"

From Apache OpenOffice Wiki
Jump to: navigation, search
(Added Test section)
(Test: added source code)
Line 19: Line 19:
  
 
Created a small application to test the new functionality. Test is located on framework/workben/xml
 
Created a small application to test the new functionality. Test is located on framework/workben/xml
 +
 +
<source lang=cpp>
 +
/*************************************************************************
 +
*
 +
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 +
*
 +
* Copyright 2008 by Sun Microsystems, Inc.
 +
*
 +
* OpenOffice.org - a multi-platform office productivity suite
 +
*
 +
* $RCSfile: cmduicollector.cxx,v $
 +
* $Revision: 1.16 $
 +
*
 +
* This file is part of OpenOffice.org.
 +
*
 +
* OpenOffice.org is free software: you can redistribute it and/or modify
 +
* it under the terms of the GNU Lesser General Public License version 3
 +
* only, as published by the Free Software Foundation.
 +
*
 +
* OpenOffice.org is distributed in the hope that it will be useful,
 +
* but WITHOUT ANY WARRANTY; without even the implied warranty of
 +
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 +
* GNU Lesser General Public License version 3 for more details
 +
* (a copy is included in the LICENSE file that accompanied this code).
 +
*
 +
* You should have received a copy of the GNU Lesser General Public License
 +
* version 3 along with OpenOffice.org.  If not, see
 +
* <http://www.openoffice.org/license.html>
 +
* for a copy of the LGPLv3 License.
 +
*
 +
************************************************************************/
 +
 +
// MARKER(update_precomp.py): autogen include statement, do not remove
 +
#include "precompiled_framework.hxx"
 +
 +
#include <sal/main.h>
 +
#include <sal/config.h>
 +
#include <osl/file.hxx>
 +
#include <osl/process.h>
 +
#include <rtl/ustring.hxx>
 +
#include <cppuhelper/bootstrap.hxx>
 +
 +
#include <vcl/svapp.hxx>
 +
 +
#include <uielement/rootitemcontainer.hxx>
 +
#include <xml/contextmenuconfiguration.hxx>
 +
 +
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
 +
#include <com/sun/star/lang/XComponent.hpp>
 +
#include <com/sun/star/ucb/XSimpleFileAccess.hpp>
 +
 +
#include <vector>
 +
 +
// -----------------------------------------------------------------------
 +
 +
using namespace rtl;
 +
using namespace ::com::sun::star::uno;
 +
using namespace ::com::sun::star::lang;
 +
 +
namespace css = ::com::sun::star;
 +
 +
// -----------------------------------------------------------------------
 +
 +
const OUString ConvertToURL(const OUString& aFileName)
 +
{
 +
    OUString uFileNameUrl;
 +
 +
    // If aFileName is *only* a file name...
 +
    // This assumes the file is in the working directory (== with our executable)
 +
    if ( aFileName.indexOf('.') == 0 || aFileName.indexOf( SAL_PATHDELIMITER ) < 0 )
 +
    {
 +
        // Get the working directory of the current process as a file URL.
 +
        OUString uWorkingDirURL;
 +
        if ( osl_getProcessWorkingDir( &uWorkingDirURL.pData ) != osl_Process_E_None )
 +
        {
 +
            OSL_ASSERT(sal_False);
 +
        }
 +
        // Convert the path relative to the working directory
 +
        // into an full qualified file URL.
 +
        if ( osl::FileBase::getAbsoluteFileURL( uWorkingDirURL, aFileName, uFileNameUrl)
 +
                != osl::FileBase::E_None )
 +
        {
 +
            OSL_ASSERT(sal_False);
 +
        }
 +
    }
 +
    // If the file name is a system path...
 +
    else
 +
    {
 +
        // Convert the system dependend path into a file URL
 +
        if ( osl::FileBase::getFileURLFromSystemPath(aFileName, uFileNameUrl)
 +
                != osl::FileBase::E_None )
 +
        {
 +
            OSL_ASSERT(sal_False);
 +
        }
 +
    }
 +
    return uFileNameUrl;
 +
}
 +
 +
// -----------------------------------------------------------------------
 +
 +
sal_Bool TestContextMenuReaderWriter(const Reference<XComponentContext>& rContext, const OUString sIn, const OUString sOut )
 +
{
 +
    sal_Bool bOk = sal_False;
 +
 +
    try
 +
    {
 +
        Reference < css::ucb::XSimpleFileAccess > xSFA(
 +
            rContext->getServiceManager()->createInstanceWithContext(
 +
                OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.ucb.SimpleFileAccess" ) ), rContext ), UNO_QUERY_THROW );
 +
 +
        OUString sInURL  = ConvertToURL(sIn);
 +
        OUString sOutURL = ConvertToURL(sOut);
 +
 +
        Reference<css::io::XInputStream> xInput = xSFA->openFileRead(sInURL);
 +
        Reference<css::io::XOutputStream> xOutput = xSFA->openFileWrite(sOutURL);
 +
 +
        Reference<css::container::XIndexContainer> xItemContainer( static_cast<cppu::OWeakObject*>(new framework::RootItemContainer()), UNO_QUERY );
 +
 +
        bOk = framework::ContextMenuConfiguration::LoadContextMenu(
 +
                  Reference<XMultiServiceFactory>(rContext->getServiceManager(),UNO_QUERY),
 +
                  xInput,
 +
                  xItemContainer );
 +
 +
        if (bOk && xItemContainer->hasElements() )
 +
        {
 +
            fprintf(stdout,"Could read XML file\n");
 +
 +
            bOk = framework::ContextMenuConfiguration::StoreContextMenu(
 +
                      Reference<XMultiServiceFactory>(rContext->getServiceManager(), UNO_QUERY),
 +
                      xOutput,
 +
                      Reference<css::container::XIndexAccess>(xItemContainer, UNO_QUERY) );
 +
 +
            if (bOk)
 +
                fprintf(stdout,"Could write XML file\n");
 +
        }
 +
 +
        xInput->closeInput();
 +
        xOutput->closeOutput();
 +
    }
 +
    catch (...)
 +
    {
 +
        bOk = sal_False;
 +
    }
 +
 +
    return bOk;
 +
}
 +
 +
// -----------------------------------------------------------------------
 +
 +
bool GetCommandOption( const ::std::vector< OUString >& rArgs, const OUString& rSwitch, OUString& rParam )
 +
{
 +
    bool        bRet = false;
 +
    OUString    aSwitch( OUString::createFromAscii( "-" ));
 +
 +
    aSwitch += rSwitch;
 +
    for ( int i = 0, nCount = rArgs.size(); ( i < nCount ) && !bRet; i++ )
 +
    {
 +
        for ( int n = 0; ( n < 2 ) && !bRet; n++ )
 +
        {
 +
            if ( aSwitch.equalsIgnoreAsciiCase( rArgs[ i ] ))
 +
            {
 +
                bRet = true;
 +
 +
                if ( i < ( nCount - 1 ) )
 +
                    rParam = rArgs[ i + 1 ];
 +
                else
 +
                    rParam = OUString();
 +
            }
 +
        }
 +
    }
 +
 +
    return bRet;
 +
}
 +
 +
// -----------------------------------------------------------------------
 +
 +
void ShowUsage()
 +
{
 +
    fprintf( stderr, "Usage: contextmenu -i input_file -o output_file\n" );
 +
    fprintf( stderr, "Options:" );
 +
    fprintf( stderr, "  -i            name of XML file that should be processed\n" );
 +
    fprintf( stderr, "  -o            name of file, XML output should be written to\n" );
 +
    fprintf( stderr, "Examples:\n" );
 +
    fprintf( stderr, "    contextmenu -i contextmenu.xml -o contextmenu-out.xml\n" );
 +
}
 +
 +
// -----------------------------------------------------------------------
 +
 +
// Forward declaration
 +
void Main(com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext > xContext);
 +
 +
// -----------------------------------------------------------------------
 +
 +
SAL_IMPLEMENT_MAIN()
 +
{
 +
    Reference< XMultiServiceFactory > xMS;
 +
 +
    Reference< XComponentContext > xComponentContext = ::cppu::bootstrap();
 +
    xMS = Reference< XMultiServiceFactory >( xComponentContext->getServiceManager() , UNO_QUERY );
 +
 +
    InitVCL( xMS );
 +
    ::Main(xComponentContext);
 +
    DeInitVCL();
 +
 +
    return 0;
 +
}
 +
 +
void Main(Reference< XComponentContext > rContext)
 +
{
 +
    ::std::vector< OUString > aArgs;
 +
    sal_uInt32 nCmds = osl_getCommandArgCount();
 +
 +
    if ( nCmds == 4 )
 +
    {
 +
        for ( sal_uInt32 i = 0; i < nCmds; i++ )
 +
        {
 +
            OUString aArg;
 +
            osl_getCommandArg( i, &aArg.pData );
 +
            aArgs.push_back( aArg );
 +
        }
 +
 +
        OUString aInput;
 +
        OUString aOutput;
 +
 +
        GetCommandOption( aArgs, OUString::createFromAscii( "i" ), aInput );
 +
        GetCommandOption( aArgs, OUString::createFromAscii( "o" ), aOutput );
 +
 +
        if ( aInput.getLength() > 0 && aOutput.getLength() > 0 )
 +
        {
 +
            TestContextMenuReaderWriter(rContext, aInput, aOutput);
 +
        }
 +
        else
 +
        {
 +
            ShowUsage();
 +
            exit( -1 );
 +
        }
 +
    }
 +
    else
 +
    {
 +
        ShowUsage();
 +
        exit( -1 );
 +
    }
 +
}
 +
 +
</source>

Revision as of 01:48, 20 August 2010

Tasks

  • Write the code
  • Test it

MenuBar Reader/Writer

Because of similarities between a context menu and a menu bar (they both are implemented as VCL Menus), the context menu reader/writer will be based on the menu bar reader/writer:

Test

Created a small application to test the new functionality. Test is located on framework/workben/xml

/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2008 by Sun Microsystems, Inc.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * $RCSfile: cmduicollector.cxx,v $
 * $Revision: 1.16 $
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/
 
// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_framework.hxx"
 
#include <sal/main.h>
#include <sal/config.h>
#include <osl/file.hxx>
#include <osl/process.h>
#include <rtl/ustring.hxx>
#include <cppuhelper/bootstrap.hxx>
 
#include <vcl/svapp.hxx>
 
#include <uielement/rootitemcontainer.hxx>
#include <xml/contextmenuconfiguration.hxx>
 
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/lang/XComponent.hpp>
#include <com/sun/star/ucb/XSimpleFileAccess.hpp>
 
#include <vector>
 
// -----------------------------------------------------------------------
 
using namespace rtl;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::lang;
 
namespace css = ::com::sun::star;
 
// -----------------------------------------------------------------------
 
const OUString ConvertToURL(const OUString& aFileName)
{
    OUString uFileNameUrl;
 
    // If aFileName is *only* a file name...
    // This assumes the file is in the working directory (== with our executable)
    if ( aFileName.indexOf('.') == 0 || aFileName.indexOf( SAL_PATHDELIMITER ) < 0 )
    {
        // Get the working directory of the current process as a file URL.
        OUString uWorkingDirURL;
        if ( osl_getProcessWorkingDir( &uWorkingDirURL.pData ) != osl_Process_E_None )
        {
            OSL_ASSERT(sal_False);
        }
        // Convert the path relative to the working directory
        // into an full qualified file URL.
        if ( osl::FileBase::getAbsoluteFileURL( uWorkingDirURL, aFileName, uFileNameUrl)
                != osl::FileBase::E_None )
        {
            OSL_ASSERT(sal_False);
        }
    }
    // If the file name is a system path...
    else
    {
        // Convert the system dependend path into a file URL
        if ( osl::FileBase::getFileURLFromSystemPath(aFileName, uFileNameUrl)
                != osl::FileBase::E_None )
        {
            OSL_ASSERT(sal_False);
        }
    }
    return uFileNameUrl;
}
 
// -----------------------------------------------------------------------
 
sal_Bool TestContextMenuReaderWriter(const Reference<XComponentContext>& rContext, const OUString sIn, const OUString sOut )
{
    sal_Bool bOk = sal_False;
 
    try
    {
        Reference < css::ucb::XSimpleFileAccess > xSFA(
            rContext->getServiceManager()->createInstanceWithContext(
                OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.ucb.SimpleFileAccess" ) ), rContext ), UNO_QUERY_THROW );
 
        OUString sInURL  = ConvertToURL(sIn);
        OUString sOutURL = ConvertToURL(sOut);
 
        Reference<css::io::XInputStream> xInput = xSFA->openFileRead(sInURL);
        Reference<css::io::XOutputStream> xOutput = xSFA->openFileWrite(sOutURL);
 
        Reference<css::container::XIndexContainer> xItemContainer( static_cast<cppu::OWeakObject*>(new framework::RootItemContainer()), UNO_QUERY );
 
        bOk = framework::ContextMenuConfiguration::LoadContextMenu(
                  Reference<XMultiServiceFactory>(rContext->getServiceManager(),UNO_QUERY),
                  xInput,
                  xItemContainer );
 
        if (bOk && xItemContainer->hasElements() )
        {
            fprintf(stdout,"Could read XML file\n");
 
            bOk = framework::ContextMenuConfiguration::StoreContextMenu(
                      Reference<XMultiServiceFactory>(rContext->getServiceManager(), UNO_QUERY),
                      xOutput,
                      Reference<css::container::XIndexAccess>(xItemContainer, UNO_QUERY) );
 
            if (bOk)
                fprintf(stdout,"Could write XML file\n");
        }
 
        xInput->closeInput();
        xOutput->closeOutput();
    }
    catch (...)
    {
        bOk = sal_False;
    }
 
    return bOk;
}
 
// -----------------------------------------------------------------------
 
bool GetCommandOption( const ::std::vector< OUString >& rArgs, const OUString& rSwitch, OUString& rParam )
{
    bool        bRet = false;
    OUString    aSwitch( OUString::createFromAscii( "-" ));
 
    aSwitch += rSwitch;
    for ( int i = 0, nCount = rArgs.size(); ( i < nCount ) && !bRet; i++ )
    {
        for ( int n = 0; ( n < 2 ) && !bRet; n++ )
        {
            if ( aSwitch.equalsIgnoreAsciiCase( rArgs[ i ] ))
            {
                bRet = true;
 
                if ( i < ( nCount - 1 ) )
                    rParam = rArgs[ i + 1 ];
                else
                    rParam = OUString();
            }
        }
    }
 
    return bRet;
}
 
// -----------------------------------------------------------------------
 
void ShowUsage()
{
    fprintf( stderr, "Usage: contextmenu -i input_file -o output_file\n" );
    fprintf( stderr, "Options:" );
    fprintf( stderr, "   -i            name of XML file that should be processed\n" );
    fprintf( stderr, "   -o            name of file, XML output should be written to\n" );
    fprintf( stderr, "Examples:\n" );
    fprintf( stderr, "    contextmenu -i contextmenu.xml -o contextmenu-out.xml\n" );
}
 
// -----------------------------------------------------------------------
 
// Forward declaration
void Main(com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext > xContext);
 
// -----------------------------------------------------------------------
 
SAL_IMPLEMENT_MAIN()
{
    Reference< XMultiServiceFactory > xMS;
 
    Reference< XComponentContext > xComponentContext = ::cppu::bootstrap();
    xMS = Reference< XMultiServiceFactory >( xComponentContext->getServiceManager() , UNO_QUERY );
 
    InitVCL( xMS );
    ::Main(xComponentContext);
    DeInitVCL();
 
    return 0;
}
 
void Main(Reference< XComponentContext > rContext)
{
    ::std::vector< OUString > aArgs;
    sal_uInt32 nCmds = osl_getCommandArgCount();
 
    if ( nCmds == 4 )
    {
        for ( sal_uInt32 i = 0; i < nCmds; i++ )
        {
            OUString aArg;
            osl_getCommandArg( i, &aArg.pData );
            aArgs.push_back( aArg );
        }
 
        OUString aInput;
        OUString aOutput;
 
        GetCommandOption( aArgs, OUString::createFromAscii( "i" ), aInput );
        GetCommandOption( aArgs, OUString::createFromAscii( "o" ), aOutput );
 
        if ( aInput.getLength() > 0 && aOutput.getLength() > 0 )
        {
            TestContextMenuReaderWriter(rContext, aInput, aOutput);
        }
        else
        {
            ShowUsage();
            exit( -1 );
        }
    }
    else
    {
        ShowUsage();
        exit( -1 );
    }
}
Personal tools