001: /*
002: * $Header: /cvs/j3dfly/J3dEditor/src/org/jdesktop/j3dedit/scenegrapheditor/JarBuilder.java,v 1.1 2005/04/20 22:20:46 paulby Exp $
003: *
004: * Sun Public License Notice
005: *
006: * The contents of this file are subject to the Sun Public License Version
007: * 1.0 (the "License"). You may not use this file except in compliance with
008: * the License. A copy of the License is available at http://www.sun.com/
009: *
010: * The Original Code is the Java 3D(tm) Scene Graph Editor.
011: * The Initial Developer of the Original Code is Paul Byrne.
012: * Portions created by Paul Byrne are Copyright (C) 2002.
013: * All Rights Reserved.
014: *
015: * Contributor(s): Paul Byrne.
016: *
017: **/
018: package org.jdesktop.j3dedit.scenegrapheditor;
019:
020: import java.io.*;
021: import java.util.jar.*;
022: import java.net.URL;
023:
024: /**
025: * @author Paul Byrne
026: * @version 1.5, 01/18/02
027: */
028: public class JarBuilder extends Object {
029:
030: org.jdesktop.j3dfly.utils.loadercontrol.ExampleFileFilter jarFilter;
031: Manifest manifest;
032:
033: /** Creates new JarBuilder */
034: public JarBuilder() {
035: jarFilter = new org.jdesktop.j3dfly.utils.loadercontrol.ExampleFileFilter(
036: "jar");
037: try {
038: InputStream in = getClass()
039: .getResource(
040: "/com/sun/j3d/j3dedit/j3ddelivery/Delivery_Manifest.mf")
041: .openStream();
042: manifest = new Manifest(in);
043: } catch (Exception e) {
044: e.printStackTrace();
045: }
046: }
047:
048: public void buildJar(File sceneGraphFile) {
049: File jarFile;
050:
051: if (sceneGraphFile == null) {
052: jarFile = WindowManager.getManager().chooseOpenFile(
053: "Choose Jar file", jarFilter, new File("test.jar"));
054: } else {
055: String filename = sceneGraphFile.getName();
056: filename.substring(0, filename.lastIndexOf('.'));
057: filename.concat("jar");
058: jarFile = WindowManager.getManager().chooseSaveFile(
059: "Build Jar file", jarFilter, new File(filename));
060: }
061:
062: if (jarFile == null)
063: return;
064:
065: try {
066: jarFile.createNewFile();
067: } catch (IOException ex) {
068: WindowManager.getManager().showMessage("File Error",
069: "Can not create file " + jarFile.getName());
070: return;
071: }
072:
073: if (!jarFile.canWrite()) {
074: WindowManager.getManager().showMessage("File Error",
075: "Can not Write to file " + jarFile.getName());
076: return;
077: }
078:
079: try {
080: JarOutputStream jarStream = new JarOutputStream(
081: new BufferedOutputStream(new FileOutputStream(
082: jarFile)), manifest);
083:
084: writeAllClassFiles(jarStream, "J3dDelivery/");
085: writeAllClassFiles(jarStream, "SceneGraphState/");
086:
087: writeJarEntry(jarStream, "graph.j3d", sceneGraphFile
088: .toURL());
089:
090: jarStream.close();
091: } catch (IOException e) {
092: e.printStackTrace();
093: }
094: }
095:
096: /**
097: * Write all the class files from the directory into the JarStream
098: */
099: private void writeAllClassFiles(JarOutputStream jarStream,
100: String directory) throws IOException {
101: File deliveryDir = new File(directory);
102: File[] classes = deliveryDir.listFiles(new FilenameFilter() {
103: public boolean accept(File file, String name) {
104: if (name.endsWith("class"))
105: return true;
106: else
107: return false;
108: }
109: });
110:
111: String name;
112: for (int i = 0; i < classes.length; i++) {
113: name = directory + classes[i].getName();
114: writeJarEntry(jarStream, name, getClass().getResource(
115: "/" + name));
116: }
117: }
118:
119: private void writeJarEntry(JarOutputStream jarStream,
120: String entryName, URL entry) throws IOException {
121: JarEntry jarEntry = new JarEntry(entryName);
122: jarStream.putNextEntry(jarEntry);
123: writeFile(jarStream, entry);
124: }
125:
126: private void writeFile(JarOutputStream jarStream, URL url)
127: throws IOException {
128: int bufferSize = 1024;
129: int dataSize;
130: byte[] data = new byte[bufferSize];
131: InputStream in = url.openStream();
132: while (in.available() != 0) {
133: dataSize = in.read(data);
134: jarStream.write(data, 0, dataSize);
135: }
136: in.close();
137: }
138: }
|