summaryrefslogtreecommitdiff
path: root/scripting/java/com/sun/star/script/framework/io
diff options
context:
space:
mode:
Diffstat (limited to 'scripting/java/com/sun/star/script/framework/io')
-rw-r--r--scripting/java/com/sun/star/script/framework/io/UCBStreamHandler.java281
-rw-r--r--scripting/java/com/sun/star/script/framework/io/XInputStreamImpl.java119
-rw-r--r--scripting/java/com/sun/star/script/framework/io/XInputStreamWrapper.java102
-rw-r--r--scripting/java/com/sun/star/script/framework/io/XOutputStreamWrapper.java128
-rw-r--r--scripting/java/com/sun/star/script/framework/io/XStorageHelper.java278
5 files changed, 908 insertions, 0 deletions
diff --git a/scripting/java/com/sun/star/script/framework/io/UCBStreamHandler.java b/scripting/java/com/sun/star/script/framework/io/UCBStreamHandler.java
new file mode 100644
index 000000000000..6ca6fabff260
--- /dev/null
+++ b/scripting/java/com/sun/star/script/framework/io/UCBStreamHandler.java
@@ -0,0 +1,281 @@
+/*************************************************************************
+*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * Copyright 2000, 2010 Oracle and/or its affiliates.
+ *
+ * OpenOffice.org - a multi-platform office productivity suite
+ *
+ * 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.
+ *
+************************************************************************/
+
+package com.sun.star.script.framework.io;
+
+import java.net.*;
+import java.io.*;
+import java.util.*;
+import java.util.zip.*;
+
+import com.sun.star.uno.XComponentContext;
+import com.sun.star.ucb.XSimpleFileAccess;
+import com.sun.star.uno.UnoRuntime;
+import com.sun.star.lang.XMultiComponentFactory;
+import com.sun.star.io.XInputStream;
+import com.sun.star.io.XOutputStream;
+import com.sun.star.io.XTruncate;
+
+import com.sun.star.script.framework.log.LogUtils;
+import com.sun.star.script.framework.provider.PathUtils;
+
+public class UCBStreamHandler extends URLStreamHandler {
+
+ public final static String separator = "/ucb/";
+
+ private XComponentContext m_xContext = null;
+ private XMultiComponentFactory m_xMultiComponentFactory = null;
+ private XSimpleFileAccess m_xSimpleFileAccess = null;
+ private HashMap m_jarStreamMap = new HashMap(12);
+ public static String m_ucbscheme;
+
+ public UCBStreamHandler( XComponentContext ctxt, String scheme, XSimpleFileAccess xSFA )
+ {
+ LogUtils.DEBUG( "UCBStreamHandler ctor, scheme = " + scheme );
+ this.m_xContext = ctxt;
+ this.m_ucbscheme = scheme;
+ this.m_xSimpleFileAccess = xSFA;
+ }
+
+ public void parseURL(URL url, String spec, int start, int limit) {
+ LogUtils.DEBUG("**XUCBStreamHandler, parseURL: " + url + " spec: " + spec + " start: " + start + " limit: " + limit );
+
+ String file = url.getFile();
+ if (file == null)
+ file = spec.substring(start, limit);
+ else
+ file += spec.substring(start, limit);
+
+ LogUtils.DEBUG("**For scheme = " + m_ucbscheme );
+ LogUtils.DEBUG("**Setting path = " + file );
+ setURL(url, m_ucbscheme, null, -1, null, null, file, null, null);
+ }
+
+ public URLConnection openConnection(URL u) throws IOException {
+ return new UCBConnection(u);
+ }
+
+ private class UCBConnection extends URLConnection {
+
+ public UCBConnection(URL url) {
+ super(url);
+ }
+
+ public void connect() {
+ }
+
+ public InputStream getInputStream() throws IOException {
+ LogUtils.DEBUG("UCBConnectionHandler GetInputStream on " + url );
+ String sUrl = url.toString();
+ if (sUrl.lastIndexOf(separator) == -1) {
+ LogUtils.DEBUG("getInputStream straight file load" );
+ return getFileStreamFromUCB(sUrl);
+ }
+ else {
+ String path = sUrl.substring(0, sUrl.lastIndexOf(separator) );
+ String file = sUrl.substring(
+ sUrl.lastIndexOf(separator) + separator.length());
+ LogUtils.DEBUG("getInputStream, load of file from another file eg. " + file + " from " + path );
+ return getUCBStream(file, path);
+ }
+ }
+ public OutputStream getOutputStream() throws IOException {
+ LogUtils.DEBUG("UCBConnectionHandler getOutputStream on " + url );
+ OutputStream os = null;
+ try
+ {
+ String sUrl = url.toString();
+ if ( !( sUrl.lastIndexOf(separator) == -1) ) {
+ String path = sUrl.substring(0, sUrl.lastIndexOf(separator));
+ String file = sUrl.substring(
+ sUrl.lastIndexOf(separator) + separator.length());
+
+ if ( m_xSimpleFileAccess.isReadOnly( path ) )
+ {
+ throw new java.io.IOException("File is read only");
+ }
+
+ LogUtils.DEBUG("getOutputStream, create o/p stream for file eg. " + path );
+
+ // we will only deal with simple file write
+ XOutputStream xos = m_xSimpleFileAccess.openFileWrite( path );
+ XTruncate xtrunc = ( XTruncate ) UnoRuntime.queryInterface( XTruncate.class, xos );
+ if ( xtrunc != null )
+ {
+ xtrunc.truncate();
+ }
+ os = new XOutputStreamWrapper( xos );
+ }
+ if ( os == null )
+ {
+ throw new IOException("Failed to get OutputStream for " + sUrl );
+ }
+ }
+ catch ( com.sun.star.ucb.CommandAbortedException cae )
+ {
+ LogUtils.DEBUG("caught exception: " + cae.toString() + " getting writable stream from " + url );
+ throw new IOException( cae.toString() );
+ }
+ catch ( com.sun.star.uno.Exception e )
+ {
+ LogUtils.DEBUG("caught unknown exception: " + e.toString() + " getting writable stream from " + url );
+ throw new IOException( e.toString() );
+ }
+ return os;
+ }
+ }
+
+
+ private InputStream getUCBStream(String file, String path)
+ throws IOException
+ {
+ InputStream is = null;
+ InputStream result = null;
+
+ try {
+ if (path.endsWith(".jar")) {
+ is = (InputStream)m_jarStreamMap.get(path);
+
+ if (is == null) {
+ is = getFileStreamFromUCB(path);
+ m_jarStreamMap.put(path, is);
+ }
+ else {
+ try {
+ is.reset();
+ }
+ catch (IOException e) {
+ is.close();
+ is = getFileStreamFromUCB(path);
+ m_jarStreamMap.put(path, is);
+ }
+ }
+ result = getFileStreamFromJarStream(file, is);
+ }
+ else
+ {
+ String fileUrl = PathUtils.make_url(path,file);
+ result = getFileStreamFromUCB(fileUrl);
+ }
+ }
+ finally {
+ if (is != null) {
+ try {
+ is.close();
+ }
+ catch (IOException ioe) {
+ LogUtils.DEBUG("Caught exception closing stream: " +
+ ioe.getMessage());
+ }
+ }
+ }
+ return result;
+ }
+
+ private InputStream getFileStreamFromJarStream(String file, InputStream is)
+ throws IOException
+ {
+ ZipInputStream zis = null;
+ ZipEntry entry = null;
+ boolean found = false;
+
+ zis = new ZipInputStream(is);
+
+ while (zis.available() != 0) {
+ entry = (ZipEntry)zis.getNextEntry();
+
+ if (entry.getName().equals(file)) {
+ return zis;
+ }
+ }
+ return null;
+ }
+
+ private InputStream getFileStreamFromUCB(String path)
+ throws IOException
+ {
+ InputStream result = null;
+ XInputStream xInputStream = null;
+
+ try {
+ LogUtils.DEBUG("Trying to read from " + path );
+ xInputStream = m_xSimpleFileAccess.openFileRead(path);
+ LogUtils.DEBUG("sfa appeared to read file " );
+ byte[][] inputBytes = new byte[1][];
+
+ int ln = 0;
+ int sz = m_xSimpleFileAccess.getSize(path);
+ // TODO don't depend on result of available() or size()
+ // just read stream 'till complete
+ if ( sz == 0 )
+ {
+ if ( xInputStream.available() > 0 )
+ {
+ sz = xInputStream.available();
+ }
+ }
+ LogUtils.DEBUG("size of file " + path + " is " + sz );
+ LogUtils.DEBUG("available = " + xInputStream.available() );
+ inputBytes[0] = new byte[sz];
+
+ ln = xInputStream.readBytes(inputBytes, sz);
+
+ if (ln != sz) {
+ throw new IOException(
+ "Failed to read " + sz + " bytes from XInputStream");
+ }
+
+ result = new ByteArrayInputStream(inputBytes[0]);
+ }
+ catch (com.sun.star.io.IOException ioe) {
+ LogUtils.DEBUG("caught exception " + ioe );
+ throw new IOException(ioe.getMessage());
+ }
+ catch (com.sun.star.uno.Exception e) {
+ LogUtils.DEBUG("caught exception " + e );
+ throw new IOException(e.getMessage());
+ }
+ finally
+ {
+ if (xInputStream != null) {
+ try {
+ xInputStream.closeInput();
+ }
+ catch (Exception e2) {
+ LogUtils.DEBUG(
+ "Error closing XInputStream:" + e2.getMessage());
+ }
+ }
+ }
+ return result;
+ }
+
+ private String convertClassNameToFileName(String name) {
+ return name.replace('.', File.separatorChar) + ".class";
+ }
+
+}
diff --git a/scripting/java/com/sun/star/script/framework/io/XInputStreamImpl.java b/scripting/java/com/sun/star/script/framework/io/XInputStreamImpl.java
new file mode 100644
index 000000000000..c83c030662a4
--- /dev/null
+++ b/scripting/java/com/sun/star/script/framework/io/XInputStreamImpl.java
@@ -0,0 +1,119 @@
+/*************************************************************************
+ *
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * Copyright 2000, 2010 Oracle and/or its affiliates.
+ *
+ * OpenOffice.org - a multi-platform office productivity suite
+ *
+ * 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.
+ *
+ ************************************************************************/
+
+package com.sun.star.script.framework.io;
+
+import com.sun.star.io.XInputStream;
+import java.io.InputStream;
+import java.io.IOException;
+
+public class XInputStreamImpl implements XInputStream
+{
+ InputStream is;
+ public XInputStreamImpl( InputStream is )
+ {
+ this.is = is;
+ }
+
+ public int readBytes( /*OUT*/byte[][] aData, /*IN*/int nBytesToRead ) throws com.sun.star.io.NotConnectedException, com.sun.star.io.BufferSizeExceededException, com.sun.star.io.IOException
+ {
+ aData[ 0 ] = new byte[ nBytesToRead ];
+
+ int totalBytesRead = 0;
+
+ try
+ {
+ int bytesRead = 0;
+ while ( ( bytesRead = is.read( aData[ 0 ], totalBytesRead, nBytesToRead ) ) > 0 && ( totalBytesRead < nBytesToRead ) )
+ {
+ totalBytesRead += bytesRead;
+ nBytesToRead -= bytesRead;
+ }
+ }
+ catch ( IOException e )
+ {
+ throw new com.sun.star.io.IOException( e.toString() );
+ }
+ catch ( IndexOutOfBoundsException aie )
+ {
+ throw new com.sun.star.io.BufferSizeExceededException( aie.toString() );
+ }
+ return totalBytesRead;
+ }
+
+ public int readSomeBytes( /*OUT*/byte[][] aData, /*IN*/int nMaxBytesToRead ) throws com.sun.star.io.NotConnectedException, com.sun.star.io.BufferSizeExceededException, com.sun.star.io.IOException
+ {
+ int bytesToRead = nMaxBytesToRead;
+ int availableBytes = available();
+ if ( availableBytes < nMaxBytesToRead )
+ {
+ bytesToRead = availableBytes;
+ }
+ int read = readBytes( aData, bytesToRead );
+ return read;
+ }
+
+ public void skipBytes( /*IN*/int nBytesToSkip ) throws com.sun.star.io.NotConnectedException, com.sun.star.io.BufferSizeExceededException, com.sun.star.io.IOException
+ {
+ long bytesSkipped = 0;
+ try
+ {
+ bytesSkipped = is.skip( (long)nBytesToSkip );
+ }
+ catch ( IOException e )
+ {
+ throw new com.sun.star.io.IOException( e.toString() );
+ }
+ }
+
+ public int available( ) throws com.sun.star.io.NotConnectedException, com.sun.star.io.IOException
+ {
+ int bytesAvail = 0;
+ try
+ {
+ bytesAvail = is.available();
+ }
+ catch ( IOException e )
+ {
+ throw new com.sun.star.io.IOException( e.toString() );
+ }
+ return bytesAvail;
+ }
+
+ public void closeInput( ) throws com.sun.star.io.NotConnectedException, com.sun.star.io.IOException
+ {
+ try
+ {
+ is.close();
+ }
+ catch( IOException e )
+ {
+ throw new com.sun.star.io.IOException( e.toString() );
+ }
+ }
+
+}
diff --git a/scripting/java/com/sun/star/script/framework/io/XInputStreamWrapper.java b/scripting/java/com/sun/star/script/framework/io/XInputStreamWrapper.java
new file mode 100644
index 000000000000..cfc1b8489739
--- /dev/null
+++ b/scripting/java/com/sun/star/script/framework/io/XInputStreamWrapper.java
@@ -0,0 +1,102 @@
+/*************************************************************************
+ *
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * Copyright 2000, 2010 Oracle and/or its affiliates.
+ *
+ * OpenOffice.org - a multi-platform office productivity suite
+ *
+ * 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.
+ *
+ ************************************************************************/
+package com.sun.star.script.framework.io;
+
+import java.io.InputStream;
+import com.sun.star.io.XInputStream;
+
+public class XInputStreamWrapper extends InputStream {
+ private XInputStream m_xInputStream;
+
+ public XInputStreamWrapper(XInputStream xInputStream) {
+ m_xInputStream = xInputStream;
+ }
+
+ public int read() throws java.io.IOException
+ {
+ byte[][] byteRet = new byte[1][0];
+ long numRead;
+
+ try {
+ numRead = m_xInputStream.readBytes(byteRet, 1);
+ }
+ catch (com.sun.star.io.IOException ioe) {
+ throw new java.io.IOException(ioe.getMessage());
+ }
+
+ if (numRead != 1) {
+ return -1;
+ }
+ return byteRet[0][0];
+ }
+
+ public int read( byte[] b ) throws java.io.IOException
+ {
+ byte[][] byteRet = new byte[1][];
+ byteRet[0] = b;
+ try
+ {
+ return m_xInputStream.readBytes( byteRet, b.length );
+ }
+ catch ( com.sun.star.io.IOException ioe)
+ {
+ throw new java.io.IOException(ioe.getMessage());
+ }
+ }
+
+ public long skip(long n) throws java.io.IOException
+ {
+ try {
+ m_xInputStream.skipBytes((int)n);
+ return n;
+ }
+ catch (com.sun.star.io.IOException ioe) {
+ throw new java.io.IOException(ioe.getMessage());
+ }
+ }
+
+ public int available() throws java.io.IOException
+ {
+ try {
+ return m_xInputStream.available();
+ }
+ catch (com.sun.star.io.IOException ioe) {
+ throw new java.io.IOException(ioe.getMessage());
+ }
+ }
+
+ public void close() throws java.io.IOException
+ {
+ try {
+ m_xInputStream.closeInput();
+ }
+ catch (com.sun.star.io.IOException ioe) {
+ throw new java.io.IOException(ioe.getMessage());
+ }
+ }
+
+}
diff --git a/scripting/java/com/sun/star/script/framework/io/XOutputStreamWrapper.java b/scripting/java/com/sun/star/script/framework/io/XOutputStreamWrapper.java
new file mode 100644
index 000000000000..dc2c7233708d
--- /dev/null
+++ b/scripting/java/com/sun/star/script/framework/io/XOutputStreamWrapper.java
@@ -0,0 +1,128 @@
+/*************************************************************************
+ *
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * Copyright 2000, 2010 Oracle and/or its affiliates.
+ *
+ * OpenOffice.org - a multi-platform office productivity suite
+ *
+ * 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.
+ *
+ ************************************************************************/
+package com.sun.star.script.framework.io;
+import java.io.*;
+import com.sun.star.io.XOutputStream;
+
+
+public class XOutputStreamWrapper extends OutputStream {
+ XOutputStream m_xOutputStream;
+ public XOutputStreamWrapper(XOutputStream xOs ) {
+ this.m_xOutputStream = xOs;
+ }
+ public void write(int b)
+ throws java.io.IOException
+ {
+ if ( m_xOutputStream == null )
+ {
+ throw new java.io.IOException("Stream is null");
+ }
+ byte[] bytes = new byte[1];
+ bytes[0] = (byte) b;
+ try
+ {
+ m_xOutputStream.writeBytes( bytes );
+ }
+ catch ( com.sun.star.io.IOException ioe )
+ {
+ throw new java.io.IOException(ioe.getMessage());
+ }
+ }
+ public void write(byte[] b)
+ throws java.io.IOException
+ {
+
+ if ( m_xOutputStream == null )
+ {
+ throw new java.io.IOException( "Stream is null" );
+ }
+ try
+ {
+ m_xOutputStream.writeBytes( b );
+ }
+ catch ( com.sun.star.io.IOException ioe )
+ {
+ throw new java.io.IOException(ioe.getMessage());
+ }
+ }
+ public void write( byte[] b, int off, int len )
+ throws java.io.IOException
+ {
+ if ( m_xOutputStream == null )
+ {
+ throw new java.io.IOException( "Stream is null" );
+ }
+ byte[] bytes = new byte[len];
+ for ( int i=off; i< off+len; i++ )
+ {
+ bytes[i] = b[i];
+ }
+ try
+ {
+ m_xOutputStream.writeBytes(bytes);
+ }
+ catch ( com.sun.star.io.IOException ioe )
+ {
+ throw new java.io.IOException(ioe.getMessage());
+ }
+ }
+
+ public void flush()
+ throws java.io.IOException
+ {
+ if ( m_xOutputStream == null )
+ {
+ throw new java.io.IOException( "Stream is null" );
+ }
+ try
+ {
+ m_xOutputStream.flush();
+ }
+ catch ( com.sun.star.io.IOException ioe )
+ {
+ throw new java.io.IOException(ioe.getMessage());
+ }
+ }
+ public void close()
+ throws java.io.IOException
+ {
+ if ( m_xOutputStream == null )
+ {
+ throw new java.io.IOException( "Stream is null" );
+ }
+ try
+ {
+ m_xOutputStream.closeOutput();
+ }
+ catch ( com.sun.star.io.IOException ioe )
+ {
+ throw new java.io.IOException(ioe.getMessage());
+ }
+ }
+
+ }
+
diff --git a/scripting/java/com/sun/star/script/framework/io/XStorageHelper.java b/scripting/java/com/sun/star/script/framework/io/XStorageHelper.java
new file mode 100644
index 000000000000..7043438bc87e
--- /dev/null
+++ b/scripting/java/com/sun/star/script/framework/io/XStorageHelper.java
@@ -0,0 +1,278 @@
+/*************************************************************************
+ *
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * Copyright 2000, 2010 Oracle and/or its affiliates.
+ *
+ * OpenOffice.org - a multi-platform office productivity suite
+ *
+ * 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.
+ *
+ ************************************************************************/
+
+package com.sun.star.script.framework.io;
+
+import com.sun.star.frame.XModel;
+
+import com.sun.star.container.XNameAccess;
+
+import com.sun.star.uno.XInterface;
+import com.sun.star.uno.UnoRuntime;
+
+import com.sun.star.lang.XComponent;
+
+import com.sun.star.uno.AnyConverter;
+
+import com.sun.star.io.XStream;
+import com.sun.star.io.XInputStream;
+import com.sun.star.io.XOutputStream;
+
+import com.sun.star.embed.XStorage;
+import com.sun.star.embed.XTransactedObject;
+
+import com.sun.star.document.XDocumentSubStorageSupplier;
+
+import com.sun.star.beans.XPropertySet;
+
+import com.sun.star.lang.XEventListener;
+import com.sun.star.lang.EventObject;
+
+import com.sun.star.script.framework.log.LogUtils;
+import com.sun.star.script.framework.provider.PathUtils;
+
+import java.util.*;
+import java.io.*;
+
+
+public class XStorageHelper implements XEventListener
+{
+ XStorage[] xStorages;
+ XStream xStream;
+ XInputStream xIs = null;
+ XOutputStream xOs = null;
+ static Map modelMap = new HashMap();
+ XModel xModel = null;
+ private static XStorageHelper listener = new XStorageHelper();
+
+ private XStorageHelper() {}
+ public XStorageHelper( String path, int mode, boolean create ) throws IOException
+ {
+ String modelUrl = null;
+ int indexOfScriptsDir = path.lastIndexOf( "Scripts" );
+ if ( indexOfScriptsDir > -1 )
+ {
+ modelUrl = path.substring( 0, indexOfScriptsDir - 1 );
+ path = path.substring( indexOfScriptsDir, path.length());
+ }
+
+ LogUtils.DEBUG("XStorageHelper ctor, path: " + path);
+ this.xModel = getModelForURL( modelUrl );
+
+ try
+ {
+ StringTokenizer tokens = new StringTokenizer(path, "/");
+
+ if (tokens.countTokens() == 0)
+ {
+ throw new IOException("Invalid path");
+ }
+ XDocumentSubStorageSupplier xDocumentSubStorageSupplier =
+ (XDocumentSubStorageSupplier) UnoRuntime.queryInterface(
+ XDocumentSubStorageSupplier.class, xModel);
+ xStorages = new XStorage[tokens.countTokens() ];
+ LogUtils.DEBUG("XStorageHelper ctor, path chunks length: " + xStorages.length );
+
+ for ( int i = 0; i < xStorages.length; i++ )
+ {
+ LogUtils.DEBUG("XStorageHelper, processing index " + i );
+ String name = tokens.nextToken();
+ LogUtils.DEBUG("XStorageHelper, getting: " + name);
+ XStorage storage = null;
+ if ( i == 0 )
+ {
+ storage = xDocumentSubStorageSupplier.getDocumentSubStorage( name, mode );
+ if ( storage == null )
+ {
+ LogUtils.DEBUG("** boo hoo Storage is null " );
+ }
+ XPropertySet xProps = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,storage );
+ if ( xProps != null )
+ {
+ String mediaType = AnyConverter.toString( xProps.getPropertyValue( "MediaType" ) );
+ LogUtils.DEBUG("***** media type is " + mediaType );
+ if ( !mediaType.equals("scripts") )
+ {
+ xProps.setPropertyValue("MediaType","scripts");
+ }
+ }
+ }
+ else
+ {
+ XNameAccess xNameAccess = (XNameAccess)
+ UnoRuntime.queryInterface(XNameAccess.class, xStorages[i-1]);
+ if (xNameAccess == null )
+ {
+ disposeObject();
+ throw new IOException("No name access " + name);
+ }
+ else if ( !xNameAccess.hasByName(name) || !xStorages[i-1].isStorageElement(name) )
+ {
+ if ( !create )
+ {
+ disposeObject();
+ throw new IOException("No subdir: " + name);
+ }
+ else
+ {
+ // attempt to create new storage
+ LogUtils.DEBUG("Attempt to create new storage for " + name );
+ }
+ }
+
+ storage = xStorages[i-1].openStorageElement(
+ name, mode );
+ }
+ if ( storage == null )
+ {
+ disposeObject();
+ throw new IOException("storage not found: " + name);
+ }
+ xStorages[ i ] = storage;
+
+ }
+ }
+ catch ( com.sun.star.io.IOException ioe)
+ {
+ disposeObject();
+ }
+ catch (com.sun.star.uno.Exception e)
+ {
+ disposeObject();
+ throw new IOException(e.getMessage());
+ }
+ }
+
+ public synchronized static void addNewModel( XModel model )
+ {
+ // TODO needs to cater for model for untitled document
+ modelMap.put( PathUtils.getOidForModel( model ), model );
+ XComponent xComp = (XComponent)
+ UnoRuntime.queryInterface(XComponent.class, model);
+
+ if ( xComp != null )
+ {
+ try
+ {
+ xComp.addEventListener( listener );
+ }
+ catch ( Exception e )
+ {
+ // What TODO here ?
+ LogUtils.DEBUG( LogUtils.getTrace( e ) );
+ }
+ }
+ }
+
+ public void disposing( EventObject Source )
+ {
+ XModel model = (XModel)
+ UnoRuntime.queryInterface(XModel.class,Source.Source );
+
+ if ( model != null )
+ {
+ LogUtils.DEBUG(" Disposing doc " + model.getURL() );
+ Object result = modelMap.remove( model );
+ result = null;
+ }
+ }
+ public XStorage getStorage()
+ {
+ return xStorages[ xStorages.length - 1 ];
+ }
+ public XModel getModel()
+ {
+ return xModel;
+ }
+ public void disposeObject()
+ {
+ disposeObject( false );
+ }
+ public void disposeObject( boolean shouldCommit )
+ {
+ LogUtils.DEBUG("In disposeObject");
+
+ for ( int i = xStorages.length -1 ; i > -1; i-- )
+ {
+ LogUtils.DEBUG("In disposeObject disposing storage " + i );
+ try
+ {
+ XStorage xStorage = xStorages[i];
+ if ( shouldCommit )
+ {
+ commit(xStorage);
+ }
+ disposeObject(xStorage);
+ LogUtils.DEBUG("In disposeObject disposed storage " + i );
+ }
+ catch( Exception ignore )
+ {
+ LogUtils.DEBUG("Exception disposing storage " + i );
+ }
+
+ }
+
+ }
+ static public void disposeObject( XInterface xInterface )
+ {
+ if (xInterface == null) {
+ return;
+ }
+
+ XComponent xComponent = (XComponent)
+ UnoRuntime.queryInterface(XComponent.class, xInterface);
+
+ if (xComponent == null) {
+ return;
+ }
+ xComponent.dispose();
+ }
+ static public void commit( XInterface xInterface )
+ {
+ XTransactedObject xTrans = (XTransactedObject)
+ UnoRuntime.queryInterface(XTransactedObject.class, xInterface);
+ if ( xTrans != null )
+ {
+ try
+ {
+ xTrans.commit();
+ }
+ catch ( Exception e )
+ {
+ LogUtils.DEBUG("Something went bellyup exception: " + e );
+ }
+ }
+ }
+
+ public XModel getModelForURL( String url )
+ {
+ //TODO does not cater for untitled documents
+ return (XModel)modelMap.get( url );
+ }
+
+}
+