00001
00002
00003
00004
package org.nees.rbnb;
00005
00006
import java.util.Properties;
00007
import java.util.Enumeration;
00008
00009
import java.io.IOException;
00010
import java.io.StringReader;
00011
import java.io.BufferedReader;
00012
00013
import org.w3c.dom.Document;
00014
import org.w3c.dom.Element;
00015
import org.w3c.dom.NodeList;
00016
import org.w3c.dom.Node;
00017
00021 public class MetaDataPropertyList extends Properties {
00022
00023
static final String PROPERTY_TAG =
"metadata-property-list";
00024
00025
public String toXmlString()
throws IOException
00026 {
00027 Document doc =
Xml.createDocument();
00028 Node node, root = doc.createElement(PROPERTY_TAG);
00029 Enumeration e =
this.keys();
00030 String key, value;
00031
while (e.hasMoreElements())
00032 {
00033 key = (String) e.nextElement();
00034 value =
this.getProperty(key);
00035 node = doc.createElement(key);
00036 node.appendChild(doc.createTextNode(value));
00037 root.appendChild(node);
00038 }
00039 doc.appendChild(root);
00040
00041 String out =
"";
00042
try
00043 {
00044 out =
Xml.writeDocumentToString(doc);
00045 }
catch (IOException e1) {
00046
throw new IOException(
"MetaDataPropertyList Failed to generate xml string " + e1);
00047 }
00048
return out;
00049 }
00050
00051
public void initializeFromXml(String xml)
throws IllegalArgumentException, IOException
00052 {
00053
this.clear();
00054 addFromXml(xml);
00055 }
00056
00057
public void addFromXml(String xml)
00058 {
00059
00060 Document doc =
Xml.readDocumentFromString(xml);
00061
00062
00063 Element root = doc.getDocumentElement();
00064
if (!(root.getTagName().equals(PROPERTY_TAG)))
00065
throw new IllegalArgumentException(
00066
"XML string does not have root tag with " + PROPERTY_TAG
00067 +
"; found " + root.getTagName() +
" instead.");
00068
00069
00070 NodeList children = root.getChildNodes();
00071
final int length = children.getLength();
00072
for(
int i = 0; i < length; i++)
00073 {
00074 Node child = children.item(i);
00075
if (child.getNodeType() == Node.ELEMENT_NODE)
00076 {
00077 Element element = (Element)child;
00078 String key = element.getTagName();
00079 Node textNode = child.getChildNodes().item(0);
00080 String value = textNode.getNodeValue();
00081
this.setProperty(key,value);
00082 }
00083 }
00084 }
00085 }