Project

General

Profile

1
/**
2
 *  '$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
 *    Release: @release@
9
 *
10
 *   '$Author: jones $'
11
 *     '$Date: 2000-08-14 13:53:34 -0700 (Mon, 14 Aug 2000) $'
12
 * '$Revision: 349 $'
13
 */
14

    
15
package edu.ucsb.nceas.metacat;
16

    
17
import java.sql.*;
18
import java.io.IOException;
19
import java.util.Vector;
20
import java.util.Enumeration;
21

    
22
/**
23
 * A Class that represents an XML element and its contents,
24
 * and can build itself from a database connection
25
 */
26
public class ElementNode extends BasicNode {
27

    
28
    private Connection	conn;
29

    
30
    /** 
31
     * Construct a new ElementNode instance
32
     *
33
     * @param conn the database connection to use to initialize 
34
     */
35
    public ElementNode (Connection conn) {
36
      this.conn = conn;
37
    }
38

    
39
    /** 
40
     * Construct a new ElementNode instance
41
     *
42
     * @param conn the database connection to use to initialize 
43
     * @param nodeid the element_id for the node to be created
44
     */
45
    public ElementNode (Connection conn, long nodeid) {
46
      this(conn);
47

    
48
      //Lookup data for self
49
      setElementInfo(nodeid);
50
      
51
      //Create child nodes (elements or attributes)
52
      setChildrenNodes(nodeid);
53
    }
54

    
55
    /** 
56
     * Construct a new ElementNode instance
57
     *
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
    public ElementNode (Connection conn, long nodeid, long parentnodeid,
64
                          String nodename, String nodetype) {
65
      this(conn);
66
      setNodeID(nodeid);
67
      setParentID(parentnodeid);
68
      setTagName(nodename);
69
      setNodeType(nodetype);
70

    
71
      
72
      // Create child nodes (elements or attributes)
73
      setChildrenNodes(nodeid);
74
    }
75

    
76
    /** Look up the info needed to construct this element from the DB */
77
    private void setElementInfo(long nodeid) {
78
      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
          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
        // 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
            } else {
113
              System.err.println("Error: no rows for nodeid: " + nodeid);
114
            }
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
      // Record our node type
127
      setNodeType(nodetype);
128

    
129
      try {
130
      if (nodetype.equals("ELEMENT") || nodetype.equals("DOCUMENT") ) {
131
        setNodeID(element_id);
132
        setParentID(parentnodeid);
133
        setTagName(nodename);
134
      }
135
      } catch (NullPointerException npe) {
136
        System.err.println("Error with nodetype: " + 
137
                           nodetype + npe.getMessage());
138
        System.err.println("Nodeid: " + element_id);
139
      } 
140
    }
141

    
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
                  "nodename, " +
155
                  "replace(" +
156
                  "replace(" +
157
                  "replace(nodedata,'&','&amp;')" +
158
                  ",'<','&lt;') " +
159
                  ",'>','&gt;') " +
160
                  "FROM xml_nodes WHERE parentnodeid = ? " +
161
                  "ORDER BY nodeindex");
162

    
163
        // 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
                if ( (nodetype.equals("ELEMENT")) ||
180
                     (nodetype.equals("DOCUMENT"))
181
                   ) {
182
                  ElementNode child = new ElementNode(conn,
183
                                element_id,parentnodeid,nodename, nodetype);
184
                  addChildNode(child);
185
                } else if (nodetype.equals("ATTRIBUTE")) {
186
                  setAttribute(nodename,nodedata);
187
                } else if (nodetype.equals("TEXT")) {
188
                  TextNode child = new TextNode(element_id,parentnodeid,
189
                                                nodedata);
190
                  addChildNode(child);
191
                } 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
                } 
200

    
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
    /** 
222
     * String representation for display purposes (recursively descends through
223
     * children to create an XML subtree)
224
     */
225
    public String toString () {
226

    
227
        StringBuffer value = new StringBuffer();
228
        String nodetype = getNodeType();
229

    
230
        if (nodetype.equals("ELEMENT")) {
231
          value.append('<');
232
          value.append(getTagName());
233
          value.append(getAttributes().toString());
234
          value.append('>');
235
        } 
236

    
237
        // Process children recursively here
238
        BasicNode child = null;
239
        Enumeration e = getChildren();
240
        while (e.hasMoreElements()) {
241
          child = (BasicNode)e.nextElement(); 
242
          value.append(child);
243
        }
244

    
245
        if (nodetype.equals("ELEMENT")) {
246
          value.append("</");
247
          value.append(getTagName());
248
          value.append('>');
249
        }
250

    
251
        return value.toString();
252
    }
253
}
254

    
255
/**
256
 * '$Log$
257
 * '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
 * '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
 * '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
 */
(18-18/27)