/** * GEDCOM 5.5 <-> XML representation converter, based on GenJ API * * Copyright (C) 2005 Werner Bailer * * This piece of code is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. * * This code is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ import genj.gedcom.Gedcom; import genj.gedcom.Entity; import genj.gedcom.Property; import genj.io.*; import java.io.*; import java.nio.charset.Charset; import java.nio.charset.UnsupportedCharsetException; import java.util.Collection; import java.util.Iterator; import javax.xml.parsers.*; import org.w3c.dom.*; import org.apache.xml.serialize.*; public class GedcomDOMWriter extends GedcomWriter { protected BufferedWriter out; protected Gedcom gedcom; public GedcomDOMWriter(Gedcom ged, java.lang.String name, java.lang.String enc, java.io.OutputStream stream) { // fixed encoding super(ged,name,"ISO-8859-1",stream); gedcom = ged; out = new BufferedWriter(new OutputStreamWriter(stream, Charset.forName("ISO-8859-1"))); } public static void write(java.util.List props, java.io.Writer writer) { // override ! } public boolean writeGedcom() { try { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); Document doc = docBuilder.newDocument(); Element rootElem = doc.createElement("GEDCOM"); doc.appendChild(rootElem); Collection entities = gedcom.getEntities(); for (Iterator it=entities.iterator();it.hasNext();) { Entity entity = (Entity)it.next(); writeProperty(entity,doc,rootElem); } OutputFormat outputFormat = new OutputFormat(doc); outputFormat.setEncoding("ISO-8859-1"); outputFormat.setIndenting(true); XMLSerializer serializer = new XMLSerializer(out,outputFormat); serializer.serialize(doc); return true; } catch (Exception e) { System.out.println(e.getMessage()); return false; } } protected void writeProperty(Property prop, Document doc, Element parent) { String tag = prop.getTag(); String value = prop.getValue(); String id =""; // write IDs for entities if (prop instanceof Entity) { Entity entity = (Entity) prop; id = entity.getId(); } Element elem = doc.createElement(tag); parent.appendChild(elem); // id if (id.length()>0) { Attr att = doc.createAttribute("id"); att.setValue(id); elem.setAttributeNode(att); } // value if (value.length()>0) { Attr att = doc.createAttribute("value"); att.setValue(value); elem.setAttributeNode(att); } // sub properties int num = prop.getNoOfProperties(); for (int i = 0; i < num; i++) { Property pi = prop.getProperty(i); if (!pi.isTransient()) { writeProperty(pi,doc,elem); } } } protected static String escapeSymbols(String str){ if(str != null){ String returnString = str.replaceAll("&","&" ); returnString = returnString.replaceAll("<","<" ); returnString = returnString.replaceAll(">",">" ); return returnString; } return null; } }