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 219 jones
                                                nodedata);
189 128 jones
                  addChildNode(child);
190 219 jones
                } else if (nodetype.equals("COMMENT")) {
191
                  CommentNode child = new CommentNode(element_id,parentnodeid,
192
                                                nodedata);
193
                  addChildNode(child);
194
                } else if (nodetype.equals("PI")) {
195
                  PINode child = new PINode(element_id,parentnodeid,nodename,
196
                                                nodedata);
197
                  addChildNode(child);
198 126 jones
                }
199 24 jones
200
              } catch (SQLException e) {
201
                System.out.println("Error with getInt: " + e.getMessage());
202
              }
203
204
              // Advance to the next record in the cursor
205
              tableHasRows = rs.next();
206
            }
207
          } catch (SQLException e) {
208
            System.out.println("Error with next: " + e.getMessage());
209
          }
210
        } catch (SQLException e) {
211
          System.out.println("Error with getrset: " + e.getMessage());
212
        }
213
        pstmt.close();
214
      } catch (SQLException e) {
215
        System.out.println("Error getting id: " + e.getMessage());
216
      }
217
218
    }
219
220 31 jones
    /**
221
     * String representation for display purposes (recursively descends through
222
     * children to create an XML subtree)
223
     */
224 128 jones
    public String toString () {
225
226 24 jones
        StringBuffer value = new StringBuffer();
227 128 jones
        String nodetype = getNodeType();
228 24 jones
229 122 jones
        if (nodetype.equals("ELEMENT")) {
230
          value.append('<');
231
          value.append(getTagName());
232
          value.append(getAttributes().toString());
233
          value.append('>');
234
        }
235
236 24 jones
        // Process children recursively here
237 129 jones
        BasicNode child = null;
238 127 jones
        Enumeration e = getChildren();
239 24 jones
        while (e.hasMoreElements()) {
240 129 jones
          child = (BasicNode)e.nextElement();
241 128 jones
          value.append(child);
242 24 jones
        }
243
244 122 jones
        if (nodetype.equals("ELEMENT")) {
245
          value.append("</");
246
          value.append(getTagName());
247
          value.append('>');
248
        }
249
250 24 jones
        return value.toString();
251
    }
252 22 jones
}
253 203 jones
254
/**
255
 * '$Log$
256 219 jones
 * 'Revision 1.20  2000/06/26 10:35:05  jones
257
 * 'Merged in substantial changes to DBWriter and associated classes and to
258
 * 'the MetaCatServlet in order to accomodate the new UPDATE and DELETE
259
 * 'functions.  The command line tools and the parameters for the
260
 * 'servlet have changed substantially.
261
 * '
262 203 jones
 * 'Revision 1.19.2.2  2000/06/25 23:38:16  jones
263
 * 'Added RCSfile keyword
264
 * '
265
 * 'Revision 1.19.2.1  2000/06/25 23:34:18  jones
266
 * 'Changed documentation formatting, added log entries at bottom of source files
267
 * ''
268
 */