package org.javaforum.y06.sept.xml;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.Parent;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
public class XMLReplacer
{
/**
* Test the XML Replacer
* @param args
* @throws IOException
* @throws JDOMException
*/
public static void main(String[] args) throws IOException, JDOMException
{
XMLReplacer xmlReplacer = new XMLReplacer();
InputStream inStream = XMLReplacer.class.getResourceAsStream("main.xml");
Document document = xmlReplacer.buildXML(inStream);
printXML(document);
xmlReplacer.substituteDocument(document);
printXML(document);
}
private static void printXML(Document document) throws IOException
{
printXML(document.getRootElement());
}
private static void printXML(Element element) throws IOException
{
XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
outputter.output(element, System.out);
System.out.println();
System.out.println("########");
}
/**
* Build a Document from a file
* @param inStream InputStream of the file
* @return Document
* @throws JDOMException
* @throws IOException
*/
public Document buildXML(InputStream inStream) throws JDOMException, IOException
{
SAXBuilder builder = new SAXBuilder(false);
return builder.build(inStream);
}
/**
* Build a Document from a file
* @param path Path to the file
* @return Document
* @throws JDOMException
* @throws IOException
*/
public Document buildXML(String path) throws JDOMException, IOException
{
SAXBuilder builder = new SAXBuilder(false);
File xmlFile = new File(path);
return builder.build(xmlFile);
}
/**
* Builds an xml and returns the RootElement
* @param name Name of the xml file
* @return RootElement
* @throws IOException
* @throws JDOMException
*/
public Element buildXMLAsElement(String name) throws JDOMException, IOException
{
InputStream inputStream = XMLReplacer.class.getResourceAsStream(name);
return buildXML(inputStream).getRootElement();
}
/**
* Substitutes all <include> elements in an given Document
* @param document Document, whichs <include> elements shall be substituted
* @throws JDOMException
* @throws IOException
*/
@SuppressWarnings("unchecked")
public void substituteDocument(Document document) throws JDOMException, IOException
{
// get the root element of the document
Element rootElement = document.getRootElement();
// get all childrens, which identify an element to substitute (=
// <include>), as an array
List<Element> includeElements = rootElement.getChildren("include");
int size = includeElements.size();
Element[] elementArray = new Element[size];
includeElements.toArray(elementArray);
// iterate over all <inlcude> elements
for (int i = 0; i < size; i++)
{
// get the include element from the list, and get its Parent and
// the Element of the parent
Element includeElement = elementArray[i];
Parent parent = includeElement.getParent();
Element parentElement = includeElement.getParentElement();
// get the index of the <include> element
int includeElementIndex = parent.indexOf(includeElement);
// get the name of the file, which shall be included
String srcValue = includeElement.getAttributeValue("src");
// get the content of the to-included file as Element
Element elementToInclude = buildXMLAsElement(srcValue);
// detach the <include> element, and insert the to include element
// at the given index
includeElement.detach();
parentElement.addContent(includeElementIndex, elementToInclude.detach());
}
}
}