Project

General

Profile

1 15 jones
/**
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$'
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 boolean		isEmpty;
24
    private long		parent_id;
25
    private Hashtable		attributes;
26 16 jones
    private Connection		conn;
27 15 jones
28 16 jones
    public DBSAXElement (Connection conn, long element_id, String tagname,
29 15 jones
                         boolean isEmpty, long parent_id) {
30 16 jones
      this.conn = conn;
31
      this.element_id = assignElementID();
32
33 15 jones
      this.tagname = tagname;
34
      this.isEmpty = isEmpty;
35
      this.parent_id = parent_id;
36
      content = new StringBuffer();
37
      attributes = new Hashtable();
38 16 jones
      writeElementToDB();
39
40
      System.out.println("******ELEMENT_ID: " + this.element_id);
41 15 jones
    };
42
43 16 jones
    private long assignElementID() {
44
        long maxid=0;
45
        Statement stmt;
46
        try {
47
          stmt = conn.createStatement();
48
          stmt.execute("SELECT MAX(nodeid) FROM xml_elements");
49
          try {
50
            ResultSet rs = stmt.getResultSet();
51
            try {
52
              boolean tableHasRows = rs.next();
53
              if (tableHasRows) {
54
                try {
55
                  maxid = rs.getInt(1);
56
                } catch (SQLException e) {
57
                  System.out.println("Error with getInt: " + e.getMessage());
58
                }
59
              }
60
            } catch (SQLException e) {
61
              System.out.println("Error with next: " + e.getMessage());
62
            }
63
          } catch (SQLException e) {
64
            System.out.println("Error with getrset: " + e.getMessage());
65
          }
66
          stmt.close();
67
        } catch (SQLException e) {
68
          System.out.println("Error getting id: " + e.getMessage());
69
        }
70
71
        // assign a new ID number
72
        return (maxid + 1);
73
    }
74
75
    private void writeElementToDB() {
76
        try {
77
          PreparedStatement pstmt;
78
          if (parent_id != 0) {
79
            pstmt = conn.prepareStatement(
80
                "INSERT INTO xml_elements(nodeid, nodename, parentnodeid) " +
81
                "VALUES (?, ?, ?)");
82
          } else {
83
            pstmt = conn.prepareStatement(
84
                "INSERT INTO xml_elements(nodeid, nodename) " +
85
                "VALUES (?, ?)");
86
          }
87
88
          // Bind the values to the query
89
          pstmt.setLong(1, element_id); // The first ? is for NODEID
90
          pstmt.setString(2, getTagName());// The second ? is for NODENAME
91
          if (parent_id != 0) {
92
            pstmt.setLong(3, parent_id);
93
          }
94
          // Do the insertion
95
          pstmt.execute();
96
          pstmt.close();
97
98
        } catch (SQLException e) {
99
          System.out.println(e.getMessage());
100
        }
101
    }
102
103 15 jones
    // used by JTree to display this node
104
    public String toString ()
105
    {
106
	StringBuffer	value = new StringBuffer ();
107
	value.append ('<');
108
	value.append (getTagName ());
109
	value.append (getAttributes ().toString ());
110
	value.append ('>');
111
	return value.toString ();
112
    }
113
114
    /** Get the name of this element */
115
    public String  getTagName() { return tagname; }
116
117
    /** Get the attributes as a string */
118
    public String getAttributes() {
119
      StringBuffer buf = new StringBuffer();
120
      String attName = null;
121
      String attValue = null;
122
123
      Enumeration attList = attributes.keys();
124
      while (attList.hasMoreElements()) {
125
        attName = (String)attList.nextElement();
126
        attValue = (String)attributes.get(attName);
127
        buf.append(" ").append(attName).append("=").append(attValue);
128
      }
129
      return buf.toString();
130
    }
131
132
    /** Add a new attribute to this element, or set its value */
133
    public void setAttribute(String attName, String attValue) {
134
      if (attName != null) {
135
        attributes.put(attName, attValue);
136
      } else {
137
        System.err.println("Attribute name must not be null!");
138
      }
139
    }
140
141
    /** Get an attribute value by name */
142
    public String getAttribute(String attName) {
143
      return (String)attributes.get(attName);
144
    }
145
146
    /** Append to the content of the element */
147
    public void appendContent(char[] cbuf, int start, int len) {
148
      this.content.append( cbuf, start, len );
149
    }
150
151
    /** Append to the content of the element */
152
    public void appendContent(String new_content) {
153
      this.content.append( new_content );
154
    }
155
156
    /** Get the content of the element */
157
    public String getContent() {
158
      return this.content.toString();
159
    }
160
}