View Javadoc
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 java.io.PrintWriter;
24  import java.io.Writer;
25  
26  import org.dbunit.dataset.Column;
27  import org.dbunit.dataset.DataSetException;
28  import org.dbunit.dataset.IDataSet;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  /**
33   * @author Manuel Laflamme
34   * @author Last changed by: $Author$
35   * @version $Revision$ $Date$
36   * @since Jun 13, 2003
37   */
38  public class FlatDtdWriter //implements IDataSetConsumer
39  {
40  
41      /**
42       * Logger for this class
43       */
44      private static final Logger logger = LoggerFactory.getLogger(FlatDtdWriter.class);
45  
46      public static final ContentModel SEQUENCE = new SequenceModel();
47      public static final ContentModel CHOICE   = new ChoiceModel();
48  
49      private Writer _writer;
50      private ContentModel _contentModel;
51  
52      public FlatDtdWriter(Writer writer)
53      {
54          _writer = writer;
55          _contentModel = SEQUENCE;
56      }
57  
58      public void setContentModel(ContentModel contentModel)
59      {
60          logger.debug("setContentModel(contentModel={}) - start", contentModel);
61          _contentModel = contentModel;
62      }
63  
64      public void write(IDataSet dataSet) throws DataSetException
65      {
66          logger.debug("write(dataSet={}) - start", dataSet);
67  
68          PrintWriter printOut = new PrintWriter(_writer);
69          String[] tableNames = dataSet.getTableNames();
70  
71          // dataset element
72          printOut.print("<!ELEMENT dataset (\n");
73          for (int i = 0; i < tableNames.length; i++)
74          {
75              _contentModel.write(printOut, tableNames[i], i, tableNames.length);
76          }
77          printOut.print(")>\n");
78          printOut.print("\n");
79  
80          // tables
81          for (int i = 0; i < tableNames.length; i++)
82          {
83              // table element
84              String tableName = tableNames[i];
85              printOut.print("<!ELEMENT ");
86              printOut.print(tableName);
87              printOut.print(" EMPTY>\n");
88  
89              // column attributes
90              printOut.print("<!ATTLIST ");
91              printOut.print(tableName);
92              printOut.print("\n");
93              // Add the columns
94              Column[] columns = dataSet.getTableMetaData(tableName).getColumns();
95              for (int j = 0; j < columns.length; j++)
96              {
97                  Column column = columns[j];
98                  printOut.print("    ");
99                  printOut.print(column.getColumnName());
100                 if (column.getNullable() == Column.NO_NULLS  && column.getDefaultValue() == null)
101                 {
102                     printOut.print(" CDATA " + FlatDtdProducer.REQUIRED + "\n");
103                 }
104                 else
105                 {
106                     printOut.print(" CDATA " + FlatDtdProducer.IMPLIED + "\n");
107                 }
108             }
109             printOut.print(">\n");
110             printOut.print("\n");
111         }
112 
113         printOut.flush();
114     }
115 
116     /**
117      * @author Manuel Laflamme
118      * @author Last changed by: $Author$
119      * @version $Revision$ $Date$
120      * @since Jun 13, 2003
121      */
122     public static abstract class ContentModel
123     {
124         private final String _name;
125 
126         private ContentModel(String name)
127         {
128             _name = name;
129         }
130 
131         public String toString()
132         {
133             return _name;
134         }
135 
136         public abstract void write(PrintWriter writer, String tableName,
137                 int tableIndex, int tableCount);
138     }
139 
140     
141     /**
142      * @author Manuel Laflamme
143      * @author Last changed by: $Author$
144      * @version $Revision$ $Date$
145      * @since Jun 13, 2003
146      */
147     public static class SequenceModel extends ContentModel
148     {
149 
150         /**
151          * Logger for this class
152          */
153         private static final Logger logger = LoggerFactory.getLogger(SequenceModel.class);
154 
155         private SequenceModel()
156         {
157             super("sequence");
158         }
159 
160         public void write(PrintWriter writer, String tableName, int tableIndex, int tableCount)
161         {
162         	if (logger.isDebugEnabled())
163         	{
164         		logger.debug("write(writer={}, tableName={}, tableIndex={}, tableCount={}) - start",
165         				new Object[]{ writer, tableName, String.valueOf(tableIndex), String.valueOf(tableCount)});
166         	}
167 
168             boolean last = (tableIndex + 1) == tableCount;
169 
170             writer.print("    ");
171             writer.print(tableName);
172             writer.print("*");
173             if (!last)
174             {
175                 writer.print(",\n");
176             }
177         }
178     }
179 
180     /**
181      * @author Manuel Laflamme
182      * @author Last changed by: $Author$
183      * @version $Revision$ $Date$
184      * @since Jun 13, 2003
185      */
186     public static class ChoiceModel extends ContentModel
187     {
188 
189         /**
190          * Logger for this class
191          */
192         private static final Logger logger = LoggerFactory.getLogger(ChoiceModel.class);
193 
194         private ChoiceModel()
195         {
196             super("sequence");
197         }
198 
199         public void write(PrintWriter writer, String tableName, int tableIndex, int tableCount)
200         {
201         	if (logger.isDebugEnabled())
202         	{
203         		logger.debug("write(writer={}, tableName={}, tableIndex={}, tableCount={}) - start",
204         				new Object[]{ writer, tableName, String.valueOf(tableIndex), String.valueOf(tableCount)});
205         	}
206 
207             boolean first = tableIndex == 0;
208             boolean last = (tableIndex + 1) == tableCount;
209 
210             if (first)
211             {
212                 writer.print("   (");
213             }
214             else
215             {
216                 writer.print("    ");
217             }
218             writer.print(tableName);
219 
220             if (!last)
221             {
222                 writer.print("|\n");
223             }
224             else
225             {
226                 writer.print(")*");
227             }
228         }
229     }
230 }