package mpicbg.tracking.data.io;

import java.io.File;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class XmlHelpers
{
	public static Element intElement( final Document doc, final String name, final int value )
	{
		final Element e = doc.createElement( name );
		e.appendChild( doc.createTextNode( Integer.toString( value ) ) );
		return e;
	}

	public static Element doubleElement( final Document doc, final String name, final double value )
	{
		final Element e = doc.createElement( name );
		e.appendChild( doc.createTextNode( Double.toString( value ) ) );
		return e;
	}

	public static Element textElement( final Document doc, final String name, final String value )
	{
		final Element e = doc.createElement( name );
		e.appendChild( doc.createTextNode( value ) );
		return e;
	}

	public static File loadPath( final Element parent, final String name, final String defaultRelativePath, final File basePath ) throws InstantiationException, IllegalAccessException, ClassNotFoundException
	{
		final NodeList nd = parent.getElementsByTagName( name );
		final String path = nd.getLength() > 0 ? nd.item( 0 ).getTextContent() : defaultRelativePath;
		final boolean isRelative = nd.getLength() > 0 ? ( ( Element ) nd.item( 0 ) ).getAttribute( "type" ).equals( "relative" ) : true;
		if ( isRelative )
		{
			if ( basePath == null )
				return null;
			else
				return new File( basePath + "/" + path );
		}
		else
			return new File( path );
	}

	public static File loadPath( final Element parent, final String name, final File basePath ) throws InstantiationException, IllegalAccessException, ClassNotFoundException
	{
		final NodeList nd = parent.getElementsByTagName( name );
		if ( nd.getLength() == 0 )
			return null;
		final String path =  nd.item( 0 ).getTextContent();
		final boolean isRelative = ( ( Element ) nd.item( 0 ) ).getAttribute( "type" ).equals( "relative" );
		if ( isRelative )
		{
			if ( basePath == null )
				return null;
			else
				return new File( basePath + "/" + path );
		}
		else
			return new File( path );
	}

}

@@ -60,113 +60,1597 @@
 ort 
-org.w3c.dom.Document;%0Aimport org.w3c.dom.Element;%0Aimport org.w3c.dom.NodeList;%0A%0Apublic class XmlHelpers%0A%7B
+javax.xml.parsers.DocumentBuilder;%0Aimport javax.xml.parsers.DocumentBuilderFactory;%0Aimport javax.xml.parsers.ParserConfigurationException;%0Aimport javax.xml.transform.OutputKeys;%0Aimport javax.xml.transform.Transformer;%0Aimport javax.xml.transform.TransformerException;%0Aimport javax.xml.transform.TransformerFactory;%0Aimport javax.xml.transform.TransformerFactoryConfigurationError;%0Aimport javax.xml.transform.dom.DOMSource;%0Aimport javax.xml.transform.stream.StreamResult;%0A%0Aimport org.w3c.dom.Document;%0Aimport org.w3c.dom.Element;%0Aimport org.w3c.dom.NodeList;%0A%0Apublic class XmlHelpers%0A%7B%0A%09public static Document newXmlDocument() throws ParserConfigurationException%0A%09%7B%0A%09%09final DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();%0A%09%09final DocumentBuilder docBuilder = docFactory.newDocumentBuilder();%0A%09%09return docBuilder.newDocument();%0A%09%7D%0A%0A%09public static void writeXmlDocument( final Document doc, final String xmlFilename ) throws TransformerFactoryConfigurationError, TransformerException%0A%09%7B%0A%09%09writeXmlDocument( doc, new File( xmlFilename ) );%0A%09%7D%0A%0A%09public static void writeXmlDocument( final Document doc, final File xmlFile ) throws TransformerFactoryConfigurationError, TransformerException%0A%09%7B%0A%09%09final Transformer transformer = TransformerFactory.newInstance().newTransformer();%0A%09%09transformer.setOutputProperty( OutputKeys.INDENT, %22yes%22 );%0A%09%09transformer.setOutputProperty( OutputKeys.ENCODING, %22UTF-8%22 );%0A%09%09transformer.setOutputProperty( %22%7Bhttp://xml.apache.org/xslt%7Dindent-amount%22, %224%22 );%0A%09%09transformer.transform( new DOMSource( doc ), new StreamResult( xmlFile ) );%0A%09%7D%0A
 %0A%09pu

add helpers to write xml documents.