001: /*
002: *
003: * The DbUnit Database Testing Framework
004: * Copyright (C)2002-2004, DbUnit.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: */
021: package org.dbunit.dataset.xml;
022:
023: import org.slf4j.Logger;
024: import org.slf4j.LoggerFactory;
025:
026: import org.dbunit.dataset.Column;
027: import org.dbunit.dataset.DataSetException;
028: import org.dbunit.dataset.IDataSet;
029:
030: import java.io.PrintWriter;
031: import java.io.Writer;
032:
033: /**
034: * @author Manuel Laflamme
035: * @since Jun 13, 2003
036: * @version $Revision: 554 $
037: */
038: public class FlatDtdWriter //implements IDataSetConsumer
039: {
040:
041: /**
042: * Logger for this class
043: */
044: private static final Logger logger = LoggerFactory
045: .getLogger(FlatDtdWriter.class);
046:
047: public static final ContentModel SEQUENCE = new SequenceModel();
048: public static final ContentModel CHOICE = new ChoiceModel();
049:
050: private Writer _writer;
051: private ContentModel _contentModel;
052:
053: public FlatDtdWriter(Writer writer) {
054: _writer = writer;
055: _contentModel = SEQUENCE;
056: }
057:
058: public void setContentModel(ContentModel contentModel) {
059: logger.debug("setContentModel(contentModel=" + contentModel
060: + ") - start");
061:
062: _contentModel = contentModel;
063: }
064:
065: public void write(IDataSet dataSet) throws DataSetException {
066: logger.debug("write(dataSet=" + dataSet + ") - start");
067:
068: PrintWriter printOut = new PrintWriter(_writer);
069: String[] tableNames = dataSet.getTableNames();
070:
071: // dataset element
072: printOut.print("<!ELEMENT dataset (\n");
073: for (int i = 0; i < tableNames.length; i++) {
074: _contentModel.write(printOut, tableNames[i], i,
075: tableNames.length);
076: }
077: printOut.print(")>\n");
078: printOut.print("\n");
079:
080: // tables
081: for (int i = 0; i < tableNames.length; i++) {
082: // table element
083: String tableName = tableNames[i];
084: printOut.print("<!ELEMENT ");
085: printOut.print(tableName);
086: printOut.print(" EMPTY>\n");
087:
088: // column attributes
089: printOut.print("<!ATTLIST ");
090: printOut.print(tableName);
091: printOut.print("\n");
092: Column[] columns = dataSet.getTableMetaData(tableName)
093: .getColumns();
094: for (int j = 0; j < columns.length; j++) {
095: Column column = columns[j];
096: printOut.print(" ");
097: printOut.print(column.getColumnName());
098: if (column.getNullable() == Column.NO_NULLS) {
099: printOut.print(" CDATA #REQUIRED\n");
100: } else {
101: printOut.print(" CDATA #IMPLIED\n");
102: }
103: }
104: printOut.print(">\n");
105: printOut.print("\n");
106: }
107:
108: printOut.flush();
109: }
110:
111: public static abstract class ContentModel {
112:
113: /**
114: * Logger for this class
115: */
116: private static final Logger logger = LoggerFactory
117: .getLogger(ContentModel.class);
118:
119: private final String _name;
120:
121: private ContentModel(String name) {
122: _name = name;
123: }
124:
125: public String toString() {
126: logger.debug("toString() - start");
127:
128: return _name;
129: }
130:
131: public abstract void write(PrintWriter writer,
132: String tableName, int tableIndex, int tableCount);
133: }
134:
135: public static class SequenceModel extends ContentModel {
136:
137: /**
138: * Logger for this class
139: */
140: private static final Logger logger = LoggerFactory
141: .getLogger(SequenceModel.class);
142:
143: private SequenceModel() {
144: super ("sequence");
145: }
146:
147: public void write(PrintWriter writer, String tableName,
148: int tableIndex, int tableCount) {
149: logger.debug("write(writer=" + writer + ", tableName="
150: + tableName + ", tableIndex=" + tableIndex
151: + ", tableCount=" + tableCount + ") - start");
152:
153: boolean last = (tableIndex + 1) == tableCount;
154:
155: writer.print(" ");
156: writer.print(tableName);
157: writer.print("*");
158: if (!last) {
159: writer.print(",\n");
160: }
161: }
162: }
163:
164: public static class ChoiceModel extends ContentModel {
165:
166: /**
167: * Logger for this class
168: */
169: private static final Logger logger = LoggerFactory
170: .getLogger(ChoiceModel.class);
171:
172: private ChoiceModel() {
173: super ("sequence");
174: }
175:
176: public void write(PrintWriter writer, String tableName,
177: int tableIndex, int tableCount) {
178: logger.debug("write(writer=" + writer + ", tableName="
179: + tableName + ", tableIndex=" + tableIndex
180: + ", tableCount=" + tableCount + ") - start");
181:
182: boolean first = tableIndex == 0;
183: boolean last = (tableIndex + 1) == tableCount;
184:
185: if (first) {
186: writer.print(" (");
187: } else {
188: writer.print(" ");
189: }
190: writer.print(tableName);
191:
192: if (!last) {
193: writer.print("|\n");
194: } else {
195: writer.print(")*");
196: }
197: }
198: }
199: }
|