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.Schema;
13 import org.w3c.dom.Document;
14 import org.w3c.dom.Element;
19 public class TorqueConverter {
20 public void createRoot(Document document) {
21 Element root = document.createElement("database");
22 document.appendChild(root);
24 public void convert(Element root, Bookmark bookmark, Schema schema) {
26 Entity[] tables = bookmark.getEntitiesForSchema(
27 schema, Entity.TABLE_TYPE);
29 for (int i = 0, length = (tables == null) ? 0 : tables.length;
32 convert(root, tables[i]);
34 } catch (SQLException e) {
38 public void convert(Element root, Entity entity) {
39 Element table = root.getOwnerDocument().createElement("table");
40 table.setAttribute("name", entity.getName());
42 Column[] columns = entity.getColumns();
43 for (int i = 0, length = (columns == null) ? 0 : columns.length;
46 convert(table, columns[i]);
49 root.appendChild(table);
52 public void convert(Element root, Column column) {
53 Element element = root.getOwnerDocument().createElement("column");
54 element.setAttribute("name", column.getName());
55 if (column.isPrimaryKey()) {
56 element.setAttribute("primaryKey", "true");
58 element.setAttribute("required", column.isNullable() ? "false" : "true");
59 if (column.isNumeric() && column.getNumberOfFractionalDigits() > 0) {
60 element.setAttribute("size", String.valueOf(column.getSize()) +
61 "," + String.valueOf(column.getNumberOfFractionalDigits()));
62 } else if (column.getSize() >= 0) {
63 element.setAttribute("size", String.valueOf(column.getSize()));
65 element.setAttribute("type", getStandardType(column.getType()));
67 root.appendChild(element);
70 private String getStandardType(int type) {
73 Field[] fields = Types.class.getFields();
74 for (int i = 0, length = (fields == null) ? 0 : fields.length;
75 result == null & i < length;
77 if (fields[i].getDeclaringClass() == Integer.TYPE &&
78 Modifier.isStatic(fields[i].getModifiers()) &&
79 Modifier.isPublic(fields[i].getModifiers()) &&
80 type == fields[i].getLong(null)) {
82 result = fields[i].getName();
85 } catch (IllegalAccessException e) {