diff --git a/scripting/java/com/sun/star/script/framework/container/Parcel.java b/scripting/java/com/sun/star/script/framework/container/Parcel.java index a5efef271874..3a9d6689804c 100644 --- a/scripting/java/com/sun/star/script/framework/container/Parcel.java +++ b/scripting/java/com/sun/star/script/framework/container/Parcel.java @@ -1,35 +1,119 @@ +/************************************************************************* + * + * $RCSfile: Parcel.java,v $ + * + * $Revision: 1.4 $ + * + * last change: $Author: hr $ $Date: 2004-07-23 13:58:15 $ + * + * The Contents of this file are made available subject to the terms of + * either of the following licenses + * + * - GNU Lesser 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 com.sun.star.script.framework.container; import com.sun.star.script.framework.log.*; +import com.sun.star.script.framework.io.*; import com.sun.star.script.framework.browse.*; +import com.sun.star.script.framework.provider.PathUtils; import com.sun.star.container.*; import com.sun.star.uno.Type; +import com.sun.star.uno.UnoRuntime; import com.sun.star.lang.*; +import com.sun.star.io.*; import com.sun.star.frame.*; + +import com.sun.star.embed.*; + +import com.sun.star.ucb.XSimpleFileAccess; +import com.sun.star.ucb.XSimpleFileAccess2; import java.io.*; import java.util.*; import java.net.*; -public abstract class Parcel implements XNameContainer +public class Parcel implements XNameContainer { - final private String[] macroList = { "user", "share", "document" }; protected ParcelDescriptor parcel; protected String name; protected ParcelContainer parent; + protected XSimpleFileAccess m_xSFA; + public Parcel( XSimpleFileAccess xSFA, ParcelContainer parent, ParcelDescriptor desc, String parcelName ) + { + this( parent, desc, parcelName ); + this.m_xSFA = xSFA; + } + public Parcel( ParcelContainer parent, ParcelDescriptor desc, String parcelName ) { this.parent = parent; this.parcel = desc; this.name = parcelName; } + + /** + * Tests if this Parcel is in an UNO package + * or within a sub package within an UNO package + * + * @return true if has parent false otherwise + */ + public boolean isUnoPkg() { return parent.isUnoPkg(); } + public String getName() { return name; } public java.lang.Object getByName( String aName ) throws com.sun.star.container.NoSuchElementException, com.sun.star.lang.WrappedTargetException { - LogUtils.DEBUG("** Parcel.getByName"); + LogUtils.DEBUG("** Parcel.getByName for " + aName ); ScriptEntry script = null; try { @@ -49,24 +133,19 @@ public abstract class Parcel implements XNameContainer } } } + // catch unknown or un-checked exceptions catch ( Exception e ) { throw new com.sun.star.lang.WrappedTargetException( e.toString() ); } if ( script == null ) { + LogUtils.DEBUG("No script for " + aName ); throw new com.sun.star.container.NoSuchElementException("No script named " + aName ); } ScriptMetaData data = new ScriptMetaData( this, script, null ); - if ( !parcel.getLanguage().equals("Java") ) - { - // TODO better seperate the ScriptMetaData Object - // so notion of having source file is better represented - // maybe needs an entry/attribute in parcel-desc.xml - LogUtils.DEBUG("**** attempting to load source " ); - data.loadSource(); - } + LogUtils.DEBUG("returning date for " + aName ); return data; } public String[] getElementNames() @@ -138,10 +217,8 @@ public abstract class Parcel implements XNameContainer ScriptEntry script = (ScriptEntry)getByName( aName ); if ( script != null ) { - // TODO needs to remove source file ( if there is one ) - parcel.removeScriptEntry( script ); + //parcel.removeScriptEntry( script ); // TODO needs to create source file ( if there is one ) - parcel.addScriptEntry( script ); //parcel.write(); } else @@ -162,6 +239,8 @@ public abstract class Parcel implements XNameContainer // create public void insertByName( String aName, java.lang.Object aElement ) throws com.sun.star.lang.IllegalArgumentException, ElementExistException, com.sun.star.lang.WrappedTargetException { + String pathToParcel = getPathToParcel(); + String pathToParcelUrl = PathUtils.make_url( pathToParcel , "parcel-descriptor.xml" ); // TODO check the type of aElement and throw# // if not appropriate try @@ -180,13 +259,44 @@ public abstract class Parcel implements XNameContainer } } parcel.addScriptEntry( script ); + + if ( m_xSFA.exists( pathToParcelUrl ) ) + { + LogUtils.DEBUG("Parcel.insertByName() opening stream " + pathToParcelUrl ); + writeParcelDesc( m_xSFA, pathToParcelUrl, parcel ); + } + else + { + String error = new String( pathToParcelUrl + " does not exist " ); + throw new com.sun.star.lang.WrappedTargetException( error ); + } } catch ( Exception e ) { + LogUtils.DEBUG("Failed to write entry " + aName + " in " + pathToParcelUrl + " : " + e); throw new com.sun.star.lang.WrappedTargetException( e.toString() ); } + } + + + private void writeParcelDesc( XSimpleFileAccess m_xSFA, String pathToParcelDesc, ParcelDescriptor pd ) throws com.sun.star.ucb.CommandAbortedException, com.sun.star.io.IOException, com.sun.star.uno.Exception, java.io.IOException + { + XSimpleFileAccess2 xSFA2 = ( XSimpleFileAccess2 ) + UnoRuntime.queryInterface( XSimpleFileAccess2.class, m_xSFA ); + if ( xSFA2 != null ) + { + LogUtils.DEBUG("writeParcelDesc2() Using XSIMPLEFILEACCESS2 " + pathToParcelDesc ); + ByteArrayOutputStream bos = new ByteArrayOutputStream( 1024 ); + pd.write( bos ); + ByteArrayInputStream bis = new ByteArrayInputStream( bos.toByteArray() ); + XInputStreamImpl xis = new XInputStreamImpl( bis ); + xSFA2.writeFile( pathToParcelDesc, xis ); + bos.close(); + bis.close(); + } } + // delete public void removeByName( String Name ) throws com.sun.star.container.NoSuchElementException, com.sun.star.lang.WrappedTargetException { @@ -195,18 +305,19 @@ public abstract class Parcel implements XNameContainer ScriptMetaData script = null; if ( ( script = (ScriptMetaData)getByName( Name ) ) != null ) { - if ( script.hasSource() ) +// if ( script.hasSource() ) { if ( !script.removeSourceFile() ) { - LogUtils.DEBUG("Failed to remove script" ); + LogUtils.DEBUG("** Parcel.removeByName Failed to remove script " + Name ); throw new com.sun.star.lang.WrappedTargetException("Failed to remove script " + Name ); } - LogUtils.DEBUG("** have remove script source file " + Name ); + LogUtils.DEBUG("** Parcel.removeByName have removed script source file " + Name ); } - LogUtils.DEBUG("about to remove entry from descriptor"); parcel.removeScriptEntry( script ); - LogUtils.DEBUG("removed entry from descriptor"); + String pathToParcelDesc = PathUtils.make_url( getPathToParcel(), "parcel-descriptor.xml" ); + writeParcelDesc( m_xSFA, pathToParcelDesc, parcel ); + } else { @@ -216,6 +327,7 @@ public abstract class Parcel implements XNameContainer } catch ( Exception e ) { + LogUtils.DEBUG("** Parcel.removeByName Exception: " + e ); throw new com.sun.star.lang.WrappedTargetException( e.toString() ); } @@ -225,15 +337,16 @@ public abstract class Parcel implements XNameContainer { this.name = name; } - public ParcelContainer getParent() { return parent; } - protected abstract String getPathToParcel(); - - protected String getParcelLocationMacro() + /** + * Returns the path of this Parcel + * + * @return String path to parcel + */ + public String getPathToParcel() { - return parent.locationMacro; - - + String path = parent.getParcelContainerDir() + "/" + name; + return path; } }