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.view.Graph2D;
18  import java.util.*;
19  import y.base.NodeMap;
20  import y.base.Node;
21  
22  /** This is a customized version of the default node parsing class.
23   * The standard parsing is done by the parent class.
24   * When parsing has finished, the {@link #end()} method
25   * performs the final work.
26   */
27  public class CustomNodeParser extends y.io.gml.NodeParser
28  {
29    private List nodeDataList;
30    
31    /** Creates a new instance of CustomNodeParser
32     * The nodeDataList is provided to gain access to the meta-data
33     * objects, which describe the additional node attributes.
34     * @param nodeDataList
35     */
36    public CustomNodeParser(Graph2D graph, Map id2Node, List nodeDataList)
37    {
38      super(graph, id2Node);
39      this.nodeDataList = nodeDataList;
40    }
41    
42    /** this method is called when the parsing of the node section
43     * has ended. This implementation calls
44     * <CODE>super.end()</CODE> and then fills the NodeMaps as
45     * obtained from the List of NodeData objects with the data
46     * collected during the parsing process.
47     */  
48    public void end(){
49      super.end();
50      Node node = (Node) super.getItem();
51      Map attributes = getAttributes();
52      for (java.util.Iterator it = nodeDataList.iterator(); it.hasNext();){
53        demo.io.CustomGMLDemo.NodeData data = (demo.io.CustomGMLDemo.NodeData) it.next();
54        Object value = attributes.get(data.getPropertyName());
55        NodeMap nodeMap = data.getNodeMap();
56        if (value != null){
57            if (data.getClassType().equals(Boolean.class)){
58              boolean b = ((Number)value).intValue()>0;
59              nodeMap.setBool(node, b);
60            } else {
61              nodeMap.set(node, value);
62            }
63        }
64      }
65    }
66  }
67