#55 Save and load as serialised string

open
nobody
5
2009-08-10
2009-08-10
Anonymous
No

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()

Discussion

  • zawmn83

    zawmn83 - 2010-06-23

    Is there any update on this feature request?

     
  • zawmn83

    zawmn83 - 2010-08-21

    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);
    }
    }

     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks