Project

General

Profile

1
/**
2
 *        Name: ReaderElement.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: ReaderElement.java 96 2000-05-12 23:01:04Z 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 ReaderElement extends BasicElement {
24

    
25
    private Connection	conn;
26
    private Vector	children;
27

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

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

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

    
54
    /** 
55
     * Construct a new ReaderElement instance
56
     *
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
     * @param nodedata the text content of the node
62
     */
63
    public ReaderElement (Connection conn, long nodeid, long parentnodeid,
64
                          String nodename, String nodedata) {
65
      this(conn);
66
      setElementID(nodeid);
67
      setParentID(parentnodeid);
68
      setTagName(nodename);
69
      appendContent(nodedata);
70

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

    
75
    /** Look up the info needed to construct this element from the DB */
76
    private void setElementInfo(long nodeid) {
77
      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
          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
        // 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
            }
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
      if (nodetype.equals("ELEMENT")) {
124
        setElementID(element_id);
125
        setParentID(parentnodeid);
126
        setTagName(nodename);
127
        appendContent(nodedata);
128
      }
129
    }
130

    
131
    /** Look up each child node from the DB and and create it */
132
    private void setChildrenNodes(long nodeid) {
133
      long element_id=0;
134
      long parentnodeid=0;
135
      String nodetype=null;
136
      String nodename=null;
137
      String nodedata=null;
138

    
139
      PreparedStatement pstmt;
140
      try {
141
        pstmt =
142
          conn.prepareStatement("SELECT nodeid,parentnodeid,nodetype, " +
143
                  "nodename, " +
144
                  "replace(" +
145
                  "replace(" +
146
                  "replace(nodedata,'&','&amp;')" +
147
                  ",'<','&lt;') " +
148
                  ",'>','&gt;') " +
149
                  "FROM xml_nodes WHERE parentnodeid = ?");
150
        // Bind the values to the query
151
        pstmt.setLong(1, nodeid);
152

    
153
        pstmt.execute();
154
        try {
155
          ResultSet rs = pstmt.getResultSet();
156
          try {
157
            boolean tableHasRows = rs.next();
158
            while (tableHasRows) {
159
              try {
160
                element_id = rs.getLong(1);
161
                parentnodeid = rs.getLong(2);
162
                nodetype = rs.getString(3);
163
                nodename = rs.getString(4);
164
                nodedata = rs.getString(5);
165

    
166
                if (nodetype.equals("ELEMENT")) {
167
                  ReaderElement child = new ReaderElement(conn,
168
                                element_id,parentnodeid,nodename,nodedata);
169
                  children.add(child);
170
                } else if (nodetype.equals("ATTRIBUTE")) {
171
                  setAttribute(nodename,nodedata);
172
                }
173

    
174
              } catch (SQLException e) {
175
                System.out.println("Error with getInt: " + e.getMessage());
176
              }
177

    
178
              // Advance to the next record in the cursor
179
              tableHasRows = rs.next();
180
            }
181
          } catch (SQLException e) {
182
            System.out.println("Error with next: " + e.getMessage());
183
          }
184
        } catch (SQLException e) {
185
          System.out.println("Error with getrset: " + e.getMessage());
186
        }
187
        pstmt.close();
188
      } catch (SQLException e) {
189
        System.out.println("Error getting id: " + e.getMessage());
190
      }
191

    
192
    }
193

    
194
    /** 
195
     * String representation for display purposes (recursively descends through
196
     * children to create an XML subtree)
197
     */
198
    public String toString ()
199
    {
200
        StringBuffer value = new StringBuffer();
201
        value.append('<');
202
        value.append(getTagName());
203
        value.append(getAttributes().toString());
204
        value.append('>');
205

    
206
        // Process children recursively here
207
        // Or do it in ReaderElement using a stack so we don;t have the
208
        // whole thing in memory at once?
209
        ReaderElement child = null;
210
        Enumeration e = children.elements();
211
        while (e.hasMoreElements()) {
212
          child = (ReaderElement)e.nextElement(); 
213
          value.append(child);
214
        }
215

    
216
        String cont = getContent();
217
        if (!cont.equals("null")) {
218
          value.append(cont);
219
        }
220

    
221
        value.append("</");
222
        value.append(getTagName());
223
        value.append('>');
224
        return value.toString();
225
    }
226
}
(19-19/20)