Source code: com/dghda/kent/ReportTemplateDataSet.java
1 /* Copyright (C) 2001 Duane Griffin <duanegriffin@users.sourceforge.net>
2 This file is part of Kent.
3
4 Kent is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
8
9 Kent is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
13
14 You should have received a copy of the GNU General Public
15 License along with Kent; see the file COPYING. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA.
18 */
19
20 package com.dghda.kent;
21
22 import org.w3c.dom.*;
23
24 /**
25 This class provides a convenient method of creating a report template data set node.
26 */
27 public class ReportTemplateDataSet {
28
29 /** A class for describing a column. */
30 public static class ColumnDefinition {
31 public ColumnDefinition (String head, String source) {
32 m_Heading = head;
33 m_Field = source;
34 }
35 public ColumnDefinition (Element elem) {
36 m_Heading = XMLConstructor.getNodeText (elem);
37 m_Field = elem.getAttribute ("Field");
38 }
39
40 /** Write the column data to the given buffer. */
41 public void addElement (StringBuffer buffer, int indent) {
42 XMLConstructor.addIndent (buffer, indent);
43 buffer.append ("<Field");
44 XMLConstructor.addAttribute (buffer, "Field", m_Field);
45 buffer.append ('>');
46 buffer.append (m_Heading);
47 buffer.append ("</Field>\n");
48 }
49
50 /** Get the heading for the column. */
51 public String getHeading() {
52 return m_Heading;
53 }
54
55 /** Get the field source for the column. */
56 public String getField() {
57 return m_Field;
58 }
59
60 private String m_Heading;
61 private String m_Field;
62 }
63
64 /** Create a new data set object which uses the given source. */
65 public ReportTemplateDataSet (String source) {
66 m_Source = source;
67 m_Columns = new java.util.ArrayList();
68 }
69
70 /** Create a new data set object by parsing the given DataSet element. */
71 public ReportTemplateDataSet (Element dataSet) {
72
73 // Grab the data source
74 m_Source = dataSet.getAttribute (DATA_SOURCE_ATTR);
75
76 // Grab the columns
77 NodeList fields = dataSet.getElementsByTagName ("Field");
78 m_Columns = new java.util.ArrayList (fields.getLength());
79 for (int index = 0; index != fields.getLength(); ++index)
80 m_Columns.add (new ColumnDefinition ((Element) fields.item (index)));
81 }
82
83 /** Set the data source. */
84 public void setSource (String source) {
85 m_Source = source;
86 }
87
88 /** Get the data source. */
89 public String getSource() {
90 return m_Source;
91 }
92
93 /** Adds a column. */
94 public void addColumn (String name, String source) {
95 addColumn (new ColumnDefinition (name, source));
96 }
97
98 /** Adds a column. */
99 public void addColumn (ColumnDefinition column) {
100 m_Columns.add (column);
101 }
102
103 /**
104 Returns a list of all columns.
105 The list contains ColumnDefinition objects.
106 */
107 public java.util.List getColumns() {
108 return m_Columns;
109 }
110
111 /** Creates an element representing the object. */
112 public void addElement (StringBuffer buffer, int indent) {
113
114 // Create the node & add the source
115 XMLConstructor.addIndent (buffer, indent);
116 buffer.append ("<DataSet");
117 XMLConstructor.addAttribute (buffer, DATA_SOURCE_ATTR, m_Source);
118 buffer.append (">\n");
119
120 // Add in the columns
121 java.util.Iterator i = m_Columns.iterator();
122 while (i.hasNext()) {
123 ColumnDefinition column = (ColumnDefinition) i.next();
124 column.addElement (buffer, indent + 1);
125 }
126
127 // Close the tag
128 XMLConstructor.addIndent (buffer, indent);
129 buffer.append ("</DataSet>\n");
130 }
131
132 private static final String DATA_SOURCE_ATTR = "Source";
133
134 private String m_Source;
135 private java.util.ArrayList m_Columns;
136 }