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.ObjectEncoder;
18  
19  /** This class is used in conjunction with {@link CustomGMLDemo}.
20   * It appends additional attribute value pairs to the encoding
21   * of a .graph.node section in a GML file. This class is designed
22   * as a decorator.
23   */
24  public class CustomNodeObjectEncoder implements ObjectEncoder
25  {
26    
27    private java.util.List list;
28    
29    private ObjectEncoder nodeEncoder;
30    
31    /** Creates a new instance of CustomNodeObjectEncoder
32     * using the decorated nodeEncoder and the list as created by
33     * the {@link CustomGMLDemo#createNodeDataList()} method.
34     * @param nodeEncoder the nodeEncoder used for the normal GML encoding
35     * @param list the list of the meta data, as obtained from
36     * {@link CustomGMLDemo#createNodeDataList()}
37     */
38    public CustomNodeObjectEncoder(ObjectEncoder nodeEncoder, java.util.List list)
39    {
40      this.list = list;
41      this.nodeEncoder = nodeEncoder;
42    }
43    
44    /** delegates the encoding to the decorated encoder and appends
45     * the custom attributes
46    */  
47    public void encode(Object item, y.io.gml.GMLEncoder encoder) throws java.io.IOException
48    {
49      // do standard encoding
50      nodeEncoder.encode(item, encoder);
51      
52      // append the attributes
53      for (java.util.Iterator it = list.iterator(); it.hasNext();){
54        demo.io.CustomGMLDemo.NodeData data = (demo.io.CustomGMLDemo.NodeData) it.next();
55        Object value = data.getNodeMap().get(item);
56        if (value != null){
57            // boolean is handled as integer by convention in GML
58            if (data.getClassType().equals(Boolean.class)){
59              int i = (((Boolean)value).booleanValue())?1:0;
60              encoder.addAttribute(data.getPropertyName(), i);
61            } else {
62              encoder.addAttribute(data.getPropertyName(), value);
63            }
64        }
65      }
66    }  
67  }
68