1 package com.quantum.model.xml;
3 import java.lang.reflect.Field;
4 import java.lang.reflect.Modifier;
5 import java.sql.SQLException;
8 import com.quantum.model.Bookmark;
9 import com.quantum.model.Column;
10 import com.quantum.model.Entity;
11 import com.quantum.model.NotConnectedException;
12 import com.quantum.model.Schema;
14 import org.w3c.dom.Document;
15 import org.w3c.dom.Element;
20 public class TorqueConverter {
21 public void createRoot(Document document) {
22 Element root = document.createElement("database");
23 document.appendChild(root);
25 public void convert(Element root, Bookmark bookmark, Schema schema) {
27 Entity[] tables = bookmark.getEntitiesForSchema(
28 schema, Entity.TABLE_TYPE);
30 for (int i = 0, length = (tables == null) ? 0 : tables.length;
33 convert(root, tables[i]);
35 } catch (SQLException e) {
39 public void convert(Element root, Entity entity) {
40 Element table = root.getOwnerDocument().createElement("table");
41 table.setAttribute("name", entity.getName());
44 Column[] columns = entity.getColumns();
45 for (int i = 0, length = (columns == null) ? 0 : columns.length;
48 convert(table, columns[i]);
50 } catch (NotConnectedException e) {
51 } catch (SQLException e) {
54 root.appendChild(table);
57 public void convert(Element root, Column column) {
58 Element element = root.getOwnerDocument().createElement("column");
59 element.setAttribute("name", column.getName());
60 if (column.isPrimaryKey()) {
61 element.setAttribute("primaryKey", "true");
63 element.setAttribute("required", column.isNullable() ? "false" : "true");
64 if (column.isNumeric() && column.getNumberOfFractionalDigits() > 0) {
65 element.setAttribute("size", String.valueOf(column.getSize()) +
66 "," + String.valueOf(column.getNumberOfFractionalDigits()));
67 } else if (column.getSize() >= 0) {
68 element.setAttribute("size", String.valueOf(column.getSize()));
70 element.setAttribute("type", getStandardType(column.getType()));
72 root.appendChild(element);
75 private String getStandardType(int type) {
78 Field[] fields = Types.class.getFields();
79 for (int i = 0, length = (fields == null) ? 0 : fields.length;
80 result == null & i < length;
82 if (fields[i].getDeclaringClass() == Integer.TYPE &&
83 Modifier.isStatic(fields[i].getModifiers()) &&
84 Modifier.isPublic(fields[i].getModifiers()) &&
85 type == fields[i].getLong(null)) {
87 result = fields[i].getName();
90 } catch (IllegalAccessException e) {