Methods Summary |
---|
private void | createXMLRepresentation(org.jdom.Element root, java.lang.String propertyName, java.lang.String propertyValue) This will convert a single property and its value to
an XML element and textual value.
int split;
String name = propertyName;
Element current = root;
Element test = null;
while ((split = name.indexOf(".")) != -1) {
String subName = name.substring(0, split);
name = name.substring(split+1);
// Check for existing element
if ((test = current.getChild(subName)) == null) {
Element subElement = new Element(subName);
current.addContent(subElement);
current = subElement;
} else {
current = test;
}
}
// When out of loop, what's left is the final element's name
Element last = new Element(name);
last.setText(propertyValue);
/** Uncomment this for Attribute usage */
/*
last.setAttribute("value", propertyValue);
*/
current.addContent(last);
|
public void | load(java.io.Reader reader) This overrides the default load()
behavior to read from an XML document.
try {
// Load XML into JDOM Document
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(reader);
// Turn into properties objects
loadFromElements(doc.getRootElement().getChildren(),
new StringBuffer(""));
} catch (JDOMException e) {
throw new IOException(e.getMessage());
}
|
public void | load(java.io.InputStream inputStream) This overrides the default load()
behavior to read from an XML document.
load(new InputStreamReader(inputStream));
|
public void | load(java.io.File xmlDocument) This overrides the default load()
behavior to read from an XML document.
load(new FileReader(xmlDocument));
|
private void | loadFromElements(java.util.List elements, java.lang.StringBuffer baseName)This helper method loads the XML properties from a specific
XML element, or set of elements.
// Iterate through each element
for (Iterator i = elements.iterator(); i.hasNext(); ) {
Element current = (Element)i.next();
String name = current.getName();
String text = current.getTextTrim();
// String text = current.getAttributeValue("value");
// Don't add "." if no baseName
if (baseName.length() > 0) {
baseName.append(".");
}
baseName.append(name);
// See if we have an element value
if ((text == null) || (text.equals(""))) {
// If no text, recurse on children
loadFromElements(current.getChildren(),
baseName);
} else {
// If text, this is a property
setProperty(baseName.toString(),
text);
}
// On unwind from recursion, remove last name
if (baseName.length() == name.length()) {
baseName.setLength(0);
} else {
baseName.setLength(baseName.length() -
(name.length() + 1));
}
}
|
public void | save(java.io.OutputStream out, java.lang.String header)
try {
store(out, header);
} catch (IOException ignored) {
// Deprecated version doesn't pass errors
}
|
public void | store(java.io.Writer writer, java.lang.String header) This will output the properties in this object
as XML to the supplied output writer.
// Create a new JDOM Document with a root element "properties"
Element root = new Element("properties");
Document doc = new Document(root);
// Add in header information
Comment comment = new Comment(header);
doc.getMixedContent().add(0, comment);
// Get the property names
Enumeration propertyNames = propertyNames();
while (propertyNames.hasMoreElements()) {
String propertyName = (String)propertyNames.nextElement();
String propertyValue = getProperty(propertyName);
createXMLRepresentation(root, propertyName, propertyValue);
}
// Output document to supplied filename
XMLOutputter outputter = new XMLOutputter(" ", true);
outputter.output(doc, writer);
writer.flush();
|
public void | store(java.io.OutputStream out, java.lang.String header) This will output the properties in this object
as XML to the supplied output stream.
store(new OutputStreamWriter(out), header);
|
public void | store(java.io.File xmlDocument, java.lang.String header) This will output the properties in this object
as XML to the supplied output file.
store(new FileWriter(xmlDocument), header);
|