VisClient/org/hfbk/vis/source/Source.java

Go to the documentation of this file.
00001 package org.hfbk.vis.source;
00002 
00003 import java.io.IOException;
00004 import java.lang.reflect.Constructor;
00005 import java.net.URL;
00006 
00007 import org.dronus.graph.Edge;
00008 import org.dronus.graph.Graph;
00009 import org.dronus.graph.Node;
00010 import org.hfbk.util.HTTPUtils;
00011 import org.hfbk.vis.Prefs;
00012 
00013 
00025 public abstract class Source extends Thread {
00026         
00027         
00028 public static String baseURL;
00029         
00030         public URL url;
00031 
00032         public Graph graph;
00033         public boolean finished;
00034         
00035         protected boolean silent;
00036         
00037         int edgecount;
00038 
00039         String keyword; 
00040 
00054         public static Source getSource(String url){
00055                 
00056                 final Class<?>[] SIGNATURE={URL.class};
00057                 
00058                 Class<?> cl=null; //class of VisNode to find
00059                 URL u=null;
00060                 try{
00061                         u=new URL(url);
00062                         String source=getParam(new URL(url), "source");
00063                         
00064                         //build java classname
00065                         String classname=Character.toUpperCase(source.charAt(0))+source.substring(1);
00066                         if (classname.matches(":.*")) classname="Class";
00067                         //look up class. if it is a helper Node with no visual class, exception below. 
00068                         cl=Class.forName("org.hfbk.vis.source.Source"+classname);
00069                         //a matching visual class is found. instantiate the node. 
00070                         Constructor cns=cl.getConstructor(SIGNATURE);
00071                         return (Source)cns.newInstance(new Object[]{u});
00072                 } catch (ClassNotFoundException e) {  //legal, not all sources exists internally  
00073                         return new SourceServer(u); //fetch from server.
00074                 } catch (NoSuchMethodException e) {  //no useful constructor, encourage coder to do some..
00075                         throw new RuntimeException("Source: No valid public constructor at class "+cl);
00076                 } catch (Exception e){ //security, class load, ... 
00077                         throw new RuntimeException(e);
00078                 }
00079         }
00080         
00081         static String getParam(URL url, String param){
00082                 String value=null;
00083                 for(String part : url.getQuery().split("&")){
00084                         String[] tmp=part.split("=");
00085                         if (tmp[0].equals(param))
00086                                 if (tmp.length>1)
00087                                         value=HTTPUtils.decode(tmp[1]);
00088                                 else 
00089                                         value="";
00090                 }
00091                 return value;
00092         }       
00093         
00094         public Source(URL url) {
00095                 super("Source");                
00096                 setPriority(Thread.MIN_PRIORITY); //don't slow down animation
00097 
00098                 this.url=url;
00099                 this.keyword=getParam(url, "keyword");
00100                 this.silent=!Prefs.current.verbose;
00101                 
00102                 graph=new Graph();
00103                         
00104                 start();
00105         }
00106         
00107         public void run() {
00108                 try {
00109                         buildGraph();
00110                         finished=true; //present the result to the fetchers listener
00111                 } catch (Exception e) {
00112                         if (Prefs.current.verbose){
00113                                 System.out.println(" failed: "+e);                              
00114                         }
00115                 }               
00116         }
00117         
00118         abstract void buildGraph() throws IOException; 
00119         
00120         Node add(String text, String type, Node parent){
00121                 int id=getId(text,type);                
00122                 Node n= new Node(id, text,type);
00123                 
00124                 if (graph.findNode(id)==null)
00125                         graph.addNode(n);
00126                 
00127                 if (parent!=null){
00128                         Edge e=new Edge(n, parent, ""+edgecount++, "in");
00129                         graph.addEdge(e);
00130                 }
00131                 
00132                 return n;
00133         }       
00134         
00135         int getId(String text, String type){
00136                 return text.hashCode()^type.hashCode();
00137         }
00138         
00139 }

Generated on Tue Apr 7 17:57:20 2009 for visclient by  doxygen 1.5.1