FileDocCategorySizeDatePackage
XMLProperties.javaAPI DocExample10552Sat Sep 01 15:40:50 BST 2001javaxml2

XMLProperties

public class XMLProperties extends Properties
XMLProperties extends Java's java.util.Properties class, and provides behavior similar to properties but that use XML as the input and output format.

Fields Summary
Constructors Summary
Methods Summary
private voidcreateXMLRepresentation(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.

param
root JDOM root Element to add children to.
param
propertyName name to base element creation on.
param
propertyValue value to use for property.

        
        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 voidload(java.io.Reader reader)

This overrides the default load() behavior to read from an XML document.

param
reader the reader to read XML from
throws
IOException - when errors occur reading.

        
        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 voidload(java.io.InputStream inputStream)

This overrides the default load() behavior to read from an XML document.

param
inputStream the input stream
throws
IOException - when errors occur reading.

         
        load(new InputStreamReader(inputStream));    
    
public voidload(java.io.File xmlDocument)

This overrides the default load() behavior to read from an XML document.

param
xmlDocument the XML document to read
throws
IOException - when errors occur reading.

        
        load(new FileReader(xmlDocument));    
    
private voidloadFromElements(java.util.List elements, java.lang.StringBuffer baseName)

This helper method loads the XML properties from a specific XML element, or set of elements.

param
elements List of elements to load from.
param
baseName the base name of this property.

        // 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 voidsave(java.io.OutputStream out, java.lang.String header)

deprecated
This method does not throw an IOException if an I/O error occurs while saving the property list. As of the Java 2 platform v1.2, the preferred way to save a properties list is via the {@link store(OutputStream out, String header} method.

        try {            
            store(out, header);
        } catch (IOException ignored) {
            // Deprecated version doesn't pass errors
        }        
    
public voidstore(java.io.Writer writer, java.lang.String header)

This will output the properties in this object as XML to the supplied output writer.

param
writer the writer to output XML to.
param
header comment to add at top of file
throws
IOException - when writing errors occur.

            
        // 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 voidstore(java.io.OutputStream out, java.lang.String header)

This will output the properties in this object as XML to the supplied output stream.

param
out the output stream.
param
header comment to add at top of file
throws
IOException - when writing errors occur.

            
        store(new OutputStreamWriter(out), header);
    
public voidstore(java.io.File xmlDocument, java.lang.String header)

This will output the properties in this object as XML to the supplied output file.

param
xmlDocument XML file to output to.
param
header comment to add at top of file
throws
IOException - when writing errors occur.

            
        store(new FileWriter(xmlDocument), header);