KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > dataset > xml > FlatDtdWriter


1 /*
2  *
3  * The DbUnit Database Testing Framework
4  * Copyright (C)2002-2004, DbUnit.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */

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 JavaDoc;
28 import java.io.Writer JavaDoc;
29
30 /**
31  * @author Manuel Laflamme
32  * @since Jun 13, 2003
33  * @version $Revision: 1.5 $
34  */

35 public class FlatDtdWriter //implements IDataSetConsumer
36
{
37     public static final ContentModel SEQUENCE = new SequenceModel();
38     public static final ContentModel CHOICE = new ChoiceModel();
39
40     private Writer JavaDoc _writer;
41     private ContentModel _contentModel;
42
43     public FlatDtdWriter(Writer JavaDoc 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 JavaDoc printOut = new PrintWriter JavaDoc(_writer);
57         String JavaDoc[] tableNames = dataSet.getTableNames();
58
59         // dataset element
60
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         // tables
69
for (int i = 0; i < tableNames.length; i++)
70         {
71             // table element
72
String JavaDoc tableName = tableNames[i];
73             printOut.print("<!ELEMENT ");
74             printOut.print(tableName);
75             printOut.print(" EMPTY>\n");
76
77             // column attributes
78
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 JavaDoc _name;
106
107         private ContentModel(String JavaDoc name)
108         {
109             _name = name;
110         }
111
112         public String JavaDoc toString()
113         {
114             return _name;
115         }
116
117         public abstract void write(PrintWriter JavaDoc writer, String JavaDoc 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 JavaDoc writer, String JavaDoc 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 JavaDoc writer, String JavaDoc 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