Project

General

Profile

« Previous | Next » 

Revision 24

Added by Matt Jones about 24 years ago

completed basic DBReader functionality. Can now read XML from DB using DBReader

View differences:

ReaderElement.java
21 21
    private Connection	conn;
22 22
    private Vector	children;
23 23

  
24
    public ReaderElement (Connection conn) {
25
      this.conn = conn;
26
      this.children = new Vector();
27
    }
28

  
24 29
    public ReaderElement (Connection conn, long nodeid) {
30
      this(conn);
25 31

  
26
      this.conn = conn;
27

  
28 32
      //Lookup data for self
29
      getElementInfo(nodeid);
33
      setElementInfo(nodeid);
30 34
      
31
      //Lookup list of child nodes
32
      //foreach childnode (childid,childtype)
33
      //  if (type.equals("ATTRIBUTE")) {
34
      //    add the att to attribute hash
35
      //  else (type.equals("ELEMENT")) {
36
      //    ReaderElement child = new ReaderElement(childid);
37
      //    add child to vector of children
38
      //  }
39
      //}
35
      //Create child nodes (elements or attributes)
36
      setChildrenNodes(nodeid);
37
    }
40 38

  
39
    public ReaderElement (Connection conn, long nodeid, long parentnodeid,
40
                          String nodename, String nodedata) {
41
      this(conn);
42
      setElementID(nodeid);
43
      setParentID(parentnodeid);
44
      setTagName(nodename);
45
      appendContent(nodedata);
46

  
47
      // Create child nodes (elements or attributes)
48
      setChildrenNodes(nodeid);
41 49
    }
42 50

  
43
    private void getElementInfo(long nodeid) {
51
    /** Look up the info needed to construct this element from the DB */
52
    private void setElementInfo(long nodeid) {
44 53
      long element_id=0;
45 54
      long parentnodeid=0;
46 55
      String nodetype=null;
......
86 95
        setElementID(element_id);
87 96
        setParentID(parentnodeid);
88 97
        setTagName(nodename);
89
        content.append(nodedata);
98
        appendContent(nodedata);
90 99
      }
91 100
    }
101

  
102
    /** Look up each child node from the DB and and create it */
103
    private void setChildrenNodes(long nodeid) {
104
      long element_id=0;
105
      long parentnodeid=0;
106
      String nodetype=null;
107
      String nodename=null;
108
      String nodedata=null;
109

  
110
      PreparedStatement pstmt;
111
      try {
112
        pstmt =
113
          conn.prepareStatement("SELECT nodeid,parentnodeid,nodetype, " +
114
                  "nodename,nodedata FROM xml_nodes WHERE parentnodeid = ?");
115
        // Bind the values to the query
116
        pstmt.setLong(1, nodeid);
117

  
118
        pstmt.execute();
119
        try {
120
          ResultSet rs = pstmt.getResultSet();
121
          try {
122
            boolean tableHasRows = rs.next();
123
            while (tableHasRows) {
124
              try {
125
                element_id = rs.getLong(1);
126
                parentnodeid = rs.getLong(2);
127
                nodetype = rs.getString(3);
128
                nodename = rs.getString(4);
129
                nodedata = rs.getString(5);
130

  
131
                if (nodetype.equals("ELEMENT")) {
132
                  ReaderElement child = new ReaderElement(conn,
133
                                element_id,parentnodeid,nodename,nodedata);
134
                  children.add(child);
135
                } else if (nodetype.equals("ATTRIBUTE")) {
136
                  setAttribute(nodename,nodedata);
137
                }
138

  
139
              } catch (SQLException e) {
140
                System.out.println("Error with getInt: " + e.getMessage());
141
              }
142

  
143
              // Advance to the next record in the cursor
144
              tableHasRows = rs.next();
145
            }
146
          } catch (SQLException e) {
147
            System.out.println("Error with next: " + e.getMessage());
148
          }
149
        } catch (SQLException e) {
150
          System.out.println("Error with getrset: " + e.getMessage());
151
        }
152
        pstmt.close();
153
      } catch (SQLException e) {
154
        System.out.println("Error getting id: " + e.getMessage());
155
      }
156

  
157
    }
158

  
159
    /** String representation for display purposes */
160
    public String toString ()
161
    {
162
        StringBuffer value = new StringBuffer();
163
        value.append('<');
164
        value.append(getTagName());
165
        value.append(getAttributes().toString());
166
        value.append('>');
167

  
168
        // Process children recursively here
169
        // Or do it in ReaderElement using a stack so we don;t have the
170
        // whole thing in memory at once?
171
        ReaderElement child = null;
172
        Enumeration e = children.elements();
173
        while (e.hasMoreElements()) {
174
          child = (ReaderElement)e.nextElement(); 
175
          value.append(child);
176
        }
177

  
178
        String cont = getContent();
179
        if (!cont.equals("null")) {
180
          value.append(cont);
181
        }
182

  
183
        value.append("</");
184
        value.append(getTagName());
185
        value.append('>');
186
        return value.toString();
187
    }
188

  
92 189
}

Also available in: Unified diff