Cleaned up class, fixed argument order in Xml.write():
now the hopobject is the first argument, and the filename the second one. This seems more consistent to me (both taken alone and in combination with the Xml.writeToString() function).
This commit is contained in:
parent
758bd0f384
commit
413432dec6
1 changed files with 16 additions and 29 deletions
|
@ -36,8 +36,8 @@ public class DomExtension extends Extension {
|
||||||
globalXml.putHiddenProperty ("length",new ESNumber(1));
|
globalXml.putHiddenProperty ("length",new ESNumber(1));
|
||||||
globalXml.putHiddenProperty ("read", new XmlRead ("read", evaluator, fp, false));
|
globalXml.putHiddenProperty ("read", new XmlRead ("read", evaluator, fp, false));
|
||||||
globalXml.putHiddenProperty ("readFromString", new XmlRead ("readFromString", evaluator, fp, true));
|
globalXml.putHiddenProperty ("readFromString", new XmlRead ("readFromString", evaluator, fp, true));
|
||||||
globalXml.putHiddenProperty ("write", new XmlWrite ("write", evaluator, fp, false));
|
globalXml.putHiddenProperty ("write", new XmlWrite ("write", evaluator, fp));
|
||||||
globalXml.putHiddenProperty ("writeToString", new XmlWrite ("writeToString", evaluator, fp, true));
|
globalXml.putHiddenProperty ("writeToString", new XmlWriteToString ("writeToString", evaluator, fp));
|
||||||
globalXml.putHiddenProperty ("get", new XmlGet ("get", evaluator, fp));
|
globalXml.putHiddenProperty ("get", new XmlGet ("get", evaluator, fp));
|
||||||
go.putHiddenProperty ("Xml", globalXml);
|
go.putHiddenProperty ("Xml", globalXml);
|
||||||
}
|
}
|
||||||
|
@ -55,15 +55,11 @@ public class DomExtension extends Extension {
|
||||||
}
|
}
|
||||||
|
|
||||||
class XmlWrite extends BuiltinFunctionObject {
|
class XmlWrite extends BuiltinFunctionObject {
|
||||||
boolean tostring;
|
XmlWrite(String name, Evaluator evaluator, FunctionPrototype fp) {
|
||||||
XmlWrite(String name, Evaluator evaluator, FunctionPrototype fp, boolean tostring) {
|
|
||||||
super(fp, evaluator, name, 1);
|
super(fp, evaluator, name, 1);
|
||||||
this.tostring = tostring;
|
|
||||||
}
|
}
|
||||||
public ESValue callFunction(ESObject thisObject, ESValue[] arguments) throws EcmaScriptException {
|
public ESValue callFunction(ESObject thisObject, ESValue[] arguments) throws EcmaScriptException {
|
||||||
if ( arguments==null ||
|
if ( arguments==null || arguments.length != 2 )
|
||||||
(tostring && arguments.length != 1) ||
|
|
||||||
(!tostring && arguments.length != 2))
|
|
||||||
throw new EcmaScriptException("wrong number of arguments");
|
throw new EcmaScriptException("wrong number of arguments");
|
||||||
INode node = null;
|
INode node = null;
|
||||||
try {
|
try {
|
||||||
|
@ -73,23 +69,14 @@ public class DomExtension extends Extension {
|
||||||
throw new EcmaScriptException("first argument is not an hopobject");
|
throw new EcmaScriptException("first argument is not an hopobject");
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
if (tostring) {
|
File tmpFile = new File(arguments[1].toString()+".tmp."+XmlWriter.generateID());
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream ();
|
XmlWriter writer = new XmlWriter (tmpFile);
|
||||||
XmlWriter writer = new XmlWriter (out, "UTF-8");
|
writer.setDatabaseMode(false);
|
||||||
writer.setDatabaseMode(false);
|
boolean result = writer.write(node);
|
||||||
boolean result = writer.write(node);
|
writer.close();
|
||||||
writer.close();
|
File finalFile = new File(arguments[1].toString());
|
||||||
return new ESString (out.toString ("utf8"));
|
tmpFile.renameTo (finalFile);
|
||||||
} else {
|
this.evaluator.reval.app.logEvent("wrote xml to " + finalFile.getAbsolutePath() );
|
||||||
File tmpFile = new File(arguments[0].toString()+".tmp."+XmlWriter.generateID());
|
|
||||||
XmlWriter writer = new XmlWriter (tmpFile);
|
|
||||||
writer.setDatabaseMode(false);
|
|
||||||
boolean result = writer.write(node);
|
|
||||||
writer.close();
|
|
||||||
File finalFile = new File(arguments[0].toString());
|
|
||||||
tmpFile.renameTo (finalFile);
|
|
||||||
this.evaluator.reval.app.logEvent("wrote xml to " + finalFile.getAbsolutePath() );
|
|
||||||
}
|
|
||||||
} catch (IOException io) {
|
} catch (IOException io) {
|
||||||
throw new EcmaScriptException (io.toString());
|
throw new EcmaScriptException (io.toString());
|
||||||
}
|
}
|
||||||
|
@ -101,8 +88,8 @@ public class DomExtension extends Extension {
|
||||||
* Xml.create() is used to get a string containing the xml-content.
|
* Xml.create() is used to get a string containing the xml-content.
|
||||||
* Useful if Xml-content should be made public through the web.
|
* Useful if Xml-content should be made public through the web.
|
||||||
*/
|
*/
|
||||||
class XmlCreate extends BuiltinFunctionObject {
|
class XmlWriteToString extends BuiltinFunctionObject {
|
||||||
XmlCreate(String name, Evaluator evaluator, FunctionPrototype fp) {
|
XmlWriteToString(String name, Evaluator evaluator, FunctionPrototype fp) {
|
||||||
super(fp, evaluator, name, 1);
|
super(fp, evaluator, name, 1);
|
||||||
}
|
}
|
||||||
public ESValue callFunction(ESObject thisObject, ESValue[] arguments) throws EcmaScriptException {
|
public ESValue callFunction(ESObject thisObject, ESValue[] arguments) throws EcmaScriptException {
|
||||||
|
@ -117,11 +104,11 @@ public class DomExtension extends Extension {
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
XmlWriter writer = new XmlWriter (out);
|
XmlWriter writer = new XmlWriter (out, "UTF-8");
|
||||||
writer.setDatabaseMode(false);
|
writer.setDatabaseMode(false);
|
||||||
boolean result = writer.write(node);
|
boolean result = writer.write(node);
|
||||||
writer.flush();
|
writer.flush();
|
||||||
return new ESString (out.toString());
|
return new ESString (out.toString("UTF-8"));
|
||||||
} catch (IOException io) {
|
} catch (IOException io) {
|
||||||
throw new EcmaScriptException (io.toString());
|
throw new EcmaScriptException (io.toString());
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue