1   /****************************************************************************
2    **
3    ** This file is part of yFiles-2.9. 
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-2011 by yWorks GmbH, Vor dem Kreuzberg 28, 
11   ** 72070 Tuebingen, Germany. All rights reserved.
12   **
13   ***************************************************************************/
14  package demo.io.graphml;
15  
16  import org.w3c.dom.NamedNodeMap;
17  import org.w3c.dom.Node;
18  import y.io.graphml.graph2d.ShapeNodeRealizerSerializer;
19  import y.io.graphml.input.GraphMLParseException;
20  import y.io.graphml.input.GraphMLParseContext;
21  import y.io.graphml.output.GraphMLWriteException;
22  import y.io.graphml.output.GraphMLWriteContext;
23  import y.io.graphml.output.XmlWriter;
24  import y.view.NodeRealizer;
25  
26  /**
27   * Serializer for instances of class {@link demo.io.graphml.CustomNodeRealizer}.
28   * <p>
29   * Generates XML markup nested within a node's GraphML <code>&lt;data></code>
30   * element similar to the following:
31   * </p>
32   * <pre>
33   *   &lt;custom:CustomNode customAttribute="v1.0">
34   *      &lt;custom:CustomElement value="333"/>
35   *   &lt;/custom:CustomNode>
36   * </pre>
37   * Note that for presentation purposes the content of the XML markup is used as
38   * the node's label.
39   *
40   * @see <a href="http://docs.yworks.com/yfiles/doc/developers-guide/graphml.html#graphml_custom_realizer_serializer">Section Support for Custom Realizer</a> in the yFiles for Java Developer's Guide
41   */
42  public class CustomNodeRealizerSerializer extends ShapeNodeRealizerSerializer {
43    /**
44     * Returns the string <tt>CustomNode</tt>.
45     */
46    public String getName() {
47      return "CustomNode";
48    }
49  
50  
51    public String getNamespaceURI() {
52      return "demo.io.graphml.CustomNodeRealizer";
53    }
54  
55  
56    public String getNamespacePrefix() {
57      return "custom";
58    }
59  
60    /**
61     * Returns class {@link CustomNodeRealizer}.
62     */
63    public Class getRealizerClass() {
64      return CustomNodeRealizer.class;
65    }
66  
67    /**
68     * Writes the <code>customElement</code> field of a CustomNodeRealizer object
69     * as an additional XML element.
70     * (This XML element is nested within the GraphML &lt;data> element of nodes.)
71     */
72    public void write(NodeRealizer realizer, XmlWriter writer, GraphMLWriteContext context) throws GraphMLWriteException {
73      super.write(realizer, writer, context);
74      CustomNodeRealizer fnr = (CustomNodeRealizer) realizer;
75      writer.writeStartElement(getNamespacePrefix(), "CustomElement", getNamespaceURI())
76          .writeAttribute("value", fnr.getCustomValue())
77          .writeEndElement();
78    }
79  
80    /**
81     * For demonstration purposes this method writes an additional <code>customAttribute</code> value as an XML attribute of a CustomNodeRealizer's
82     * XML markup. 
83     * (This XML attribute enhances the GraphML &lt;data&gt; element of nodes.)
84     */
85    public void writeAttributes(NodeRealizer realizer, XmlWriter writer, GraphMLWriteContext context) {
86      super.writeAttributes(realizer, writer, context);
87      CustomNodeRealizer fnr = (CustomNodeRealizer) realizer;
88      writer.writeAttribute("customAttribute", fnr.getCustomAttribute());
89    }
90  
91    /**
92     * Parses parts of the content of a GraphML file by processing its DOM structure. 
93     */
94    public void parse(NodeRealizer realizer, Node domNode, GraphMLParseContext context) throws GraphMLParseException {
95      super.parse(realizer, domNode, context);
96  
97      CustomNodeRealizer result = (CustomNodeRealizer) realizer;
98  
99      //parse attributes
100     NamedNodeMap nm = domNode.getAttributes();
101     Node a = nm.getNamedItem("customAttribute");
102     if (a != null) {
103       result.setCustomAttribute(a.getNodeValue());
104     }
105 
106     //parse elements
107     for (Node child = domNode.getFirstChild(); child != null; child = child.getNextSibling()) {
108       if (child.getNodeType() == Node.ELEMENT_NODE &&
109           "CustomElement".equals(child.getLocalName())) {
110         nm = child.getAttributes();
111         a = nm.getNamedItem("value");
112         if (a != null) {
113           result.setCustomValue(Integer.parseInt(a.getNodeValue()));
114         }
115       }
116     }
117   }
118 }
119