Project

General

Profile

1 22 jones
/**
2 203 jones
 *  '$RCSfile$'
3
 *    Purpose: A Class that represents an XML element and its contents,
4
 *             and can build itself from a database connection
5
 *  Copyright: 2000 Regents of the University of California and the
6
 *             National Center for Ecological Analysis and Synthesis
7
 *    Authors: Matt Jones
8 22 jones
 *
9 203 jones
 *   '$Author$'
10
 *     '$Date$'
11
 * '$Revision$'
12 22 jones
 */
13
14 51 jones
package edu.ucsb.nceas.metacat;
15 22 jones
16
import java.sql.*;
17
import java.io.IOException;
18 23 jones
import java.util.Vector;
19 22 jones
import java.util.Enumeration;
20
21 31 jones
/**
22
 * A Class that represents an XML element and its contents,
23
 * and can build itself from a database connection
24
 */
25 130 jones
public class ElementNode extends BasicNode {
26 22 jones
27 23 jones
    private Connection	conn;
28 22 jones
29 31 jones
    /**
30 130 jones
     * Construct a new ElementNode instance
31 31 jones
     *
32
     * @param conn the database connection to use to initialize
33
     */
34 130 jones
    public ElementNode (Connection conn) {
35 24 jones
      this.conn = conn;
36
    }
37
38 31 jones
    /**
39 130 jones
     * Construct a new ElementNode instance
40 31 jones
     *
41
     * @param conn the database connection to use to initialize
42
     * @param nodeid the element_id for the node to be created
43
     */
44 130 jones
    public ElementNode (Connection conn, long nodeid) {
45 24 jones
      this(conn);
46 22 jones
47
      //Lookup data for self
48 24 jones
      setElementInfo(nodeid);
49 22 jones
50 24 jones
      //Create child nodes (elements or attributes)
51
      setChildrenNodes(nodeid);
52
    }
53 22 jones
54 31 jones
    /**
55 130 jones
     * Construct a new ElementNode instance
56 31 jones
     *
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
     */
62 130 jones
    public ElementNode (Connection conn, long nodeid, long parentnodeid,
63 127 jones
                          String nodename, String nodetype) {
64 24 jones
      this(conn);
65 134 jones
      setNodeID(nodeid);
66 24 jones
      setParentID(parentnodeid);
67
      setTagName(nodename);
68 127 jones
      setNodeType(nodetype);
69 24 jones
70 132 jones
71 24 jones
      // Create child nodes (elements or attributes)
72
      setChildrenNodes(nodeid);
73 22 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 96 jones
          conn.prepareStatement("SELECT nodeid,parentnodeid,nodetype,nodename,"+
87
               "replace(" +
88
               "replace(" +
89
               "replace(nodedata,'&','&') " +
90
               ",'<','&lt;') " +
91
               ",'>','&gt;') " +
92
               "FROM xml_nodes WHERE nodeid = ?");
93 23 jones
        // Bind the values to the query
94
        pstmt.setLong(1, nodeid);
95
96
        pstmt.execute();
97
        try {
98
          ResultSet rs = pstmt.getResultSet();
99
          try {
100
            boolean tableHasRows = rs.next();
101
            if (tableHasRows) {
102
              try {
103
                element_id = rs.getLong(1);
104
                parentnodeid = rs.getLong(2);
105
                nodetype = rs.getString(3);
106
                nodename = rs.getString(4);
107
                nodedata = rs.getString(5);
108
              } catch (SQLException e) {
109
                System.out.println("Error with getInt: " + e.getMessage());
110
              }
111 132 jones
            } else {
112
              System.err.println("Error: no rows for nodeid: " + nodeid);
113 23 jones
            }
114
          } catch (SQLException e) {
115
            System.out.println("Error with next: " + e.getMessage());
116
          }
117
        } catch (SQLException e) {
118
          System.out.println("Error with getrset: " + e.getMessage());
119
        }
120
        pstmt.close();
121
      } catch (SQLException e) {
122
        System.out.println("Error getting id: " + e.getMessage());
123
      }
124
125 122 jones
      // Record our node type
126
      setNodeType(nodetype);
127
128 132 jones
      try {
129 122 jones
      if (nodetype.equals("ELEMENT") || nodetype.equals("DOCUMENT") ) {
130 134 jones
        setNodeID(element_id);
131 23 jones
        setParentID(parentnodeid);
132
        setTagName(nodename);
133
      }
134 132 jones
      } catch (NullPointerException npe) {
135
        System.err.println("Error with nodetype: " +
136
                           nodetype + npe.getMessage());
137
        System.err.println("Nodeid: " + element_id);
138
      }
139 23 jones
    }
