package com.jigen;
import java.io.File;
import java.io.IOException;
import java.math.BigInteger;
import java.util.ArrayList;
import org.apache.xmlbeans.XmlError;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlOptions;
import com.jigen.ant.Associate;
import com.jigen.ant.Environment;
import com.jigen.ant.Icon;
import com.jigen.ant.Java;
import com.jigen.ant.Jigen;
import com.jigen.ant.Product;
import com.jigen.ant.Resource;
import com.jigen.ant.RunnableResource;
import com.jigen.ant.Shortcut;
import com.jigen.ant.Installer;
import com.jigen.xsd.JigenDocument;
import com.jigen.xsd.OsName;
import com.jigen.xsd.Pathelement;
public class XmlReader
{
final private File wixHome, jigenFile;
public XmlReader(File wixHome, File jigenFile)
{
this.wixHome = wixHome;
this.jigenFile = jigenFile;
}
public Jigen importJigen(JigenDocument document)
{
Jigen antJigen = new Jigen();
antJigen.setWixHome(wixHome);
com.jigen.xsd.JigenDocument.Jigen jigen = document.getJigen();
antJigen.addProduct (importProduct (jigen.getProduct()));
antJigen.addEnvironment(importEnvironment(jigen.getEnvironment()));
for (com.jigen.xsd.Resource resource : jigen.getResourceArray())
antJigen.addResource(importResource(resource));
for (com.jigen.xsd.Runnable resource : jigen.getRunnableArray())
antJigen.addRunnable(importRunnable(resource));
for (com.jigen.xsd.Installer installer : jigen.getInstallerArray())
antJigen.addInstaller(importInstaller(installer));
return antJigen;
}
private Product importProduct(com.jigen.xsd.Product product)
{
Product antProduct = new Product();
antProduct.setName (product.getName());
antProduct.setUnixname (product.getUnixname());
antProduct.setVersion (product.getVersion());
antProduct.setManufacturer(product.getManufacturer());
antProduct.setLanguageCode(product.getLanguagecode().intValue());
if (product.isSetLicense())
antProduct.setLicense (getFile(product.getLicense()));
return antProduct;
}
private Environment importEnvironment(com.jigen.xsd.Environment environment)
{
Environment antEnvironment = new Environment();
antEnvironment.setAddToPath (environment.isSetAddtopath() && environment.getAddtopath());
antEnvironment.setRequiredJava(environment.getRequiredjava());
return antEnvironment;
}
private RunnableResource importRunnable(com.jigen.xsd.Runnable resource)
{
RunnableResource antResource = new RunnableResource();
antResource.setName (resource.getName());
antResource.setType (resource.getType().toString());
antResource.addJava (importJava(resource.getJava()));
if (resource.isSetIcon())
antResource.addIcon (importIcon(resource.getIcon()));
for (com.jigen.xsd.Shortcut shortcut : resource.getShortcutArray())
antResource.addShortcut(importShortcut(shortcut));
for (com.jigen.xsd.Associate associate : resource.getAssociateArray())
antResource.addAssociate(importAssociate(associate));
return antResource;
}
private Java importJava(com.jigen.xsd.Java java)
{
Java antJava = new Java();
antJava.setOpenConsole(java.getOpenconsole());
antJava.setJavaOpts (java.getJavaopts());
if (java.isSetJar())
antJava.setJar(getFile(java.getJar()));
else
antJava.setClass(java.getClass1());
antJava.setParams(java.getParams());
for (Pathelement pathelement : java.getClasspath().getPathelementArray())
antJava.appendClasspathElement(getFile(pathelement.getLocation()));
return antJava;
}
private Resource importResource(com.jigen.xsd.Resource resource)
{
Resource antResource = new Resource();
antResource.setSource (getFile(resource.getSource()));
antResource.setDestination(resource.getDestination());
if (resource.isSetIcon())
antResource.addIcon(importIcon(resource.getIcon()));
for (com.jigen.xsd.Shortcut shortcut : resource.getShortcutArray())
antResource.addShortcut(importShortcut(shortcut));
return antResource;
}
private Shortcut importShortcut(com.jigen.xsd.Shortcut shortcut)
{
Shortcut antShortcut = new Shortcut();
antShortcut.setName (shortcut.getName());
antShortcut.setDescription(shortcut.getDescription());
antShortcut.setLocation (shortcut.getLocation().toString());
return antShortcut;
}
private Icon importIcon(com.jigen.xsd.Icon icon)
{
Icon antIcon = new Icon();
antIcon.setSource(getFile(icon.getSource()));
antIcon.setIndex (icon.getIndex().intValue());
return antIcon;
}
private Associate importAssociate(com.jigen.xsd.Associate associate)
{
Associate antAssociate = new Associate();
antAssociate.setExtension (associate.getExtension());
antAssociate.setContentType(associate.getContenttype());
antAssociate.setCommand (associate.getCommand());
antAssociate.setDescription(associate.getDescription());
antAssociate.addIcon (importIcon(associate.getIcon()));
return antAssociate;
}
private Installer importInstaller(com.jigen.xsd.Installer installer)
{
Installer antInstaller = new Installer();
antInstaller.setDestination(getFile(installer.getDestination()));
for(OsName.Enum target : installer.getTargetArray())
antInstaller.addTarget(target.toString());
return antInstaller;
}
private File getFile(String file)
{
File f = new File(file);
return f.isAbsolute() ? f : new File(jigenFile.getParent() + File.separator + file);
}
static public JigenDocument createEmptyJigenDocument()
{
JigenDocument document = JigenDocument.Factory.newInstance();
com.jigen.xsd.JigenDocument.Jigen jigen = document.addNewJigen();
com.jigen.xsd.Product product = jigen.addNewProduct();
com.jigen.xsd.Environment environment = jigen.addNewEnvironment();
product.setName("");
product.setManufacturer("");
product.setUnixname("");
product.setLanguagecode(BigInteger.ZERO);
product.setVersion("");
product.setLicense("");
environment.setAddtopath(false);
environment.setRequiredjava("");
return document;
}
static public JigenDocument parseJigenDocument(File jigenFile) throws XmlException, IOException
{
XmlOptions validateOptions = new XmlOptions();
ArrayList<XmlError> errorList = new ArrayList<XmlError>();
validateOptions.setLoadLineNumbers();
validateOptions.setErrorListener(errorList);
JigenDocument document = JigenDocument.Factory.parse(jigenFile, validateOptions);
if (!document.validate(validateOptions))
{
for (XmlError error : errorList)
System.err.println("Message: " + error.getMessage() + " (" + jigenFile + ":" + error.getLine() + ")");
throw new XmlException("Error validatinng jigen XML file: " + errorList);
}
return document;
}
}