[ Main Page ]

DOMとJTreeとの相互変換

最近ちょっとしたことで必要になった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;
	    }
	}
	      
      
5. Authentication:

In the simple old days, to recognize someone you'd just look at his face and
try to remember who it is (if you didn't forget it in one of the Blue Screen
of Death episodes). But there's a much better way, which is more
mathematically-sound: RSA! Why try to remember a (many times ugly) face, when
instead you can remember a person's 1024 bit RSA key? (remembering 1024 ones
and zeros is a lot of fun! try it!) Then, when you meet the other person, and
you want to be sure it is *really* that guy, not some Hannibal Lektor who
pealed his face off and wore it, all you need to do is to make up a random
number (try not to choose 7, because that is too easily guessable!), do some
fun arithmetic with 1024 digit numbers, and then tell the other person the
result (hoping that the other guy doesn't get bored by you reading out aloud
the digits "one" and "zero" a thousand times) and ask him to try to guess the
random number from it. If he succeeds, he's not Hannibal Lektor - but he's
probably mad anyway.

    -- Nadav Har'El
    -- Hackers-IL Message No. 1,408 ( http://tech.groups.yahoo.com/group/hackers-il/message/1408 )

Chuck Norris is the ghost author of the entire Debian GNU/Linux distribution.
And he wrote it in 24 hours, while taking snack breaks.

    -- Shlomi Fish
    -- Chuck 
                      Norris Facts by Shlomi Fish and Friends ( http://www.shlomifish.org/humour/bits/facts/Chuck-Norris/ )


Powered by UNIX fortune(6)
[ Main Page ]