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.sdl.writer.impl;
010:
011: import com.completex.objective.components.sdl.writer.SdlWriter;
012:
013: import java.io.IOException;
014: import java.io.Writer;
015: import java.sql.Timestamp;
016: import java.text.SimpleDateFormat;
017: import java.util.*;
018:
019: /**
020: * @author Andrew Suprun
021: */
022: public class SdlPrinterImpl extends AbstractSdlWriter implements
023: SdlWriter {
024:
025: static final private SimpleDateFormat SDF_LONG = new SimpleDateFormat(
026: "@yyyy-MM-dd'T'HH:mm:ss");
027: static final private SimpleDateFormat SDF_SHORT = new SimpleDateFormat(
028: "@yyyy-MM-dd");
029:
030: public void write(Writer target, Object object) throws IOException {
031: write(target, object, 0);
032: target.write("\n");
033: }
034:
035: private void write(Writer target, Object object, int level)
036: throws IOException {
037: if ("keyGenerator".equals(object)) {
038: int k = 0;
039: }
040: if (object == null) {
041: target.write("NULL");
042: } else if (object instanceof Map) {
043: writeMap(target, (Map) object, level);
044: } else if (object instanceof List) {
045: writeList(target, (List) object, level);
046: } else if (object instanceof String) {
047: writeStr(target, (String) object);
048: } else if (object instanceof Timestamp) {
049: writeTimestamp((Timestamp) object, target);
050: } else if (object instanceof Number) {
051: target.write(object.toString());
052: } else if (object instanceof Boolean) {
053: target.write(((Boolean) object).booleanValue() ? "TRUE"
054: : "FALSE");
055: } else {
056: Map map = get(object);
057: writeMap(target, map, level);
058: }
059: }
060:
061: private void writeMap(Writer target, Map map, int level)
062: throws IOException {
063: target.write('{');
064: if (map.size() > 0) {
065: int nextLevel = level + 1;
066: Set set = map.entrySet();
067: for (Iterator iterator = set.iterator(); iterator.hasNext();) {
068: Map.Entry entry = (Map.Entry) iterator.next();
069: Object value = entry.getValue();
070: if (value != null || isPreserveNulls()) {
071: ident(target, nextLevel);
072: Object key = entry.getKey();
073: if (!isComment(((String) key))) {
074: write(target, entry.getKey(), nextLevel);
075: target.write(" = ");
076: write(target, value, nextLevel);
077: } else {
078: writeComment(target, key + " = " + value);
079: }
080:
081: }
082: }
083: ident(target, level);
084: }
085: target.write("}");
086: }
087:
088: private void writeList(Writer target, List list, int level)
089: throws IOException {
090: target.write('[');
091: if (list.size() > 0) {
092: int nextLevel = level + 1;
093: for (Iterator iterator = list.iterator(); iterator
094: .hasNext();) {
095: ident(target, nextLevel);
096: write(target, iterator.next(), nextLevel);
097: }
098: ident(target, level);
099: }
100: target.write("]");
101: }
102:
103: private void writeStr(Writer target, String s) throws IOException {
104: if (!isComment(s)) {
105: if (isSymbol(s)) {
106: target.write(s);
107: } else {
108: writeString(target, s);
109: }
110: } else {
111: writeComment(target, s);
112: }
113: }
114:
115: private boolean isComment(String s) {
116: return s.trim().startsWith("#");
117: }
118:
119: private void ident(Writer target, int level) throws IOException {
120: target.write('\n');
121: for (int i = 0; i < level; i++) {
122: target.write(" ");
123: }
124: }
125:
126: private void writeString(Writer target, String s)
127: throws IOException {
128: target.write('\"');
129: for (int i = 0; i < s.length(); i++) {
130: if (s.charAt(i) == '\"' || s.charAt(i) == '\\') {
131: target.write('\\');
132: }
133: target.write(s.charAt(i));
134: }
135: target.write('\"');
136: }
137:
138: private void writeComment(Writer target, String s)
139: throws IOException {
140: for (int i = 0; i < s.length(); i++) {
141: target.write(s.charAt(i));
142: }
143: }
144:
145: private boolean isSymbol(String s) {
146: if (s.length() == 0) {
147: return false;
148: }
149: char ch = s.charAt(0);
150: if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')
151: || ch == '_' || ch == '$' || ch == '/') {
152: for (int i = 1; i < s.length(); i++) {
153: if (!isSymbolChar(s.charAt(i))) {
154: return false;
155: }
156: }
157: return true;
158: } else {
159: return false;
160: }
161: }
162:
163: private boolean isSymbolChar(char c) {
164: return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z')
165: || (c >= 'A' && c <= 'Z') || c == '-' || c == '_'
166: || c == ':' || c == '.' || c == '$' || c == '/';
167: }
168:
169: private void writeTimestamp(Timestamp timestamp, Writer target)
170: throws IOException {
171: GregorianCalendar calendar = new GregorianCalendar();
172: calendar.setTime(new Date(timestamp.getTime()));
173: if (calendar.get(Calendar.HOUR_OF_DAY) == 0
174: && calendar.get(Calendar.MINUTE) == 0
175: && calendar.get(Calendar.SECOND) == 0
176: && calendar.get(Calendar.MILLISECOND) == 0) {
177: target.write(SDF_SHORT.format(timestamp));
178: } else {
179: target.write(SDF_LONG.format(timestamp));
180: }
181: }
182: }
|