📄 win32shellviewer.java
字号:
/*** $Id: Win32ShellViewer.java,v 1.5 2001/05/07 12:37:22 kunugi Exp $**** Copyright (c) 2000-2001 Jeff Gay** on behalf of ICEMail.org <http://www.icemail.org>** Copyright (c) 1998-2000 by Timothy Gerard Endres** ** This program is free software.** ** You may redistribute it and/or modify it under the terms of the GNU** General Public License as published by the Free Software Foundation.** Version 2 of the license should be included with this distribution in** the file LICENSE, as well as License.html. If the license is not** included with this distribution, you may find a copy at the FSF web** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.**** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR** REDISTRIBUTION OF THIS SOFTWARE. */package org.icemail.mail;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.InputStream;import java.io.IOException;import javax.activation.CommandObject;import javax.activation.DataHandler;import javax.activation.MimeType;import javax.activation.MimeTypeParseException;import javax.swing.JOptionPane;import org.icemail.Package; import org.icemail.util.ComponentFactory; import org.icemail.util.FileUtilities; import org.icemail.util.UserProperties; import com.ice.jni.dde.JNIDDE;import com.ice.jni.dde.DDEException;import com.ice.jni.registry.Registry;import com.ice.jni.registry.RegistryKey;import com.ice.jni.registry.RegistryException;import com.ice.jni.registry.NoSuchKeyException;/** * Class Win32ShellViewer implements a CommandObject for the Java Activation Framework. * Win32ShellViewer tries to write the contents of the data source (source of the provided * data hander) to a file. The file is written to an appropriately named file based on * the MIME type of the data handler. The file is then 'opened' using the Win32 operating * system, which starts up the appropriate application to view the data. * <p> * The only verb supported is "view". And as you've already guessed, allows the data to * be viewed by appropriate application. * <p> * A lot of this is made possible via the ICE JNI library. */public class Win32ShellViewer implements CommandObject{ private static final int Debug_ = Package.DEBUG ? Package.getLevel( "Win32ShellViewer" ) : 0; /** * Construct an empty viewer, which can be loaded later. */ public Win32ShellViewer() { } /** * Initialize the command object from the data handler as described by the verb. * <p> * Implements CommandObject.setCommandContext(). * <p> * @param verb the command from the activation framework * @param dh the datahandler containing the data contents and type * @see javax.activation.CommandObject */ public void setCommandContext( String verb, DataHandler dh ) throws IOException { if ( Package.DEBUG && Package.isTraceable( "Win32ShellViewer" ) ) { System.out.println( "Win32ShellViewer.setCommandContext(s,dh): " + verb ); } if ( verb == null || dh == null ) throw new IOException( "Invalid parameters to Win32ShellViewer.setCommandContext()" ); if ( ! verb.equalsIgnoreCase( "view" ) ) throw new IOException( "Only 'view' accepted to Win32ShellViewer.setCommandContext()" ); viewContent( dh ); } /** * sets the current message to be displayed in the viewer */ private void viewContent( DataHandler dataHandler ) { if ( Package.DEBUG && Package.isTraceable( "Win32ShellViewer" ) ) { System.out.println( "Win32ShellViewer.viewContent()" ); } MimeType mimeType = null; String contentType = null; InputStream content = null; try { JNIDDE dde = new JNIDDE(); // Do this just to check the dll RegistryKey rkey = Registry.HKEY_CLASSES_ROOT; contentType = dataHandler.getContentType().toLowerCase(); if ( Package.DEBUG && Debug_ > 0 ) { System.err.println( "Win32ShellViewer.viewContent(): contentType = '" + contentType + "'" ); } mimeType = new MimeType( contentType ); String tempDirName = ICEMail.getTempFileManager().getDirectory(); if ( Package.DEBUG && Debug_ > 0 ) { System.err.println( "Win32ShellViewer.viewContent(): tempDirName = '" + tempDirName + "'" ); } rkey = rkey.openSubKey( "MIME\\Database\\Content Type\\" + mimeType.getBaseType() ); String fileNameSuffix = rkey.getStringValue( "Extension" ); rkey.closeKey(); if ( Package.DEBUG && Debug_ > 0 ) { System.err.println( "Win32ShellViewer.viewContent(): fileNameSuffix = '" + fileNameSuffix + "'" ); } String tempFileName = null; // First consult the MIME header for a name String mimeName = mimeType.getParameter( "name" ); if ( Package.DEBUG && Debug_ > 0 ) { System.err.println( "Win32ShellViewer.viewContent(): mimeName = '" + mimeName + "'" ); } if ( mimeName != null && FileUtilities.fileEqualsExtension( mimeName, fileNameSuffix ) ) { tempFileName = tempDirName + File.separator + mimeName; } else { // Second, consult the DataHandler for a name String dhName = dataHandler.getName(); if ( Package.DEBUG && Debug_ > 0 ) { System.err.println( "Win32ShellViewer: dhName = '" + dhName + "'" ); } if ( dhName != null && FileUtilities.fileEqualsExtension ( dhName, fileNameSuffix ) ) { tempFileName = tempDirName + File.separator + dhName; } else { if ( mimeName != null && mimeName.length() > 0 ) { tempFileName = tempDirName + File.separator + mimeName; } else if ( dhName != null && dhName.length() > 0 ) { tempFileName = tempDirName + File.separator + dhName; } else { tempFileName = ICEMail.getTempFileManager().getFilename( fileNameSuffix ); } String regContentType = null; int sufIdx = tempFileName.lastIndexOf( "." ); if ( sufIdx != -1 ) { String regSuffix = tempFileName.substring( sufIdx ); try { RegistryKey skey = Registry.HKEY_CLASSES_ROOT; skey = skey.openSubKey( regSuffix ); regContentType = skey.getStringValue( "Content Type" ); skey.closeKey(); } catch ( Exception ex ) { } } if ( Package.DEBUG && Debug_ > 0 ) { System.err.println( "Win32ShellViewer: reContentType = '" + regContentType + "'" ); } if ( regContentType == null || ! mimeType.match( regContentType ) ) { tempFileName = tempFileName + fileNameSuffix; } } } if ( Package.DEBUG && Debug_ > 0 ) { System.err.println( "Win32ShellViewer: tempFileName = '" + tempFileName + "'" ); } content = dataHandler.getInputStream(); // write the contents to a temporary file FileOutputStream xfos = new FileOutputStream( tempFileName ); byte[] xbuffer = new byte[ 32 * 1024 ]; int xcount; for ( ; ; ) { xcount = content.read( xbuffer, 0, xbuffer.length ); if ( xcount == -1 ) break; xfos.write( xbuffer, 0, xcount ); } xfos.close(); content.close(); JNIDDE.shellExecute( "open", tempFileName, null, tempDirName, JNIDDE.SW_SHOWNORMAL ); } catch ( FileNotFoundException ex ) { // ex.printStackTrace( System.err ); Object[] xargs = new Object[1]; xargs[0] = ex.getMessage(); ComponentFactory.showDialog( ICEMail.getBundle(), "WinViewer.FileNotFound", 0, JOptionPane.ERROR_MESSAGE, xargs ); } catch ( NoSuchKeyException ex ) { // ex.printStackTrace( System.err ); Object[] xargs = new Object[0]; xargs[0] = ( mimeType == null ? contentType : mimeType.getBaseType() ); xargs[1] = ex.getMessage(); ComponentFactory.showDialog( ICEMail.getBundle(), "WinViewer.NoSuchKey", 0, JOptionPane.ERROR_MESSAGE, xargs ); } catch ( UnsatisfiedLinkError ex ) { // er.printStackTrace( System.err ); Object[] xargs = new Object[1]; xargs[0] = ex.getMessage(); ComponentFactory.showDialog( ICEMail.getBundle(), "WinViewer.UnsatisfiedLink", 0, JOptionPane.ERROR_MESSAGE, xargs ); } catch ( DDEException ex ) { // ex.printStackTrace( System.err ); Object[] xargs = new Object[1]; xargs[0] = ex.getMessage(); ComponentFactory.showDialog( ICEMail.getBundle(), "WinViewer.DDE", 0, JOptionPane.ERROR_MESSAGE, xargs ); } catch ( RegistryException ex ) { //ex.printStackTrace( System.err ); Object[] xargs = new Object[1]; xargs[0] = ex.getMessage(); ComponentFactory.showDialog( ICEMail.getBundle(), "WinViewer.Registry", 0, JOptionPane.ERROR_MESSAGE, xargs ); } catch ( IOException ex ) { //ex.printStackTrace( System.err ); Object[] xargs = new Object[1]; xargs[0] = ex.getMessage(); ComponentFactory.showDialog( ICEMail.getBundle(), "WinViewer.IO", 0, JOptionPane.ERROR_MESSAGE, xargs ); } catch ( MimeTypeParseException ex ) { // ex.printStackTrace( System.err ); Object[] xargs = new Object[1]; xargs[0] = ex.getMessage(); ComponentFactory.showDialog( ICEMail.getBundle(), "WinViewer.MimeTypeParse", 0, JOptionPane.ERROR_MESSAGE, xargs ); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -