Project

General

Profile

1
/**
2
 *        Name: DBSAXElement.java
3
 *     Purpose: A Class that represents an XML element and its contents
4
 * Institution: National Center for Ecological Analysis and Synthesis
5
 *   Copyright: 2000
6
 *     Authors: Matt Jones
7
 *
8
 *     Version: '$Id: DBSAXElement.java 18 2000-04-11 16:40:51Z jones $'
9
 */
10

    
11
//package project;
12

    
13
import java.sql.*;
14
import java.io.IOException;
15
import java.util.Hashtable;
16
import java.util.Enumeration;
17

    
18
public class DBSAXElement {
19

    
20
    private long		element_id;
21
    private String		tagname;
22
    private StringBuffer	content;
23
    private long		parent_id;
24
    private Hashtable		attributes;
25
    private Connection		conn;
26

    
27
    public DBSAXElement (Connection conn, String tagname, 
28
                         long parent_id) {
29
      this.conn = conn;
30
      this.element_id = assignElementID();
31

    
32
      this.tagname = tagname;
33
      this.parent_id = parent_id;
34
      content = new StringBuffer();
35
      attributes = new Hashtable(); 
36
      writeElementToDB();
37
    };
38
    
39
    private long assignElementID() {
40
        long maxid=0;
41
        Statement stmt;
42
        try {
43
          stmt = conn.createStatement();
44
          stmt.execute("SELECT MAX(nodeid) FROM xml_elements");
45
          try {
46
            ResultSet rs = stmt.getResultSet();
47
            try {
48
              boolean tableHasRows = rs.next();
49
              if (tableHasRows) {
50
                try {
51
                  maxid = rs.getInt(1);
52
                } catch (SQLException e) {
53
                  System.out.println("Error with getInt: " + e.getMessage());
54
                }
55
              }
56
            } catch (SQLException e) {
57
              System.out.println("Error with next: " + e.getMessage());
58
            }
59
          } catch (SQLException e) {
60
            System.out.println("Error with getrset: " + e.getMessage());
61
          }
62
          stmt.close();
63
        } catch (SQLException e) {
64
          System.out.println("Error getting id: " + e.getMessage());
65
        }
66

    
67
        // assign a new ID number
68
        return (maxid + 1);
69
    }
70

    
71
    private void writeElementToDB() {
72
        try {
73
          PreparedStatement pstmt;
74
          if (parent_id != 0) {
75
            pstmt = conn.prepareStatement(
76
                "INSERT INTO xml_elements(nodeid, nodename, parentnodeid) " +
77
                "VALUES (?, ?, ?)");
78
          } else {
79
            pstmt = conn.prepareStatement(
80
                "INSERT INTO xml_elements(nodeid, nodename) " +
81
                "VALUES (?, ?)");
82
          }
83

    
84
          // Bind the values to the query
85
          pstmt.setLong(1, element_id); // The first ? is for NODEID
86
          pstmt.setString(2, getTagName());// The second ? is for NODENAME
87
          if (parent_id != 0) {
88
            pstmt.setLong(3, parent_id);
89
          }
90
          // Do the insertion
91
          pstmt.execute();
92
          pstmt.close();
93

    
94
        } catch (SQLException e) {
95
          System.out.println(e.getMessage());
96
        }
97
    }
98

    
99
    // used by JTree to display this node
100
    public String toString ()
101
    {
102
	StringBuffer	value = new StringBuffer ();
103
	value.append ('<');
104
	value.append (getTagName ());
105
	value.append (getAttributes ().toString ());
106
	value.append ('>');
107
	return value.toString ();
108
    }
109

    
110
    /** Get the id of this element */
111
    public long  getElementID() { return element_id; }
112

    
113
    /** Get the name of this element */
114
    public String  getTagName() { return tagname; }
115

    
116
    /** Get the attributes as a string */
117
    public String getAttributes() {
118
      StringBuffer buf = new StringBuffer();
119
      String attName = null;
120
      String attValue = null;
121

    
122
      Enumeration attList = attributes.keys();
123
      while (attList.hasMoreElements()) {
124
        attName = (String)attList.nextElement();
125
        attValue = (String)attributes.get(attName);
126
        buf.append(" ").append(attName).append("=").append(attValue);        
127
      }
128
      return buf.toString();      
129
    }
130

    
131
    /** Add a new attribute to this element, or set its value */
132
    public void setAttribute(String attName, String attValue) {
133
      if (attName != null) {
134
        attributes.put(attName, attValue);
135
      } else {
136
        System.err.println("Attribute name must not be null!");
137
      }
138
    }
139

    
140
    /** Get an attribute value by name */
141
    public String getAttribute(String attName) {
142
      return (String)attributes.get(attName);
143
    }
144

    
145
    /** Append to the content of the element */
146
    public void appendContent(char[] cbuf, int start, int len) {
147
      this.content.append( cbuf, start, len );
148
    }
149

    
150
    /** Append to the content of the element */
151
    public void appendContent(String new_content) {
152
      this.content.append( new_content );
153
    }
154

    
155
    /** Get the content of the element */
156
    public String getContent() {
157
      return this.content.toString();
158
    }
159

    
160
    /** Write the element content to the db connection */
161
    public void writeContentToDB() {
162
        try {
163
          PreparedStatement pstmt = conn.prepareStatement(
164
                "UPDATE xml_elements SET nodedata = ? WHERE nodeid = ?");
165

    
166
          // Bind the values to the query
167
          pstmt.setString(1, getContent());// The first ? is for NODEDATA
168
          pstmt.setLong(2, element_id); // The second ? is for NODEID
169

    
170
          // Do the update
171
          pstmt.execute();
172
          pstmt.close();
173

    
174
        } catch (SQLException e) {
175
          System.out.println(e.getMessage());
176
        }
177
    }
178

    
179
}
(3-3/9)