INTEGRATION: CWS sdk02 (1.1.2); FILE ADDED

2003/05/09 11:54:15 jsc 1.1.2.1: #109045# insert new and remove example zip file
This commit is contained in:
Vladimir Glazounov 2003-06-10 09:33:39 +00:00
parent afe777963d
commit 8d5d10d317

View file

@ -0,0 +1,253 @@
/*************************************************************************
*
* $RCSfile: FilterOptions.java,v $
*
* $Revision: 1.2 $
*
* last change: $Author: vg $ $Date: 2003-06-10 10:33:39 $
*
* The Contents of this file are made available subject to the terms of
* either of the following licenses
*
* - GNU Lesser cp General Public License Version 2.1
* - Sun Industry Standards Source License Version 1.1
*
* Sun Microsystems Inc., October, 2000
*
* GNU Lesser General Public License Version 2.1
* =============================================
* Copyright 2000 by Sun Microsystems, Inc.
* 901 San Antonio Road, Palo Alto, CA 94303, USA
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software Foundation.
*
* This library 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 for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
*
* Sun Industry Standards Source License Version 1.1
* =================================================
* The contents of this file are subject to the Sun Industry Standards
* Source License Version 1.1 (the "License"); You may not use this file
* except in compliance with the License. You may obtain a copy of the
* License at http://www.openoffice.org/license.html.
*
* Software provided under this License is provided on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
* WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
* MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
* See the License for the specific provisions governing your rights and
* obligations concerning the Software.
*
* The Initial Developer of the Original Code is: Sun Microsystems, Inc.
*
* Copyright: 2000 by Sun Microsystems, Inc.
*
* All Rights Reserved.
*
* Contributor(s): _______________________________________
*
*
************************************************************************/
package OfficeDev.samples.Filter;
import com.sun.star.uno.AnyConverter;
import com.sun.star.beans.PropertyValue;
import com.sun.star.io.XInputStream;
import com.sun.star.io.XOutputStream;
import com.sun.star.ucb.XSimpleFileAccess;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.lang.XMultiServiceFactory;
import com.sun.star.uno.Exception;
import com.sun.star.ucb.CommandAbortedException;
import com.sun.star.lang.IllegalArgumentException;
/*-************************************************************************
@title helper to analyze neccessary option properties of our filter
@description Our filter needs some neccessary properties for working:
- a stream for input or output
- or an URL for creating such streams
- informations about required action on filtering
@attention This class mustn't be threadsafe - because instances of it
are used temp. only - not as members. So no concurrent access
should occure.
Another reason: It wuold be very difficult to safe every
access on our internal member. To do so - we must implement
special methods instead of allowing pure member access.
************************************************************************-*/
public class FilterOptions
{
//_____________________________________
// public member to provide these options to our outside filter class
public com.sun.star.io.XInputStream m_xInput ;
public com.sun.star.io.XOutputStream m_xOutput ;
public boolean m_bStreamOwner ;
public String m_sURL ;
public String m_sOld ;
public String m_sNew ;
public boolean m_bCaseChange ;
public boolean m_bLower ;
//_____________________________________
// private member for internal things
private com.sun.star.lang.XMultiServiceFactory m_xSMGR ;
//_____________________________________
// interface
/**
* creates a new instance of this class
* It use the given MediaDescriptor to find right
* properties for initialization of the internal members.
* To do so it use another interface method analyze()
* which can be used after creation of an object instance
* to set a new descriptor here.
*
* @param xSMGR
* we need it to create special help service top open
* streams in case they are not already a part of given
* MediaDescriptor
*
* @param bImport
* we must know which stream member should be valid initialized
*
* @param lDescriptor
* the initial MediaDescriptor to set internal member from it
*/
public FilterOptions( com.sun.star.lang.XMultiServiceFactory xSMGR ,
boolean bImport ,
com.sun.star.beans.PropertyValue[] lDescriptor )
{
m_xSMGR = xSMGR;
analyze(bImport, lDescriptor);
}
/**
* analyze given MediaDescriptor to find values for our internal member
* It reset all members to defaults before - to prevent us against
* mixed descriptor values!
*
* @param bImport
* we must know which stream member should be valid initialized
*
* @param lDescriptor
* the new MediaDescriptor to set internal member from it
*/
public void analyze( boolean bImport ,
com.sun.star.beans.PropertyValue[] lDescriptor )
{
m_xInput = null ;
m_xOutput = null ;
m_bStreamOwner = false ;
m_sURL = null ;
m_sOld = new String();
m_sNew = new String();
m_bCaseChange = false ;
m_bLower = false ;
for ( int i=0; i<lDescriptor.length; ++i )
{
try
{
if (lDescriptor[i].Name.equals("FileName"))
m_sURL = AnyConverter.toString(lDescriptor[i].Value);
else
if (lDescriptor[i].Name.equals("InputStream"))
m_xInput = (com.sun.star.io.XInputStream)AnyConverter.toObject(new com.sun.star.uno.Type(com.sun.star.io.XInputStream.class), lDescriptor[i].Value);
else
if (lDescriptor[i].Name.equals("OutputStream"))
m_xOutput = (com.sun.star.io.XOutputStream)AnyConverter.toObject(new com.sun.star.uno.Type(com.sun.star.io.XOutputStream.class), lDescriptor[i].Value);
else
if (lDescriptor[i].Name.equals("FilterData"))
{
com.sun.star.beans.PropertyValue[] lFilterProps = (com.sun.star.beans.PropertyValue[])AnyConverter.toArray(lDescriptor[i].Value);
int nCount = lFilterProps.length;
for (int p=0; p<nCount; ++p)
{
if (lFilterProps[p].Name.equals("OldString"))
m_sOld = AnyConverter.toString(lFilterProps[p].Value);
else
if (lFilterProps[p].Name.equals("NewString"))
m_sNew = AnyConverter.toString(lFilterProps[p].Value);
else
if (lFilterProps[p].Name.equals("LowerCase"))
{
m_bLower = AnyConverter.toBoolean(lFilterProps[p].Value);
m_bCaseChange = true; // Set it after m_bLower - because an exception can occure and we must use default values then!
}
}
}
}
catch(com.sun.star.lang.IllegalArgumentException exConvert)
{
// ONE argument has the wrong type
// But I think we mustn't react here - because we setted
// default values for every neccessary item we need.
// In case this exception occures - this default exist
// and we can live with it.
}
}
// Decide if it's neccessary AND possible to open streams.
// Outside user can check for valid FilterOptions by using
// corresponding method isValid(). So it's not neccessary to
// handle this error here in any case.
if (m_xInput==null && m_xOutput==null && m_sURL!=null)
impl_openStreams(bImport);
}
/**
* with this method it's possible for the outside filter to decide
* if he can use this FilterOptions realy or not.
* That means especialy if neccessary streams are available or not.
*/
public boolean isValid()
{
return(m_xInput!=null || m_xOutput!=null);
}
//_____________________________________
// helper
/**
* In case we couldn't found any valid stream inside the given MediaDescriptor,
* we must create it. Then we use a special helper service in combination
* with an existing URL to open a stream for reading or writing. It depends
* from given parameter bImport.
*
* Note: This method doesn't check for a valid URL. It must be done before.
*
* @param bImport
* inidcates which stream member must be valid as result of this call
*/
private void impl_openStreams( boolean bImport )
{
try{
com.sun.star.ucb.XSimpleFileAccess xHelper = (com.sun.star.ucb.XSimpleFileAccess)UnoRuntime.queryInterface(
com.sun.star.ucb.XSimpleFileAccess.class,
m_xSMGR.createInstance("com.sun.star.ucb.SimpleFileAccess"));
if (xHelper!=null)
{
if (bImport==true)
m_xInput = xHelper.openFileRead(m_sURL);
else
m_xOutput = xHelper.openFileWrite(m_sURL);
}
m_bStreamOwner = (m_xInput!=null || m_xOutput!=null);
}
catch(com.sun.star.ucb.CommandAbortedException exAborted) {}
catch(com.sun.star.uno.Exception exUno ) {}
}
}