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 ReportImportXML extends Report { public String xmlFile = "input.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 { println("starting import from XML"); Gedcom gedcom = (Gedcom) context; // if XSL is specified, transfrom to intermediate XML first if (xslFile != null) { println("doing XSL transform ..."); doXSLTransform(intermediateXmlFile,xmlFile,xslFile); println("XSL transform done"); } String inputFile = xmlFile; if (xslFile != null) inputFile = intermediateXmlFile; println("reading XML file ..."); File file = new File(inputFile); URL url = file.toURL(); GedcomDOMReader gdr = new GedcomDOMReader(Origin.create(url)); gdr.setGedcom(gedcom); println("parsing XML file ..."); context = gdr.read(); println("done with XML import"); /* FileOutputStream fops = new FileOutputStream(gedFile); GedcomWriter writer = new GedcomWriter(gedcom, gedFile, "LATIN1", fops); writer.writeGedcom(); fops.close(); */ } catch (Exception e) { println(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))); } }