Commit c1090b4a authored by Bill Lynch's avatar Bill Lynch Committed by bill

Added method to load XML from a stream and to get node attributes.


git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@246 b35dd754-fafc-0310-a699-88a17e54d16e
parent 2278c594
......@@ -57,6 +57,17 @@ public class XMLProperties {
this(new File(fileName));
}
/**
* Loads XML properties from a stream.
*
* @param in the input stream of XML.
* @throws IOException if an exception occurs when reading the stream.
*/
public XMLProperties(InputStream in) throws IOException {
Reader reader = new BufferedReader(new InputStreamReader(in));
buildDoc(reader);
}
/**
* Creates a new XMLProperties object.
*
......@@ -92,21 +103,9 @@ public class XMLProperties {
if (!file.canWrite()) {
throw new IOException("XML properties file must be writable: " + file.getName());
}
FileReader reader = null;
try {
reader = new FileReader(file);
SAXReader xmlReader = new SAXReader();
document = xmlReader.read(reader);
}
catch (Exception e) {
Log.error("Error reading XML properties file " + file.getName() + ".", e);
throw new IOException(e.getMessage());
}
finally {
if (reader != null) {
reader.close();
}
}
FileReader reader = new FileReader(file);
buildDoc(reader);
}
/**
......@@ -237,6 +236,38 @@ public class XMLProperties {
return props.iterator();
}
/**
* Returns the value of the attribute of the given property name or <tt>null</tt>
* if it doesn't exist. Note, this
*
* @param name the property name to lookup - ie, "foo.bar"
* @param attribute the name of the attribute, ie "id"
* @return the value of the attribute of the given property or <tt>null</tt> if
* it doesn't exist.
*/
public String getAttribute(String name, String attribute) {
if (name == null || attribute == null) {
return null;
}
String[] propName = parsePropertyName(name);
// Search for this property by traversing down the XML heirarchy.
Element element = document.getRootElement();
for (int i = 0; i < propName.length; i++) {
String child = propName[i];
element = element.element(child);
if (element == null) {
// This node doesn't match this part of the property name which
// indicates this property doesn't exist so return empty array.
break;
}
}
if (element != null) {
// Get its attribute values
return element.attributeValue(attribute);
}
return null;
}
/**
* Sets a property to an array of values. Multiple values matching the same property
* is mapped to an XML file as multiple elements containing each value.
......@@ -373,6 +404,25 @@ public class XMLProperties {
saveProperties();
}
/**
* Builds the document XML model up based the given reader of XML data.
*/
private void buildDoc(Reader in) throws IOException {
try {
SAXReader xmlReader = new SAXReader();
document = xmlReader.read(in);
}
catch (Exception e) {
Log.error("Error reading XML properties", e);
throw new IOException(e.getMessage());
}
finally {
if (in != null) {
in.close();
}
}
}
/**
* Saves the properties to disk as an XML document. A temporary file is
* used during the writing process for maximum safety.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment