1   /****************************************************************************
2    **
3    ** This file is part of yFiles-2.6. 
4    ** 
5    ** yWorks proprietary/confidential. Use is subject to license terms.
6    **
7    ** Redistribution of this file or of an unauthorized byte-code version
8    ** of this file is strictly forbidden.
9    **
10   ** Copyright (c) 2000-2008 by yWorks GmbH, Vor dem Kreuzberg 28, 
11   ** 72070 Tuebingen, Germany. All rights reserved.
12   **
13   ***************************************************************************/
14  
15  package demo.io;
16  
17  import y.io.gml.GMLTokenizer.Callback;
18  import y.view.Graph2D;
19  import y.io.gml.*;
20  
21  /** This class is used to customize the process of GML encoding
22   * and parsing.
23   */
24  public class CustomGMLFactory implements y.io.gml.EncoderFactory, y.io.gml.ParserFactory
25  {
26    
27    private java.util.List nodeDataList;
28    
29    /** Constructs a new factory
30     * @param nodeDataList the list of meta data object as created in the
31     * <CODE>CustomGMLDemo#createNodeDataList()</CODE> method
32     */  
33    public CustomGMLFactory(java.util.List nodeDataList)
34    {
35      this.nodeDataList = nodeDataList;
36    }
37    
38    /** Returns a modified version of the default edge parser,
39     * this version will not encode the edgerealizer, that
40     * is edges will not contain any graphical information.
41     * This is an example of a less elaborate modification of the
42     * parser
43     */  
44    public ObjectEncoder createEdgeEncoder(ObjectEncoder graphEncoder)
45    {
46      return new y.io.gml.EdgeObjectEncoder(null, null);
47    }
48    
49    /**
50     * @param graph the graph which will be modified by this parser
51     * @return a parser
52     */
53    public Callback createEdgeParser(Graph2D graph, Callback graphParser)
54    {
55      GraphParser gp = (GraphParser) graphParser;
56      ItemParser parser = new y.io.gml.EdgeParser(graph, gp.getId2Node(), gp.getId2Edge());
57      return parser;
58    }
59    
60    public ObjectEncoder createGMLEncoder()
61    {
62      GmlObjectEncoder gmlEncoder = new GmlObjectEncoder();
63      gmlEncoder.setGraphEncoder(createGraphEncoder(gmlEncoder));
64      return gmlEncoder;
65    }
66    
67    /** return a parser which is capable of parsing a gml stream
68     * and putting the result into a graph
69     * @param graph the graph which will be modified by this parser
70     * @return a parser
71     */
72    public Callback createGMLParser(Graph2D graph)
73    {
74      ItemParser parser = new ItemParser();
75      parser.addChild("graph", (ItemParser) createGraphParser(graph, parser));
76      return parser;
77    }
78    
79    public ObjectEncoder createGraphEncoder(ObjectEncoder gmlEncoder)
80    {
81      y.io.gml.GraphObjectEncoder graphEncoder = new y.io.gml.GraphObjectEncoder();
82      graphEncoder.setEdgeEncoder(createEdgeEncoder(graphEncoder));
83      graphEncoder.setNodeEncoder(createNodeEncoder(graphEncoder));
84      return graphEncoder;
85    }
86    
87    /** return a parser which is capable of parsing the graph scope
88     * and putting the result into a graph
89     * @param graph the graph which will be modified by this parser
90     * @return a parser
91     */
92    public Callback createGraphParser(Graph2D graph, Callback gmlParser)
93    {
94      ItemParser parser = new GraphParser(graph);
95      parser.addChild("node", (ItemParser) createNodeParser(graph, parser));
96      parser.addChild("edge", (ItemParser) createEdgeParser(graph, parser));
97      return parser;
98    }
99    
100   /** Returns a customized ObjectEncoder, namely
101    * {@link CustomNodeObjectEncoder}
102    *
103    */  
104   public ObjectEncoder createNodeEncoder(ObjectEncoder graphEncoder)
105   {
106     ObjectEncoder realizerEncoder = new y.io.gml.NodeRealizerObjectEncoder();
107     ObjectEncoder nodeEncoder = new y.io.gml.NodeObjectEncoder(realizerEncoder, null);
108     return new CustomNodeObjectEncoder(nodeEncoder, nodeDataList);
109   }
110   
111   /** Returns a customized ItemParser, namely
112    * {@link CustomNodeObjectEncoder}
113    *
114    * @param graph the graph which will be modified by this parser
115    * @return a parser
116    */
117   public Callback createNodeParser(Graph2D graph, Callback graphParser)
118   {
119     GraphParser gp = (GraphParser) graphParser;
120     ItemParser parser = new CustomNodeParser(graph, gp.getId2Node(), nodeDataList);
121     return parser;
122   }
123   
124 }
125