001: package com.jeta.forms.store.jml;
002:
003: import java.io.InputStream;
004: import java.io.PrintWriter;
005: import java.io.StringWriter;
006:
007: import javax.xml.parsers.SAXParser;
008: import javax.xml.parsers.SAXParserFactory;
009:
010: import org.xml.sax.SAXException;
011:
012: import com.jeta.forms.store.jml.dom.DefaultXMLDocument;
013: import com.jeta.forms.store.jml.dom.JMLDocument;
014: import com.jeta.forms.store.jml.dom.JMLNode;
015: import com.jeta.forms.store.xml.parser.CustomSAXException;
016: import com.jeta.forms.store.xml.parser.MainHandler;
017:
018: public class JMLUtils {
019:
020: public static void verifyObjectType(Object obj, Class cls)
021: throws JMLException {
022: if (obj != null && obj.getClass() != cls) {
023: throw new JMLException(
024: "Verify object class failed. Expecting: " + cls
025: + " but got: " + obj.getClass());
026: }
027: }
028:
029: public static JMLNode writeObject(Object obj) throws JMLException {
030: try {
031: JMLSerializer serializer = JMLSerializerFactory
032: .getInstance().createSerializer(obj);
033: assert (serializer != null);
034: return serializer.serialize(new DefaultXMLDocument(), obj);
035: } catch (Exception e) {
036: e.printStackTrace();
037: throw new JMLException(e);
038: }
039: }
040:
041: public static Object readObject(InputStream istream)
042: throws JMLException {
043: try {
044: SAXParser parser = SAXParserFactory.newInstance()
045: .newSAXParser();
046: MainHandler handler = new MainHandler();
047: parser.parse(istream, handler);
048: return handler.getObject();
049: } catch (Exception e) {
050: e.printStackTrace();
051: throw new JMLException(e);
052: }
053: }
054:
055: public static JMLNode createObjectNode2(JMLDocument document,
056: String className) {
057: JMLNode e = document.createNode("object");
058: if (className == null)
059: e.setAttribute("classname", "null");
060: else
061: e.setAttribute("classname", className);
062:
063: return e;
064: }
065:
066: public static JMLNode createObjectNode(JMLDocument document,
067: Object obj) {
068: JMLNode e = document.createNode("object");
069: if (obj == null)
070: e.setAttribute("classname", "null");
071: else
072: e.setAttribute("classname", obj.getClass().getName());
073:
074: return e;
075: }
076:
077: public static JMLNode createSuperClassNode(JMLDocument document,
078: Class super Class) {
079: JMLNode e = document.createNode("super");
080: if (super Class != null)
081: e.setAttribute("classname", super Class.getName());
082:
083: return e;
084: }
085:
086: public static JMLNode createPropertyNode(JMLDocument document,
087: String propName, Object pvalue) throws JMLException {
088: JMLNode e = document.createNode("at");
089: e.setAttribute("name", propName);
090: if (pvalue != null) {
091: JMLSerializerFactory factory = JMLSerializerFactory
092: .getInstance();
093: JMLSerializer serializer = factory.createSerializer(pvalue);
094: if (serializer == null) {
095: System.out
096: .println(" xmlutils.createPropertyNode failed propName: "
097: + propName
098: + " value: "
099: + (pvalue == null ? "NULL" : pvalue
100: .getClass().getName()));
101: System.exit(0);
102: }
103: e.appendChild(serializer.serialize(document, pvalue));
104: if (serializer instanceof InlineJMLSerializer)
105: e.setAttribute("object",
106: ((InlineJMLSerializer) serializer)
107: .getObjectName());
108: }
109: return e;
110: }
111:
112: public static JMLNode createPropertiesNode(JMLDocument document) {
113: return document.createNode("properties");
114: }
115:
116: /**
117: * If the object is a Java Object primitive type, we need to store a holder
118: * to the type. This is needed when storing collections because we simply
119: * don't want to emit the primitive value. When we deserialize the
120: * collection, there would be no way to determine the class for the value.
121: * So we use holders instead. If the specified object is not a Java
122: * primtive, then we simply return it.
123: */
124: public static Object getPrimitiveHolder(Object obj) {
125: if (isPrimitive(obj)) {
126: return new PrimitiveHolder(obj);
127: } else
128: return obj;
129:
130: }
131:
132: public static SAXException createSAXException(String msg) {
133: return createSAXException(msg, null);
134: }
135:
136: public static SAXException createSAXException(Exception e) {
137: return createSAXException(null, e);
138: }
139:
140: public static SAXException createSAXException(String msg,
141: Exception e) {
142: if (e instanceof SAXException)
143: return (SAXException) e;
144: else {
145:
146: StringBuffer sbuff = new StringBuffer();
147: if (msg != null)
148: sbuff.append(msg);
149: StringWriter writer = new StringWriter();
150: PrintWriter pw = new PrintWriter(writer);
151: if (e == null)
152: new Exception().printStackTrace(pw);
153: else {
154: sbuff.append(e.getMessage());
155: e.printStackTrace(pw);
156: }
157:
158: sbuff.append("\n");
159: sbuff.append(writer.toString());
160: sbuff.append("\n");
161:
162: if (e == null)
163: return CustomSAXException.create(msg, sbuff.toString());
164: else
165: return CustomSAXException.create(msg, sbuff.toString());
166: }
167:
168: }
169:
170: public static boolean isPrimitive(Object obj) {
171: return (obj instanceof Byte || obj instanceof Boolean
172: || obj instanceof Short || obj instanceof Character
173: || obj instanceof Integer || obj instanceof Float || obj instanceof Double);
174: }
175:
176: }
|