Project

General

Profile

« Previous | Next » 

Revision 130

Added by Matt Jones over 24 years ago

renamed ReaderElement to ElementNode

View differences:

src/edu/ucsb/nceas/metacat/ReaderElement.java
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$'
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 BasicNode {
24

  
25
    private Connection	conn;
26

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

  
36
    /** 
37
     * Construct a new ReaderElement 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 ReaderElement (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 ReaderElement 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 ReaderElement (Connection conn, long nodeid, long parentnodeid,
61
                          String nodename, String nodetype) {
62
      this(conn);
63
      setElementID(nodeid);
64
      setParentID(parentnodeid);
65
      setTagName(nodename);
66
      setNodeType(nodetype);
67

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

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

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

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

  
120
      // Record our node type
121
      setNodeType(nodetype);
122

  
123
      if (nodetype.equals("ELEMENT") || nodetype.equals("DOCUMENT") ) {
124
        setElementID(element_id);
125
        setParentID(parentnodeid);
126
        setTagName(nodename);
127
      }
128
    }
129

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

  
138
      PreparedStatement pstmt;
139
      try {
140
        pstmt =
141
          conn.prepareStatement("SELECT nodeid,parentnodeid,nodetype, " +
142
                  "nodename, " +
143
                  "replace(" +
144
                  "replace(" +
145
                  "replace(nodedata,'&','&amp;')" +
146
                  ",'<','&lt;') " +
147
                  ",'>','&gt;') " +
148
                  "FROM xml_nodes WHERE parentnodeid = ?");
149

  
150
                  // ORDER BY nodeindex
151

  
152
        // Bind the values to the query
153
        pstmt.setLong(1, nodeid);
154

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

  
168
                if ( (nodetype.equals("ELEMENT")) ||
169
                     (nodetype.equals("DOCUMENT"))
170
                   ) {
171
                  ReaderElement child = new ReaderElement(conn,
172
                                element_id,parentnodeid,nodename, nodetype);
173
                  addChildNode(child);
174
                } else if (nodetype.equals("ATTRIBUTE")) {
175
                  setAttribute(nodename,nodedata);
176
                } else if (nodetype.equals("TEXT")) {
177
                  TextNode child = new TextNode(conn,
178
                                element_id,parentnodeid,nodedata,nodetype);
179
                  addChildNode(child);
180
                } 
181

  
182
              } catch (SQLException e) {
183
                System.out.println("Error with getInt: " + e.getMessage());
184
              }
185

  
186
              // Advance to the next record in the cursor
187
              tableHasRows = rs.next();
188
            }
189
          } catch (SQLException e) {
190
            System.out.println("Error with next: " + e.getMessage());
191
          }
192
        } catch (SQLException e) {
193
          System.out.println("Error with getrset: " + e.getMessage());
194
        }
195
        pstmt.close();
196
      } catch (SQLException e) {
197
        System.out.println("Error getting id: " + e.getMessage());
198
      }
199

  
200
    }
201

  
202
    /** 
203
     * String representation for display purposes (recursively descends through
204
     * children to create an XML subtree)
205
     */
206
    public String toString () {
207

  
208
        StringBuffer value = new StringBuffer();
209
        String nodetype = getNodeType();
210

  
211
        if (nodetype.equals("ELEMENT")) {
212
          value.append('<');
213
          value.append(getTagName());
214
          value.append(getAttributes().toString());
215
          value.append('>');
216
        } 
217

  
218
        // Process children recursively here
219
        BasicNode child = null;
220
        Enumeration e = getChildren();
221
        while (e.hasMoreElements()) {
222
          child = (BasicNode)e.nextElement(); 
223
          value.append(child);
224
        }
225

  
226
        if (nodetype.equals("ELEMENT")) {
227
          value.append("</");
228
          value.append(getTagName());
229
          value.append('>');
230
        }
231

  
232
        return value.toString();
233
    }
234
}
235 0

  
src/edu/ucsb/nceas/metacat/ElementNode.java
1 1
/**
2
 *        Name: ReaderElement.java
2
 *        Name: ElementNode.java
3 3
 *     Purpose: A Class that represents an XML element and its contents,
4 4
 *              and can build itself from a database connection
5 5
 *   Copyright: 2000 Regents of the University of California and the
......
20 20
 * A Class that represents an XML element and its contents,
21 21
 * and can build itself from a database connection
22 22
 */
23
public class ReaderElement extends BasicNode {
23
public class ElementNode extends BasicNode {
24 24

  
25 25
    private Connection	conn;
26 26

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

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

  
45 45
      //Lookup data for self
......
50 50
    }
51 51

  
52 52
    /** 
53
     * Construct a new ReaderElement instance
53
     * Construct a new ElementNode instance
54 54
     *
55 55
     * @param conn the database connection to use to initialize 
56 56
     * @param nodeid the element_id for the node to be created
57 57
     * @param parentnodeid the id of the parent node
58 58
     * @param nodename the name of the element
59 59
     */
60
    public ReaderElement (Connection conn, long nodeid, long parentnodeid,
60
    public ElementNode (Connection conn, long nodeid, long parentnodeid,
61 61
                          String nodename, String nodetype) {
62 62
      this(conn);
63 63
      setElementID(nodeid);
......
168 168
                if ( (nodetype.equals("ELEMENT")) ||
169 169
                     (nodetype.equals("DOCUMENT"))
170 170
                   ) {
171
                  ReaderElement child = new ReaderElement(conn,
171
                  ElementNode child = new ElementNode(conn,
172 172
                                element_id,parentnodeid,nodename, nodetype);
173 173
                  addChildNode(child);
174 174
                } else if (nodetype.equals("ATTRIBUTE")) {
src/edu/ucsb/nceas/metacat/DBReader.java
149 149
    String doctype = dti.getDoctype();
150 150
    String sysid = dti.getSystemID();
151 151

  
152
    ReaderElement element = new ReaderElement(conn, getRootNode(docid));
152
    ElementNode element = new ElementNode(conn, getRootNode(docid));
153 153
    doc.append("<?xml version=\"1.0\"?>\n");
154 154
    
155 155
    if (docname != null) {

Also available in: Unified diff