diff --git a/src/helma/objectmodel/dom/XmlReader.java b/src/helma/objectmodel/dom/XmlReader.java index eb2597b7..76b0cd03 100644 --- a/src/helma/objectmodel/dom/XmlReader.java +++ b/src/helma/objectmodel/dom/XmlReader.java @@ -26,235 +26,233 @@ import helma.objectmodel.db.Property; public class XmlReader implements XmlConstants { - private HashMap convertedNodes; - private NodeManager nmgr = null; + private HashMap convertedNodes; + private NodeManager nmgr = null; - public XmlReader () { + public XmlReader () { + } + + public XmlReader (NodeManager nmgr) { + this.nmgr = nmgr; + } + + /** + * main entry to read an xml-file. + */ + public INode read (File file, INode helmaNode) throws RuntimeException { + try { + return read (new FileInputStream(file), helmaNode); + } catch (FileNotFoundException notfound) { + System.err.println ("couldn't find xml-file: " + file.getAbsolutePath ()); + return helmaNode; } - - public XmlReader (NodeManager nmgr) { - this.nmgr = nmgr; + } + + /** + * read an InputStream with xml-content. + */ + public INode read (InputStream in, INode helmaNode) throws RuntimeException { + return read (new InputSource (in), helmaNode); + } + + /** + * read an character reader with xml-content. + */ + public INode read (Reader in, INode helmaNode) throws RuntimeException { + return read (new InputSource (in), helmaNode); + } + + /** + * read an InputSource with xml-content. + */ + public INode read (InputSource in, INode helmaNode) throws RuntimeException { + if (helmaNode==null && nmgr==null) + throw new RuntimeException ("can't create a new Node without a NodeManager"); + Document document = XmlUtil.parse (in); + Element element = XmlUtil.getFirstElement(document); + if (element==null) + throw new RuntimeException ("corrupted xml-file"); + + if (helmaNode==null) { + return convert (element); + } else { + convertedNodes = new HashMap (); + INode convertedNode = convert (element, helmaNode); + convertedNodes = null; + return convertedNode; + } + } + + /** + * convert children of an Element to a given helmaNode + */ + public INode convert (Element element, INode helmaNode) { + String idref = element.getAttribute("idref"); + String key = idref + "-" + element.getAttribute("prototyperef"); + if( idref!=null && !idref.equals("") ) { + if( convertedNodes.containsKey(key) ) { + return (INode)convertedNodes.get(key); + } + } + key = element.getAttribute("id") + "-" + element.getAttribute("prototype"); + convertedNodes.put( key, helmaNode ); + String prototype = element.getAttribute("prototype"); + if( !prototype.equals("") && !prototype.equals("hopobject") ) { + helmaNode.setPrototype( prototype ); + } + children(helmaNode, element); + return helmaNode; + } + + + // used by convert(Element,INode) + private INode children( INode helmaNode, Element element ) { + NodeList list = element.getChildNodes(); + int len = list.getLength(); + Element childElement; + for ( int i=0; i0 ) - helmaNode.setPropMap (propMap); - else - helmaNode.setPropMap (null); - if ( subnodes.size()>0 ) - helmaNode.setSubnodes (subnodes); - else - helmaNode.setSubnodes (null); - return helmaNode; + // now loop through all child elements and retrieve properties/subnodes for this node. + NodeList list = element.getChildNodes(); + int len = list.getLength(); + Hashtable propMap = new Hashtable(); + List subnodes = new ExternalizableVector(); + + for ( int i=0; i0 ) + helmaNode.setPropMap (propMap); + else + helmaNode.setPropMap (null); + if ( subnodes.size()>0 ) + helmaNode.setSubnodes (subnodes); + else + helmaNode.setSubnodes (null); + return helmaNode; + } }