Project

General

Profile

1 21 jones
/**
2
 *        Name: ReaderElement.java
3
 *     Purpose: A Class that represents an XML element and its contents,
4
 *              and can build itself from a database connection
5 35 jones
 *   Copyright: 2000 Regents of the University of California and the
6
 *              National Center for Ecological Analysis and Synthesis
7 21 jones
 *     Authors: Matt Jones
8
 *
9
 *     Version: '$Id$'
10
 */
11
12 51 jones
package edu.ucsb.nceas.metacat;
13 21 jones
14
import java.sql.*;
15
import java.io.IOException;
16 23 jones
import java.util.Vector;
17 21 jones
import java.util.Enumeration;
18
19 31 jones
/**
20
 * A Class that represents an XML element and its contents,
21
 * and can build itself from a database connection
22
 */
23 21 jones
public class ReaderElement extends BasicElement {
24
25 23 jones
    private Connection	conn;
26
    private Vector	children;
27 21 jones
28 31 jones
    /**
29
     * Construct a new ReaderElement instance
30
     *
31
     * @param conn the database connection to use to initialize
32
     */
33 24 jones
    public ReaderElement (Connection conn) {
34
      this.conn = conn;
35
      this.children = new Vector();
36
    }
37
38 31 jones
    /**
39
     * Construct a new ReaderElement instance
40
     *
41
     * @param conn the database connection to use to initialize
42
     * @param nodeid the element_id for the node to be created
43
     */
44 21 jones
    public ReaderElement (Connection conn, long nodeid) {
45 24 jones
      this(conn);
46 21 jones
47
      //Lookup data for self
48 24 jones
      setElementInfo(nodeid);
49 21 jones
50 24 jones
      //Create child nodes (elements or attributes)
51
      setChildrenNodes(nodeid);
52
    }
53 21 jones
54 31 jones
    /**
55
     * Construct a new ReaderElement instance
56
     *
57
     * @param conn the database connection to use to initialize
58
     * @param nodeid the element_id for the node to be created
59
     * @param parentnodeid the id of the parent node
60
     * @param nodename the name of the element
61
     * @param nodedata the text content of the node
62
     */
63 24 jones
    public ReaderElement (Connection conn, long nodeid, long parentnodeid,
64
                          String nodename, String nodedata) {
65
      this(conn);
66
      setElementID(nodeid);
67
      setParentID(parentnodeid);
68
      setTagName(nodename);
69
      appendContent(nodedata);
70
71
      // Create child nodes (elements or attributes)
72
      setChildrenNodes(nodeid);
73 21 jones
    }
74 23 jones
75 24 jones
    /** Look up the info needed to construct this element from the DB */
76
    private void setElementInfo(long nodeid) {
77 23 jones
      long element_id=0;
78
      long parentnodeid=0;
79
      String nodetype=null;
80
      String nodename=null;
81
      String nodedata=null;
82
83
      PreparedStatement pstmt;
84
      try {
85
        pstmt =
86
          conn.prepareStatement("SELECT nodeid,parentnodeid,nodetype, " +
87
                  "nodename,nodedata FROM xml_nodes WHERE nodeid = ?");
88
        // Bind the values to the query
89
        pstmt.setLong(1, nodeid);
90
91
        pstmt.execute();
92
        try {
93
          ResultSet rs = pstmt.getResultSet();
94
          try {
95
            boolean tableHasRows = rs.next();
96
            if (tableHasRows) {
97
              try {
98
                element_id = rs.getLong(1);
99
                parentnodeid = rs.getLong(2);
100
                nodetype = rs.getString(3);
101
                nodename = rs.getString(4);
102
                nodedata = rs.getString(5);
103
              } catch (SQLException e) {
104
                System.out.println("Error with getInt: " + e.getMessage());
105
              }
106
            }
107
          } catch (SQLException e) {
108
            System.out.println("Error with next: " + e.getMessage());
109
          }
110
        } catch (SQLException e) {
111
          System.out.println("Error with getrset: " + e.getMessage());
112
        }
113
        pstmt.close();
114
      } catch (SQLException e) {
115
        System.out.println("Error getting id: " + e.getMessage());
116
      }
117
118
      if (nodetype.equals("ELEMENT")) {
119
        setElementID(element_id);
120
        setParentID(parentnodeid);
121
        setTagName(nodename);
122 24 jones
        appendContent(nodedata);
123 23 jones
      }
124
    }
125 24 jones
126
    /** Look up each child node from the DB and and create it */
127
    private void setChildrenNodes(long nodeid) {
128
      long element_id=0;
129
      long parentnodeid=0;
130
      String nodetype=null;
131
      String nodename=null;
132
      String nodedata=null;
133
134
      PreparedStatement pstmt;
135
      try {
136
        pstmt =
137
          conn.prepareStatement("SELECT nodeid,parentnodeid,nodetype, " +
138
                  "nodename,nodedata FROM xml_nodes WHERE parentnodeid = ?");
139
        // Bind the values to the query
140
        pstmt.setLong(1, nodeid);
141
142
        pstmt.execute();
143
        try {
144
          ResultSet rs = pstmt.getResultSet();
145
          try {
146
            boolean tableHasRows = rs.next();
147
            while (tableHasRows) {
148
              try {
149
                element_id = rs.getLong(1);
150
                parentnodeid = rs.getLong(2);
151
                nodetype = rs.getString(3);
152
                nodename = rs.getString(4);
153
                nodedata = rs.getString(5);
154
155
                if (nodetype.equals("ELEMENT")) {
156
                  ReaderElement child = new ReaderElement(conn,
157
                                element_id,parentnodeid,nodename,nodedata);
158
                  children.add(child);
159
                } else if (nodetype.equals("ATTRIBUTE")) {
160
                  setAttribute(nodename,nodedata);
161
                }
162
163
              } catch (SQLException e) {
164
                System.out.println("Error with getInt: " + e.getMessage());
165
              }
166
167
              // Advance to the next record in the cursor
168
              tableHasRows = rs.next();
169
            }
170
          } catch (SQLException e) {
171
            System.out.println("Error with next: " + e.getMessage());
172
          }
173
        } catch (SQLException e) {
174
          System.out.println("Error with getrset: " + e.getMessage());
175
        }
176
        pstmt.close();
177
      } catch (SQLException e) {
178
        System.out.println("Error getting id: " + e.getMessage());
179
      }
180
181
    }
182
183 31 jones
    /**
184
     * String representation for display purposes (recursively descends through
185
     * children to create an XML subtree)
186
     */
187 24 jones
    public String toString ()
188
    {
189
        StringBuffer value = new StringBuffer();
190
        value.append('<');
191
        value.append(getTagName());
192
        value.append(getAttributes().toString());
193
        value.append('>');
194
195
        // Process children recursively here
196
        // Or do it in ReaderElement using a stack so we don;t have the
197
        // whole thing in memory at once?
198
        ReaderElement child = null;
199
        Enumeration e = children.elements();
200
        while (e.hasMoreElements()) {
201
          child = (ReaderElement)e.nextElement();
202
          value.append(child);
203
        }
204
205
        String cont = getContent();
206
        if (!cont.equals("null")) {
207
          value.append(cont);
208
        }
209
210
        value.append("</");
211
        value.append(getTagName());
212
        value.append('>');
213
        return value.toString();
214
    }
215 21 jones
}