1   package demo.yext.graphml;
2   
3   import org.graphdrawing.graphml.reader.GraphMLParseContext;
4   import org.graphdrawing.graphml.writer.GraphMLWriteContext;
5   import org.graphdrawing.graphml.writer.XmlWriter;
6   import org.w3c.dom.NamedNodeMap;
7   import org.w3c.dom.Node;
8   import y.view.NodeRealizer;
9   import yext.graphml.graph2D.ShapeNodeRealizerSerializer;
10  
11  /**
12   * Serializer for instances of class {@link CustomNodeRealizer}.
13   * <p>
14   * Generates XML markup nested within a node's GraphML <code>&lt;data></code>
15   * element similar to the following:
16   * </p>
17   * <pre>
18   *   &lt;custom:CustomNode customAttribute="v1.0">
19   *      &lt;custom:CustomElement value="333"/>
20   *   &lt;/custom:CustomNode>
21   * </pre>
22   * Note that for presentation purposes the content of the XML markup is used as
23   * the node's label.
24   */
25  public class CustomNodeRealizerSerializer extends ShapeNodeRealizerSerializer
26  {
27    /**
28     * Returns the string <tt>CustomNode</tt>.
29     */
30    public String getName() {
31      return "CustomNode";
32    }
33  
34  
35    public String getNamespaceURI() {
36      return "demo.yext.graphml.CustomNodeRealizerSerializer";
37    }
38  
39  
40    /**
41     * Returns class {@link CustomNodeRealizer}.
42     */
43    public Class getRealizerClass()
44    {
45      return CustomNodeRealizer.class;
46    }
47    
48    /**
49     * Writes the <code>customElement</code> field of a CustomNodeRealizer object
50     * as an additional XML element.
51     * (This XML element is nested within the GraphML &lt;data> element of nodes.)
52     */
53    public void write(NodeRealizer realizer, XmlWriter writer, GraphMLWriteContext context)
54    {
55      super.write(realizer, writer, context);
56      CustomNodeRealizer fnr = (CustomNodeRealizer)realizer;
57      writer.writeStartElement("CustomElement",getNamespaceURI())
58          .writeAttribute("value", fnr.customValue )
59          .writeEndElement();
60    }
61    
62    /**
63     * For demonstration purposes this method writes an additional <code>customAttribute</code> value as an XML attribute of a CustomNodeRealizer's
64     * XML markup. 
65     * (This XML attribute enhances the GraphML &lt;data&gt; element of nodes.)
66     */
67    public void writeAttributes(NodeRealizer realizer, XmlWriter writer, GraphMLWriteContext context)
68    {
69      super.writeAttributes(realizer, writer, context);
70      CustomNodeRealizer fnr = (CustomNodeRealizer)realizer;
71      writer.writeAttribute("customAttribute",fnr.customAttribute);
72    }
73    
74    /**
75     * Parses parts of the content of a GraphML file by processing its DOM structure. 
76     */
77    public void parse(NodeRealizer realizer, Node domNode, GraphMLParseContext context)
78    {
79      super.parse(realizer, domNode, context);
80  
81      CustomNodeRealizer result = (CustomNodeRealizer)realizer;
82  
83      //parse attributes
84      NamedNodeMap nm = domNode.getAttributes();
85      Node a = nm.getNamedItem("customAttribute");
86      if (a != null) {
87        result.customAttribute = a.getNodeValue();
88      }
89  
90      //parse elements
91      for(Node child = domNode.getFirstChild(); child != null; child = child.getNextSibling())
92      {
93        if (child.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE &&
94          "CustomElement".equals(child.getLocalName()))
95        {
96          nm = child.getAttributes();
97          a = nm.getNamedItem("value");
98          if (a != null)
99          {
100           result.customValue = Integer.parseInt(a.getNodeValue());
101         }
102       }
103     }
104   }
105 }
106