import genj.gedcom.Gedcom; import genj.io.GedcomReader; import genj.io.GedcomWriter; import genj.util.Origin; import java.net.URL; import java.io.*; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; import genj.report.Report; public class ReportExportXML extends Report { public String xmlFile = "output.xml"; public String xslFile = null; public String intermediateXmlFile = null; public static final String VERSION = "0.1"; public String getVersion() { return VERSION; } /** * Author */ public String getAuthor() { return "Werner Bailer"; } /** Returns the name of this report - should be localized. */ public String getName() { return i18n("name"); } /** * Some information about this report * @return Information as String */ public String getInfo() { return i18n("info"); } /** * @see genj.report.Report#accepts(java.lang.Object) */ public String accepts(Object context) { // we accept GEDCOM return context instanceof Gedcom ? getName() : null; } /** * This method actually starts this report */ public void start(Object context) { try { // convert GEDCOM file to intermediate XML println("converting to XML ... "); Gedcom gedcom = (Gedcom) context; String outputFile = xmlFile; if (xslFile != null) outputFile = intermediateXmlFile; FileOutputStream fops = new FileOutputStream(outputFile); GedcomDOMWriter gdw = new GedcomDOMWriter(gedcom,"","",fops); gdw.writeGedcom(); fops.close(); println("XML file has been written ... "); // transform intermediate XML using XSLT if (xslFile != null) { println("XSL transform ... "); doXSLTransform(intermediateXmlFile,xmlFile,xslFile); println("XSL transform done"); } println("XML Export done"); } catch (Exception e) { println("ERROR: "+e.getMessage()); } } protected static void doXSLTransform(String xmlin, String xmlout, String xsl) throws TransformerException, TransformerConfigurationException, FileNotFoundException, IOException { TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(new StreamSource(xsl)); transformer.transform(new StreamSource(new FileInputStream(xmlin)), new StreamResult(new FileOutputStream(xmlout))); } }