001: /**
002: * Objective Database Abstraction Layer (ODAL)
003: * Copyright (c) 2004, The ODAL Development Group
004: * All rights reserved.
005: * For definition of the ODAL Development Group please refer to LICENCE.txt file
006: *
007: * Distributable under LGPL license.
008: * See terms of license at gnu.org.
009: */package com.completex.objective.components.persistency.io.xml.impl;
010:
011: import com.completex.objective.components.persistency.xml.XmlRuntimeException;
012: import com.completex.objective.util.XmlDomHelper;
013: import org.w3c.dom.Document;
014: import org.w3c.dom.Element;
015: import org.xml.sax.SAXException;
016:
017: import javax.xml.parsers.DocumentBuilder;
018: import javax.xml.parsers.DocumentBuilderFactory;
019: import javax.xml.parsers.ParserConfigurationException;
020: import java.io.ByteArrayInputStream;
021: import java.io.IOException;
022: import java.math.BigDecimal;
023: import java.math.BigInteger;
024: import java.sql.Time;
025: import java.sql.Timestamp;
026: import java.util.Date;
027: import java.util.HashMap;
028: import java.util.Map;
029:
030: /**
031: * Basic XML DOM stream
032: *
033: * @author Gennady Krizhevsky
034: */
035: public abstract class XmlDomStream implements XmlStreamTags {
036: protected boolean useReferences = true;
037: protected Document document;
038: protected Element rootElement;
039: protected Element currentElement;
040: protected boolean useNames;
041: protected boolean useNamesJavaStyle;
042: protected boolean preserveOriginalValues;
043: protected boolean initialized;
044: protected HashMap references = new HashMap();
045: protected XmlDomHelper xmlDomHelper;
046: protected String rootName;
047:
048: public static final Map NUMERIC = new HashMap();
049: public static final LongFactory LONG_FACTORY = new LongFactory();
050: public static final IntegerFactory INTEGER_FACTORY = new IntegerFactory();
051: public static final ShortFactory SHORT_FACTORY = new ShortFactory();
052: public static final FloatFactory FLOAT_FACTORY = new FloatFactory();
053: public static final DoubleFactory DOUBLE_FACTORY = new DoubleFactory();
054: public static final ByteFactory BYTE_FACTORY = new ByteFactory();
055:
056: static {
057: NUMERIC.put(Long.class.getName(), LONG_FACTORY);
058: NUMERIC.put(Integer.class.getName(), INTEGER_FACTORY);
059: NUMERIC.put(Short.class.getName(), SHORT_FACTORY);
060: NUMERIC.put(Float.class.getName(), FLOAT_FACTORY);
061: NUMERIC.put(Double.class.getName(), DOUBLE_FACTORY);
062: NUMERIC.put(Byte.class.getName(), BYTE_FACTORY);
063: NUMERIC
064: .put(BigDecimal.class.getName(),
065: new BigDecimalFactory());
066: NUMERIC
067: .put(BigInteger.class.getName(),
068: new BigIntegerFactory());
069: //
070: // Primitives:
071: //
072: NUMERIC.put(long.class.getName(), LONG_FACTORY);
073: NUMERIC.put(int.class.getName(), INTEGER_FACTORY);
074: NUMERIC.put(short.class.getName(), SHORT_FACTORY);
075: NUMERIC.put(float.class.getName(), FLOAT_FACTORY);
076: NUMERIC.put(double.class.getName(), DOUBLE_FACTORY);
077: NUMERIC.put(byte.class.getName(), BYTE_FACTORY);
078: }
079:
080: public static final Map DATE = new HashMap();
081:
082: static {
083: DATE.put(Date.class.getName(), new DateFactory());
084: DATE.put(java.sql.Date.class.getName(), new SqlDateFactory());
085: DATE.put(Time.class.getName(), new SqlTimeFactory());
086: DATE.put(Timestamp.class.getName(), new SqlTimestampFactory());
087: }
088:
089: public static final Map BOOLEAN = new HashMap();
090:
091: public static final BooleanFactory BOOLEAN_FACTORY = new BooleanFactory();
092:
093: static {
094: BOOLEAN.put(Boolean.class.getName(), BOOLEAN_FACTORY);
095: //
096: // Primitives:
097: //
098: BOOLEAN.put(boolean.class.getName(), BOOLEAN_FACTORY);
099: }
100:
101: public static final Map VALUE_FACTORIES = new HashMap();
102: public static final ByteArrayInputStream NULL_BYTE_ARRAY_INPUT_STREAM = new ByteArrayInputStream(
103: new byte[0]);
104: private static final boolean DEBUG = false;
105: protected String version = "1.0.0";
106:
107: static {
108: VALUE_FACTORIES.putAll(NUMERIC);
109: VALUE_FACTORIES.putAll(DATE);
110: VALUE_FACTORIES.putAll(BOOLEAN);
111: }
112:
113: protected void setup() throws IOException {
114: DocumentBuilderFactory dbf = DocumentBuilderFactory
115: .newInstance();
116: //Using factory get an instance of document builder
117: DocumentBuilder builder = null;
118: try {
119: builder = dbf.newDocumentBuilder();
120: } catch (ParserConfigurationException e) {
121: new IOException(e.toString());
122: }
123: try {
124: document = createDocument(builder);
125: } catch (SAXException e) {
126: new IOException(e.toString());
127: }
128: xmlDomHelper = new XmlDomHelper(document);
129: setupRootElement();
130: rootName = rootElement.getTagName();
131: }
132:
133: protected abstract void setupRootElement();
134:
135: protected abstract Document createDocument(DocumentBuilder builder)
136: throws IOException, SAXException;
137:
138: /**
139: * Return true if persistent object origianl values are being preserved
140: *
141: * @return true if persistent object origianl values are being preserved
142: */
143: public boolean isPreserveOriginalValues() {
144: return preserveOriginalValues;
145: }
146:
147: /**
148: * Return true if persistent object column names are to be shown in JavaBeans style
149: *
150: * @return true if persistent object column names are to be shown in JavaBeans style
151: */
152: public boolean isUseNamesJavaStyle() {
153: return useNamesJavaStyle;
154: }
155:
156: protected void ensureOpen() {
157: if (document == null) {
158: throw new XmlRuntimeException("Stream is closed");
159: }
160: }
161:
162: protected Element resolveParentElement() {
163: return initialized && currentElement != null ? currentElement
164: : rootElement;
165: }
166:
167: public void close() throws IOException {
168: currentElement = null;
169: references = null;
170: rootElement = null;
171: document = null;
172: }
173:
174: protected void println(String s) {
175: if (DEBUG) {
176: System.out.println(s);
177: }
178: }
179:
180: public Document getDocument() {
181: return document;
182: }
183:
184: public Element getRootElement() {
185: return rootElement;
186: }
187:
188: public String getVersion() {
189: return version;
190: }
191:
192: protected boolean isNumericValue(Object value) {
193: if (value == null) {
194: return false;
195: }
196: return isNumericClass(value.getClass().getName());
197: }
198:
199: protected boolean isNumericClass(String className) {
200: return NUMERIC.containsKey(className);
201: }
202:
203: protected boolean isDate(Object value) {
204: if (value == null) {
205: return false;
206: }
207: return isDateClass(value);
208: }
209:
210: protected boolean isDateClass(Object className) {
211: return DATE.containsKey(className);
212: }
213:
214: protected boolean isBoolean(Object value) {
215: if (value == null) {
216: return false;
217: }
218: return isBooleanClass(value.getClass().getName());
219: }
220:
221: protected boolean isBooleanClass(String className) {
222: return BOOLEAN.containsKey(className);
223: }
224:
225: protected Object string2value0(String className, String valueString) {
226: if (valueString == null) {
227: return null;
228: }
229:
230: ValueFactory valueFactory = (ValueFactory) VALUE_FACTORIES
231: .get(className);
232: if (valueFactory == null) {
233: throw new XmlRuntimeException(
234: "Cannot find valueFactory for className "
235: + className);
236: }
237: return valueFactory.newValueInstance(valueString);
238: }
239:
240: /**
241: * Value factory
242: */
243: protected static interface ValueFactory {
244: Object newValueInstance(String valueString);
245: }
246:
247: /**
248: * Numeric:
249: */
250: protected static class LongFactory implements ValueFactory {
251: public Object newValueInstance(String valueString) {
252: return Long.valueOf(valueString);
253: }
254: }
255:
256: protected static class IntegerFactory implements ValueFactory {
257: public Object newValueInstance(String valueString) {
258: return Integer.valueOf(valueString);
259: }
260: }
261:
262: protected static class ShortFactory implements ValueFactory {
263: public Object newValueInstance(String valueString) {
264: return Short.valueOf(valueString);
265: }
266: }
267:
268: protected static class FloatFactory implements ValueFactory {
269: public Object newValueInstance(String valueString) {
270: return Float.valueOf(valueString);
271: }
272: }
273:
274: protected static class DoubleFactory implements ValueFactory {
275: public Object newValueInstance(String valueString) {
276: return Double.valueOf(valueString);
277: }
278: }
279:
280: protected static class ByteFactory implements ValueFactory {
281: public Object newValueInstance(String valueString) {
282: return Byte.valueOf(valueString);
283: }
284: }
285:
286: protected static class BigDecimalFactory implements ValueFactory {
287: public Object newValueInstance(String valueString) {
288: return new BigDecimal(valueString);
289: }
290: }
291:
292: protected static class BigIntegerFactory implements ValueFactory {
293: public Object newValueInstance(String valueString) {
294: return new BigInteger(valueString);
295: }
296: }
297:
298: /**
299: * Date
300: */
301: protected static class DateFactory implements ValueFactory {
302: public Object newValueInstance(String valueString) {
303: return XmlDomHelper.S2D(valueString);
304: }
305: }
306:
307: protected static class SqlDateFactory implements ValueFactory {
308: public Object newValueInstance(String valueString) {
309: Date date = XmlDomHelper.S2D(valueString);
310: return new java.sql.Date(date.getTime());
311: }
312: }
313:
314: protected static class SqlTimeFactory implements ValueFactory {
315: public Object newValueInstance(String valueString) {
316: Date date = XmlDomHelper.S2D(valueString);
317: return new java.sql.Time(date.getTime());
318: }
319: }
320:
321: protected static class SqlTimestampFactory implements ValueFactory {
322: public Object newValueInstance(String valueString) {
323: Date date = XmlDomHelper.S2D(valueString);
324: return new java.sql.Timestamp(date.getTime());
325: }
326: }
327:
328: /**
329: * Boolean
330: */
331: protected static class BooleanFactory implements ValueFactory {
332: public Object newValueInstance(String valueString) {
333: return XmlDomHelper.S2B(valueString);
334: }
335: }
336:
337: }
|