1   /****************************************************************************
2    * This demo file is part of yFiles for Java 2.14.
3    * Copyright (c) 2000-2017 by yWorks GmbH, Vor dem Kreuzberg 28,
4    * 72070 Tuebingen, Germany. All rights reserved.
5    * 
6    * yFiles demo files exhibit yFiles for Java functionalities. Any redistribution
7    * of demo files in source code or binary form, with or without
8    * modification, is not permitted.
9    * 
10   * Owners of a valid software license for a yFiles for Java version that this
11   * demo is shipped with are allowed to use the demo source code as basis
12   * for their own yFiles for Java powered applications. Use of such programs is
13   * governed by the rights and conditions as set out in the yFiles for Java
14   * license agreement.
15   * 
16   * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESS OR IMPLIED
17   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18   * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
19   * NO EVENT SHALL yWorks BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21   * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22   * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26   *
27   ***************************************************************************/
28  package demo.io.graphml;
29  
30  import org.w3c.dom.NamedNodeMap;
31  import org.w3c.dom.Node;
32  import y.io.graphml.graph2d.ShapeNodeRealizerSerializer;
33  import y.io.graphml.input.GraphMLParseException;
34  import y.io.graphml.input.GraphMLParseContext;
35  import y.io.graphml.output.GraphMLWriteException;
36  import y.io.graphml.output.GraphMLWriteContext;
37  import y.io.graphml.output.XmlWriter;
38  import y.view.NodeRealizer;
39  
40  /**
41   * Serializer for instances of class {@link demo.io.graphml.CustomNodeRealizer}.
42   * <p>
43   * Generates XML markup nested within a node's GraphML <code>&lt;data></code>
44   * element similar to the following:
45   * </p>
46   * <pre>
47   *   &lt;custom:CustomNode customAttribute="v1.0">
48   *      &lt;custom:CustomElement value="333"/>
49   *   &lt;/custom:CustomNode>
50   * </pre>
51   * Note that for presentation purposes the content of the XML markup is used as
52   * the node's label.
53   *
54   * @see <a href="http://docs.yworks.com/yfiles/doc/api/index.html#/dguide/graphml#graphml_custom_realizer_serializer" target="_blank">Section Support for Custom Realizer</a> in the yFiles for Java Developer's Guide
55   */
56  public class CustomNodeRealizerSerializer extends ShapeNodeRealizerSerializer {
57    /**
58     * Returns the string <tt>CustomNode</tt>.
59     */
60    public String getName() {
61      return "CustomNode";
62    }
63  
64  
65    public String getNamespaceURI() {
66      return "demo.io.graphml.CustomNodeRealizer";
67    }
68  
69  
70    public String getNamespacePrefix() {
71      return "custom";
72    }
73  
74    /**
75     * Returns class {@link CustomNodeRealizer}.
76     */
77    public Class getRealizerClass() {
78      return CustomNodeRealizer.class;
79    }
80  
81    /**
82     * Writes the <code>customElement</code> field of a CustomNodeRealizer object
83     * as an additional XML element.
84     * (This XML element is nested within the GraphML &lt;data> element of nodes.)
85     */
86    public void write(NodeRealizer realizer, XmlWriter writer, GraphMLWriteContext context) throws GraphMLWriteException {
87      super.write(realizer, writer, context);
88      CustomNodeRealizer fnr = (CustomNodeRealizer) realizer;
89      writer.writeStartElement(getNamespacePrefix(), "CustomElement", getNamespaceURI())
90          .writeAttribute("value", fnr.getCustomValue())
91          .writeEndElement();
92    }
93  
94    /**
95     * For demonstration purposes this method writes an additional <code>customAttribute</code> value as an XML attribute of a CustomNodeRealizer's
96     * XML markup. 
97     * (This XML attribute enhances the GraphML &lt;data&gt; element of nodes.)
98     */
99    public void writeAttributes(NodeRealizer realizer, XmlWriter writer, GraphMLWriteContext context) {
100     super.writeAttributes(realizer, writer, context);
101     CustomNodeRealizer fnr = (CustomNodeRealizer) realizer;
102     writer.writeAttribute("customAttribute", fnr.getCustomAttribute());
103   }
104 
105   /**
106    * Parses parts of the content of a GraphML file by processing its DOM structure. 
107    */
108   public void parse(NodeRealizer realizer, Node domNode, GraphMLParseContext context) throws GraphMLParseException {
109     super.parse(realizer, domNode, context);
110 
111     CustomNodeRealizer result = (CustomNodeRealizer) realizer;
112 
113     //parse attributes
114     NamedNodeMap nm = domNode.getAttributes();
115     Node a = nm.getNamedItem("customAttribute");
116     if (a != null) {
117       result.setCustomAttribute(a.getNodeValue());
118     }
119 
120     //parse elements
121     for (Node child = domNode.getFirstChild(); child != null; child = child.getNextSibling()) {
122       if (child.getNodeType() == Node.ELEMENT_NODE &&
123           "CustomElement".equals(child.getLocalName())) {
124         nm = child.getAttributes();
125         a = nm.getNamedItem("value");
126         if (a != null) {
127           result.setCustomValue(Integer.parseInt(a.getNodeValue()));
128         }
129       }
130     }
131   }
132 }
133