Revision: 18658 http://sourceforge.net/p/jmol/code/18658 Author: hansonr Date: 2013-09-17 14:28:46 +0000 (Tue, 17 Sep 2013) Log Message: ----------- abstraction of file writing for web
Modified Paths: -------------- trunk/Jmol/src/org/jmol/adapter/readers/pymol/PyMOLReader.java trunk/Jmol/src/org/jmol/api/ApiPlatform.java trunk/Jmol/src/org/jmol/api/JmolFileAdapterInterface.java trunk/Jmol/src/org/jmol/api/JmolStateCreator.java trunk/Jmol/src/org/jmol/api/JmolViewer.java trunk/Jmol/src/org/jmol/api/JmolZipUtility.java trunk/Jmol/src/org/jmol/awt/JmolFileAdapter.java trunk/Jmol/src/org/jmol/awt/Platform.java trunk/Jmol/src/org/jmol/awtjs2d/JmolFileAdapter.java trunk/Jmol/src/org/jmol/awtjs2d/Platform.java trunk/Jmol/src/org/jmol/export/_ObjExporter.java trunk/Jmol/src/org/jmol/export/___Exporter.java trunk/Jmol/src/org/jmol/export/image/GenericImageCreator.java trunk/Jmol/src/org/jmol/exportjs/Exporter.java trunk/Jmol/src/org/jmol/io/JmolBinary.java trunk/Jmol/src/org/jmol/io2/ZipUtil.java trunk/Jmol/src/org/jmol/viewer/StateCreator.java trunk/Jmol/src/org/jmol/viewer/StateManager.java trunk/Jmol/src/org/jmol/viewer/Viewer.java Added Paths: ----------- trunk/Jmol/src/org/jmol/awtjs2d/WebPostOutputChannel.java Modified: trunk/Jmol/src/org/jmol/adapter/readers/pymol/PyMOLReader.java =================================================================== --- trunk/Jmol/src/org/jmol/adapter/readers/pymol/PyMOLReader.java 2013-09-16 22:56:51 UTC (rev 18657) +++ trunk/Jmol/src/org/jmol/adapter/readers/pymol/PyMOLReader.java 2013-09-17 14:28:46 UTC (rev 18658) @@ -275,7 +275,7 @@ // just log and display some information here - logging = (viewer.getLogFile().length() > 0); + logging = (viewer.getLogFileName().length() > 0); JmolList<Object> names = getMapList(map, "names"); for (Map.Entry<String, Object> e : map.entrySet()) { String name = e.getKey(); Modified: trunk/Jmol/src/org/jmol/api/ApiPlatform.java =================================================================== --- trunk/Jmol/src/org/jmol/api/ApiPlatform.java 2013-09-16 22:56:51 UTC (rev 18657) +++ trunk/Jmol/src/org/jmol/api/ApiPlatform.java 2013-09-17 14:28:46 UTC (rev 18658) @@ -8,7 +8,7 @@ public interface ApiPlatform { - void setViewer(JmolViewer viewer, Object display); + void setViewer(Viewer viewer, Object display); /////// Display @@ -72,7 +72,7 @@ Object newOffScreenImage(int w, int h); - void renderScreenImage(JmolViewer jmolViewer, Object g, Object currentSize); + void renderScreenImage(Object g, Object currentSize); int[] getTextPixels(String text, JmolFont font3d, Object gObj, Object image, int mapWidth, int height, Modified: trunk/Jmol/src/org/jmol/api/JmolFileAdapterInterface.java =================================================================== --- trunk/Jmol/src/org/jmol/api/JmolFileAdapterInterface.java 2013-09-16 22:56:51 UTC (rev 18657) +++ trunk/Jmol/src/org/jmol/api/JmolFileAdapterInterface.java 2013-09-17 14:28:46 UTC (rev 18658) @@ -1,7 +1,12 @@ package org.jmol.api; +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.InputStream; import java.net.URL; +import org.jmol.viewer.FileManager; + public interface JmolFileAdapterInterface { public Object getBufferedURLInputStream(URL url, byte[] outputBytes, @@ -9,4 +14,14 @@ public Object getBufferedFileInputStream(String name); + public Object openOutputChannel(double privateKey, FileManager fm, String fileName, boolean asWriter) throws IOException; + + public InputStream openFileInputStream(double privateKey, String fileName) throws IOException; + + public String getAbsolutePath(double privateKey, String fileName); + + public long getFileLength(double privateKey, String fileName) throws IOException; + + public Object openLogFile(double privateKey, String logFileName, boolean asAppend) throws IOException ; + } Modified: trunk/Jmol/src/org/jmol/api/JmolStateCreator.java =================================================================== --- trunk/Jmol/src/org/jmol/api/JmolStateCreator.java 2013-09-16 22:56:51 UTC (rev 18657) +++ trunk/Jmol/src/org/jmol/api/JmolStateCreator.java 2013-09-17 14:28:46 UTC (rev 18658) @@ -18,7 +18,7 @@ public interface JmolStateCreator { - void setViewer(Viewer viewer); + void setViewer(Viewer viewer, double privateKey); Object getWrappedState(String fileName, String[] scripts, boolean isImage, boolean asJmolZip, int width, int height); @@ -104,5 +104,9 @@ String getAtomDefs(Map<String, Object> names); + String getLogFileName(); + String setLogFile(String name); + + } Modified: trunk/Jmol/src/org/jmol/api/JmolViewer.java =================================================================== --- trunk/Jmol/src/org/jmol/api/JmolViewer.java 2013-09-16 22:56:51 UTC (rev 18657) +++ trunk/Jmol/src/org/jmol/api/JmolViewer.java 2013-09-17 14:28:46 UTC (rev 18658) @@ -560,7 +560,7 @@ */ public void renderScreenImage(Object g, Object currentSize, Object rectClip) { - apiPlatform.renderScreenImage(this, g, currentSize); + apiPlatform.renderScreenImage(g, currentSize); } public Object getJsObjectInfo(Object[] jsObject, String method, Object[] args) { Modified: trunk/Jmol/src/org/jmol/api/JmolZipUtility.java =================================================================== --- trunk/Jmol/src/org/jmol/api/JmolZipUtility.java 2013-09-16 22:56:51 UTC (rev 18657) +++ trunk/Jmol/src/org/jmol/api/JmolZipUtility.java 2013-09-17 14:28:46 UTC (rev 18658) @@ -37,13 +37,13 @@ String shortName, Map<String, byte[]> pngjCache); - public Object writeZipFile(FileManager fm, Viewer viewer, String outFileName, + public Object writeZipFile(double privateKey, FileManager fm, Viewer viewer, String outFileName, JmolList<Object> fileNamesAndByteArrays, String msg); public String getSceneScript(String[] scenes, Map<String, String> htScenes, JmolList<Integer> list); - public Object createZipSet(FileManager fm, Viewer viewer, String fileName, + public Object createZipSet(double privateKey, FileManager fm, Viewer viewer, String fileName, String script, String[] scripts, boolean includeRemoteFiles); Modified: trunk/Jmol/src/org/jmol/awt/JmolFileAdapter.java =================================================================== --- trunk/Jmol/src/org/jmol/awt/JmolFileAdapter.java 2013-09-16 22:56:51 UTC (rev 18657) +++ trunk/Jmol/src/org/jmol/awt/JmolFileAdapter.java 2013-09-17 14:28:46 UTC (rev 18658) @@ -1,18 +1,31 @@ package org.jmol.awt; import java.io.BufferedInputStream; +import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.FileWriter; import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.URL; import java.net.URLConnection; import org.jmol.api.JmolFileAdapterInterface; import org.jmol.api.JmolFileInterface; +import org.jmol.viewer.FileManager; +import org.jmol.viewer.Viewer; public class JmolFileAdapter implements JmolFileAdapterInterface { + private Viewer viewer; + + public JmolFileAdapter(Viewer viewer) { + this.viewer = viewer; + } + public Object getBufferedURLInputStream(URL url, byte[] outputBytes, String post) { try { @@ -63,4 +76,30 @@ return new JmolFile(name); } + public Object openOutputChannel(double privateKey, FileManager fm, String fileName, boolean asWriter) throws IOException { + if (!viewer.checkPrivateKey(privateKey)) + return null; + OutputStream os = new FileOutputStream(fileName); + return (asWriter ? new BufferedWriter(new OutputStreamWriter(os)) : os); + } + + public InputStream openFileInputStream(double privateKey, String fileName) + throws IOException { + return (viewer.checkPrivateKey(privateKey) ? new FileInputStream(fileName) : null); + } + + public String getAbsolutePath(double privateKey, String fileName) { + return (viewer.isApplet() || !viewer.checkPrivateKey(privateKey) ? fileName + : (new File(fileName).getAbsolutePath())); + } + + public long getFileLength(double privateKey, String fileName) { + return (new File(fileName)).length(); + } + + public Object openLogFile(double privateKey, String logFileName, boolean asAppend) throws IOException { + return (viewer.checkPrivateKey(privateKey) && logFileName.indexOf("JmolLog_") >= 0 ? + new BufferedWriter(new FileWriter(logFileName, asAppend)) : null); + } + } Modified: trunk/Jmol/src/org/jmol/awt/Platform.java =================================================================== --- trunk/Jmol/src/org/jmol/awt/Platform.java 2013-09-16 22:56:51 UTC (rev 18657) +++ trunk/Jmol/src/org/jmol/awt/Platform.java 2013-09-17 14:28:46 UTC (rev 18658) @@ -17,7 +17,6 @@ import org.jmol.api.JmolFileInterface; import org.jmol.api.JmolMouseInterface; import org.jmol.api.JmolPopupInterface; -import org.jmol.api.JmolViewer; import org.jmol.api.JmolFileAdapterInterface; import org.jmol.util.JmolFont; import org.jmol.util.P3; @@ -26,9 +25,9 @@ public class Platform implements ApiPlatform { - JmolViewer viewer; + Viewer viewer; - public void setViewer(JmolViewer viewer, Object display) { + public void setViewer(Viewer viewer, Object display) { this.viewer = viewer; } @@ -62,11 +61,10 @@ /** * legacy apps will use this * - * @param viewer * @param g * @param size */ - public void renderScreenImage(JmolViewer viewer, Object g, Object size) { + public void renderScreenImage(Object g, Object size) { Display.renderScreenImage(viewer, g, size); } @@ -87,7 +85,7 @@ } public JmolFileAdapterInterface getFileAdapter() { - return new JmolFileAdapter(); + return new JmolFileAdapter(viewer); } ////// Mouse @@ -246,5 +244,4 @@ .format(new Date()); } - } Modified: trunk/Jmol/src/org/jmol/awtjs2d/JmolFileAdapter.java =================================================================== --- trunk/Jmol/src/org/jmol/awtjs2d/JmolFileAdapter.java 2013-09-16 22:56:51 UTC (rev 18657) +++ trunk/Jmol/src/org/jmol/awtjs2d/JmolFileAdapter.java 2013-09-17 14:28:46 UTC (rev 18658) @@ -1,10 +1,13 @@ package org.jmol.awtjs2d; +import java.io.BufferedWriter; import java.io.IOException; +import java.io.InputStream; import java.net.URL; import java.net.UnknownServiceException; import org.jmol.api.JmolFileAdapterInterface; +import org.jmol.viewer.FileManager; public class JmolFileAdapter implements JmolFileAdapterInterface { @@ -31,5 +34,29 @@ } } + public Object openOutputChannel(double privateKey, FileManager fm, String fileName, boolean asWriter) { + return new WebPostOutputChannel(fm, fileName, asWriter); + } + public InputStream openFileInputStream(double privateKey, String fileName) + throws IOException { + // unused (to date) in JSmol/HTML5 + return null; + } + + public String getAbsolutePath(double privateKey, String fileName) { + return fileName; + } + + public long getFileLength(double privateKey, String fileName) + throws IOException { + return 0; + } + + public BufferedWriter openLogFile(double privateKey, String logFileName, boolean asAppend) { + // could implement remote logging here + return null; + } + + } Modified: trunk/Jmol/src/org/jmol/awtjs2d/Platform.java =================================================================== --- trunk/Jmol/src/org/jmol/awtjs2d/Platform.java 2013-09-16 22:56:51 UTC (rev 18657) +++ trunk/Jmol/src/org/jmol/awtjs2d/Platform.java 2013-09-17 14:28:46 UTC (rev 18658) @@ -9,7 +9,6 @@ import org.jmol.api.JmolFileInterface; import org.jmol.api.JmolMouseInterface; import org.jmol.api.JmolPopupInterface; -import org.jmol.api.JmolViewer; import org.jmol.util.JmolFont; import org.jmol.util.P3; import org.jmol.viewer.ActionManager; @@ -30,10 +29,10 @@ */ public class Platform implements ApiPlatform { Object canvas; - JmolViewer viewer; + Viewer viewer; Object context; - public void setViewer(JmolViewer viewer, Object canvas) { + public void setViewer(Viewer viewer, Object canvas) { /** * @j2sNative * @@ -130,11 +129,10 @@ /** * legacy apps will use this * - * @param viewer * @param context * @param size */ - public void renderScreenImage(JmolViewer viewer, Object context, Object size) { + public void renderScreenImage(Object context, Object size) { Display.renderScreenImage(viewer, context, size); } @@ -333,5 +331,4 @@ } } - } Added: trunk/Jmol/src/org/jmol/awtjs2d/WebPostOutputChannel.java =================================================================== --- trunk/Jmol/src/org/jmol/awtjs2d/WebPostOutputChannel.java (rev 0) +++ trunk/Jmol/src/org/jmol/awtjs2d/WebPostOutputChannel.java 2013-09-17 14:28:46 UTC (rev 18658) @@ -0,0 +1,45 @@ +package org.jmol.awtjs2d; + +import java.io.BufferedWriter; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; + +import org.jmol.io.JmolBinary; +import org.jmol.viewer.FileManager; + +public class WebPostOutputChannel extends ByteArrayOutputStream { + + private String fileName; + private FileManager fm; + private BufferedWriter bw; + + public WebPostOutputChannel(FileManager fm, String fileName, boolean asWriter) { + this.fm = fm; + this.fileName = fileName; + if (asWriter) + bw = new BufferedWriter(new OutputStreamWriter(this)); + } + + // methods of BufferedWriter. These will be accessed by JavaScript, so no type checking + + public void write(String data) throws IOException { + bw.write(data); + } + + @Override + public void flush() { + if (bw != null) + try { + bw.flush(); + } catch (IOException e) { + } + else + flush(); + } + + @Override + public void close() { + JmolBinary.postByteArray(fm, fileName, toByteArray()); + } +} Modified: trunk/Jmol/src/org/jmol/export/_ObjExporter.java =================================================================== --- trunk/Jmol/src/org/jmol/export/_ObjExporter.java 2013-09-16 22:56:51 UTC (rev 18657) +++ trunk/Jmol/src/org/jmol/export/_ObjExporter.java 2013-09-17 14:28:46 UTC (rev 18658) @@ -5,11 +5,7 @@ import java.awt.image.BufferedImage; import java.io.BufferedWriter; import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; import java.io.IOException; -//import java.io.OutputStream; -import java.io.OutputStreamWriter; import org.jmol.util.JmolList; import java.util.HashSet; @@ -75,8 +71,6 @@ private boolean normalizeUV = true; /** BufferedWriter for the .mtl file. */ private BufferedWriter mtlbw; - /** FileOutputStream for the .mtl file. */ - private FileOutputStream mtlos; /** Path of the OBJ file without the extension. */ String objFileRootName; /** File for the .mtl file. */ @@ -463,10 +457,9 @@ String mtlFileName = objFileRootName + ".mtl"; mtlFile = new File(mtlFileName); System.out.println("_WavefrontObjExporter writing to " - + mtlFile.getAbsolutePath()); - mtlos = new FileOutputStream(mtlFile); - mtlbw = new BufferedWriter(new OutputStreamWriter(mtlos)); - } catch (FileNotFoundException ex) { + + viewer.getAbsolutePath(privateKey, mtlFileName)); + mtlbw = (BufferedWriter) viewer.openOutputChannel(privateKey, mtlFileName, true); + } catch (IOException ex) { debugPrint("End initializeOutput (" + ex.getMessage() + "):"); return false; } @@ -492,7 +485,6 @@ try { mtlbw.flush(); mtlbw.close(); - mtlos = null; } catch (IOException ex) { ex.printStackTrace(); if (retVal.startsWith("OK")) { Modified: trunk/Jmol/src/org/jmol/export/___Exporter.java =================================================================== --- trunk/Jmol/src/org/jmol/export/___Exporter.java 2013-09-16 22:56:51 UTC (rev 18657) +++ trunk/Jmol/src/org/jmol/export/___Exporter.java 2013-09-17 14:28:46 UTC (rev 18658) @@ -29,10 +29,7 @@ import java.awt.Image; import java.io.BufferedWriter; import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; import java.io.IOException; -import java.io.OutputStreamWriter; import java.text.SimpleDateFormat; import org.jmol.util.JmolList; @@ -150,7 +147,6 @@ protected JmolRendererInterface jmolRenderer; protected SB output; protected BufferedWriter bw; - private FileOutputStream os; protected String fileName; protected String commandLineOptions; @@ -244,9 +240,8 @@ try { File f = new File(fileName); System.out.println("__Exporter writing to " + f.getAbsolutePath()); - os = new FileOutputStream(fileName); - bw = new BufferedWriter(new OutputStreamWriter(os)); - } catch (FileNotFoundException e) { + bw = (BufferedWriter)viewer.openOutputChannel(privateKey, fileName, true); + } catch (IOException e) { return false; } } else { @@ -341,7 +336,6 @@ try { bw.flush(); bw.close(); - os = null; } catch (IOException e) { System.out.println(e.getMessage()); return "ERROR EXPORTING FILE"; Modified: trunk/Jmol/src/org/jmol/export/image/GenericImageCreator.java =================================================================== --- trunk/Jmol/src/org/jmol/export/image/GenericImageCreator.java 2013-09-16 22:56:51 UTC (rev 18657) +++ trunk/Jmol/src/org/jmol/export/image/GenericImageCreator.java 2013-09-17 14:28:46 UTC (rev 18658) @@ -25,12 +25,8 @@ package org.jmol.export.image; import java.io.BufferedWriter; -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; -import java.io.OutputStreamWriter; import org.jmol.api.JmolImageCreatorInterface; import org.jmol.api.JmolViewer; @@ -66,42 +62,43 @@ * @param quality * @return null (canceled) or a message starting with OK or an error message */ - public Object createImage(String fileName, String type, String text, Object bytes_or_image, - String[] scripts, Object appendix, int quality) { + public Object createImage(String fileName, String type, String text, + Object bytes_or_image, String[] scripts, + Object appendix, int quality) { // this method may not be accessed, though public, unless // accessed via viewer, which provides its private key. boolean isBytes = (bytes_or_image != null); boolean isText = (!isBytes && quality == Integer.MIN_VALUE); - FileOutputStream os = null; + OutputStream os = null; long len = -1; try { if (!viewer.checkPrivateKey(privateKey)) return "NO SECURITY"; // returns message starting with OK or an error message if ("OutputStream".equals(type)) - return (viewer.isJS ? new ByteArrayOutputStream() : new FileOutputStream(fileName)); + return viewer.openOutputChannel(privateKey, fileName, false); if (isBytes) { if (bytes_or_image instanceof byte[]) { len = ((byte[]) bytes_or_image).length; - os = new FileOutputStream(fileName); + os = (OutputStream) viewer.openOutputChannel(privateKey, fileName, false); byte[] b = (byte[]) bytes_or_image; os.write(b, 0, b.length); os.flush(); os.close(); + os = null; } else { - getImageBytes(type, quality, fileName, scripts, bytes_or_image, null, null); + getImageBytes(type, quality, fileName, scripts, bytes_or_image, null, + null); return fileName; } } else if (isText) { if (text == null) return "NO DATA"; - os = new FileOutputStream(fileName); - OutputStreamWriter osw = new OutputStreamWriter(os); - BufferedWriter bw = new BufferedWriter(osw, 8192); + BufferedWriter bw = (BufferedWriter) viewer.openOutputChannel( + privateKey, fileName, true); len = text.length(); bw.write(text); bw.close(); - os = null; } else { len = 1; Object bytesOrError = getImageBytes(type, quality, fileName, scripts, @@ -111,7 +108,7 @@ byte[] bytes = (byte[]) bytesOrError; if (bytes != null) return (fileName == null ? bytes : new String(bytes)); - len = (new File(fileName)).length(); + len = viewer.getFileLength(privateKey, fileName); } } catch (IOException exc) { Logger.errorEx("IO Exception", exc); @@ -126,8 +123,8 @@ } } return (len < 0 ? "Creation of " + fileName + " failed: " - + viewer.getErrorMessageUn() : "OK " + type + " " + len + " " - + fileName + + viewer.getErrorMessageUn() : "OK " + type + " " + + (len > 0 ? len + " " : "") + fileName + (quality == Integer.MIN_VALUE ? "" : "; quality=" + quality)); } @@ -155,7 +152,7 @@ viewer.apiPlatform.getImageWidth(image), viewer.apiPlatform .getImageHeight(image)); if (isOsTemp) - os = new FileOutputStream(fileName); + os = (OutputStream) viewer.openOutputChannel(privateKey, fileName, false); if (type.equals("JPEG") || type.equals("JPG")) { if (quality <= 0) quality = 100; // high quality Modified: trunk/Jmol/src/org/jmol/exportjs/Exporter.java =================================================================== --- trunk/Jmol/src/org/jmol/exportjs/Exporter.java 2013-09-16 22:56:51 UTC (rev 18657) +++ trunk/Jmol/src/org/jmol/exportjs/Exporter.java 2013-09-17 14:28:46 UTC (rev 18658) @@ -28,11 +28,7 @@ import java.awt.Image; import java.io.BufferedWriter; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; import java.io.IOException; -import java.io.OutputStreamWriter; import org.jmol.util.TextFormat; @@ -142,7 +138,6 @@ protected JmolRendererInterface jmolRenderer; protected SB output; protected BufferedWriter bw; - private FileOutputStream os; protected String fileName; protected String commandLineOptions; @@ -226,11 +221,9 @@ } //viewer.writeTextFile(fileName + ".spt", viewer.getSavedState("_Export")); try { - File f = new File(fileName); - System.out.println("__Exporter writing to " + f.getAbsolutePath()); - os = new FileOutputStream(fileName); - bw = new BufferedWriter(new OutputStreamWriter(os)); - } catch (FileNotFoundException e) { + System.out.println("__Exporter writing to " + viewer.getAbsolutePath(privateKey, fileName)); + bw = (BufferedWriter) viewer.openOutputChannel(privateKey, fileName, true); + } catch (IOException e) { return false; } } else { @@ -291,7 +284,6 @@ try { bw.flush(); bw.close(); - os = null; } catch (IOException e) { System.out.println(e.toString()); return "ERROR EXPORTING FILE"; Modified: trunk/Jmol/src/org/jmol/io/JmolBinary.java =================================================================== --- trunk/Jmol/src/org/jmol/io/JmolBinary.java 2013-09-16 22:56:51 UTC (rev 18657) +++ trunk/Jmol/src/org/jmol/io/JmolBinary.java 2013-09-17 14:28:46 UTC (rev 18658) @@ -413,8 +413,8 @@ return getJzu().getZipFileContentsAsBytes(bis, subFileList, i); } - public static Object createZipSet(FileManager fm, Viewer viewer, String fileName, String script, String[] scripts, boolean includeRemoteFiles) { - return getJzu().createZipSet(fm, viewer, fileName, script, scripts, includeRemoteFiles); + public static Object createZipSet(double privateKey, FileManager fm, Viewer viewer, String fileName, String script, String[] scripts, boolean includeRemoteFiles) { + return getJzu().createZipSet(privateKey, fm, viewer, fileName, script, scripts, includeRemoteFiles); } public static Object getStreamAsBytes(BufferedInputStream bis, @@ -443,6 +443,7 @@ /** * generic method to create a zip file based on * http://www.exampledepot.com/egs/java.util.zip/CreateZip.html + * @param privateKey * @param fm * @param viewer * @@ -453,10 +454,10 @@ * @param msg * @return msg bytes filename or errorMessage or byte[] */ - public static Object writeZipFile(FileManager fm, Viewer viewer, String outFileName, + public static Object writeZipFile(double privateKey, FileManager fm, Viewer viewer, String outFileName, JmolList<Object> fileNamesAndByteArrays, String msg) { - return getJzu().writeZipFile(fm, viewer, outFileName, fileNamesAndByteArrays, msg); + return getJzu().writeZipFile(privateKey, fm, viewer, outFileName, fileNamesAndByteArrays, msg); } Modified: trunk/Jmol/src/org/jmol/io2/ZipUtil.java =================================================================== --- trunk/Jmol/src/org/jmol/io2/ZipUtil.java 2013-09-16 22:56:51 UTC (rev 18657) +++ trunk/Jmol/src/org/jmol/io2/ZipUtil.java 2013-09-17 14:28:46 UTC (rev 18658) @@ -29,8 +29,6 @@ import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; -import java.io.FileInputStream; -import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -400,7 +398,8 @@ return newName; } - public Object writeZipFile(FileManager fm, Viewer viewer, String outFileName, + public Object writeZipFile(double privateKey, FileManager fm, Viewer viewer, + String outFileName, JmolList<Object> fileNamesAndByteArrays, String msg) { byte[] buf = new byte[1024]; long nBytesOut = 0; @@ -414,7 +413,8 @@ || outFileName.startsWith("http://") ? new ByteArrayOutputStream() : null); ZipOutputStream os = new ZipOutputStream( - bos == null ? (OutputStream) new FileOutputStream(outFileName) : bos); + bos == null ? (OutputStream) viewer.openOutputChannel(privateKey, + outFileName, false) : bos); for (int i = 0; i < fileNamesAndByteArrays.size(); i += 3) { String fname = (String) fileNamesAndByteArrays.get(i); byte[] bytes = null; @@ -446,7 +446,7 @@ int nOut = 0; if (bytes == null) { // get data from disk - FileInputStream in = new FileInputStream(fname); + InputStream in = viewer.openFileInputStream(privateKey, fname); int len; while ((len = in.read(buf, 0, 1024)) > 0) { os.write(buf, 0, len); @@ -560,7 +560,7 @@ + "\n}\n"; } - public Object createZipSet(FileManager fm, Viewer viewer, String fileName, + public Object createZipSet(double privateKey, FileManager fm, Viewer viewer, String fileName, String script, String[] scripts, boolean includeRemoteFiles) { JmolList<Object> v = new JmolList<Object>(); @@ -660,7 +660,7 @@ v.addLast(bytes); } } - return JmolBinary.writeZipFile(fm, viewer, fileName, v, "OK JMOL"); + return JmolBinary.writeZipFile(privateKey, fm, viewer, fileName, v, "OK JMOL"); } public Object getAtomSetCollectionOrBufferedReaderFromZip( Modified: trunk/Jmol/src/org/jmol/viewer/StateCreator.java =================================================================== --- trunk/Jmol/src/org/jmol/viewer/StateCreator.java 2013-09-16 22:56:51 UTC (rev 18657) +++ trunk/Jmol/src/org/jmol/viewer/StateCreator.java 2013-09-17 14:28:46 UTC (rev 18658) @@ -25,7 +25,6 @@ import java.io.BufferedOutputStream; import java.io.BufferedWriter; -import java.io.FileWriter; import java.io.IOException; import java.io.OutputStream; import org.jmol.util.JmolList; @@ -44,6 +43,7 @@ import org.jmol.constant.EnumStereoMode; import org.jmol.constant.EnumStructure; import org.jmol.constant.EnumVdw; +import org.jmol.i18n.GT; import org.jmol.io.Base64; import org.jmol.io.JmolBinary; import org.jmol.io.OutputStringBuilder; @@ -103,11 +103,13 @@ } private Viewer viewer; + private double privateKey; - public void setViewer(Viewer viewer) { + public void setViewer(Viewer viewer, double privateKey) { this.viewer = viewer; + this.privateKey = privateKey; } - + public static String SIMULATION_PROTOCOL = "http://SIMULATION/"; public Object getWrappedState(String fileName, String[] scripts, @@ -122,7 +124,7 @@ viewer.fileManager.clearPngjCache(fileName); // when writing a file, we need to make sure // the pngj cache for that file is cleared - return JmolBinary.createZipSet(viewer.fileManager, viewer, null, s, + return JmolBinary.createZipSet(privateKey, viewer.fileManager, viewer, null, s, scripts, true); } // we remove local file references in the embedded states for images @@ -2040,7 +2042,7 @@ if (type.equals("ZIP") || type.equals("ZIPALL")) { if (scripts != null && type.equals("ZIP")) type = "ZIPALL"; - ret = JmolBinary.createZipSet(viewer.fileManager, viewer, localName, + ret = JmolBinary.createZipSet(privateKey, viewer.fileManager, viewer, localName, text, scripts, type.equals("ZIPALL")); } else if (type.equals("SCENE")) { ret = (viewer.isJS ? "ERROR: Not Available" : createSceneSet( @@ -2264,11 +2266,13 @@ boolean useDialog = (fileName.indexOf("?") == 0); if (useDialog) fileName = fileName.substring(1); - useDialog |= viewer.isApplet && (fileName.indexOf("http:") < 0); + useDialog |= viewer.isApplet() && (fileName.indexOf("http:") < 0); fileName = FileManager.getLocalPathForWritingFile(viewer, fileName); if (useDialog) fileName = viewer.dialogAsk(quality == Integer.MIN_VALUE ? "save" : "saveImage", fileName); + if (viewer.isJS && fileName.indexOf("http:") < 0) + fileName = "POST://" + fileName; return fileName; } @@ -2409,7 +2413,7 @@ fileName = fileName.substring(1); fileName = fileName.replace('\\', '/'); boolean isCached = fileName.startsWith("cache://"); - if (viewer.isApplet && fileName.indexOf("://") < 0) + if (viewer.isApplet() && fileName.indexOf("://") < 0) fileName = "file://" + (fileName.startsWith("/") ? "" : "/") + fileName; if (fileName.endsWith(".pse")) { viewer.evalString((isCached ? "" : "zap;") + "load SYNC " + Escape.eS(fileName) @@ -2471,24 +2475,54 @@ scriptEditor.setVisible(true); } + private String logFileName = null; + + public String getLogFileName() { + return (logFileName == null ? "" : logFileName); + } + + public String setLogFile(String value) { + String path = null; + String logFilePath = viewer.getLogFilePath(); + if (logFilePath == null || value.indexOf("\\") >= 0 + || value.indexOf("/") >= 0) { + value = null; + } else if (value.length() > 0) { + if (!value.startsWith("JmolLog_")) + value = "JmolLog_" + value; + path = viewer.getAbsolutePath(privateKey, logFilePath + value); + } + if (path == null) + value = null; + else + Logger.info(GT._("Setting log file to {0}", path)); + if (value == null || !viewer.isRestricted(ACCESS.ALL)) { + Logger.info(GT._("Cannot set log file path.")); + value = null; + } else { + logFileName = path; + viewer.global.setS("_logFile", viewer.isApplet() ? value : path); + } + return value; + } + public void logToFile(String data) { try { boolean doClear = (data.equals("$CLEAR$")); if (data.indexOf("$NOW$") >= 0) data = TextFormat.simpleReplace(data, "$NOW$", (new Date()).toString()); - if (viewer.logFile == null) { + if (logFileName == null) { System.out.println(data); return; } - FileWriter fstream = new FileWriter(viewer.logFile, !doClear); - BufferedWriter out = new BufferedWriter(fstream); + BufferedWriter out = (BufferedWriter) viewer.openLogFile(privateKey, logFileName, !doClear); if (!doClear) { int ptEnd = data.indexOf('\0'); if (ptEnd >= 0) data = data.substring(0, ptEnd); out.write(data); if (ptEnd < 0) - out.write('\n'); + out.write("\n"); } out.close(); } catch (Exception e) { Modified: trunk/Jmol/src/org/jmol/viewer/StateManager.java =================================================================== --- trunk/Jmol/src/org/jmol/viewer/StateManager.java 2013-09-16 22:56:51 UTC (rev 18657) +++ trunk/Jmol/src/org/jmol/viewer/StateManager.java 2013-09-17 14:28:46 UTC (rev 18658) @@ -722,7 +722,7 @@ setB("hideNotSelected", false); //maintained by the selectionManager setS("hoverLabel", ""); // maintained by the Hover shape setB("isKiosk", viewer.isKiosk()); // maintained by Viewer - setS("logFile", viewer.getLogFile()); // maintained by Viewer + setS("logFile", viewer.getLogFileName()); // maintained by Viewer setI("logLevel", Logger.getLogLevel()); setF("mouseWheelFactor", ActionManager.DEFAULT_MOUSE_WHEEL_FACTOR); setF("mouseDragFactor", ActionManager.DEFAULT_MOUSE_DRAG_FACTOR); Modified: trunk/Jmol/src/org/jmol/viewer/Viewer.java =================================================================== --- trunk/Jmol/src/org/jmol/viewer/Viewer.java 2013-09-16 22:56:51 UTC (rev 18657) +++ trunk/Jmol/src/org/jmol/viewer/Viewer.java 2013-09-17 14:28:46 UTC (rev 18658) @@ -134,7 +134,9 @@ import java.net.URL; import java.io.BufferedInputStream; import java.io.BufferedReader; -import java.io.File; +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.InputStream; import java.io.OutputStream; import java.io.Reader; import java.io.StringReader; @@ -214,7 +216,12 @@ public ModelSet modelSet; public FileManager fileManager; - boolean isApplet; + private boolean isApplet; + @Override + public boolean isApplet() { + return isApplet; + } + public boolean isSyntaxAndFileCheck = false; public boolean isSyntaxCheck = false; public boolean listCommands = false; @@ -249,10 +256,17 @@ private String syncId = ""; private String logFilePath = ""; - + String getLogFilePath() { + return logFilePath; + } + private boolean allowScripting; private boolean isPrintOnly = false; private boolean isSignedApplet = false; + public boolean isSignedApplet() { + return isSignedApplet; + } + private boolean isSignedAppletLocal = false; private boolean isSilent; private boolean multiTouch; @@ -338,11 +352,6 @@ return statusManager; } - @Override - public boolean isApplet() { - return isApplet; - } - public boolean isRestricted(ACCESS a) { // disables WRITE, LOAD file:/, set logFile // command line -g and -w options ARE available for final writing of image @@ -3551,7 +3560,7 @@ public JmolStateCreator getStateCreator() { if (sc == null) (sc = (JmolStateCreator) Interface - .getOptionInterface("viewer.StateCreator")).setViewer(this); + .getOptionInterface("viewer.StateCreator")).setViewer(this, privateKey); return sc; } @@ -6077,7 +6086,7 @@ global.defaultLoadFilter = value; break; case T.logfile: - value = setLogFile(value); + value = getStateCreator().setLogFile(value); if (value == null) return; break; @@ -7617,10 +7626,6 @@ return (!isJS && isSignedApplet && !isSignedAppletLocal || frankOn); } - public boolean isSignedApplet() { - return isSignedApplet; - } - @Override public void setShowMeasurements(boolean TF) { // setbooleanProperty @@ -9198,43 +9203,6 @@ return (global.preserveState && scriptManager != null); } - String logFile = null; - - public String getLogFile() { - return (logFile == null ? "" : logFile); - } - - private String setLogFile(String value) { - String path = null; - if (logFilePath == null || value.indexOf("\\") >= 0 - || value.indexOf("/") >= 0) { - value = null; - } else if (value.length() > 0) { - if (!value.startsWith("JmolLog_")) - value = "JmolLog_" + value; - try { - path = (isApplet ? logFilePath + value : (new File(logFilePath + value) - .getAbsolutePath())); - } catch (Exception e) { - value = null; - } - } - if (value == null || !isRestricted(ACCESS.ALL)) { - Logger.info(GT._("Cannot set log file path.")); - value = null; - } else { - if (path != null) - Logger.info(GT._("Setting log file to {0}", path)); - global.setS("_logFile", logFile = path); - } - return value; - } - - public void log(String data) { - if (data != null) - getStateCreator().logToFile(data); - } - boolean isKiosk; boolean isKiosk() { @@ -10380,4 +10348,39 @@ return global.platformSpeed >= n; } + // /////////////////////////////////////////////////////////////// + // delegated to JmolFileAdapter + // /////////////////////////////////////////////////////////////// + + public Object openOutputChannel(double privateKey, String fileName, boolean asWriter) + throws IOException { + return getFileAdapter().openOutputChannel(privateKey, fileManager, fileName, asWriter); + } + + public InputStream openFileInputStream(double privateKey, String fileName) throws IOException { + return getFileAdapter().openFileInputStream(privateKey, fileName); + } + + public String getAbsolutePath(double privateKey, String fileName) { + return getFileAdapter().getAbsolutePath(privateKey, fileName); + } + + public long getFileLength(double privateKey, String fileName) throws IOException { + return getFileAdapter().getFileLength(privateKey, fileName); + } + + public Object openLogFile(double privateKey, String logFileName, + boolean asAppend) throws IOException { + return getFileAdapter().openLogFile(privateKey, logFileName, asAppend); + } + + public void log(String data) { + if (data != null) + getStateCreator().logToFile(data); + } + + public String getLogFileName() { + return getStateCreator().getLogFileName(); + } + } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------------ LIMITED TIME SALE - Full Year of Microsoft Training For Just $49.99! 1,500+ hours of tutorials including VisualStudio 2012, Windows 8, SharePoint 2013, SQL 2012, MVC 4, more. BEST VALUE: New Multi-Library Power Pack includes Mobile, Cloud, Java, and UX Design. Lowest price ever! Ends 9/20/13. http://pubads.g.doubleclick.net/gampad/clk?id=58041151&iu=/4140/ostg.clktrk _______________________________________________ Jmol-commits mailing list Jmol-commits@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/jmol-commits