140 24 jones
141
    /** Look up each child node from the DB and and create it */
142
    private void setChildrenNodes(long nodeid) {
143
      long element_id=0;
144
      long parentnodeid=0;
145
      String nodetype=null;
146
      String nodename=null;
147
      String nodedata=null;
148
149
      PreparedStatement pstmt;
150
      try {
151
        pstmt =
152
          conn.prepareStatement("SELECT nodeid,parentnodeid,nodetype, " +
153 96 jones
                  "nodename, " +
154
                  "replace(" +
155
                  "replace(" +
156
                  "replace(nodedata,'&','&amp;')" +
157
                  ",'<','&lt;') " +
158
                  ",'>','&gt;') " +
159 136 jones
                  "FROM xml_nodes WHERE parentnodeid = ? " +
160
                  "ORDER BY nodeindex");
161 122 jones
162 24 jones
        // Bind the values to the query
163
        pstmt.setLong(1, nodeid);
164
165
        pstmt.execute();
166
        try {
167
          ResultSet rs = pstmt.getResultSet();
168
          try {
169
            boolean tableHasRows = rs.next();
170
            while (tableHasRows) {
171
              try {
172
                element_id = rs.getLong(1);
173
                parentnodeid = rs.getLong(2);
174
                nodetype = rs.getString(3);
175
                nodename = rs.getString(4);
176
                nodedata = rs.getString(5);
177
178 122 jones
                if ( (nodetype.equals("ELEMENT")) ||
179 128 jones
                     (nodetype.equals("DOCUMENT"))
180 122 jones
                   ) {
181 130 jones
                  ElementNode child = new ElementNode(conn,
182 127 jones
                                element_id,parentnodeid,nodename, nodetype);
183
                  addChildNode(child);
184 24 jones
                } else if (nodetype.equals("ATTRIBUTE")) {
185
                  setAttribute(nodename,nodedata);
186 128 jones
                } else if (nodetype.equals("TEXT")) {
187 157 jones
                  TextNode child = new TextNode(element_id,parentnodeid,
188
                                                nodedata,nodetype);
189 128 jones
                  addChildNode(child);
190 126 jones
                }
191 24 jones
192
              } catch (SQLException e) {
193
                System.out.println("Error with getInt: " + e.getMessage());
194
              }
195
196
              // Advance to the next record in the cursor
197
              tableHasRows = rs.next();
198
            }
199
          } catch (SQLException e) {
200
            System.out.println("Error with next: " + e.getMessage());
201
          }
202
        } catch (SQLException e) {
203
          System.out.println("Error with getrset: " + e.getMessage());
204
        }
205
        pstmt.close();
206
      } catch (SQLException e) {
207
        System.out.println("Error getting id: " + e.getMessage());
208
      }
209
210
    }
211
212 31 jones
    /**
213
     * String representation for display purposes (recursively descends through
214
     * children to create an XML subtree)
215
     */
216 128 jones
    public String toString () {
217
218 24 jones
        StringBuffer value = new StringBuffer();
219 128 jones
        String nodetype = getNodeType();
220 24 jones
221 122 jones
        if (nodetype.equals("ELEMENT")) {
222
          value.append('<');
223
          value.append(getTagName());
224
          value.append(getAttributes().toString());
225
          value.append('>');
226
        }
227
228 24 jones
        // Process children recursively here
229 129 jones
        BasicNode child = null;
230 127 jones
        Enumeration e = getChildren();
231 24 jones
        while (e.hasMoreElements()) {
232 129 jones
          child = (BasicNode)e.nextElement();
233 128 jones
          value.append(child);
234 24 jones
        }
235
236 122 jones
        if (nodetype.equals("ELEMENT")) {
237
          value.append("</");
238
          value.append(getTagName());
239
          value.append('>');
240
        }
241
242 24 jones
        return value.toString();
243
    }
244 22 jones
}
245 203 jones
246
/**
247
 * '$Log$
248
 * 'Revision 1.19.2.2  2000/06/25 23:38:16  jones
249
 * 'Added RCSfile keyword
250
 * '
251
 * 'Revision 1.19.2.1  2000/06/25 23:34:18  jones
252
 * 'Changed documentation formatting, added log entries at bottom of source files
253
 * ''
254
 */