最近ちょっとしたことで必要になったDOMとJTreeとの相互変換について、ちょっとしたものを作りました。
// Very simple JTree <-> DOM converter
import java.io.*;
import javax.swing.tree.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
/**
JTree内に保持されているconvert javax.swing.tree.DefaultMutableTreeNode
を保存/読み込みできるようにするためのorg.w3c.dom.Elementとの相互変換
のためのクラス。
例えば、以下のようなJTreeは下のようなDOMに変換されます。
root
a
b
<?xml version="1.0" encoding="utf-8"?>
<TreeNode tag="root">
<TreeNode tag="a"/>
<TreeNode tag="b"/>
</TreeNode>
また、下のDOMから上のようなDefaultMutaleTreeNodeを作成できます。
tag名と属性名はコンストラクタで設定可能です。
*/
public class TreeDOM
{
private String attrName = null;
private String tagName = null;
TreeDOM()
{
this("tag", "TreeNode");
}
TreeDOM(String attr, String tag)
{
attrName = attr;
tagName = tag;
}
/**
* nodeからDefaultMutabletTreeNodeを作成。見た通り。
*
* @param node DOMのNode
*/
public DefaultMutableTreeNode nodeToTreeNode(Node node)
{
DefaultMutableTreeNode treeNode = new DefaultMutableTreeNode();
NamedNodeMap map = node.getAttributes();
if (map == null)
return null;
Node n = map.getNamedItem(attrName);
if (n == null)
return null;
treeNode.setUserObject(n.getNodeValue());
if (node.hasChildNodes())
{
NodeList nodeList = node.getChildNodes();
for (int i = 0;i < nodeList.getLength();i ++)
{
DefaultMutableTreeNode childTreeNode = nodeToTreeNode(nodeList.item(i));
if (childTreeNode != null)
{
treeNode.add(childTreeNode);
}
}
}
return treeNode;
}
/**
* DefaultMutableTreeNodeからDOMのElementを作成。
*
* @param treeNode 変換元
*/
public Element treeNodeToNode(DefaultMutableTreeNode treeNode)
{
Document document = null;
try
{
// DOM 作成
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
DOMImplementation domImpl = builder.getDOMImplementation();
document = domImpl.createDocument("", tagName, null);
}
// あったら困る。
catch (javax.xml.parsers.ParserConfigurationException e)
{
return null;
}
Element el = treeNodeToNode(treeNode, document, document.getDocumentElement());
return (Element)el.getFirstChild();
}
/**
* 再帰の関数。documentでNodeを作ったり、タグを足したりするので、ちょっと
* 引数が多め。(改善の余地あり。)
*
* @param treeNode 変換するべき残っている子どもnode
* @param document Node, Attr作成用
* @param node 子どもを足すcurrent node
*/
private Element treeNodeToNode(DefaultMutableTreeNode treeNode, Document document, Element node)
{
Element tagNode = document.createElement(tagName);
Attr attr = document.createAttribute(attrName);
attr.setValue((String)treeNode.getUserObject());
tagNode.setAttributeNode(attr);
node.appendChild(tagNode);
if (!treeNode.isLeaf())
{
for (int i = 0;i < treeNode.getChildCount();i ++)
{
DefaultMutableTreeNode childTreeNode =
(DefaultMutableTreeNode)treeNode.getChildAt(i);
Element n = null;
if (childTreeNode != null)
{
n = treeNodeToNode(childTreeNode, document, tagNode);
}
}
}
return node;
}
}
I want to tell you two stories from my career which I think are classic
illustrations of the difference between tech companies that are well-managed
and tech companies that are disasters. It comes down to the difference between
trusting employees and letting them get things done, versus treating them like
burger flippers that need to be monitored and controlled every minute, lest
they wander off and sabotage everything.
-- Joel Spolsky
-- "Two Stories" ( http://www.joelonsoftware.com/articles/TwoStories.html )
Knuth is not God! Typing "God" into Google and pressing "I'm Feeling Lucky"
would not lead you to his homepage.
Shlomi Fish in Hackers-IL message No. 2084 ("The Great WWW-Wisdom Shootout")
-- Shlomi Fish
-- Shlomi Fish's Aphorisms Collection ( http://www.shlomifish.org/humour.html )