Initial checkin of org.openoffice.idesupport java package for shared
classes supporting Office Scripting in Java in various IDEs
This commit is contained in:
82
scripting/java/org/openoffice/idesupport/LocalOffice.java
Normal file
82
scripting/java/org/openoffice/idesupport/LocalOffice.java
Normal file
@@ -0,0 +1,82 @@
|
||||
|
||||
package org.openoffice.idesupport;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.ConnectException;
|
||||
import java.util.Vector;
|
||||
|
||||
/**
|
||||
* LocalOffice represents a connection to the local office.
|
||||
*
|
||||
* This class allows to get access to some scripting framework
|
||||
* releated functionality of the locally running office. The
|
||||
* office has to be started with options appropriate for establishing
|
||||
* local connection.
|
||||
*
|
||||
* @author misha <misha@openoffice.org>
|
||||
*/
|
||||
public class LocalOffice
|
||||
{
|
||||
/**
|
||||
* Creates an instance of the local office connection.
|
||||
*
|
||||
* @param parent is an application specific class loader.
|
||||
* @param officePath is a platform specific path string
|
||||
* to the office distribution.
|
||||
* @param port is a communication port.
|
||||
*/
|
||||
public static final LocalOffice create(
|
||||
ClassLoader parent, String officePath, int port)
|
||||
{
|
||||
Vector path = new Vector();
|
||||
path.addElement(officePath + "/program/classes/ridl.jar");
|
||||
path.addElement(officePath + "/program/classes/jurt.jar");
|
||||
path.addElement(officePath + "/program/classes/sandbox.jar");
|
||||
path.addElement(officePath + "/program/classes/unoil.jar");
|
||||
path.addElement(officePath + "/program/classes/juh.jar");
|
||||
path.addElement(System.getProperties().getProperty("netbeans.home") +
|
||||
File.separator + "modules" +
|
||||
File.separator + "ext" +
|
||||
File.separator + "localoffice.jar");
|
||||
ClassLoader appcl = new DefaultScriptClassLoader(parent, path);
|
||||
Class clazz = null;
|
||||
LocalOffice office = null;
|
||||
try {
|
||||
clazz = appcl.loadClass(
|
||||
"org.openoffice.idesupport.localoffice.LocalOfficeImpl");
|
||||
office = (LocalOffice)clazz.newInstance();
|
||||
office.connect(officePath, port);
|
||||
} catch (java.lang.Exception exp) {
|
||||
office = null;
|
||||
}
|
||||
return office;
|
||||
}
|
||||
|
||||
/**
|
||||
* Connects to the running office.
|
||||
*
|
||||
* @param officePath is a platform specific path string
|
||||
* to the office distribution.
|
||||
* @param port is a communication port.
|
||||
*/
|
||||
protected void connect(String officePath, int port)
|
||||
throws ConnectException
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes the connection to the running office.
|
||||
*/
|
||||
public void disconnect()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Refresh the script storage.
|
||||
*
|
||||
* @param uri is an identifier of storage has to be refreshed.
|
||||
*/
|
||||
public void refreshStorage(String uri)
|
||||
{
|
||||
}
|
||||
}
|
114
scripting/java/org/openoffice/idesupport/OfficeDocument.java
Normal file
114
scripting/java/org/openoffice/idesupport/OfficeDocument.java
Normal file
@@ -0,0 +1,114 @@
|
||||
package org.openoffice.idesupport;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.zip.*;
|
||||
import java.util.Enumeration;
|
||||
import java.util.StringTokenizer;
|
||||
import java.beans.PropertyVetoException;
|
||||
import javax.naming.InvalidNameException;
|
||||
|
||||
import org.openoffice.idesupport.filter.FileFilter;
|
||||
import org.openoffice.idesupport.filter.BinaryOnlyFilter;
|
||||
import org.openoffice.idesupport.zip.ParcelZipper;
|
||||
|
||||
public class OfficeDocument
|
||||
{
|
||||
public static final String PARCEL_PREFIX_DIR = "Scripts/java/";
|
||||
public static final String OFFICE_EXTENSIONS = "sxc,sxw";
|
||||
public static final String ARCHIVE_TAG = "[PARCEL_FILE]";
|
||||
|
||||
private static ParcelZipper zipper = ParcelZipper.getParcelZipper();
|
||||
private File officeFile = null;
|
||||
private String parcelName = null;
|
||||
private String extension = null;
|
||||
|
||||
public OfficeDocument(File officeFile) throws InvalidNameException
|
||||
{
|
||||
this.officeFile = officeFile;
|
||||
if( !checkIfOfficeDocument() )
|
||||
{
|
||||
throw new InvalidNameException("This is not a valid StarOffice document.");
|
||||
}
|
||||
}
|
||||
|
||||
private boolean checkIfOfficeDocument()
|
||||
{
|
||||
if( officeFile.isDirectory() )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
String tmpName = officeFile.getName();
|
||||
if( tmpName.lastIndexOf(".") == 0 )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
this.extension = tmpName.substring(tmpName.lastIndexOf(".")+1);
|
||||
if( (OFFICE_EXTENSIONS.indexOf(extension)==-1) )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
this.parcelName = tmpName.substring(0,tmpName.lastIndexOf("."));
|
||||
return true;
|
||||
}
|
||||
|
||||
public Enumeration getParcels()
|
||||
{
|
||||
java.util.Vector parcelEntries = new java.util.Vector();
|
||||
try
|
||||
{
|
||||
ZipFile zp = new ZipFile(this.officeFile);
|
||||
|
||||
for (Enumeration officeEntries = zp.entries(); officeEntries.hasMoreElements(); )
|
||||
{
|
||||
ZipEntry ze = (ZipEntry)officeEntries.nextElement();
|
||||
if (ze.getName().endsWith(ParcelZipper.PARCEL_DESCRIPTOR_XML))
|
||||
{
|
||||
String tmp = ze.getName();
|
||||
int end = tmp.lastIndexOf("/");
|
||||
tmp = tmp.substring(0, end);
|
||||
int start = tmp.lastIndexOf("/") + 1;
|
||||
|
||||
String parcelName = ARCHIVE_TAG +
|
||||
ze.getName().substring(start, end);
|
||||
parcelEntries.add(parcelName);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch(ZipException ze)
|
||||
{
|
||||
ze.printStackTrace();
|
||||
}
|
||||
catch(IOException ioe)
|
||||
{
|
||||
ioe.printStackTrace();
|
||||
}
|
||||
return parcelEntries.elements();
|
||||
}
|
||||
|
||||
public String getParcelNameFromEntry(String parcelName)
|
||||
{
|
||||
return parcelName.substring(PARCEL_PREFIX_DIR.length(), parcelName.length()-1);
|
||||
}
|
||||
|
||||
public String getParcelEntryFromName(String parcelName)
|
||||
{
|
||||
return parcelName.substring(ARCHIVE_TAG.length()) + "/";
|
||||
}
|
||||
|
||||
public boolean removeParcel(String parcelName)
|
||||
{
|
||||
try {
|
||||
ParcelZipper.getParcelZipper().unzipToZipExceptParcel(this.officeFile, getParcelEntryFromName(parcelName));
|
||||
}
|
||||
catch (IOException ioe) {
|
||||
ioe.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public String unzipOneParcel(String parcelName)
|
||||
{
|
||||
return new String("location");
|
||||
}
|
||||
}
|
138
scripting/java/org/openoffice/idesupport/SVersionRCFile.java
Normal file
138
scripting/java/org/openoffice/idesupport/SVersionRCFile.java
Normal file
@@ -0,0 +1,138 @@
|
||||
package org.openoffice.idesupport;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
public class SVersionRCFile {
|
||||
|
||||
public static final String DEFAULT_NAME =
|
||||
System.getProperty("os.name").startsWith("Windows") == true ?
|
||||
System.getProperty("user.home") + File.separator +
|
||||
"Application Data" + File.separator + "sversion.ini" :
|
||||
System.getProperty("user.home") + File.separator +
|
||||
".sversionrc";
|
||||
|
||||
private static final String VERSIONS_LINE = "[Versions]";
|
||||
private static final String UNOILJAR = "skip_registration" + File.separator + "unoil.jar";
|
||||
private static final String UNOPACKAGEDIR = File.separator + "user" +
|
||||
File.separator + "uno_packages" +
|
||||
File.separator + "cache" +
|
||||
File.separator + "uno_packages";
|
||||
/* Make sure this is in LowerCase !!!!! */
|
||||
private static final String SCRIPTF = "scriptf";
|
||||
private static final String FILE_URL_PREFIX = "file://";
|
||||
private File file = null;
|
||||
|
||||
public SVersionRCFile() {
|
||||
this(DEFAULT_NAME);
|
||||
}
|
||||
|
||||
public SVersionRCFile(String name) {
|
||||
file = new File(name);
|
||||
}
|
||||
|
||||
public Hashtable getVersions() throws IOException {
|
||||
BufferedReader br;
|
||||
|
||||
try {
|
||||
br = new BufferedReader(new FileReader(file));
|
||||
}
|
||||
catch (FileNotFoundException fnfe) {
|
||||
throw new IOException(fnfe.getMessage());
|
||||
}
|
||||
|
||||
Hashtable versions = load(br);
|
||||
br.close();
|
||||
return versions;
|
||||
}
|
||||
|
||||
private Hashtable load(BufferedReader br) throws IOException {
|
||||
Hashtable versions = new Hashtable();
|
||||
String s;
|
||||
|
||||
while ((s = br.readLine()) != null &&
|
||||
(s.equals(VERSIONS_LINE)) != true);
|
||||
|
||||
while ((s = br.readLine()) != null &&
|
||||
(s.equals("")) != true) {
|
||||
StringTokenizer tokens = new StringTokenizer(s, "=");
|
||||
int count = tokens.countTokens();
|
||||
|
||||
if (count != 2)
|
||||
continue;
|
||||
|
||||
String name = tokens.nextToken();
|
||||
String path = tokens.nextToken();
|
||||
if (path.startsWith(FILE_URL_PREFIX) == true)
|
||||
path = path.substring(FILE_URL_PREFIX.length());
|
||||
|
||||
if (isValidPath(path) == true)
|
||||
versions.put(name, path);
|
||||
}
|
||||
return versions;
|
||||
}
|
||||
|
||||
private boolean isValidPath(String path) {
|
||||
// System.out.println("Testing: " + path);
|
||||
File file = new File(path + File.separator + "program");
|
||||
if (file.exists())
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
public static String getPathForUnoil(String officeInstall)
|
||||
{
|
||||
File unopkgdir = new File(officeInstall, UNOPACKAGEDIR);
|
||||
if(!unopkgdir.exists())
|
||||
{
|
||||
return null;
|
||||
}
|
||||
File scriptf = null;
|
||||
String[] listunopkg = unopkgdir.list();
|
||||
int size = listunopkg.length;
|
||||
for(int i=0; i<size; i++)
|
||||
{
|
||||
if (listunopkg[i].toLowerCase().indexOf(SCRIPTF)>-1)
|
||||
{
|
||||
scriptf = new File(unopkgdir, listunopkg[i]);
|
||||
}
|
||||
}
|
||||
if(scriptf != null)
|
||||
{
|
||||
File unoil = new File(scriptf, UNOILJAR);
|
||||
if(unoil.exists())
|
||||
{
|
||||
String path = unoil.getParent();
|
||||
path = path.substring(path.indexOf(UNOPACKAGEDIR));
|
||||
return officeInstall + path;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/* public static void main(String[] args) {
|
||||
SVersionRCFile ov;
|
||||
Hashtable versions;
|
||||
|
||||
if (args.length == 0)
|
||||
ov = new SVersionRCFile();
|
||||
else
|
||||
ov = new SVersionRCFile(args[0]);
|
||||
|
||||
try {
|
||||
versions = ov.getVersions();
|
||||
}
|
||||
catch (IOException ioe) {
|
||||
System.err.println("Error getting versions: " + ioe.getMessage());
|
||||
return;
|
||||
}
|
||||
|
||||
Enumeration enum = versions.keys();
|
||||
|
||||
while (enum.hasMoreElements()) {
|
||||
String name = (String)enum.nextElement();
|
||||
System.out.println("Name: " + name + ", Path: " +
|
||||
(String)versions.get(name));
|
||||
}
|
||||
} */
|
||||
}
|
@@ -0,0 +1,20 @@
|
||||
package org.openoffice.idesupport.filter;
|
||||
|
||||
public class AllFilesFilter implements FileFilter {
|
||||
private static final AllFilesFilter filter = new AllFilesFilter();
|
||||
|
||||
private AllFilesFilter() {
|
||||
}
|
||||
|
||||
public static AllFilesFilter getInstance() {
|
||||
return filter;
|
||||
}
|
||||
|
||||
public boolean validate(String name) {
|
||||
return true;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return "<all files>";
|
||||
}
|
||||
}
|
@@ -0,0 +1,30 @@
|
||||
package org.openoffice.idesupport.filter;
|
||||
|
||||
public class BinaryOnlyFilter implements FileFilter {
|
||||
private static final String[] EXTENSIONS = {".class", ".jar"};
|
||||
private static final String DESCRIPTION = "Binaries Only";
|
||||
private static final BinaryOnlyFilter filter = new BinaryOnlyFilter();
|
||||
|
||||
private BinaryOnlyFilter() {
|
||||
}
|
||||
|
||||
public static BinaryOnlyFilter getInstance() {
|
||||
return filter;
|
||||
}
|
||||
public boolean validate(String name) {
|
||||
for (int i = 0; i < EXTENSIONS.length; i++)
|
||||
if (name.endsWith(EXTENSIONS[i]))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer buf = new StringBuffer(DESCRIPTION + ": ");
|
||||
|
||||
for (int i = 0; i < EXTENSIONS.length - 1; i++)
|
||||
buf.append("<" + EXTENSIONS[i] + "> ");
|
||||
buf.append("<" + EXTENSIONS[EXTENSIONS.length - 1] + ">");
|
||||
|
||||
return buf.toString();
|
||||
}
|
||||
}
|
@@ -0,0 +1,32 @@
|
||||
package org.openoffice.idesupport.filter;
|
||||
|
||||
public class ExceptParcelFilter implements FileFilter {
|
||||
private static final String DESCRIPTION = "Remove specified Parcel";
|
||||
private static final ExceptParcelFilter filter = new ExceptParcelFilter();
|
||||
private static String parcelName = null;
|
||||
|
||||
private ExceptParcelFilter() {
|
||||
}
|
||||
|
||||
public void setParcelToRemove(String parcelName)
|
||||
{
|
||||
this.parcelName = parcelName;
|
||||
}
|
||||
|
||||
public static ExceptParcelFilter getInstance() {
|
||||
return filter;
|
||||
}
|
||||
public boolean validate(String name) {
|
||||
if (name.startsWith(this.parcelName))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer buf = new StringBuffer(DESCRIPTION + ": ");
|
||||
|
||||
buf.append("<" + this.parcelName + ">");
|
||||
|
||||
return buf.toString();
|
||||
}
|
||||
}
|
@@ -0,0 +1,5 @@
|
||||
package org.openoffice.idesupport.filter;
|
||||
|
||||
public interface FileFilter {
|
||||
public boolean validate(String name);
|
||||
}
|
@@ -0,0 +1,132 @@
|
||||
|
||||
package org.openoffice.idesupport.localoffice;
|
||||
|
||||
import java.net.ConnectException;
|
||||
|
||||
import com.sun.star.lang.XMultiServiceFactory;
|
||||
import com.sun.star.lang.XMultiComponentFactory;
|
||||
import com.sun.star.lang.XComponent;
|
||||
import com.sun.star.bridge.XUnoUrlResolver;
|
||||
import com.sun.star.beans.XPropertySet;
|
||||
import com.sun.star.comp.helper.Bootstrap;
|
||||
import com.sun.star.uno.XComponentContext;
|
||||
import com.sun.star.uno.UnoRuntime;
|
||||
import com.sun.star.uno.Exception;
|
||||
|
||||
import drafts.com.sun.star.script.framework.storage.XScriptStorageManager;
|
||||
|
||||
import org.openoffice.idesupport.LocalOffice;
|
||||
|
||||
/**
|
||||
* LocalOfficeImpl represents a connection to the local office.
|
||||
*
|
||||
* This class is an implementation of LocalOffice ane allows to
|
||||
* get access to some scripting framework releated functionality
|
||||
* of the locally running office. The office has to be started
|
||||
* with options appropriate for establishing local connection.
|
||||
*
|
||||
* @author misha <misha@openoffice.org>
|
||||
*/
|
||||
public final class LocalOfficeImpl
|
||||
extends LocalOffice
|
||||
{
|
||||
private final static String STORAGE_MRG_SINGLETON =
|
||||
"/singletons/drafts.com.sun.star.script.framework.storage.theScriptStorageManager";
|
||||
|
||||
private transient String mOfficePath;
|
||||
private transient XMultiComponentFactory mComponentFactory;
|
||||
private transient XComponentContext mComponentContext;
|
||||
private transient XMultiServiceFactory mServiceFactory;
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
public LocalOfficeImpl()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Connects to the running office.
|
||||
*
|
||||
* @param officePath is a platform specific path string
|
||||
* to the office distribution.
|
||||
* @param port is a communication port.
|
||||
*/
|
||||
protected void connect(String officePath, int port)
|
||||
throws ConnectException
|
||||
{
|
||||
mOfficePath = officePath;
|
||||
try {
|
||||
bootstrap(port);
|
||||
} catch (java.lang.Exception ex) {
|
||||
throw new ConnectException(ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Refresh the script storage.
|
||||
*
|
||||
* @param uri is an identifier of storage has to be refreshed.
|
||||
*/
|
||||
public void refreshStorage(String uri)
|
||||
{
|
||||
try {
|
||||
Object object = null;
|
||||
object = mComponentContext.getValueByName(STORAGE_MRG_SINGLETON);
|
||||
XScriptStorageManager storageMgr;
|
||||
storageMgr = (XScriptStorageManager)UnoRuntime.queryInterface(
|
||||
XScriptStorageManager.class, object);
|
||||
storageMgr.refreshScriptStorage(uri);
|
||||
} catch (java.lang.Exception ex) {
|
||||
System.out.println("*** LocalOfficeImpl.refreshStorage: FAILED " + ex.getMessage());
|
||||
System.out.println("*** LocalOfficeImpl.refreshStorage: FAILED " + ex.getClass().getName());
|
||||
}
|
||||
System.out.println("*** LocalOfficeImpl.refreshStorage: DONE");
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes the connection to the running office.
|
||||
*/
|
||||
public void disconnect()
|
||||
{
|
||||
/*
|
||||
if(mComponentFactory != null) {
|
||||
XComponent comp = (XComponent)UnoRuntime.queryInterface(
|
||||
XComponent.class, mComponentFactory);
|
||||
comp.dispose();
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
/**
|
||||
* Boot straps UNO.
|
||||
*
|
||||
* The office has to be started with following string:
|
||||
* "-accept=socket,host=localhost,port=<PORT>;urp;StarOffice.ServiceManager"
|
||||
*
|
||||
* @param port is a communication port.
|
||||
*/
|
||||
private void bootstrap(int port)
|
||||
throws java.lang.Exception
|
||||
{
|
||||
Object object;
|
||||
mComponentContext = Bootstrap.createInitialComponentContext(null);
|
||||
mComponentFactory = mComponentContext.getServiceManager();
|
||||
object = mComponentFactory.createInstanceWithContext(
|
||||
"com.sun.star.bridge.UnoUrlResolver", mComponentContext);
|
||||
XUnoUrlResolver urlresolver;
|
||||
urlresolver = (XUnoUrlResolver)UnoRuntime.queryInterface(
|
||||
XUnoUrlResolver.class, object);
|
||||
object = urlresolver.resolve(
|
||||
"uno:socket,host=localhost,port=" +
|
||||
port +
|
||||
";urp;StarOffice.ServiceManager");
|
||||
mComponentFactory = (XMultiComponentFactory)UnoRuntime.queryInterface(
|
||||
XMultiComponentFactory.class, object);
|
||||
XPropertySet factoryProps;
|
||||
factoryProps = (XPropertySet)UnoRuntime.queryInterface(
|
||||
XPropertySet.class, mComponentFactory);
|
||||
object = factoryProps.getPropertyValue("DefaultContext");
|
||||
mComponentContext = (XComponentContext)UnoRuntime.queryInterface(
|
||||
XComponentContext.class, object);
|
||||
}
|
||||
}
|
150
scripting/java/org/openoffice/idesupport/ui/ConfigurePanel.java
Normal file
150
scripting/java/org/openoffice/idesupport/ui/ConfigurePanel.java
Normal file
@@ -0,0 +1,150 @@
|
||||
package org.openoffice.idesupport.ui;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Vector;
|
||||
import java.util.Enumeration;
|
||||
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.AbstractButton;
|
||||
import javax.swing.ImageIcon;
|
||||
import javax.swing.border.LineBorder;
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.GridBagLayout;
|
||||
import java.awt.GridBagConstraints;
|
||||
import java.awt.Insets;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
import org.openoffice.idesupport.xml.ParcelDescriptor;
|
||||
import org.openoffice.idesupport.zip.ParcelZipper;
|
||||
|
||||
public class ConfigurePanel extends JPanel {
|
||||
|
||||
private File basedir;
|
||||
private Vector classpath;
|
||||
|
||||
private MethodPanel methodPanel;
|
||||
private ScriptPanel scriptPanel;
|
||||
private ParcelDescriptor descriptor = ParcelDescriptor.getParcelDescriptor();
|
||||
|
||||
public ConfigurePanel(String basedir, Vector classpath, Document doc) {
|
||||
this.basedir = new File(basedir);
|
||||
this.classpath = classpath;
|
||||
initUI(doc);
|
||||
}
|
||||
|
||||
public Document getConfiguration() throws Exception {
|
||||
Enumeration scripts = scriptPanel.getScriptEntries();
|
||||
return descriptor.generate(scripts);
|
||||
}
|
||||
|
||||
private void initUI(Document doc) {
|
||||
JPanel leftPanel = new JPanel();
|
||||
JPanel methodButtons = initMethodButtons();
|
||||
methodPanel = new MethodPanel(basedir, classpath);
|
||||
leftPanel.setLayout(new BorderLayout());
|
||||
leftPanel.add(methodPanel, BorderLayout.CENTER);
|
||||
|
||||
JPanel rightPanel = new JPanel();
|
||||
JPanel scriptButtons = initScriptButtons();
|
||||
scriptPanel = new ScriptPanel(descriptor.parse(doc));
|
||||
rightPanel.setLayout(new BorderLayout());
|
||||
rightPanel.add(scriptPanel, BorderLayout.CENTER);
|
||||
rightPanel.add(scriptButtons, BorderLayout.SOUTH);
|
||||
|
||||
setLayout(new GridBagLayout());
|
||||
setPreferredSize(new java.awt.Dimension(700, 300));
|
||||
setBorder(LineBorder.createBlackLineBorder());
|
||||
|
||||
GridBagConstraints gbc = new GridBagConstraints();
|
||||
gbc.gridx = 0;
|
||||
gbc.gridy = 0;
|
||||
gbc.fill = java.awt.GridBagConstraints.BOTH;
|
||||
gbc.ipadx = 40;
|
||||
gbc.anchor = java.awt.GridBagConstraints.WEST;
|
||||
gbc.insets = new Insets(10, 5, 5, 5);
|
||||
gbc.weightx = 0.75;
|
||||
add(leftPanel, gbc);
|
||||
|
||||
gbc = new java.awt.GridBagConstraints();
|
||||
gbc.gridx = 1;
|
||||
gbc.gridy = 0;
|
||||
add(methodButtons, gbc);
|
||||
|
||||
gbc = new java.awt.GridBagConstraints();
|
||||
gbc.gridx = 2;
|
||||
gbc.gridy = 0;
|
||||
gbc.gridwidth = java.awt.GridBagConstraints.REMAINDER;
|
||||
gbc.fill = java.awt.GridBagConstraints.BOTH;
|
||||
gbc.anchor = java.awt.GridBagConstraints.EAST;
|
||||
gbc.insets = new Insets(10, 5, 5, 5);
|
||||
gbc.weightx = 1.0;
|
||||
gbc.weighty = 1.0;
|
||||
add(rightPanel, gbc);
|
||||
}
|
||||
|
||||
private JPanel initMethodButtons() {
|
||||
JPanel panel = new JPanel();
|
||||
panel.setLayout(new GridBagLayout());
|
||||
ImageIcon icon = new ImageIcon(getClass().getResource("/org/openoffice/idesupport/ui/add.gif"));
|
||||
JButton addButton = new JButton("Add", icon);
|
||||
addButton.setHorizontalTextPosition(AbstractButton.LEFT);
|
||||
|
||||
addButton.addActionListener(
|
||||
new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
scriptPanel.addScriptEntries(methodPanel.getSelectedEntries());
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
GridBagConstraints gbc = new java.awt.GridBagConstraints();
|
||||
gbc.gridwidth = java.awt.GridBagConstraints.REMAINDER;
|
||||
gbc.fill = java.awt.GridBagConstraints.HORIZONTAL;
|
||||
gbc.insets = new Insets(5, 5, 5, 5);
|
||||
panel.add(addButton, gbc);
|
||||
|
||||
JPanel dummyPanel = new JPanel();
|
||||
gbc = new java.awt.GridBagConstraints();
|
||||
gbc.gridwidth = java.awt.GridBagConstraints.REMAINDER;
|
||||
gbc.gridheight = java.awt.GridBagConstraints.REMAINDER;
|
||||
gbc.fill = java.awt.GridBagConstraints.BOTH;
|
||||
gbc.weightx = 1.0;
|
||||
gbc.weighty = 1.0;
|
||||
panel.add(dummyPanel, gbc);
|
||||
|
||||
return panel;
|
||||
}
|
||||
|
||||
private JPanel initScriptButtons() {
|
||||
JPanel panel = new JPanel();
|
||||
JButton removeButton = new JButton("Remove");
|
||||
JButton removeAllButton = new JButton("Remove All");
|
||||
|
||||
removeButton.addActionListener(
|
||||
new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
scriptPanel.removeSelectedRows();
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
removeAllButton.addActionListener(
|
||||
new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
scriptPanel.removeAllRows();
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
panel.add(removeButton);
|
||||
panel.add(removeAllButton);
|
||||
|
||||
return panel;
|
||||
}
|
||||
}
|
171
scripting/java/org/openoffice/idesupport/ui/MethodPanel.java
Normal file
171
scripting/java/org/openoffice/idesupport/ui/MethodPanel.java
Normal file
@@ -0,0 +1,171 @@
|
||||
package org.openoffice.idesupport.ui;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Vector;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JScrollPane;
|
||||
import javax.swing.JList;
|
||||
import javax.swing.JLabel;
|
||||
import java.awt.BorderLayout;
|
||||
|
||||
import org.openoffice.idesupport.ScriptEntry;
|
||||
import org.openoffice.idesupport.DefaultScriptClassLoader;
|
||||
import org.openoffice.idesupport.zip.ParcelZipper;
|
||||
|
||||
public class MethodPanel extends JPanel {
|
||||
|
||||
private File basedir;
|
||||
private Vector classpath;
|
||||
private final static String FIRST_PARAM = "drafts.com.sun.star.script.framework.XScriptContext";
|
||||
|
||||
private JList list;
|
||||
private Vector values = new Vector(11);
|
||||
|
||||
public MethodPanel(File basedir, Vector classpath) {
|
||||
this.basedir = basedir;
|
||||
this.classpath = classpath;
|
||||
initValues();
|
||||
initUI();
|
||||
}
|
||||
|
||||
public ScriptEntry[] getSelectedEntries() {
|
||||
Object[] selections = list.getSelectedValues();
|
||||
ScriptEntry[] entries = new ScriptEntry[selections.length];
|
||||
|
||||
for (int i = 0; i < selections.length; i++) {
|
||||
entries[i] = (ScriptEntry)selections[i];
|
||||
}
|
||||
|
||||
return entries;
|
||||
}
|
||||
|
||||
private void initUI() {
|
||||
JLabel label = new JLabel("Available Methods:");
|
||||
list = new JList(values);
|
||||
JScrollPane pane = new JScrollPane(list);
|
||||
label.setLabelFor(pane);
|
||||
|
||||
BorderLayout layout = new BorderLayout();
|
||||
setLayout(layout);
|
||||
layout.setVgap(5);
|
||||
|
||||
add(label, BorderLayout.NORTH);
|
||||
add(pane, BorderLayout.CENTER);
|
||||
}
|
||||
|
||||
private void initValues() {
|
||||
String[] classNames;
|
||||
String parcelName;
|
||||
|
||||
if (basedir == null || basedir.exists() == false ||
|
||||
basedir.isDirectory() == false)
|
||||
return;
|
||||
|
||||
classNames = findClassNames();
|
||||
if (classNames == null || classNames.length == 0)
|
||||
return;
|
||||
|
||||
parcelName = basedir.getName();
|
||||
if (parcelName.equals(ParcelZipper.CONTENTS_DIRNAME))
|
||||
parcelName = basedir.getParentFile().getName();
|
||||
|
||||
DefaultScriptClassLoader classloader =
|
||||
new DefaultScriptClassLoader(classpath);
|
||||
|
||||
for (int i = 0; i < classNames.length; i++)
|
||||
{
|
||||
try
|
||||
{
|
||||
Class clazz = classloader.loadClass(classNames[i]);
|
||||
Method[] methods = clazz.getDeclaredMethods();
|
||||
for (int k = 0; k < methods.length; k++)
|
||||
{
|
||||
if (Modifier.isPublic(methods[k].getModifiers()))
|
||||
{
|
||||
Class[] params = methods[k].getParameterTypes();
|
||||
if(params.length > 0)
|
||||
{
|
||||
if(params[0].getName().equals(FIRST_PARAM))
|
||||
{
|
||||
ScriptEntry entry =
|
||||
new ScriptEntry(classNames[i] + "." + methods[k].getName(),
|
||||
parcelName);
|
||||
values.addElement(entry);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (ClassNotFoundException e)
|
||||
{
|
||||
System.err.println("Class Not Found Exception...");
|
||||
continue;
|
||||
}
|
||||
catch (NoClassDefFoundError nc)
|
||||
{
|
||||
System.err.println("No Class Definition Found...");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private ArrayList findFiles(File basedir, String suffix) {
|
||||
ArrayList result = new ArrayList();
|
||||
File[] children = basedir.listFiles();
|
||||
|
||||
for (int i = 0; i < children.length; i++) {
|
||||
if (children[i].isDirectory())
|
||||
result.addAll(findFiles(children[i], suffix));
|
||||
else if (children[i].getName().endsWith(suffix))
|
||||
result.add(children[i]);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private String[] findClassNames()
|
||||
{
|
||||
ArrayList classFiles = findFiles(basedir, ".class");
|
||||
if(classFiles == null || classFiles.size() == 0)
|
||||
return null;
|
||||
|
||||
ArrayList javaFiles = findFiles(basedir, ".java");
|
||||
if(javaFiles == null || javaFiles.size() == 0)
|
||||
return null;
|
||||
|
||||
ArrayList result = new ArrayList();
|
||||
for (int i = 0; i < classFiles.size(); i++)
|
||||
{
|
||||
File classFile = (File)classFiles.get(i);
|
||||
String className = classFile.getName();
|
||||
className = className.substring(0, className.lastIndexOf(".class"));
|
||||
boolean finished = false;
|
||||
|
||||
|
||||
for (int j = 0; j < javaFiles.size() && finished == false; j++)
|
||||
{
|
||||
File javaFile = (File)javaFiles.get(j);
|
||||
String javaName = javaFile.getName();
|
||||
javaName = javaName.substring(0, javaName.lastIndexOf(".java"));
|
||||
|
||||
|
||||
if (javaName.equals(className))
|
||||
{
|
||||
String path = classFile.getAbsolutePath();
|
||||
path = path.substring(basedir.getAbsolutePath().length() + 1);
|
||||
path = path.replace(File.separatorChar, '.');
|
||||
path = path.substring(0, path.lastIndexOf(".class"));
|
||||
|
||||
result.add(path);
|
||||
javaFiles.remove(j);
|
||||
finished = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return (String[])result.toArray(new String[0]);
|
||||
}
|
||||
}
|
166
scripting/java/org/openoffice/idesupport/ui/ScriptPanel.java
Normal file
166
scripting/java/org/openoffice/idesupport/ui/ScriptPanel.java
Normal file
@@ -0,0 +1,166 @@
|
||||
package org.openoffice.idesupport.ui;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Vector;
|
||||
import java.util.Enumeration;
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.event.FocusEvent;
|
||||
import java.awt.event.FocusAdapter;
|
||||
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JScrollPane;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JTextField;
|
||||
import javax.swing.JTable;
|
||||
import javax.swing.DefaultCellEditor;
|
||||
import javax.swing.table.TableCellEditor;
|
||||
import javax.swing.table.TableColumn;
|
||||
import javax.swing.table.AbstractTableModel;
|
||||
|
||||
import org.openoffice.idesupport.ScriptEntry;
|
||||
|
||||
public class ScriptPanel extends JPanel {
|
||||
private ScriptTableModel model;
|
||||
private JTable table;
|
||||
private File descriptor;
|
||||
|
||||
public ScriptPanel(ScriptEntry[] scripts) {
|
||||
model = new ScriptTableModel(scripts);
|
||||
initUI();
|
||||
}
|
||||
|
||||
public void addScriptEntries(ScriptEntry[] entries) {
|
||||
for (int i = 0; i < entries.length; i++) {
|
||||
ScriptEntry entry = new ScriptEntry(entries[i].getLanguageName(),
|
||||
entries[i].getLocation());
|
||||
entry.setLogicalName(entries[i].getLanguageName());
|
||||
model.add(entry);
|
||||
}
|
||||
}
|
||||
|
||||
public void removeSelectedRows() {
|
||||
int[] selections = table.getSelectedRows();
|
||||
|
||||
for (int i = selections.length - 1; i >= 0; i--) {
|
||||
model.remove(selections[i]);
|
||||
}
|
||||
}
|
||||
|
||||
public void removeAllRows() {
|
||||
model.removeAll();
|
||||
}
|
||||
|
||||
public Enumeration getScriptEntries() {
|
||||
return model.getScriptEntries();
|
||||
}
|
||||
|
||||
private void initUI() {
|
||||
table = new JTable(model);
|
||||
TableColumn column = table.getColumnModel().getColumn(1);
|
||||
column.setCellEditor(new DefaultCellEditor(new JTextField()));
|
||||
|
||||
table.addFocusListener(new FocusAdapter() {
|
||||
public void focusLost(FocusEvent evt) {
|
||||
tableFocusLost(evt);
|
||||
}
|
||||
});
|
||||
|
||||
JScrollPane pane = new JScrollPane(table);
|
||||
JLabel label = new JLabel("Scripts:");
|
||||
label.setLabelFor(pane);
|
||||
|
||||
BorderLayout layout = new BorderLayout();
|
||||
setLayout(layout);
|
||||
layout.setVgap(5);
|
||||
add(label, BorderLayout.NORTH);
|
||||
add(pane, BorderLayout.CENTER);
|
||||
}
|
||||
|
||||
private void tableFocusLost(FocusEvent evt) {
|
||||
TableCellEditor editor = table.getCellEditor();
|
||||
if (editor != null) {
|
||||
Object value = editor.getCellEditorValue();
|
||||
if (value != null)
|
||||
model.setValueAt(value,
|
||||
table.getEditingRow(), table.getEditingColumn());
|
||||
}
|
||||
}
|
||||
|
||||
private class ScriptTableModel extends AbstractTableModel {
|
||||
final String[] columnNames = {"Exported Method",
|
||||
"Script Name"};
|
||||
|
||||
private Vector scripts;
|
||||
private int nextRow;
|
||||
|
||||
public ScriptTableModel(ScriptEntry[] entries) {
|
||||
scripts = new Vector(entries.length + 11);
|
||||
for (int i = 0; i < entries.length; i++) {
|
||||
scripts.addElement(entries[i]);
|
||||
}
|
||||
nextRow = entries.length;
|
||||
}
|
||||
|
||||
public int getColumnCount() {
|
||||
return columnNames.length;
|
||||
}
|
||||
|
||||
public int getRowCount() {
|
||||
return scripts.size();
|
||||
}
|
||||
|
||||
public String getColumnName(int col) {
|
||||
return columnNames[col];
|
||||
}
|
||||
|
||||
public void add(ScriptEntry entry) {
|
||||
scripts.addElement(entry);
|
||||
fireTableRowsInserted(nextRow, nextRow);
|
||||
nextRow++;
|
||||
}
|
||||
|
||||
public void remove(int row) {
|
||||
scripts.removeElementAt(row);
|
||||
fireTableRowsDeleted(row, row);
|
||||
nextRow--;
|
||||
}
|
||||
|
||||
public void removeAll() {
|
||||
scripts.removeAllElements();
|
||||
fireTableRowsDeleted(0, nextRow);
|
||||
nextRow = 0;
|
||||
}
|
||||
|
||||
public Enumeration getScriptEntries() {
|
||||
return scripts.elements();
|
||||
}
|
||||
|
||||
public Object getValueAt(int row, int col) {
|
||||
String result = "";
|
||||
ScriptEntry entry;
|
||||
|
||||
entry = (ScriptEntry)scripts.elementAt(row);
|
||||
|
||||
if (col == 0)
|
||||
result = entry.getLanguageName();
|
||||
else if (col == 1)
|
||||
result = entry.getLogicalName();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public boolean isCellEditable(int row, int col) {
|
||||
if (col == 0)
|
||||
return false;
|
||||
else
|
||||
return true;
|
||||
}
|
||||
|
||||
public void setValueAt(Object value, int row, int col) {
|
||||
ScriptEntry entry = (ScriptEntry)scripts.elementAt(row);
|
||||
entry.setLogicalName((String)value);
|
||||
fireTableCellUpdated(row, col);
|
||||
}
|
||||
}
|
||||
}
|
BIN
scripting/java/org/openoffice/idesupport/ui/add.gif
Normal file
BIN
scripting/java/org/openoffice/idesupport/ui/add.gif
Normal file
Binary file not shown.
After Width: | Height: | Size: 103 B |
138
scripting/java/org/openoffice/idesupport/xml/Manifest.java
Normal file
138
scripting/java/org/openoffice/idesupport/xml/Manifest.java
Normal file
@@ -0,0 +1,138 @@
|
||||
package org.openoffice.idesupport.xml;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.IOException;
|
||||
|
||||
import java.util.Enumeration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.NodeList;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
public class Manifest {
|
||||
|
||||
private Document document = null;
|
||||
private XMLParser parser = null;
|
||||
private boolean baseElementsExist = false;
|
||||
|
||||
public Manifest(InputStream inputStream, XMLParser parser) {
|
||||
this.parser = parser;
|
||||
document = parser.parse(inputStream);
|
||||
}
|
||||
|
||||
public void add(String entry) {
|
||||
add(entry, "");
|
||||
}
|
||||
|
||||
private void add(String entry, String type) {
|
||||
Element root, el;
|
||||
|
||||
ensureBaseElementsExist();
|
||||
|
||||
try {
|
||||
root = (Element)
|
||||
document.getElementsByTagName("manifest:manifest").item(0);
|
||||
|
||||
el = document.createElement("manifest:file-entry");
|
||||
el.setAttribute("manifest:media-type", type);
|
||||
el.setAttribute("manifest:full-path", entry);
|
||||
// System.out.println("added: " + el.toString());
|
||||
root.appendChild(el);
|
||||
}
|
||||
catch (Exception e) {
|
||||
System.out.println("Error adding entry: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private void ensureBaseElementsExist() {
|
||||
if (baseElementsExist == false) {
|
||||
baseElementsExist = true;
|
||||
add("Scripts/", "application/script-parcel");
|
||||
add("Scripts/java/", "");
|
||||
}
|
||||
}
|
||||
|
||||
public void remove(String entry) {
|
||||
Element root, el;
|
||||
int len;
|
||||
|
||||
try {
|
||||
root = (Element)
|
||||
document.getElementsByTagName("manifest:manifest").item(0);
|
||||
|
||||
NodeList nl = root.getElementsByTagName("manifest:file-entry");
|
||||
if (nl == null || (len = nl.getLength()) == 0)
|
||||
return;
|
||||
|
||||
ArrayList list = new ArrayList();
|
||||
for (int i = 0; i < len; i++) {
|
||||
el = (Element)nl.item(i);
|
||||
if (el.getAttribute("manifest:full-path").startsWith(entry)) {
|
||||
// System.out.println("found: " + el.toString());
|
||||
list.add(el);
|
||||
}
|
||||
}
|
||||
|
||||
Iterator iter = list.iterator();
|
||||
while (iter.hasNext())
|
||||
root.removeChild((Element)iter.next());
|
||||
|
||||
// System.out.println("and after root is: " + root.toString());
|
||||
}
|
||||
catch (Exception e) {
|
||||
System.out.println("Error removing entry: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public InputStream getInputStream() throws IOException {
|
||||
InputStream result = null;
|
||||
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
write(out);
|
||||
result = new ByteArrayInputStream(out.toByteArray());
|
||||
// result = replaceNewlines(out.toByteArray());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private InputStream replaceNewlines(byte[] bytes) throws IOException {
|
||||
InputStream result;
|
||||
ByteArrayOutputStream out;
|
||||
BufferedReader reader;
|
||||
|
||||
reader = new BufferedReader(new InputStreamReader(
|
||||
new ByteArrayInputStream(bytes)));
|
||||
out = new ByteArrayOutputStream();
|
||||
|
||||
int previous = reader.read();
|
||||
out.write(previous);
|
||||
int current;
|
||||
|
||||
while ((current = reader.read()) != -1) {
|
||||
if (((char)current == '\n' || (char)current == ' ') &&
|
||||
(char)previous == '\n')
|
||||
continue;
|
||||
else {
|
||||
out.write(current);
|
||||
previous = current;
|
||||
}
|
||||
}
|
||||
result = new ByteArrayInputStream(out.toByteArray());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public void write(OutputStream out) throws IOException {
|
||||
parser.write(document, out);
|
||||
}
|
||||
}
|
477
scripting/java/org/openoffice/idesupport/zip/ParcelZipper.java
Normal file
477
scripting/java/org/openoffice/idesupport/zip/ParcelZipper.java
Normal file
@@ -0,0 +1,477 @@
|
||||
package org.openoffice.idesupport.zip;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.Enumeration;
|
||||
import java.util.zip.*;
|
||||
import java.beans.PropertyVetoException;
|
||||
|
||||
import org.openoffice.idesupport.filter.FileFilter;
|
||||
import org.openoffice.idesupport.filter.BinaryOnlyFilter;
|
||||
import org.openoffice.idesupport.filter.ExceptParcelFilter;
|
||||
|
||||
import org.openoffice.idesupport.xml.XMLParser;
|
||||
import org.openoffice.idesupport.xml.Manifest;
|
||||
|
||||
public class ParcelZipper
|
||||
{
|
||||
public static final String PARCEL_PREFIX_DIR = "Scripts/java/";
|
||||
public static final String PARCEL_EXTENSION = "sxp";
|
||||
public static final String CONTENTS_DIRNAME = "Contents";
|
||||
public static final String PARCEL_DESCRIPTOR_XML = "parcel-descriptor.xml";
|
||||
|
||||
private static ParcelZipper zipper = null;
|
||||
private static XMLParser parser = null;
|
||||
|
||||
private static final FileFilter DEFAULT_FILTER =
|
||||
BinaryOnlyFilter.getInstance();
|
||||
|
||||
private ParcelZipper() {
|
||||
}
|
||||
|
||||
public static ParcelZipper getParcelZipper() {
|
||||
if (zipper == null) {
|
||||
synchronized(ParcelZipper.class) {
|
||||
if (zipper == null)
|
||||
zipper = new ParcelZipper();
|
||||
}
|
||||
}
|
||||
return zipper;
|
||||
}
|
||||
|
||||
public static void setXMLParser(XMLParser parser) {
|
||||
getParcelZipper().parser = parser;
|
||||
}
|
||||
|
||||
public String zipParcel(File basedir) throws IOException {
|
||||
File targetfile, targetdir;
|
||||
|
||||
if (basedir.getName().equals(CONTENTS_DIRNAME))
|
||||
targetdir = basedir.getParentFile();
|
||||
else
|
||||
targetdir = basedir;
|
||||
|
||||
targetfile = new File(targetdir, targetdir.getName() + "." + PARCEL_EXTENSION);
|
||||
|
||||
return zipParcel(basedir, targetfile, DEFAULT_FILTER);
|
||||
}
|
||||
|
||||
public String zipParcel(File basedir, File targetfile) throws IOException {
|
||||
return zipParcel(basedir, targetfile, DEFAULT_FILTER);
|
||||
}
|
||||
|
||||
public String zipParcel(File basedir, FileFilter filter) throws IOException {
|
||||
File targetfile, targetdir;
|
||||
|
||||
if (basedir.getName().equals(CONTENTS_DIRNAME))
|
||||
targetdir = basedir.getParentFile();
|
||||
else
|
||||
targetdir = basedir;
|
||||
|
||||
targetfile = new File(targetdir, targetdir.getName() + "." + PARCEL_EXTENSION);
|
||||
|
||||
return zipParcel(basedir, targetfile, filter);
|
||||
}
|
||||
|
||||
public String zipParcel(File basedir, File targetfile, FileFilter filter)
|
||||
throws IOException {
|
||||
String realpath, tmppath;
|
||||
|
||||
realpath = targetfile.getPath();
|
||||
tmppath = realpath + ".tmp";
|
||||
|
||||
File tmpfile = new File(tmppath);
|
||||
try {
|
||||
if (tmpfile.exists() == true)
|
||||
tmpfile.delete();
|
||||
|
||||
ZipOutputStream out =
|
||||
new ZipOutputStream(new FileOutputStream(tmpfile));
|
||||
|
||||
File[] children = basedir.listFiles();
|
||||
for (int i = 0; i < children.length; i++)
|
||||
addFileToParcel(children[i], "", out, filter);
|
||||
|
||||
out.close();
|
||||
}
|
||||
catch (IOException ioe) {
|
||||
tmpfile.delete();
|
||||
throw ioe;
|
||||
}
|
||||
|
||||
if (targetfile.exists() == true)
|
||||
targetfile.delete();
|
||||
tmpfile.renameTo(targetfile);
|
||||
return targetfile.getAbsolutePath();
|
||||
}
|
||||
|
||||
private void addFileToParcel(File root, String path, ZipOutputStream out, FileFilter filter)
|
||||
throws IOException {
|
||||
ZipEntry ze;
|
||||
|
||||
if (root.isDirectory() == true) {
|
||||
ze = new ZipEntry(/* PARCEL_PREFIX_DIR + */ path + root.getName() + "/");
|
||||
out.putNextEntry(ze);
|
||||
out.closeEntry();
|
||||
File[] children = root.listFiles();
|
||||
|
||||
for (int i = 0; i < children.length; i++)
|
||||
addFileToParcel(children[i], path + root.getName() + "/", out, filter);
|
||||
}
|
||||
else {
|
||||
if (filter.validate(root.getName()) == false &&
|
||||
root.getName().equals("parcel-descriptor.xml") == false)
|
||||
return;
|
||||
|
||||
ze = new ZipEntry(/* PARCEL_PREFIX_DIR + */ path + root.getName());
|
||||
out.putNextEntry(ze);
|
||||
|
||||
byte[] bytes = new byte[1024];
|
||||
int len;
|
||||
FileInputStream fis = new FileInputStream(root);
|
||||
|
||||
while ((len = fis.read(bytes)) != -1)
|
||||
out.write(bytes, 0, len);
|
||||
|
||||
out.closeEntry();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isOverwriteNeeded(File parcel, File target) {
|
||||
boolean result;
|
||||
|
||||
if (target.isDirectory())
|
||||
result = isDirectoryOverwriteNeeded(parcel, target);
|
||||
else
|
||||
result = isDocumentOverwriteNeeded(parcel, target);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private boolean isDirectoryOverwriteNeeded(File parcel, File target) {
|
||||
String parcelDir = getParcelDirFromParcelZip(parcel.getName());
|
||||
File[] children = target.listFiles();
|
||||
|
||||
for (int i = 0; i < children.length; i++)
|
||||
if (children[i].getName().equals(parcelDir))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean isDocumentOverwriteNeeded(File parcel, File document) {
|
||||
ZipFile documentZip;
|
||||
|
||||
try {
|
||||
documentZip = new ZipFile(document);
|
||||
}
|
||||
catch (IOException ioe) {
|
||||
return false;
|
||||
}
|
||||
|
||||
String name =
|
||||
PARCEL_PREFIX_DIR + getParcelDirFromParcelZip(parcel.getName()) +
|
||||
"/" + PARCEL_DESCRIPTOR_XML;
|
||||
|
||||
if (documentZip.getEntry(name) != null)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public String deployParcel(File parcel, File target)
|
||||
throws IOException {
|
||||
|
||||
String output = null;
|
||||
if (target.isDirectory())
|
||||
output = unzipToDirectory(parcel, target);
|
||||
else
|
||||
output = unzipToZip(parcel, target);
|
||||
return output;
|
||||
}
|
||||
|
||||
private String getParcelDirFromParcelZip(String zipname) {
|
||||
String result = zipname.substring(0, zipname.lastIndexOf("."));
|
||||
return result;
|
||||
}
|
||||
|
||||
private String unzipToDirectory(File parcel, File targetDirectory)
|
||||
throws IOException {
|
||||
|
||||
ZipInputStream in;
|
||||
File parcelDir = new File(targetDirectory,
|
||||
getParcelDirFromParcelZip(parcel.getName()));
|
||||
|
||||
if (isDirectoryOverwriteNeeded(parcel, targetDirectory)) {
|
||||
if (deleteDir(parcelDir) == false) {
|
||||
throw new IOException("Could not overwrite: " +
|
||||
parcelDir.getAbsolutePath());
|
||||
}
|
||||
}
|
||||
|
||||
in = new ZipInputStream(new FileInputStream(parcel));
|
||||
|
||||
File outFile;
|
||||
ZipEntry inEntry = in.getNextEntry();
|
||||
byte[] bytes = new byte[1024];
|
||||
int len;
|
||||
|
||||
while (inEntry != null) {
|
||||
outFile = new File(parcelDir, inEntry.getName());
|
||||
if (inEntry.isDirectory()) {
|
||||
//System.err.println("Creating directory: " + outFile.getAbsolutePath());
|
||||
outFile.mkdir();
|
||||
}
|
||||
else {
|
||||
if (outFile.getParentFile().exists() != true)
|
||||
outFile.getParentFile().mkdirs();
|
||||
|
||||
FileOutputStream out = new FileOutputStream(outFile);
|
||||
// System.out.println("Writing file: " + outFile.getAbsolutePath());
|
||||
while ((len = in.read(bytes)) != -1)
|
||||
out.write(bytes, 0, len);
|
||||
out.close();
|
||||
}
|
||||
|
||||
inEntry = in.getNextEntry();
|
||||
}
|
||||
in.close();
|
||||
return parcelDir.getAbsolutePath();
|
||||
}
|
||||
|
||||
private boolean deleteDir(File basedir) {
|
||||
if (basedir.isDirectory()) {
|
||||
String[] children = basedir.list();
|
||||
for (int i=0; i<children.length; i++) {
|
||||
boolean success = deleteDir(new File(basedir, children[i]));
|
||||
if (!success) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return basedir.delete();
|
||||
}
|
||||
|
||||
private String unzipToZip(File parcel, File targetDocument)
|
||||
throws IOException {
|
||||
|
||||
ZipInputStream documentStream, parcelStream;
|
||||
ZipOutputStream outStream;
|
||||
Manifest manifest;
|
||||
|
||||
if (isDocumentOverwriteNeeded(parcel, targetDocument)) {
|
||||
String parcelName = getParcelDirFromParcelZip(parcel.getName());
|
||||
unzipToZipExceptParcel(targetDocument, parcelName);
|
||||
}
|
||||
|
||||
// first write contents of document to tmpfile
|
||||
File tmpfile = new File(targetDocument.getAbsolutePath() + ".tmp");
|
||||
|
||||
manifest = addParcelToManifest(targetDocument, parcel);
|
||||
|
||||
documentStream =
|
||||
new ZipInputStream(new FileInputStream(targetDocument));
|
||||
parcelStream = new ZipInputStream(new FileInputStream(parcel));
|
||||
outStream = new ZipOutputStream(new FileOutputStream(tmpfile));
|
||||
|
||||
try {
|
||||
copyParcelToZip(parcelStream, outStream, PARCEL_PREFIX_DIR +
|
||||
getParcelDirFromParcelZip(parcel.getName()));
|
||||
copyDocumentToZip(documentStream, outStream, manifest);
|
||||
documentStream.close();
|
||||
parcelStream.close();
|
||||
outStream.close();
|
||||
}
|
||||
catch (IOException ioe) {
|
||||
tmpfile.delete();
|
||||
throw ioe;
|
||||
}
|
||||
|
||||
if (targetDocument.delete() == false) {
|
||||
tmpfile.delete();
|
||||
throw new IOException("Could not overwrite " + targetDocument);
|
||||
}
|
||||
else
|
||||
tmpfile.renameTo(targetDocument);
|
||||
|
||||
return targetDocument.getAbsolutePath();
|
||||
}
|
||||
|
||||
private void copyParcelToZip(ZipInputStream in, ZipOutputStream out,
|
||||
String parcelName) throws IOException {
|
||||
|
||||
ZipEntry outEntry;
|
||||
ZipEntry inEntry = in.getNextEntry();
|
||||
byte[] bytes = new byte[1024];
|
||||
int len;
|
||||
|
||||
while (inEntry != null) {
|
||||
if(parcelName!=null)
|
||||
outEntry = new ZipEntry(parcelName + "/" + inEntry.getName());
|
||||
else
|
||||
outEntry = new ZipEntry(inEntry);
|
||||
out.putNextEntry(outEntry);
|
||||
|
||||
if (inEntry.isDirectory() == false)
|
||||
while ((len = in.read(bytes)) != -1)
|
||||
out.write(bytes, 0, len);
|
||||
|
||||
out.closeEntry();
|
||||
inEntry = in.getNextEntry();
|
||||
}
|
||||
}
|
||||
|
||||
private void copyDocumentToZip(ZipInputStream in, ZipOutputStream out,
|
||||
Manifest manifest) throws IOException {
|
||||
|
||||
ZipEntry outEntry;
|
||||
ZipEntry inEntry;
|
||||
byte[] bytes = new byte[1024];
|
||||
int len;
|
||||
|
||||
while ((inEntry = in.getNextEntry()) != null) {
|
||||
|
||||
outEntry = new ZipEntry(inEntry);
|
||||
out.putNextEntry(outEntry);
|
||||
|
||||
if(inEntry.getName().equals("META-INF/manifest.xml") &&
|
||||
manifest != null) {
|
||||
InputStream manifestStream = manifest.getInputStream();
|
||||
while ((len = manifestStream.read(bytes)) != -1)
|
||||
out.write(bytes, 0, len);
|
||||
}
|
||||
else if (inEntry.isDirectory() == false) {
|
||||
while ((len = in.read(bytes)) != -1)
|
||||
out.write(bytes, 0, len);
|
||||
}
|
||||
|
||||
out.closeEntry();
|
||||
}
|
||||
}
|
||||
|
||||
public String unzipToZipExceptParcel(File document, String parcelName)
|
||||
throws IOException {
|
||||
|
||||
ZipInputStream documentStream;
|
||||
ZipOutputStream outStream;
|
||||
Manifest manifest = null;
|
||||
|
||||
parcelName = PARCEL_PREFIX_DIR + parcelName;
|
||||
manifest = removeParcelFromManifest(document, parcelName);
|
||||
|
||||
// first write contents of document to tmpfile
|
||||
File tmpfile = new File(document.getAbsolutePath() + ".tmp");
|
||||
|
||||
documentStream = new ZipInputStream(new FileInputStream(document));
|
||||
outStream = new ZipOutputStream(new FileOutputStream(tmpfile));
|
||||
|
||||
try {
|
||||
ZipEntry outEntry;
|
||||
ZipEntry inEntry;
|
||||
byte[] bytes = new byte[1024];
|
||||
int len;
|
||||
|
||||
while ((inEntry = documentStream.getNextEntry()) != null) {
|
||||
|
||||
if(inEntry.getName().startsWith(parcelName))
|
||||
continue;
|
||||
|
||||
outEntry = new ZipEntry(inEntry);
|
||||
outStream.putNextEntry(outEntry);
|
||||
|
||||
if(inEntry.getName().equals("META-INF/manifest.xml") &&
|
||||
manifest != null) {
|
||||
InputStream manifestStream = manifest.getInputStream();
|
||||
while ((len = manifestStream.read(bytes)) != -1)
|
||||
outStream.write(bytes, 0, len);
|
||||
}
|
||||
else if (inEntry.isDirectory() == false) {
|
||||
while ((len = documentStream.read(bytes)) != -1)
|
||||
outStream.write(bytes, 0, len);
|
||||
}
|
||||
|
||||
outStream.closeEntry();
|
||||
}
|
||||
}
|
||||
catch (IOException ioe) {
|
||||
tmpfile.delete();
|
||||
throw ioe;
|
||||
}
|
||||
finally {
|
||||
documentStream.close();
|
||||
outStream.close();
|
||||
}
|
||||
|
||||
if (document.delete() == false) {
|
||||
tmpfile.delete();
|
||||
throw new IOException("Could not overwrite " + document);
|
||||
}
|
||||
else
|
||||
tmpfile.renameTo(document);
|
||||
|
||||
return document.getAbsolutePath();
|
||||
}
|
||||
|
||||
private Manifest getManifestFromDocument(File document) {
|
||||
ZipFile documentZip;
|
||||
Manifest result = null;
|
||||
|
||||
try {
|
||||
documentZip = new ZipFile(document);
|
||||
}
|
||||
catch (IOException ioe) {
|
||||
return null;
|
||||
}
|
||||
|
||||
ZipEntry original = documentZip.getEntry("META-INF/manifest.xml");
|
||||
if (original == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
result = new Manifest(documentZip.getInputStream(original), parser);
|
||||
}
|
||||
catch (IOException ioe) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private Manifest addParcelToManifest(File document, File parcel) {
|
||||
ZipFile documentZip, parcelZip;
|
||||
Manifest result = null;
|
||||
|
||||
result = getManifestFromDocument(document);
|
||||
if (result == null)
|
||||
return null;
|
||||
|
||||
try {
|
||||
parcelZip = new ZipFile(parcel);
|
||||
}
|
||||
catch (IOException ioe) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String prefix = PARCEL_PREFIX_DIR +
|
||||
getParcelDirFromParcelZip(parcel.getName()) + "/";
|
||||
|
||||
Enumeration entries = parcelZip.entries();
|
||||
while (entries.hasMoreElements()) {
|
||||
ZipEntry entry = (ZipEntry)entries.nextElement();
|
||||
result.add(prefix + entry.getName());
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private Manifest removeParcelFromManifest(File document, String name) {
|
||||
Manifest result = null;
|
||||
|
||||
result = getManifestFromDocument(document);
|
||||
if (result == null)
|
||||
return null;
|
||||
|
||||
result.remove(name);
|
||||
return result;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user