001: package com.jeta.swingbuilder.gui.utils;
002:
003: import java.io.BufferedWriter;
004: import java.io.FileInputStream;
005: import java.io.FileNotFoundException;
006: import java.io.FileOutputStream;
007: import java.io.IOException;
008: import java.io.InputStream;
009: import java.io.ObjectOutputStream;
010: import java.io.OutputStream;
011: import java.io.OutputStreamWriter;
012: import java.io.Writer;
013:
014: import com.jeta.forms.gui.common.FormException;
015: import com.jeta.forms.gui.common.FormUtils;
016: import com.jeta.forms.gui.form.FormComponent;
017: import com.jeta.forms.gui.formmgr.FormManagerUtils;
018: import com.jeta.forms.store.jml.JMLException;
019: import com.jeta.forms.store.jml.JMLUtils;
020: import com.jeta.forms.store.jml.dom.JMLNode;
021: import com.jeta.forms.store.memento.FormPackage;
022: import com.jeta.forms.store.memento.StateRequest;
023: import com.jeta.forms.store.xml.writer.XMLWriter;
024: import com.jeta.open.registry.JETARegistry;
025: import com.jeta.swingbuilder.common.ComponentNames;
026: import com.jeta.swingbuilder.gui.project.UserPreferencesNames;
027: import com.jeta.swingbuilder.main.AbeilleForms;
028:
029: /**
030: * This is a utility class for converting forms to/from XML/binary format. You
031: * must provide a valid Abeille project file or linked forms (nested forms that
032: * refer to other forms on disk) will not be resolved.
033: *
034: * Note that this class is in the com.jeta.swingbuilder package. This means that
035: * you will need to include designer.jar as well as formsrt.jar in your
036: * classpath if you decide to use this class to convert forms.
037: *
038: * @author Jeff Tassin
039: */
040: public class FormConverter {
041:
042: /**
043: * ctor creates a FormConverter object associated with the specified
044: * project.
045: *
046: * @param projectFile
047: * the full path and name of an Abeille project file. This is
048: * needed to resolve linked forms. For example:
049: * /home/jeff/abeille/myproject.jfpr
050: */
051: public FormConverter(String projectFile) {
052: JETARegistry.rebind(UserPreferencesNames.ID_LAST_PROJECT,
053: projectFile);
054: }
055:
056: /**
057: * Basic initialization needed by the designer when storing files.
058: */
059: private static void initialize() {
060: if (JETARegistry.lookup(ComponentNames.APPLICATION_STATE_STORE) == null) {
061: System.setProperty("jeta1.debug", "true");
062: new AbeilleForms().launch(new String[0], false);
063: }
064: }
065:
066: /**
067: * Converts a binary .jfrm to an XML file.
068: *
069: * @param xmlDestFile
070: * the path and name of an XML file to create
071: * @param binarySrcFile
072: * the path an name of an existing binary .jfrm to read and
073: * convert.
074: * @throws FormException
075: * @throws JMLException
076: */
077: public void convertToXML(String xmlDestFile, String binarySrcFile)
078: throws IOException, FormException, JMLException {
079: convertToXML(new FileOutputStream(xmlDestFile),
080: new FileInputStream(binarySrcFile));
081: }
082:
083: /**
084: * Converts a binary .jfrm to an XML file.
085: *
086: * @throws FormException
087: * @throws JMLException
088: * @throws IOException
089: */
090: public void convertToXML(OutputStream xmlOutputStream,
091: InputStream binaryInputStream) throws FormException,
092: JMLException, IOException {
093:
094: initialize();
095: FormUtils.setDesignMode(true);
096: FormComponent fc = FormManagerUtils.openForm(binaryInputStream);
097: FormPackage fpackage = new FormPackage(fc
098: .getExternalState(StateRequest.SHALLOW_COPY));
099: JMLNode node = JMLUtils.writeObject(fpackage);
100: FormUtils.setDesignMode(false);
101:
102: Writer writer = new BufferedWriter(new OutputStreamWriter(
103: xmlOutputStream));
104: new XMLWriter().write(writer, node);
105: writer.write('\n');
106: writer.flush();
107: writer.close();
108: }
109:
110: /**
111: * Converts an XML form to a binary .jfrm
112: *
113: * @param binaryDestFile
114: * @param xmlSrcFile
115: * @throws FileNotFoundException
116: * @throws ClassNotFoundException
117: * @throws IOException
118: * @throws JMLException
119: * @throws FormException
120: */
121: public void convertToBinary(String binaryDestFile, String xmlSrcFile)
122: throws FileNotFoundException, ClassNotFoundException,
123: IOException, JMLException, FormException {
124: convertToBinary(new FileOutputStream(binaryDestFile),
125: new FileInputStream(xmlSrcFile));
126: }
127:
128: /**
129: * Converts an XML form to a binary .jfrm
130: *
131: * @throws FileNotFoundException
132: * @throws ClassNotFoundException
133: * @throws IOException
134: * @throws JMLException
135: * @throws FormException
136: */
137: public void convertToBinary(OutputStream binaryOutputStream,
138: InputStream xmlInputStream) throws ClassNotFoundException,
139: IOException, JMLException, FormException {
140: initialize();
141: FormUtils.setDesignMode(true);
142: FormComponent fc = FormManagerUtils.openForm(xmlInputStream);
143: FormPackage fpackage = new FormPackage(fc
144: .getExternalState(StateRequest.SHALLOW_COPY));
145: ObjectOutputStream oos = null;
146: if (binaryOutputStream instanceof ObjectOutputStream)
147: oos = (ObjectOutputStream) binaryOutputStream;
148: else
149: oos = new ObjectOutputStream(binaryOutputStream);
150:
151: oos.writeObject(fpackage);
152: oos.flush();
153: oos.close();
154: FormUtils.setDesignMode(false);
155:
156: }
157:
158: }
|