001: package com.jeta.forms.store.jml;
002:
003: import java.io.IOException;
004:
005: import com.jeta.forms.store.JETAObjectOutput;
006: import com.jeta.forms.store.JETAPersistable;
007: import com.jeta.forms.store.jml.dom.JMLDocument;
008: import com.jeta.forms.store.jml.dom.JMLNode;
009: import com.jeta.forms.store.properties.JETAProperty;
010:
011: public class JMLObjectOutput implements JETAObjectOutput {
012: private JMLDocument m_document;
013: private JMLNode m_objnode;
014:
015: public JMLObjectOutput(JMLDocument document, JMLNode objNode) {
016: m_document = document;
017: m_objnode = objNode;
018: }
019:
020: /**
021: * No need to write the version number for XML since we all some fields as
022: * optional
023: */
024: public void writeVersion(int version) throws IOException {
025: // no op
026: }
027:
028: public void writeInt(String tagName, int value) throws IOException {
029:
030: try {
031: m_objnode.appendChild(JMLUtils.createPropertyNode(
032: m_document, tagName, String.valueOf(value)));
033: } catch (Exception e) {
034: throw new IOException(e.getMessage());
035: }
036: }
037:
038: public void writeObject(String tagName, Object obj)
039: throws IOException {
040: try {
041: if (obj != null)
042: m_objnode.appendChild(JMLUtils.createPropertyNode(
043: m_document, tagName, obj));
044: } catch (Exception e) {
045: e.printStackTrace();
046: throw new IOException(e.getMessage());
047: }
048:
049: }
050:
051: public void writeBoolean(String tagName, boolean bval)
052: throws IOException {
053: try {
054: m_objnode.appendChild(JMLUtils.createPropertyNode(
055: m_document, tagName, String.valueOf(bval)));
056: } catch (Exception e) {
057: throw new IOException(e.getMessage());
058: }
059: }
060:
061: public void writeFloat(String tagName, float fval)
062: throws IOException {
063: try {
064: m_objnode.appendChild(JMLUtils.createPropertyNode(
065: m_document, tagName, String.valueOf(fval)));
066: } catch (Exception e) {
067: throw new IOException(e.getMessage());
068: }
069: }
070:
071: public JETAObjectOutput getSuperClassOutput(Class super Class) {
072: /**
073: * Special case for storing JETAProperty classes. The main reason is
074: * because this object is stable and we don't want to polute every
075: * property with 'super' in the XML. The only attribute we need is
076: * 'name' from JETAProperty.
077: */
078: if (super Class == JETAProperty.class)
079: return this ;
080:
081: JMLNode super node = JMLUtils.createSuperClassNode(m_document,
082: super Class);
083: m_objnode.appendChild(super node);
084: return new JMLObjectOutput(m_document, super node);
085: }
086:
087: public static class XMLObjectOutputSerializer implements
088: JMLSerializer {
089:
090: /**
091: * XMLSerializer implementation
092: */
093: public JMLNode serialize(JMLDocument document, Object obj)
094: throws JMLException {
095:
096: JETAPersistable persistable = (JETAPersistable) obj;
097:
098: JMLNode node = JMLUtils.createObjectNode(document, obj);
099: JMLObjectOutput xoo = new JMLObjectOutput(document, node);
100: try {
101: persistable.write(xoo);
102: return node;
103: } catch (IOException e) {
104: e.printStackTrace();
105: throw new JMLException(e.getMessage());
106: }
107: }
108: }
109:
110: public void writeBoolean(String string, boolean value,
111: boolean defaultValue) throws IOException {
112: if (value != defaultValue)
113: writeBoolean(string, value);
114: }
115:
116: public void writeInt(String string, int value, int defaultValue)
117: throws IOException {
118: if (value != defaultValue)
119: writeInt(string, value);
120: }
121:
122: public void writeFloat(String string, float value,
123: float defaultValue) throws IOException {
124: if (value != defaultValue)
125: writeFloat(string, value);
126: }
127:
128: }
|