Prefuse 4未完

今天才发现,prefuse的官方用户手册没有写完,只能自己边研究边写了。

    之前用到一个GraphMLReader类的GraphReader函数,就先从他入手吧:

    1、GraphMlReader

    定义:

public class GraphMLReader extends AbstractGraphReader  implements GraphReader{
/***
.....
***/


   其中:GraphReader仅仅定义了四个不同参数reader函数,其内容为:

public interface GraphReader {
  public Graph readGraph(String location) throws DataIOException;
  public Graph readGraph(URL url) throws DataIOException;
  public Graph readGraph(File f) throws DataIOException;
  public Graph readGraph(InputStream is) throws DataIOException;
}

    AbstractGraphReader也是一个抽象类,对前三个Reader进行了实现,最后一个参数为InputStream is的仍然保留为抽象函数,但是,如果观察这些实现的函数会发现,所有的读入动作,最后都交给了public Graph readGraph(InputStream is) throws DataIOException; 函数来实现,这样具体怎么读入数据,就要看集成这个抽象类的类来决定了。

    而GraphMLReader直接对这个函数进行了实现:

public Graph readGraph(InputStream is) throws DataIOException {
        try {       
            SAXParserFactory factory   = SAXParserFactory.newInstance();
            SAXParser        saxParser = factory.newSAXParser();
            
            GraphMLHandler   handler   = new GraphMLHandler();
            System.out.println("here");
            saxParser.parse(is, handler);
            return handler.getGraph();
        } catch ( Exception e ) {
            if ( e instanceof DataIOException ) {
                throw (DataIOException)e;
            } else {
                throw new DataIOException(e);
            }
        }
    }

     其中SAXParserFactory,SAXParser是java提供的一个解析XML的库文件,输入的第一个参数为InputStream,第二个参数是DefaultHandler。这里的GraphMLHandler就是一个继承了DefaultHandler的静态类,见该文件中后边部分。

posted @ 2013-10-16 20:32  joy06  阅读(238)  评论(0编辑  收藏  举报