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