fixed formatting and indentation.
This commit is contained in:
parent
96cd8c406c
commit
47a3202ba8
1 changed files with 222 additions and 224 deletions
|
@ -26,235 +26,233 @@ import helma.objectmodel.db.Property;
|
||||||
|
|
||||||
public class XmlReader implements XmlConstants {
|
public class XmlReader implements XmlConstants {
|
||||||
|
|
||||||
private HashMap convertedNodes;
|
private HashMap convertedNodes;
|
||||||
private NodeManager nmgr = null;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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; i<len; i++ ) {
|
||||||
|
try {
|
||||||
|
childElement = (Element)list.item(i);
|
||||||
|
} catch( ClassCastException e ) {
|
||||||
|
// ignore CDATA, comments etc
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
INode workNode = null;
|
||||||
|
|
||||||
|
if ( childElement.getTagName().equals("hop:child") ) {
|
||||||
|
convert (childElement, helmaNode.createNode(null));
|
||||||
|
} else if ( !"".equals(childElement.getAttribute("id")) ||
|
||||||
|
!"".equals(childElement.getAttribute("idref")) ) {
|
||||||
|
String childTagName = childElement.getTagName();
|
||||||
|
INode newNode = convert (childElement, helmaNode.createNode (childTagName));
|
||||||
|
helmaNode.setNode (childTagName, newNode);
|
||||||
|
} else {
|
||||||
|
String type = childElement.getAttribute("type");
|
||||||
|
String key = childElement.getTagName();
|
||||||
|
String content = XmlUtil.getTextContent(childElement);
|
||||||
|
if ( type.equals("boolean") ) {
|
||||||
|
if ( content.equals("true") ) {
|
||||||
|
helmaNode.setBoolean(key,true);
|
||||||
|
} else {
|
||||||
|
helmaNode.setBoolean(key,false);
|
||||||
|
}
|
||||||
|
} else if ( type.equals("date") ) {
|
||||||
|
SimpleDateFormat format = new SimpleDateFormat ( DATEFORMAT );
|
||||||
|
try {
|
||||||
|
Date date = format.parse(content);
|
||||||
|
helmaNode.setDate(key, date);
|
||||||
|
} catch ( ParseException e ) {
|
||||||
|
helmaNode.setString(key,content);
|
||||||
|
}
|
||||||
|
} else if ( type.equals("float") ) {
|
||||||
|
helmaNode.setFloat(key, (new Double(content)).doubleValue() );
|
||||||
|
} else if ( type.equals("integer") ) {
|
||||||
|
helmaNode.setInteger(key, (new Long(content)).longValue() );
|
||||||
|
} else {
|
||||||
|
helmaNode.setString(key,content);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return helmaNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This is a basic de-serialization method for XML-2-Node conversion.
|
||||||
|
* It reads a Node from a database-like file and should return a Node
|
||||||
|
* that matches exactly the one dumped to that file before.
|
||||||
|
* It only supports persistent-capable Nodes (from objectmodel.db-package).
|
||||||
|
*/
|
||||||
|
public helma.objectmodel.db.Node convert (Element element) {
|
||||||
|
// FIXME: this method should use Element.getAttributeNS():
|
||||||
|
// FIXME: do we need the name value or is it retrieved through mappings anyway?
|
||||||
|
String name = element.getAttribute("name");
|
||||||
|
// String name = null;
|
||||||
|
String id = element.getAttribute("id");
|
||||||
|
String prototype = element.getAttribute("prototype");
|
||||||
|
if ( "".equals(prototype) )
|
||||||
|
prototype = "hopobject";
|
||||||
|
helma.objectmodel.db.Node helmaNode = null;
|
||||||
|
try {
|
||||||
|
long created = Long.parseLong (element.getAttribute ("created"));
|
||||||
|
long lastmodified = Long.parseLong (element.getAttribute ("lastModified"));
|
||||||
|
helmaNode = new helma.objectmodel.db.Node (name,id,prototype,nmgr.safe,created,lastmodified);
|
||||||
|
} catch ( NumberFormatException e ) {
|
||||||
|
helmaNode = new helma.objectmodel.db.Node (name,id,prototype,nmgr.safe);
|
||||||
}
|
}
|
||||||
|
|
||||||
public XmlReader (NodeManager nmgr) {
|
// now loop through all child elements and retrieve properties/subnodes for this node.
|
||||||
this.nmgr = nmgr;
|
NodeList list = element.getChildNodes();
|
||||||
}
|
int len = list.getLength();
|
||||||
|
Hashtable propMap = new Hashtable();
|
||||||
/**
|
List subnodes = new ExternalizableVector();
|
||||||
* main entry to read an xml-file.
|
|
||||||
*/
|
for ( int i=0; i<len; i++ ) {
|
||||||
public INode read (File file, INode helmaNode) throws RuntimeException {
|
Element childElement;
|
||||||
try {
|
try {
|
||||||
return read (new FileInputStream(file), helmaNode);
|
childElement = (Element)list.item(i);
|
||||||
} catch (FileNotFoundException notfound) {
|
} catch( ClassCastException e ) {
|
||||||
System.err.println ("couldn't find xml-file: " + file.getAbsolutePath ());
|
// ignore CDATA, comments etc
|
||||||
return helmaNode;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
if ( childElement.getTagName().equals("hop:child") ) {
|
||||||
/**
|
// add a new NodeHandle, presume all IDs in this objectcache are unique,
|
||||||
* read an InputStream with xml-content.
|
// a prerequisite for a simple internal database.
|
||||||
*/
|
subnodes.add (new NodeHandle (new DbKey(null,childElement.getAttribute("idref") ) ) );
|
||||||
public INode read (InputStream in, INode helmaNode) throws RuntimeException {
|
continue;
|
||||||
return read (new InputSource (in), helmaNode);
|
}
|
||||||
}
|
|
||||||
|
if ( childElement.getTagName().equals("hop:parent") ) {
|
||||||
/**
|
// add a NodeHandle to parent object
|
||||||
* read an character reader with xml-content.
|
helmaNode.setParentHandle (new NodeHandle (new DbKey(null,childElement.getAttribute("idref") ) ) );
|
||||||
*/
|
continue;
|
||||||
public INode read (Reader in, INode helmaNode) throws RuntimeException {
|
}
|
||||||
return read (new InputSource (in), helmaNode);
|
|
||||||
}
|
// if we come until here, childelement is a property value
|
||||||
|
Property prop = new Property (childElement.getTagName(), helmaNode);
|
||||||
/**
|
if ( !"".equals(childElement.getAttribute("id")) ||
|
||||||
* read an InputSource with xml-content.
|
!"".equals(childElement.getAttribute("idref")) ) {
|
||||||
*/
|
// we've got an object!
|
||||||
public INode read (InputSource in, INode helmaNode) throws RuntimeException {
|
String idref = childElement.getAttribute("idref");
|
||||||
if (helmaNode==null && nmgr==null)
|
prop.setNodeHandle (new NodeHandle(new DbKey(null,idref)));
|
||||||
throw new RuntimeException ("can't create a new Node without a NodeManager");
|
} else {
|
||||||
Document document = XmlUtil.parse (in);
|
String type = childElement.getAttribute("type");
|
||||||
Element element = XmlUtil.getFirstElement(document);
|
String content = XmlUtil.getTextContent(childElement);
|
||||||
if (element==null)
|
if ( type.equals("boolean") ) {
|
||||||
throw new RuntimeException ("corrupted xml-file");
|
if ( content.equals("true") ) {
|
||||||
|
prop.setBooleanValue(true);
|
||||||
if (helmaNode==null) {
|
} else {
|
||||||
return convert (element);
|
prop.setBooleanValue(false);
|
||||||
} else {
|
}
|
||||||
convertedNodes = new HashMap ();
|
} else if ( type.equals("date") ) {
|
||||||
INode convertedNode = convert (element, helmaNode);
|
SimpleDateFormat format = new SimpleDateFormat ( DATEFORMAT );
|
||||||
convertedNodes = null;
|
try {
|
||||||
return convertedNode;
|
Date date = format.parse(content);
|
||||||
}
|
prop.setDateValue (date);
|
||||||
}
|
} catch ( ParseException e ) {
|
||||||
|
prop.setStringValue (content);
|
||||||
/**
|
}
|
||||||
* convert children of an Element to a given helmaNode
|
} else if ( type.equals("float") ) {
|
||||||
*/
|
prop.setFloatValue ((new Double(content)).doubleValue());
|
||||||
public INode convert (Element element, INode helmaNode) {
|
} else if ( type.equals("integer") ) {
|
||||||
String idref = element.getAttribute("idref");
|
prop.setIntegerValue ((new Long(content)).longValue());
|
||||||
String key = idref + "-" + element.getAttribute("prototyperef");
|
} else {
|
||||||
if( idref!=null && !idref.equals("") ) {
|
prop.setStringValue (content);
|
||||||
if( convertedNodes.containsKey(key) ) {
|
}
|
||||||
return (INode)convertedNodes.get(key);
|
}
|
||||||
}
|
propMap.put (childElement.getTagName().toLowerCase(), prop);
|
||||||
}
|
|
||||||
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; i<len; i++ ) {
|
|
||||||
try {
|
|
||||||
childElement = (Element)list.item(i);
|
|
||||||
} catch( ClassCastException e ) {
|
|
||||||
continue; // ignore CDATA, comments etc
|
|
||||||
}
|
|
||||||
INode workNode = null;
|
|
||||||
|
|
||||||
if ( childElement.getTagName().equals("hop:child") ) {
|
|
||||||
|
|
||||||
convert (childElement, helmaNode.createNode(null));
|
|
||||||
|
|
||||||
} else if ( !"".equals(childElement.getAttribute("id")) || !"".equals(childElement.getAttribute("idref")) ) {
|
|
||||||
|
|
||||||
String childTagName = childElement.getTagName();
|
|
||||||
INode newNode = convert (childElement, helmaNode.createNode (childTagName));
|
|
||||||
helmaNode.setNode (childTagName, newNode);
|
|
||||||
|
|
||||||
} else {
|
|
||||||
|
|
||||||
String type = childElement.getAttribute("type");
|
|
||||||
String key = childElement.getTagName();
|
|
||||||
String content = XmlUtil.getTextContent(childElement);
|
|
||||||
if ( type.equals("boolean") ) {
|
|
||||||
if ( content.equals("true") ) {
|
|
||||||
helmaNode.setBoolean(key,true);
|
|
||||||
} else {
|
|
||||||
helmaNode.setBoolean(key,false);
|
|
||||||
}
|
|
||||||
} else if ( type.equals("date") ) {
|
|
||||||
SimpleDateFormat format = new SimpleDateFormat ( DATEFORMAT );
|
|
||||||
try {
|
|
||||||
Date date = format.parse(content);
|
|
||||||
helmaNode.setDate(key, date);
|
|
||||||
} catch ( ParseException e ) {
|
|
||||||
helmaNode.setString(key,content);
|
|
||||||
}
|
|
||||||
} else if ( type.equals("float") ) {
|
|
||||||
helmaNode.setFloat(key, (new Double(content)).doubleValue() );
|
|
||||||
} else if ( type.equals("integer") ) {
|
|
||||||
helmaNode.setInteger(key, (new Long(content)).longValue() );
|
|
||||||
} else {
|
|
||||||
helmaNode.setString(key,content);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return helmaNode;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This is a basic de-serialization method for XML-2-Node conversion.
|
|
||||||
* It reads a Node from a database-like file and should return a Node
|
|
||||||
* that matches exactly the one dumped to that file before.
|
|
||||||
* It only supports persistent-capable Nodes (from objectmodel.db-package).
|
|
||||||
*/
|
|
||||||
public helma.objectmodel.db.Node convert (Element element) {
|
|
||||||
// FIXME: this method should use Element.getAttributeNS():
|
|
||||||
// FIXME: do we need the name value or is it retrieved through mappings anyway?
|
|
||||||
String name = element.getAttribute("name");
|
|
||||||
// String name = null;
|
|
||||||
String id = element.getAttribute("id");
|
|
||||||
String prototype = element.getAttribute("prototype");
|
|
||||||
if ( "".equals(prototype) )
|
|
||||||
prototype = "hopobject";
|
|
||||||
helma.objectmodel.db.Node helmaNode = null;
|
|
||||||
try {
|
|
||||||
long created = Long.parseLong (element.getAttribute ("created"));
|
|
||||||
long lastmodified = Long.parseLong (element.getAttribute ("lastModified"));
|
|
||||||
helmaNode = new helma.objectmodel.db.Node (name,id,prototype,nmgr.safe,created,lastmodified);
|
|
||||||
} catch ( NumberFormatException e ) {
|
|
||||||
helmaNode = new helma.objectmodel.db.Node (name,id,prototype,nmgr.safe);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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; i<len; i++ ) {
|
|
||||||
|
|
||||||
Element childElement;
|
|
||||||
try {
|
|
||||||
childElement = (Element)list.item(i);
|
|
||||||
} catch( ClassCastException e ) {
|
|
||||||
continue; // ignore CDATA, comments etc
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( childElement.getTagName().equals("hop:child") ) {
|
|
||||||
// add a new NodeHandle, presume all IDs in this objectcache are unique,
|
|
||||||
// a prerequisite for a simple internal database.
|
|
||||||
subnodes.add (new NodeHandle (new DbKey(null,childElement.getAttribute("idref") ) ) );
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( childElement.getTagName().equals("hop:parent") ) {
|
|
||||||
// add a NodeHandle to parent object
|
|
||||||
helmaNode.setParentHandle (new NodeHandle (new DbKey(null,childElement.getAttribute("idref") ) ) );
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// if we come until here, childelement is a property value
|
|
||||||
Property prop = new Property (childElement.getTagName(), helmaNode);
|
|
||||||
if ( !"".equals(childElement.getAttribute("id")) || !"".equals(childElement.getAttribute("idref")) ) {
|
|
||||||
// we've got an object!
|
|
||||||
String idref = childElement.getAttribute("idref");
|
|
||||||
prop.setNodeHandle (new NodeHandle(new DbKey(null,idref)));
|
|
||||||
|
|
||||||
} else {
|
|
||||||
String type = childElement.getAttribute("type");
|
|
||||||
String content = XmlUtil.getTextContent(childElement);
|
|
||||||
if ( type.equals("boolean") ) {
|
|
||||||
if ( content.equals("true") ) {
|
|
||||||
prop.setBooleanValue(true);
|
|
||||||
} else {
|
|
||||||
prop.setBooleanValue(false);
|
|
||||||
}
|
|
||||||
} else if ( type.equals("date") ) {
|
|
||||||
SimpleDateFormat format = new SimpleDateFormat ( DATEFORMAT );
|
|
||||||
try {
|
|
||||||
Date date = format.parse(content);
|
|
||||||
prop.setDateValue (date);
|
|
||||||
} catch ( ParseException e ) {
|
|
||||||
prop.setStringValue (content);
|
|
||||||
}
|
|
||||||
} else if ( type.equals("float") ) {
|
|
||||||
prop.setFloatValue ((new Double(content)).doubleValue());
|
|
||||||
} else if ( type.equals("integer") ) {
|
|
||||||
prop.setIntegerValue ((new Long(content)).longValue());
|
|
||||||
} else {
|
|
||||||
prop.setStringValue (content);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
propMap.put (childElement.getTagName().toLowerCase(), prop);
|
|
||||||
}
|
|
||||||
if ( propMap.size()>0 )
|
|
||||||
helmaNode.setPropMap (propMap);
|
|
||||||
else
|
|
||||||
helmaNode.setPropMap (null);
|
|
||||||
if ( subnodes.size()>0 )
|
|
||||||
helmaNode.setSubnodes (subnodes);
|
|
||||||
else
|
|
||||||
helmaNode.setSubnodes (null);
|
|
||||||
return helmaNode;
|
|
||||||
}
|
}
|
||||||
|
if ( propMap.size()>0 )
|
||||||
|
helmaNode.setPropMap (propMap);
|
||||||
|
else
|
||||||
|
helmaNode.setPropMap (null);
|
||||||
|
if ( subnodes.size()>0 )
|
||||||
|
helmaNode.setSubnodes (subnodes);
|
||||||
|
else
|
||||||
|
helmaNode.setSubnodes (null);
|
||||||
|
return helmaNode;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue