Implemented Xml.getFromString()

This commit is contained in:
hns 2003-01-31 21:08:14 +00:00
parent d5ea622ad4
commit f74c21d405
2 changed files with 36 additions and 0 deletions

View file

@ -6,6 +6,7 @@ import java.util.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.InputSource;
import helma.objectmodel.*;
import helma.util.SystemProperties;
@ -75,6 +76,15 @@ public class XmlConverter implements XmlConstants {
}
}
public INode convertFromString( String xml, INode helmaNode ) throws RuntimeException {
Document document = XmlUtil.parse (new InputSource (new StringReader (xml)));
if ( document!=null && document.getDocumentElement()!=null ) {
return convert( document.getDocumentElement(), helmaNode, new HashMap() );
} else {
return helmaNode;
}
}
public INode convert( Element element, INode helmaNode, Map nodeCache ) {
offset++;
// previousNode is used to cache previous nodes with the same prototype

View file

@ -39,6 +39,7 @@ public class DomExtension extends Extension {
globalXml.putHiddenProperty ("write", new XmlWrite ("write", evaluator, fp));
globalXml.putHiddenProperty ("writeToString", new XmlWriteToString ("writeToString", evaluator, fp));
globalXml.putHiddenProperty ("get", new XmlGet ("get", evaluator, fp));
globalXml.putHiddenProperty ("getFromString", new XmlGetFromString ("getFromString", evaluator, fp));
go.putHiddenProperty ("Xml", globalXml);
}
@ -175,6 +176,31 @@ public class DomExtension extends Extension {
}
}
class XmlGetFromString extends BuiltinFunctionObject {
XmlGetFromString(String name, Evaluator evaluator, FunctionPrototype fp) {
super(fp, evaluator, name, 1);
}
public ESValue callFunction(ESObject thisObject, ESValue[] arguments) throws EcmaScriptException {
if ( arguments==null || arguments.length==0 )
throw new EcmaScriptException("Xml.getFromString() needs an XML string as parameter");
try {
XmlConverter converter;
if ( arguments.length>1 ) {
converter = new XmlConverter (arguments[1].toString());
} else {
converter = new XmlConverter ();
}
INode node = new helma.objectmodel.db.Node ( (String)null, (String)null, this.evaluator.engine.getApplication().getWrappedNodeManager() );
INode result = converter.convertFromString (arguments[0].toString(),node);
return this.evaluator.engine.getNodeWrapper(result);
} catch ( NoClassDefFoundError e ) {
throw new EcmaScriptException("Can't load dom-capable xml parser.");
} catch ( RuntimeException f ) {
throw new EcmaScriptException(f.toString());
}
}
}
}