I'm working on integrating DragMath with Daisy CMS (http://www.daisycms.org/) a content management system backed by a database.
I'm trying to work out a streamlined method for saving and loading dragmath file to the database. I think a couple of methods in the MainApplet that would return strings containing the serialised dragmath objects:
public String saveAsSerialisedString()
public void loadFromSerialisedString()
Just found this library which may make this trivial:
http://xstream.codehaus.org/tutorial.html
Also forgot to include my name above...
Cheers
Tim
Is there any update on this feature request?
I tried to use http://xstream.codehaus.org/tutorial.html . But there is security problem to access this library from html.
By using the following class it is possible to get and set Math object.
//////////////////////////////////////////////////////////////////////////////////
package Display;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class WSHelper {
static private BASE64Encoder encode = new BASE64Encoder();
static private BASE64Decoder decode = new BASE64Decoder();
static public String OToS(Object obj) {
long start=System.currentTimeMillis();
String out = null;
if (obj != null) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(obj);
out = encode.encode(baos.toByteArray());
}
catch (IOException e)
{
e.printStackTrace();
return null;
}
}
long end=System.currentTimeMillis();
System.out.println("Encode:"+(end-start));
return out;
}
static public Object SToO(String str) {
long start=System.currentTimeMillis();
Object out = null;
if (str != null) {
try {
ByteArrayInputStream bios = new ByteArrayInputStream(decode.decodeBuffer(str));
ObjectInputStream ois = new ObjectInputStream(bios);
out = ois.readObject();
}
catch (IOException e) {
e.printStackTrace();
return null;
}
catch (ClassNotFoundException e) {
e.printStackTrace();
return null;
}
}
long end=System.currentTimeMillis();
System.out.println("Decode:"+(end-start));
return out;
}
}
//////////////////////////////////////////////////////////////////////////////////
I added new functions getFile() and setFile() as follow in MainApplet.java
//////////////////////////////////////////////////////////////////////////////////
private WSHelper xstream = new WSHelper();
public String getFile()
{
String retstr = "";
try {
Tree.MathObject mobj = buildTree.generateTree(jPanelWorkspace, false, 0, 0);
retstr = xstream.OToS(mobj);
} catch (ParseException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage(), "DragMath", JOptionPane.ERROR_MESSAGE);
}
return retstr;
}
public void setFile(String mathstr)
{
Tree.MathObject tree = null;
try {
tree = (Tree.MathObject) xstream.SToO(mathstr);
jPanelWorkspace.removeAll();
addComponent.pasteTree(jPanelWorkspace, 0, tree, 0);
addComponent.resetUndoRedo();
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex.toString(), "DragMath", JOptionPane.ERROR_MESSAGE);
}
}
//////////////////////////////////////////////////////////////////////////////////
And also added new Parameter in init function of MainApplet.java
//////////////////////////////////////////////////////////////////////////////////
openWithObject = getParameter("openWithObject");
if (openWithExpression != null) {
openWithExpression(openWithExpression);
}
else //added by zmn to load from object
{
if(openWithObject != null)
{
setFile(openWithObject);
}
}