From: Gordon M. <go...@us...> - 2006-01-10 07:54:22
|
Update of /cvsroot/bitcollider/webcollider/src/java/org/bitpedia In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv11877/src/java/org/bitpedia Modified Files: Webcollider.java Log Message: * Webcollider.java add flavor combo that's available under windows xp * webcollider.jar updated signed jar that works on XP Index: Webcollider.java =================================================================== RCS file: /cvsroot/bitcollider/webcollider/src/java/org/bitpedia/Webcollider.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -r1.1 -r1.2 *** Webcollider.java 10 Jan 2006 06:57:59 -0000 1.1 --- Webcollider.java 10 Jan 2006 07:54:12 -0000 1.2 *************** *** 1,283 **** ! /* ! * Webcollider.java ! * ! * Created on Jan 7, 2006 ! * (c) 2006 Bitzi Inc ! */ ! ! package org.bitpedia; ! import java.applet.Applet; ! import java.awt.Color; ! import java.awt.datatransfer.DataFlavor; ! import java.awt.datatransfer.Transferable; ! import java.awt.dnd.DnDConstants; ! import java.awt.dnd.DropTarget; ! import java.awt.dnd.DropTargetAdapter; ! import java.awt.dnd.DropTargetDragEvent; ! import java.awt.dnd.DropTargetDropEvent; ! import java.awt.dnd.DropTargetEvent; ! import java.io.BufferedInputStream; ! import java.io.BufferedReader; ! import java.io.Reader; ! import java.net.URL; ! import java.net.URLConnection; ! import java.util.ArrayList; ! ! import javax.swing.JOptionPane; ! ! import netscape.javascript.JSObject; ! ! import org.bitpedia.collider.BitprintAnalyzer; ! import org.bitpedia.collider.ColliderAnalyzer; ! import org.bitpedia.collider.First20Analyzer; ! import org.json.JSONException; ! import org.json.JSONObject; ! ! /** ! * Webcollider applet; accepts dropped files from local desktop, ! * calculates hashes and extracts other metadata, returns values ! * to page via call to Javascript and JSONObject. ! * ! * @author gojomo ! */ ! public class Webcollider extends Applet ! // implements Runnable ! { ! ! private static final long serialVersionUID = 1L; ! JSObject window; ! private FileDropTargetListener dropTargetAdapter; ! ! public void init() { ! try { ! window = JSObject.getWindow(this); ! } catch (Exception e) { ! System.out.println(e.getMessage()); ! } ! this.dropTargetAdapter = new FileDropTargetListener(); ! this.setDropTarget(new DropTarget(this, ! DnDConstants.ACTION_COPY_OR_MOVE, ! this.dropTargetAdapter, ! true)); ! } ! ! /** ! * Get the 'filename' from an URL (removing any leading path ! * segments). ! * ! * @param url ! * @return String filename ! */ ! public String filenameFromUrl(URL url) { ! String filename = url.getFile(); ! int lastSlash = url.getPath().lastIndexOf("/"); ! if(lastSlash==filename.length()) { ! filename = filename.substring(0,filename.length()-1); ! lastSlash = url.getPath().lastIndexOf("/"); ! } ! if(lastSlash>0) { ! filename = filename.substring(lastSlash+1); ! } ! return filename; ! } ! ! /** ! * Call the given javascript function back in the browser ! * window, with the given argument, wrapped in an array if ! * necessary. ! * @param callback String name of function to call ! * @param arg Object or Object[] to pass as arguments ! */ ! public void windowCallback(String callback, Object arg) { ! Object [] args; ! if(arg instanceof Object[]) { ! args = (Object[]) arg ; ! } else { ! args = new Object[] { arg }; ! } ! if(getParameter(callback) != null) { ! window.call(getParameter(callback),args); ! } ! } ! ! /** ! * Perform all hashing/analysis on the given URL, loading ! * the results into a JSONObject. ! * ! * @param url URL to hash/analyze ! * @return JSONObject of results ! */ ! protected JSONObject collide(URL url) { ! JSONObject json = new JSONObject(); ! long startTime = System.currentTimeMillis(); ! try { ! if(!url.toString().startsWith("file:")) { ! // only enter URL if it's not a 'file:' ! json.put("url",url.toString()); ! } ! String filename = filenameFromUrl(url); ! json.put("filename",filename); ! ! URLConnection conn = url.openConnection(); ! BufferedInputStream bis = new BufferedInputStream(conn.getInputStream()); ! ! String ct = conn.getContentType(); ! if(ct.equals("content/unknown")) { ! // TODO: bundle an extension->type map in applet ! // (javax.activations.MimetypesFileMap isn't available) ! } ! json.put("content-type",ct); ! ! ColliderAnalyzer[] analyzers = getAnalyzers(); ! for(int i = 0; i<analyzers.length;i++) { ! analyzers[i].start(json); ! } ! ! byte[] buff = new byte[65536]; ! long totalRead = 0; ! long totalSize = 0; ! long percentDone = 0; ! int read; ! while((read=bis.read(buff))>-1) { ! totalRead += read; ! if(totalSize==0) { ! totalSize = conn.getContentLength(); // TODO handle long ! // lengths ! } ! ! for(int i = 0; i<analyzers.length;i++) { ! analyzers[i].update(json,buff,read); ! } ! ! long newPercentDone = totalRead * 100 / totalSize; ! if(newPercentDone > percentDone && newPercentDone < 100) { ! percentDone = newPercentDone; ! json.put("percent-done",percentDone); ! windowCallback("progress_call",json); ! } ! } ! ! json.put("percent-done",100); ! // pass length as string to preserve precision ! json.put("length",String.valueOf(totalRead)); ! ! for(int i = 0; i<analyzers.length;i++) { ! analyzers[i].finish(json); ! } ! ! long finishTime = System.currentTimeMillis(); ! json.put("collide-time-ms",(int)(finishTime-startTime)); ! } catch (Exception e) { ! try { ! json.put("error", e.getMessage()); ! } catch (JSONException e1) { ! // TODO Auto-generated catch block ! e1.printStackTrace(); ! } ! } ! return json; ! } ! ! ! /** ! * Return an array of all applicable ColliderAnalyzer modules. ! * ! * @return array of analyzers to apply ! */ ! private ColliderAnalyzer[] getAnalyzers() { ! return new ColliderAnalyzer[] { ! new BitprintAnalyzer(), ! new First20Analyzer() ! }; ! } ! ! ! ! class FileDropTargetListener extends DropTargetAdapter { ! public void dragEnter(DropTargetDragEvent evt) { ! Webcollider.this.requestFocusInWindow(); ! Webcollider.this.setBackground(Color.PINK); ! // Iterator iter = evt.getCurrentDataFlavorsAsList().iterator(); ! // while(iter.hasNext()) { ! // System.out.println("evt flavors:"); ! // System.out.println(iter.next()); ! // } ! ! // evt.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE | DnDConstants.ACTION_LINK); ! } ! // public void dragOver(DropTargetDragEvent evt) { ! // Webcollider.this.requestFocusInWindow(); ! // evt.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE | DnDConstants.ACTION_LINK); ! // } ! public void dragExit(DropTargetEvent evt) { ! Webcollider.this.setBackground(Color.WHITE); ! // Webcollider.this.repaint(); ! // evt.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE | DnDConstants.ACTION_LINK); ! } ! ! public void drop(DropTargetDropEvent evt) { ! try { ! Transferable tr = evt.getTransferable(); ! DataFlavor[] flavors = tr.getTransferDataFlavors(); ! for (int i = 0; i < flavors.length; i++) { ! // if (flavors[i].isFlavorJavaFileListType()) { ! // evt.acceptDrop(DnDConstants.ACTION_COPY); ! // java.util.List list2 = (java.util.List) tr ! // .getTransferData(flavors[i]); ! // ArrayList filenames = new ArrayList(); ! // for (int j = 0; j < list2.size(); j++) { ! // String path = list2.get(j).toString(); ! // filenames.add(path); ! // //window.eval("alert("+tr+");"); ! // } ! // if (filenames.size() > 0) { ! // // DOIT ! // evt.dropComplete(true); ! // return; ! // } ! // } ! System.out.println(flavors[i]); ! System.out.println(" "+flavors[i].getMimeType()); ! System.out.println(" "+flavors[i].getRepresentationClass()); ! if(flavors[i].getMimeType().startsWith("text/uri-list") ! && flavors[i].getRepresentationClass() == Reader.class) { ! evt.acceptDrop(DnDConstants.ACTION_COPY); ! Webcollider.this.setBackground(Color.RED); ! Webcollider.this.repaint(); ! BufferedReader reader = new BufferedReader((Reader)tr.getTransferData(flavors[i])); ! String line; ! ArrayList urls = new ArrayList(); ! ArrayList filenames = new ArrayList(); ! while((line = reader.readLine())!=null) { ! URL url = new URL(line.trim()); ! urls.add(url); ! filenames.add(filenameFromUrl(url)); ! } ! windowCallback("start_call",filenames ); ! for(int j = 0;j<urls.size();j++) { ! JSONObject ret = collide((URL) urls.get(j)); ! System.out.println(ret.toString(1)); ! windowCallback("finish_call",ret); ! } ! ! evt.dropComplete(true); ! Webcollider.this.setBackground(Color.WHITE); ! return; ! } ! } ! JOptionPane.showMessageDialog( ! Webcollider.this, ! "none accepted", ! "Invalid File Type", ! JOptionPane.WARNING_MESSAGE); ! evt.rejectDrop(); ! ! } catch (Exception e) { ! e.printStackTrace(); ! evt.rejectDrop(); ! } ! Webcollider.this.setBackground(Color.WHITE); ! } ! } ! ! } --- 1,303 ---- ! /* ! * Webcollider.java ! * ! * Created on Jan 7, 2006 ! * (c) 2006 Bitzi Inc ! */ ! ! package org.bitpedia; ! import java.applet.Applet; ! import java.awt.Color; ! import java.awt.datatransfer.DataFlavor; ! import java.awt.datatransfer.Transferable; ! import java.awt.dnd.DnDConstants; ! import java.awt.dnd.DropTarget; ! import java.awt.dnd.DropTargetAdapter; ! import java.awt.dnd.DropTargetDragEvent; ! import java.awt.dnd.DropTargetDropEvent; ! import java.awt.dnd.DropTargetEvent; ! import java.io.BufferedInputStream; ! import java.io.BufferedReader; ! import java.io.File; ! import java.io.Reader; ! import java.net.URL; ! import java.net.URLConnection; ! import java.util.ArrayList; ! import java.util.Iterator; ! import java.util.List; ! ! import javax.swing.JOptionPane; ! ! import netscape.javascript.JSObject; ! ! import org.bitpedia.collider.BitprintAnalyzer; ! import org.bitpedia.collider.ColliderAnalyzer; ! import org.bitpedia.collider.First20Analyzer; ! import org.json.JSONException; ! import org.json.JSONObject; ! ! /** ! * Webcollider applet; accepts dropped files from local desktop, ! * calculates hashes and extracts other metadata, returns values ! * to page via call to Javascript and JSONObject. ! * ! * @author gojomo ! */ ! public class Webcollider extends Applet ! // implements Runnable ! { ! ! private static final long serialVersionUID = 1L; ! JSObject window; ! private FileDropTargetListener dropTargetAdapter; ! ! public void init() { ! try { ! window = JSObject.getWindow(this); ! } catch (Exception e) { ! System.out.println(e.getMessage()); ! } ! this.dropTargetAdapter = new FileDropTargetListener(); ! this.setDropTarget(new DropTarget(this, ! DnDConstants.ACTION_COPY_OR_MOVE, ! this.dropTargetAdapter, ! true)); ! } ! ! /** ! * Get the 'filename' from an URL (removing any leading path ! * segments). ! * ! * @param url ! * @return String filename ! */ ! public String filenameFromUrl(URL url) { ! String filename = url.getFile(); ! int lastSlash = url.getPath().lastIndexOf("/"); ! if(lastSlash==filename.length()) { ! filename = filename.substring(0,filename.length()-1); ! lastSlash = url.getPath().lastIndexOf("/"); ! } ! if(lastSlash>0) { ! filename = filename.substring(lastSlash+1); ! } ! return filename; ! } ! ! /** ! * Call the given javascript function back in the browser ! * window, with the given argument, wrapped in an array if ! * necessary. ! * @param callback String name of function to call ! * @param arg Object or Object[] to pass as arguments ! */ ! public void windowCallback(String callback, Object arg) { ! Object [] args; ! if(arg instanceof Object[]) { ! args = (Object[]) arg ; ! } else { ! args = new Object[] { arg }; ! } ! if(getParameter(callback) != null) { ! window.call(getParameter(callback),args); ! } ! } ! ! /** ! * Perform all hashing/analysis on the given URL, loading ! * the results into a JSONObject. ! * ! * @param url URL to hash/analyze ! * @return JSONObject of results ! */ ! protected JSONObject collide(URL url) { ! JSONObject json = new JSONObject(); ! long startTime = System.currentTimeMillis(); ! try { ! if(!url.toString().startsWith("file:")) { ! // only enter URL if it's not a 'file:' ! json.put("url",url.toString()); ! } ! String filename = filenameFromUrl(url); ! json.put("filename",filename); ! ! URLConnection conn = url.openConnection(); ! BufferedInputStream bis = new BufferedInputStream(conn.getInputStream()); ! ! String ct = conn.getContentType(); ! if(ct.equals("content/unknown")) { ! // TODO: bundle an extension->type map in applet ! // (javax.activations.MimetypesFileMap isn't available) ! } ! json.put("content-type",ct); ! ! ColliderAnalyzer[] analyzers = getAnalyzers(); ! for(int i = 0; i<analyzers.length;i++) { ! analyzers[i].start(json); ! } ! ! byte[] buff = new byte[65536]; ! long totalRead = 0; ! long totalSize = 0; ! long percentDone = 0; ! int read; ! while((read=bis.read(buff))>-1) { ! totalRead += read; ! if(totalSize==0) { ! totalSize = conn.getContentLength(); // TODO handle long ! // lengths ! } ! ! for(int i = 0; i<analyzers.length;i++) { ! analyzers[i].update(json,buff,read); ! } ! ! long newPercentDone = totalRead * 100 / totalSize; ! if(newPercentDone > percentDone && newPercentDone < 100) { ! percentDone = newPercentDone; ! json.put("percent-done",percentDone); ! windowCallback("progress_call",json); ! } ! } ! ! json.put("percent-done",100); ! // pass length as string to preserve precision ! json.put("length",String.valueOf(totalRead)); ! ! for(int i = 0; i<analyzers.length;i++) { ! analyzers[i].finish(json); ! } ! ! long finishTime = System.currentTimeMillis(); ! json.put("collide-time-ms",(int)(finishTime-startTime)); ! } catch (Exception e) { ! try { ! json.put("error", e.getMessage()); ! } catch (JSONException e1) { ! // TODO Auto-generated catch block ! e1.printStackTrace(); ! } ! } ! return json; ! } ! ! ! /** ! * Return an array of all applicable ColliderAnalyzer modules. ! * ! * @return array of analyzers to apply ! */ ! private ColliderAnalyzer[] getAnalyzers() { ! return new ColliderAnalyzer[] { ! new BitprintAnalyzer(), ! new First20Analyzer() ! }; ! } ! ! ! ! class FileDropTargetListener extends DropTargetAdapter { ! public void dragEnter(DropTargetDragEvent evt) { ! Webcollider.this.requestFocusInWindow(); ! Webcollider.this.setBackground(Color.PINK); ! // Iterator iter = evt.getCurrentDataFlavorsAsList().iterator(); ! // while(iter.hasNext()) { ! // System.out.println("evt flavors:"); ! // System.out.println(iter.next()); ! // } ! ! // evt.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE | DnDConstants.ACTION_LINK); ! } ! // public void dragOver(DropTargetDragEvent evt) { ! // Webcollider.this.requestFocusInWindow(); ! // evt.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE | DnDConstants.ACTION_LINK); ! // } ! public void dragExit(DropTargetEvent evt) { ! Webcollider.this.setBackground(Color.WHITE); ! // Webcollider.this.repaint(); ! // evt.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE | DnDConstants.ACTION_LINK); ! } ! ! public void drop(DropTargetDropEvent evt) { ! try { ! Transferable tr = evt.getTransferable(); ! DataFlavor[] flavors = tr.getTransferDataFlavors(); ! for (int i = 0; i < flavors.length; i++) { ! // if (flavors[i].isFlavorJavaFileListType()) { ! // evt.acceptDrop(DnDConstants.ACTION_COPY); ! // java.util.List list2 = (java.util.List) tr ! // .getTransferData(flavors[i]); ! // ArrayList filenames = new ArrayList(); ! // for (int j = 0; j < list2.size(); j++) { ! // String path = list2.get(j).toString(); ! // filenames.add(path); ! // //window.eval("alert("+tr+");"); ! // } ! // if (filenames.size() > 0) { ! // // DOIT ! // evt.dropComplete(true); ! // return; ! // } ! // } ! System.out.println(flavors[i]); ! System.out.println(" "+flavors[i].getMimeType()); ! System.out.println(" "+flavors[i].getRepresentationClass()); ! ArrayList urls = new ArrayList(); ! ArrayList filenames = new ArrayList(); ! if(flavors[i].getMimeType().startsWith("text/uri-list") ! && flavors[i].getRepresentationClass() == Reader.class) { ! // this combo was available in Firefox/Linux FC3 ! evt.acceptDrop(DnDConstants.ACTION_COPY); ! BufferedReader reader = new BufferedReader((Reader)tr.getTransferData(flavors[i])); ! String line; ! urls = new ArrayList(); ! filenames = new ArrayList(); ! while((line = reader.readLine())!=null) { ! URL url = new URL(line.trim()); ! urls.add(url); ! filenames.add(filenameFromUrl(url)); ! } ! } else if (flavors[i].getMimeType().startsWith("application/x-java-file-list") ! && flavors[i].getRepresentationClass() == List.class) { ! // this combo was available in Firefox/ Windows XP ! evt.acceptDrop(DnDConstants.ACTION_COPY); ! List files = (List) tr.getTransferData(flavors[i]); ! Iterator iter = files.iterator(); ! while(iter.hasNext()) { ! File f = (File) iter.next(); ! urls.add(f.toURL()); ! filenames.add(f.getName()); ! } ! } ! ! if(!urls.isEmpty()) { ! Webcollider.this.setBackground(Color.RED); ! Webcollider.this.repaint(); ! windowCallback("start_call",filenames ); ! for(int j = 0;j<urls.size();j++) { ! JSONObject ret = collide((URL) urls.get(j)); ! System.out.println(ret.toString(1)); ! windowCallback("finish_call",ret); ! } ! ! evt.dropComplete(true); ! Webcollider.this.setBackground(Color.WHITE); ! return; ! } ! } ! JOptionPane.showMessageDialog( ! Webcollider.this, ! "none accepted", ! "Invalid File Type", ! JOptionPane.WARNING_MESSAGE); ! evt.rejectDrop(); ! ! } catch (Exception e) { ! e.printStackTrace(); ! evt.rejectDrop(); ! } ! Webcollider.this.setBackground(Color.WHITE); ! } ! } ! ! } |