com.mxgraph.io.mxStylesheetCodec Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jgraphx Show documentation
Show all versions of jgraphx Show documentation
JGraphX Swing Component - Java Graph Visualization Library
This is a binary & source redistribution of the original, unmodified JGraphX library originating from:
"https://github.com/jgraph/jgraphx/archive/v3.4.1.3.zip".
The purpose of this redistribution is to make the library available to other Maven projects.
/**
* $Id: mxStylesheetCodec.java,v 1.11 2011/06/13 08:18:42 gaudenz Exp $
* Copyright (c) 2006-2010, JGraph Ltd
*/
package com.mxgraph.io;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import com.mxgraph.util.mxUtils;
import com.mxgraph.view.mxStylesheet;
/**
* Codec for mxStylesheets. This class is created and registered
* dynamically at load time and used implicitely via mxCodec
* and the mxCodecRegistry.
*/
public class mxStylesheetCodec extends mxObjectCodec
{
/**
* Constructs a new model codec.
*/
public mxStylesheetCodec()
{
this(new mxStylesheet());
}
/**
* Constructs a new stylesheet codec for the given template.
*/
public mxStylesheetCodec(Object template)
{
this(template, null, null, null);
}
/**
* Constructs a new model codec for the given arguments.
*/
public mxStylesheetCodec(Object template, String[] exclude,
String[] idrefs, Map mapping)
{
super(template, exclude, idrefs, mapping);
}
/**
* Encodes the given mxStylesheet.
*/
public Node encode(mxCodec enc, Object obj)
{
Element node = enc.document.createElement(getName());
if (obj instanceof mxStylesheet)
{
mxStylesheet stylesheet = (mxStylesheet) obj;
Iterator>> it = stylesheet
.getStyles().entrySet().iterator();
while (it.hasNext())
{
Map.Entry> entry = it.next();
Element styleNode = enc.document.createElement("add");
String stylename = entry.getKey();
styleNode.setAttribute("as", stylename);
Map style = entry.getValue();
Iterator> it2 = style.entrySet()
.iterator();
while (it2.hasNext())
{
Map.Entry entry2 = it2.next();
Element entryNode = enc.document.createElement("add");
entryNode.setAttribute("as",
String.valueOf(entry2.getKey()));
entryNode.setAttribute("value", getStringValue(entry2));
styleNode.appendChild(entryNode);
}
if (styleNode.getChildNodes().getLength() > 0)
{
node.appendChild(styleNode);
}
}
}
return node;
}
/**
* Returns the string for encoding the given value.
*/
protected String getStringValue(Map.Entry entry)
{
if (entry.getValue() instanceof Boolean)
{
return ((Boolean) entry.getValue()) ? "1" : "0";
}
return entry.getValue().toString();
}
/**
* Decodes the given mxStylesheet.
*/
public Object decode(mxCodec dec, Node node, Object into)
{
Object obj = null;
if (node instanceof Element)
{
String id = ((Element) node).getAttribute("id");
obj = dec.objects.get(id);
if (obj == null)
{
obj = into;
if (obj == null)
{
obj = cloneTemplate(node);
}
if (id != null && id.length() > 0)
{
dec.putObject(id, obj);
}
}
node = node.getFirstChild();
while (node != null)
{
if (!processInclude(dec, node, obj)
&& node.getNodeName().equals("add")
&& node instanceof Element)
{
String as = ((Element) node).getAttribute("as");
if (as != null && as.length() > 0)
{
String extend = ((Element) node).getAttribute("extend");
Map style = (extend != null) ? ((mxStylesheet) obj)
.getStyles().get(extend) : null;
if (style == null)
{
style = new Hashtable();
}
else
{
style = new Hashtable(style);
}
Node entry = node.getFirstChild();
while (entry != null)
{
if (entry instanceof Element)
{
Element entryElement = (Element) entry;
String key = entryElement.getAttribute("as");
if (entry.getNodeName().equals("add"))
{
String text = entry.getTextContent();
Object value = null;
if (text != null && text.length() > 0)
{
value = mxUtils.eval(text);
}
else
{
value = entryElement
.getAttribute("value");
}
if (value != null)
{
style.put(key, value);
}
}
else if (entry.getNodeName().equals("remove"))
{
style.remove(key);
}
}
entry = entry.getNextSibling();
}
((mxStylesheet) obj).putCellStyle(as, style);
}
}
node = node.getNextSibling();
}
}
return obj;
}
}