FlatDtdWriter.java

  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. import java.io.PrintWriter;
  23. import java.io.Writer;

  24. import org.dbunit.dataset.Column;
  25. import org.dbunit.dataset.DataSetException;
  26. import org.dbunit.dataset.IDataSet;
  27. import org.slf4j.Logger;
  28. import org.slf4j.LoggerFactory;

  29. /**
  30.  * @author Manuel Laflamme
  31.  * @author Last changed by: $Author$
  32.  * @version $Revision$ $Date$
  33.  * @since Jun 13, 2003
  34.  */
  35. public class FlatDtdWriter //implements IDataSetConsumer
  36. {

  37.     /**
  38.      * Logger for this class
  39.      */
  40.     private static final Logger logger = LoggerFactory.getLogger(FlatDtdWriter.class);

  41.     public static final ContentModel SEQUENCE = new SequenceModel();
  42.     public static final ContentModel CHOICE   = new ChoiceModel();

  43.     private Writer _writer;
  44.     private ContentModel _contentModel;

  45.     public FlatDtdWriter(Writer writer)
  46.     {
  47.         _writer = writer;
  48.         _contentModel = SEQUENCE;
  49.     }

  50.     public void setContentModel(ContentModel contentModel)
  51.     {
  52.         logger.debug("setContentModel(contentModel={}) - start", contentModel);
  53.         _contentModel = contentModel;
  54.     }

  55.     public void write(IDataSet dataSet) throws DataSetException
  56.     {
  57.         logger.debug("write(dataSet={}) - start", dataSet);

  58.         PrintWriter printOut = new PrintWriter(_writer);
  59.         String[] tableNames = dataSet.getTableNames();

  60.         // dataset element
  61.         printOut.print("<!ELEMENT dataset (\n");
  62.         for (int i = 0; i < tableNames.length; i++)
  63.         {
  64.             _contentModel.write(printOut, tableNames[i], i, tableNames.length);
  65.         }
  66.         printOut.print(")>\n");
  67.         printOut.print("\n");

  68.         // tables
  69.         for (int i = 0; i < tableNames.length; i++)
  70.         {
  71.             // table element
  72.             String tableName = tableNames[i];
  73.             printOut.print("<!ELEMENT ");
  74.             printOut.print(tableName);
  75.             printOut.print(" EMPTY>\n");

  76.             // column attributes
  77.             printOut.print("<!ATTLIST ");
  78.             printOut.print(tableName);
  79.             printOut.print("\n");
  80.             // Add the columns
  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  && column.getDefaultValue() == null)
  88.                 {
  89.                     printOut.print(" CDATA " + FlatDtdProducer.REQUIRED + "\n");
  90.                 }
  91.                 else
  92.                 {
  93.                     printOut.print(" CDATA " + FlatDtdProducer.IMPLIED + "\n");
  94.                 }
  95.             }
  96.             printOut.print(">\n");
  97.             printOut.print("\n");
  98.         }

  99.         printOut.flush();
  100.     }

  101.     /**
  102.      * @author Manuel Laflamme
  103.      * @author Last changed by: $Author$
  104.      * @version $Revision$ $Date$
  105.      * @since Jun 13, 2003
  106.      */
  107.     public static abstract class ContentModel
  108.     {
  109.         private final String _name;

  110.         private ContentModel(String name)
  111.         {
  112.             _name = name;
  113.         }

  114.         public String toString()
  115.         {
  116.             return _name;
  117.         }

  118.         public abstract void write(PrintWriter writer, String tableName,
  119.                 int tableIndex, int tableCount);
  120.     }

  121.    
  122.     /**
  123.      * @author Manuel Laflamme
  124.      * @author Last changed by: $Author$
  125.      * @version $Revision$ $Date$
  126.      * @since Jun 13, 2003
  127.      */
  128.     public static class SequenceModel extends ContentModel
  129.     {

  130.         /**
  131.          * Logger for this class
  132.          */
  133.         private static final Logger logger = LoggerFactory.getLogger(SequenceModel.class);

  134.         private SequenceModel()
  135.         {
  136.             super("sequence");
  137.         }

  138.         public void write(PrintWriter writer, String tableName, int tableIndex, int tableCount)
  139.         {
  140.             if (logger.isDebugEnabled())
  141.             {
  142.                 logger.debug("write(writer={}, tableName={}, tableIndex={}, tableCount={}) - start",
  143.                         new Object[]{ writer, tableName, String.valueOf(tableIndex), String.valueOf(tableCount)});
  144.             }

  145.             boolean last = (tableIndex + 1) == tableCount;

  146.             writer.print("    ");
  147.             writer.print(tableName);
  148.             writer.print("*");
  149.             if (!last)
  150.             {
  151.                 writer.print(",\n");
  152.             }
  153.         }
  154.     }

  155.     /**
  156.      * @author Manuel Laflamme
  157.      * @author Last changed by: $Author$
  158.      * @version $Revision$ $Date$
  159.      * @since Jun 13, 2003
  160.      */
  161.     public static class ChoiceModel extends ContentModel
  162.     {

  163.         /**
  164.          * Logger for this class
  165.          */
  166.         private static final Logger logger = LoggerFactory.getLogger(ChoiceModel.class);

  167.         private ChoiceModel()
  168.         {
  169.             super("sequence");
  170.         }

  171.         public void write(PrintWriter writer, String tableName, int tableIndex, int tableCount)
  172.         {
  173.             if (logger.isDebugEnabled())
  174.             {
  175.                 logger.debug("write(writer={}, tableName={}, tableIndex={}, tableCount={}) - start",
  176.                         new Object[]{ writer, tableName, String.valueOf(tableIndex), String.valueOf(tableCount)});
  177.             }

  178.             boolean first = tableIndex == 0;
  179.             boolean last = (tableIndex + 1) == tableCount;

  180.             if (first)
  181.             {
  182.                 writer.print("   (");
  183.             }
  184.             else
  185.             {
  186.                 writer.print("    ");
  187.             }
  188.             writer.print(tableName);

  189.             if (!last)
  190.             {
  191.                 writer.print("|\n");
  192.             }
  193.             else
  194.             {
  195.                 writer.print(")*");
  196.             }
  197.         }
  198.     }
  199. }