1 21 package org.dbunit.dataset.xml; 22 23 import org.dbunit.dataset.Column; 24 import org.dbunit.dataset.DataSetException; 25 import org.dbunit.dataset.IDataSet; 26 27 import java.io.PrintWriter ; 28 import java.io.Writer ; 29 30 35 public class FlatDtdWriter { 37 public static final ContentModel SEQUENCE = new SequenceModel(); 38 public static final ContentModel CHOICE = new ChoiceModel(); 39 40 private Writer _writer; 41 private ContentModel _contentModel; 42 43 public FlatDtdWriter(Writer writer) 44 { 45 _writer = writer; 46 _contentModel = SEQUENCE; 47 } 48 49 public void setContentModel(ContentModel contentModel) 50 { 51 _contentModel = contentModel; 52 } 53 54 public void write(IDataSet dataSet) throws DataSetException 55 { 56 PrintWriter printOut = new PrintWriter (_writer); 57 String [] tableNames = dataSet.getTableNames(); 58 59 printOut.print("<!ELEMENT dataset (\n"); 61 for (int i = 0; i < tableNames.length; i++) 62 { 63 _contentModel.write(printOut, tableNames[i], i, tableNames.length); 64 } 65 printOut.print(")>\n"); 66 printOut.print("\n"); 67 68 for (int i = 0; i < tableNames.length; i++) 70 { 71 String tableName = tableNames[i]; 73 printOut.print("<!ELEMENT "); 74 printOut.print(tableName); 75 printOut.print(" EMPTY>\n"); 76 77 printOut.print("<!ATTLIST "); 79 printOut.print(tableName); 80 printOut.print("\n"); 81 Column[] columns = dataSet.getTableMetaData(tableName).getColumns(); 82 for (int j = 0; j < columns.length; j++) 83 { 84 Column column = columns[j]; 85 printOut.print(" "); 86 printOut.print(column.getColumnName()); 87 if (column.getNullable() == Column.NO_NULLS) 88 { 89 printOut.print(" CDATA #REQUIRED\n"); 90 } 91 else 92 { 93 printOut.print(" CDATA #IMPLIED\n"); 94 } 95 } 96 printOut.print(">\n"); 97 printOut.print("\n"); 98 } 99 100 printOut.flush(); 101 } 102 103 public static abstract class ContentModel 104 { 105 private final String _name; 106 107 private ContentModel(String name) 108 { 109 _name = name; 110 } 111 112 public String toString() 113 { 114 return _name; 115 } 116 117 public abstract void write(PrintWriter writer, String tableName, 118 int tableIndex, int tableCount); 119 } 120 121 public static class SequenceModel extends ContentModel 122 { 123 private SequenceModel() 124 { 125 super("sequence"); 126 } 127 128 public void write(PrintWriter writer, String tableName, int tableIndex, int tableCount) 129 { 130 boolean last = (tableIndex + 1) == tableCount; 131 132 writer.print(" "); 133 writer.print(tableName); 134 writer.print("*"); 135 if (!last) 136 { 137 writer.print(",\n"); 138 } 139 } 140 } 141 142 public static class ChoiceModel extends ContentModel 143 { 144 private ChoiceModel() 145 { 146 super("sequence"); 147 } 148 149 public void write(PrintWriter writer, String tableName, int tableIndex, int tableCount) 150 { 151 boolean first = tableIndex == 0; 152 boolean last = (tableIndex + 1) == tableCount; 153 154 if (first) 155 { 156 writer.print(" ("); 157 } 158 else 159 { 160 writer.print(" "); 161 } 162 writer.print(tableName); 163 164 if (!last) 165 { 166 writer.print("|\n"); 167 } 168 else 169 { 170 writer.print(")*"); 171 } 172 } 173 } 174 } 175 | Popular Tags |