Project

General

Profile

1
/**
2
 *        Name: ElementNode.java
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
 *
9
 *     Version: '$Id: ElementNode.java 136 2000-06-08 00:19:37Z jones $'
10
 */
11

    
12
package edu.ucsb.nceas.metacat;
13

    
14
import java.sql.*;
15
import java.io.IOException;
16
import java.util.Vector;
17
import java.util.Enumeration;
18

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

    
25
    private Connection	conn;
26

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

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

    
45
      //Lookup data for self
46
      setElementInfo(nodeid);
47
      
48
      //Create child nodes (elements or attributes)
49
      setChildrenNodes(nodeid);
50
    }
51

    
52
    /** 
53
     * Construct a new ElementNode instance
54
     *
55
     * @param conn the database connection to use to initialize 
56
     * @param nodeid the element_id for the node to be created
57
     * @param parentnodeid the id of the parent node
58
     * @param nodename the name of the element
59
     */
60
    public ElementNode (Connection conn, long nodeid, long parentnodeid,
61
                          String nodename, String nodetype) {
62
      this(conn);
63
      setNodeID(nodeid);
64
      setParentID(parentnodeid);
65
      setTagName(nodename);
66
      setNodeType(nodetype);
67

    
68
      
69
      // Create child nodes (elements or attributes)
70
      setChildrenNodes(nodeid);
71
    }
72

    
73
    /** Look up the info needed to construct this element from the DB */
74
    private void setElementInfo(long nodeid) {
75
      long element_id=0;
76
      long parentnodeid=0;
77
      String nodetype=null;
78
      String nodename=null;
79
      String nodedata=null;
80

    
81
      PreparedStatement pstmt;
82
      try {
83
        pstmt =
84
          conn.prepareStatement("SELECT nodeid,parentnodeid,nodetype,nodename,"+
85
               "replace(" +
86
               "replace(" +
87
               "replace(nodedata,'&','&') " +
88
               ",'<','&lt;') " +
89
               ",'>','&gt;') " +
90
               "FROM xml_nodes WHERE nodeid = ?");
91
        // Bind the values to the query
92
        pstmt.setLong(1, nodeid);
93

    
94
        pstmt.execute();
95
        try {
96
          ResultSet rs = pstmt.getResultSet();
97
          try {
98
            boolean tableHasRows = rs.next();
99
            if (tableHasRows) {
100
              try {
101
                element_id = rs.getLong(1);
102
                parentnodeid = rs.getLong(2);
103
                nodetype = rs.getString(3);
104
                nodename = rs.getString(4);
105
                nodedata = rs.getString(5);
106
              } catch (SQLException e) {
107
                System.out.println("Error with getInt: " + e.getMessage());
108
              }
109
            } else {
110
              System.err.println("Error: no rows for nodeid: " + nodeid);
111
            }
112
          } catch (SQLException e) {
113
            System.out.println("Error with next: " + e.getMessage());
114
          }
115
        } catch (SQLException e) {
116
          System.out.println("Error with getrset: " + e.getMessage());
117
        }
118
        pstmt.close();
119
      } catch (SQLException e) {
120
        System.out.println("Error getting id: " + e.getMessage());
121
      }
122

    
123
      // Record our node type
124
      setNodeType(nodetype);
125

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

    
139
    /** Look up each child node from the DB and and create it */
140
    private void setChildrenNodes(long nodeid) {
141
      long element_id=0;
142
      long parentnodeid=0;
143
      String nodetype=null;
144
      String nodename=null;
145
      String nodedata=null;
146

    
147
      PreparedStatement pstmt;
148
      try {
149
        pstmt =
150
          conn.prepareStatement("SELECT nodeid,parentnodeid,nodetype, " +
151
                  "nodename, " +
152
                  "replace(" +
153
                  "replace(" +
154
                  "replace(nodedata,'&','&amp;')" +
155
                  ",'<','&lt;') " +
156
                  ",'>','&gt;') " +
157
                  "FROM xml_nodes WHERE parentnodeid = ? " +
158
                  "ORDER BY nodeindex");
159

    
160
        // Bind the values to the query
161
        pstmt.setLong(1, nodeid);
162

    
163
        pstmt.execute();
164
        try {
165
          ResultSet rs = pstmt.getResultSet();
166
          try {
167
            boolean tableHasRows = rs.next();
168
            while (tableHasRows) {
169
              try {
170
                element_id = rs.getLong(1);
171
                parentnodeid = rs.getLong(2);
172
                nodetype = rs.getString(3);
173
                nodename = rs.getString(4);
174
                nodedata = rs.getString(5);
175

    
176
                if ( (nodetype.equals("ELEMENT")) ||
177
                     (nodetype.equals("DOCUMENT"))
178
                   ) {
179
                  ElementNode child = new ElementNode(conn,
180
                                element_id,parentnodeid,nodename, nodetype);
181
                  addChildNode(child);
182
                } else if (nodetype.equals("ATTRIBUTE")) {
183
                  setAttribute(nodename,nodedata);
184
                } else if (nodetype.equals("TEXT")) {
185
                  TextNode child = new TextNode(conn,
186
                                element_id,parentnodeid,nodedata,nodetype);
187
                  addChildNode(child);
188
                } 
189

    
190
              } catch (SQLException e) {
191
                System.out.println("Error with getInt: " + e.getMessage());
192
              }
193

    
194
              // Advance to the next record in the cursor
195
              tableHasRows = rs.next();
196
            }
197
          } catch (SQLException e) {
198
            System.out.println("Error with next: " + e.getMessage());
199
          }
200
        } catch (SQLException e) {
201
          System.out.println("Error with getrset: " + e.getMessage());
202
        }
203
        pstmt.close();
204
      } catch (SQLException e) {
205
        System.out.println("Error getting id: " + e.getMessage());
206
      }
207

    
208
    }
209

    
210
    /** 
211
     * String representation for display purposes (recursively descends through
212
     * children to create an XML subtree)
213
     */
214
    public String toString () {
215

    
216
        StringBuffer value = new StringBuffer();
217
        String nodetype = getNodeType();
218

    
219
        if (nodetype.equals("ELEMENT")) {
220
          value.append('<');
221
          value.append(getTagName());
222
          value.append(getAttributes().toString());
223
          value.append('>');
224
        } 
225

    
226
        // Process children recursively here
227
        BasicNode child = null;
228
        Enumeration e = getChildren();
229
        while (e.hasMoreElements()) {
230
          child = (BasicNode)e.nextElement(); 
231
          value.append(child);
232
        }
233

    
234
        if (nodetype.equals("ELEMENT")) {
235
          value.append("</");
236
          value.append(getTagName());
237
          value.append('>');
238
        }
239

    
240
        return value.toString();
241
    }
242
}
(14-14/19)