/** * 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.*; 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 genj.util.Origin; import org.apache.xerces.parsers.*; import org.xml.sax.*; public class GedcomDOMReader extends GedcomReader { private Gedcom gedcom; private BufferedReader in; private Origin origin; private Thread worker; private Object lock = new Object(); public GedcomDOMReader(Origin org) throws IOException { super(org); // open origin InputStream oin = org.open(); // init some data in = new BufferedReader(new InputStreamReader(oin, Charset.forName("ISO-8859-1"))); origin = org; gedcom = new Gedcom(origin); gedcom.setEncoding(Gedcom.LATIN1); // Done } public void setGedcom(Gedcom gedcom) { this.gedcom = gedcom; } public Gedcom read() throws GedcomIOException, GedcomFormatException { // Remember working thread synchronized (lock) { worker=Thread.currentThread(); } // try it try { DOMParser domParser = new DOMParser(); InputSource ips = new InputSource(in); domParser.parse(ips); Document doc = domParser.getDocument(); Element rootElem = doc.getDocumentElement(); if (!rootElem.getTagName().equals("GEDCOM")) throw new GedcomIOException("XML root tag is not GEDCOM", 0); parseProperty(doc,rootElem,gedcom,null); //readGedcom(); return gedcom; } catch (Exception e) { e.printStackTrace(); throw new GedcomIOException(e.getMessage(), 0); } finally { // close in try { in.close(); } catch (Throwable t) {}; // forget working thread synchronized (lock) { worker=null; } } // nothing happening here } protected void parseProperty(Document doc, Element propertyElem, Gedcom gedcom, Property prop) throws GedcomException { String tag = propertyElem.getTagName(); Property newProperty = null; if (!tag.equals("GEDCOM")) { String id = null; String value = null; if (propertyElem.hasAttribute("id")) id = propertyElem.getAttribute("id"); if (propertyElem.hasAttribute("value")) value = propertyElem.getAttribute("value"); // if we are on top level, create an entity if (prop == null) { genj.gedcom.Entity entity = gedcom.createEntity(tag,id); newProperty = entity; } else { // get tag path to parent TagPath tagpath = prop.getPath(); tagpath = new TagPath(tagpath,tag); // get meta-property MetaProperty mp = MetaProperty.get(tagpath); if (value == null) value = ""; newProperty = mp.create(value); prop.addProperty(newProperty); } } // iterate through children Node childNode = propertyElem.getFirstChild(); Element child = null; while (childNode != null) { if (childNode instanceof Element) { child = (Element) childNode; parseProperty(doc,child,gedcom,newProperty); } childNode = childNode.getNextSibling(); } } } //GedcomDOMReader