Project

General

Profile

« Previous | Next » 

Revision 21

Added by Matt Jones over 24 years ago

created code to read XML doc from db source and emit it as an XML stream

View differences:

src/edu/ucsb/nceas/metacat/DBSAXNode.java
15 15
import java.util.Hashtable;
16 16
import java.util.Enumeration;
17 17

  
18
public class DBSAXElement {
18
public class DBSAXElement extends BasicElement {
19 19

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

  
27 22
    public DBSAXElement (Connection conn, String tagname, 
28 23
                         long parent_id) {
24

  
25
      super(tagname, parent_id);
26

  
29 27
      this.conn = conn;
30

  
31
      this.tagname = tagname;
32
      this.parent_id = parent_id;
33
      content = new StringBuffer();
34
      attributes = new Hashtable(); 
35 28
      writeElementToDB();
36 29
    };
37 30
    
......
100 93
        return assigned_id;
101 94
    }
102 95

  
103
    // 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 id of this element */
115
    public long  getElementID() { return element_id; }
116

  
117
    /** Get the name of this element */
118
    public String  getTagName() { return tagname; }
119

  
120
    /** Get the attributes as a string */
121
    public String getAttributes() {
122
      StringBuffer buf = new StringBuffer();
123
      String attName = null;
124
      String attValue = null;
125

  
126
      Enumeration attList = attributes.keys();
127
      while (attList.hasMoreElements()) {
128
        attName = (String)attList.nextElement();
129
        attValue = (String)attributes.get(attName);
130
        buf.append(" ").append(attName).append("=").append(attValue);        
131
      }
132
      return buf.toString();      
133
    }
134

  
135 96
    /** Add a new attribute to this element, or set its value */
136 97
    public void setAttribute(String attName, String attValue) {
137 98
      if (attName != null) {
......
162 123
      }
163 124
    }
164 125

  
165
    /** Get an attribute value by name */
166
    public String getAttribute(String attName) {
167
      return (String)attributes.get(attName);
168
    }
169

  
170
    /** Append to the content of the element */
171
    public void appendContent(char[] cbuf, int start, int len) {
172
      this.content.append( cbuf, start, len );
173
    }
174

  
175
    /** Append to the content of the element */
176
    public void appendContent(String new_content) {
177
      this.content.append( new_content );
178
    }
179

  
180
    /** Get the content of the element */
181
    public String getContent() {
182
      return this.content.toString();
183
    }
184

  
185 126
    /** Write the element content to the db connection */
186 127
    public void writeContentToDB() {
187 128
        try {
......
200 141
          System.out.println(e.getMessage());
201 142
        }
202 143
    }
203

  
204 144
}
src/edu/ucsb/nceas/metacat/BasicNode.java
1
/**
2
 *        Name: BasicElement.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.io.IOException;
14
import java.util.Hashtable;
15
import java.util.Enumeration;
16

  
17
public class BasicElement {
18

  
19
    protected long		element_id;
20
    protected String		tagname;
21
    protected StringBuffer	content;
22
    protected long		parent_id;
23
    protected Hashtable		attributes;
24

  
25
    public BasicElement (String tagname, long parent_id) {
26
      this.tagname = tagname;
27
      this.parent_id = parent_id;
28
      content = new StringBuffer();
29
      attributes = new Hashtable(); 
30
    }
31
    
32
    public BasicElement (long element_id, String tagname, long parent_id) {
33
      this(tagname,parent_id);
34
      this.element_id = element_id;
35
    }
36

  
37
    // used by JTree to display this node
38
    public String toString ()
39
    {
40
	StringBuffer	value = new StringBuffer ();
41
	value.append ('<');
42
	value.append (getTagName ());
43
	value.append (getAttributes ().toString ());
44
	value.append ('>');
45
	return value.toString ();
46
    }
47

  
48
    /** Get the id of this element */
49
    public long  getElementID() { return element_id; }
50

  
51
    /** Get the name of this element */
52
    public String  getTagName() { return tagname; }
53

  
54
    /** Get the attributes as a string */
55
    public String getAttributes() {
56
      StringBuffer buf = new StringBuffer();
57
      String attName = null;
58
      String attValue = null;
59

  
60
      Enumeration attList = attributes.keys();
61
      while (attList.hasMoreElements()) {
62
        attName = (String)attList.nextElement();
63
        attValue = (String)attributes.get(attName);
64
        buf.append(" ").append(attName).append("=").append(attValue);        
65
      }
66
      return buf.toString();      
67
    }
68

  
69
    /** Add a new attribute to this element, or set its value */
70
    public void setAttribute(String attName, String attValue) {
71
      if (attName != null) {
72
        // Enter the attribute in the hash table
73
        attributes.put(attName, attValue);
74
 
75
      } else {
76
        System.err.println("Attribute name must not be null!");
77
      }
78
    }
79

  
80
    /** Get an attribute value by name */
81
    public String getAttribute(String attName) {
82
      return (String)attributes.get(attName);
83
    }
84

  
85
    /** Append to the content of the element */
86
    public void appendContent(char[] cbuf, int start, int len) {
87
      this.content.append( cbuf, start, len );
88
    }
89

  
90
    /** Append to the content of the element */
91
    public void appendContent(String new_content) {
92
      this.content.append( new_content );
93
    }
94

  
95
    /** Get the content of the element */
96
    public String getContent() {
97
      return this.content.toString();
98
    }
99

  
100
}
0 101

  
src/edu/ucsb/nceas/metacat/DBReader.java
1
/**
2
 *        Name: DBReader.java
3
 *     Purpose: A Class that creates an XML text document
4
 *              from a query to a relational DB containing a DOM representation
5
 * Institution: National Center for Ecological Analysis and Synthesis
6
 *   Copyright: 2000
7
 *     Authors: Matt Jones
8
 *
9
 *     Version: '$Id$'
10
 */
11

  
12
import java.io.*;
13
import java.net.URL;
14
import java.net.MalformedURLException;
15
import java.sql.*;
16
import java.util.Stack;
17

  
18
public class DBReader {
19

  
20
  static  String 	defaultDB = "jdbc:oracle:thin:@localhost:1521:test";
21
  private Connection	conn = null;
22

  
23
  static public void main(String[] args) {
24
     
25
     if (args.length < 3)
26
     {
27
        System.err.println("Wrong number of arguments!!!");
28
        System.err.println("USAGE: java DBReader " +
29
                           "<nodeid> <user> <password> [dbstring]");
30
        return;
31
     } else {
32
        try {
33
                    
34
          String nodeid = args[0];
35
          String user     = args[1];
36
          String password = args[2];
37
          String dbstring = null;
38

  
39
          if (args.length <= 3) {
40
            dbstring = defaultDB;
41
          } else {
42
            dbstring = args[3];
43
          }
44

  
45
          DBReader rd = new DBReader(user, password, dbstring);
46
          String xml = rd.readDocument(nodeid);
47
          System.out.println(xml);
48

  
49
        } catch (Exception e) {
50
          System.err.println("EXCEPTION HANDLING REQUIRED");
51
          System.err.println(e.getMessage());
52
          e.printStackTrace(System.err);
53
        }
54
     }
55
  }
56
  
57
  private DBReader( String user, String password, String dbstring) 
58
                  throws IOException, 
59
                         SQLException, 
60
                         ClassNotFoundException
61
  {
62
     // Open a connection to the database
63
     conn = openDBConnection(
64
                "oracle.jdbc.driver.OracleDriver",
65
                dbstring, user, password);
66

  
67
  }
68
  
69
  private Connection openDBConnection(String dbDriver, String connection,
70
                String user, String password)
71
                throws SQLException, ClassNotFoundException {
72
     // Load the Oracle JDBC driver
73
     Class.forName (dbDriver);
74

  
75
     // Connect to the database
76
     Connection conn = DriverManager.getConnection( connection, user, password);
77
     return conn;
78
  }
79

  
80
  public String readDocument(String nodeid) {
81
    StringBuffer doc = new StringBuffer();
82

  
83
    System.out.println("\nGetting document with nodeid: " + nodeid + "\n");
84
    BasicElement element = readNodeFromDB(nodeid);
85
    doc.append(element);
86

  
87
    return (doc.toString());
88
  }
89

  
90
  /** look up the assigned element id from DB connection */
91
  private BasicElement readNodeFromDB(String nodeid) {
92
      long element_id=0;
93
      long nodeparentid=0;
94
      String nodetype=null;
95
      String nodename=null;
96
      String nodedata=null;
97

  
98
      PreparedStatement pstmt;
99
      try {
100
        pstmt = 
101
          conn.prepareStatement("SELECT nodeid,nodeparentid,nodetype,
102
                  nodename,nodedata FROM xml_nodes WHERE nodeid = ?");
103
        // Bind the values to the query
104
        pstmt.setString(1, nodeid);
105

  
106
        pstmt.execute();
107
        try {
108
          ResultSet rs = pstmt.getResultSet();
109
          try {
110
            boolean tableHasRows = rs.next();
111
            if (tableHasRows) {
112
              try {
113
                element_id = rs.getLong(1);
114
                nodeparentid = rs.getLong(2);
115
                nodetype = rs.getString(3);
116
                nodename = rs.getString(4);
117
                nodedata = rs.getString(5);
118
              } catch (SQLException e) {
119
                System.out.println("Error with getInt: " + e.getMessage());
120
              }
121
            }
122
          } catch (SQLException e) {
123
            System.out.println("Error with next: " + e.getMessage());
124
          }
125
        } catch (SQLException e) {
126
          System.out.println("Error with getrset: " + e.getMessage());
127
        }
128
        pstmt.close();
129
      } catch (SQLException e) {
130
        System.out.println("Error getting id: " + e.getMessage());
131
      }
132

  
133
      if (nodetype.equals("ELEMENT")) {
134
        BasicElement element = 
135
                 new BasicElement(element_id,nodename,nodeparentid);
136
        element.appendContent(nodedata);
137
      }
138

  
139
      // Get a list of child nodes and recursively retrieve them as well
140
      try {
141
        pstmt = 
142
          conn.prepareStatement("SELECT nodeid,nodeparentid,nodetype, " +
143
                                "nodename,nodedata FROM xml_nodes " +
144
                                "WHERE nodeparentid = ?");
145
        // Bind the values to the query
146
        pstmt.setString(1, nodeid);
147

  
148
        pstmt.execute();
149
        try {
150
          ResultSet rs = pstmt.getResultSet();
151
          try {
152
            boolean tableHasRows = rs.next();
153
            if (tableHasRows) {
154
              try {
155
                element_id = rs.getLong(1);
156
                nodeparentid = rs.getLong(2);
157
                nodetype = rs.getString(3);
158
                nodename = rs.getString(4);
159
                nodedata = rs.getString(5);
160
              } catch (SQLException e) {
161
                System.out.println("Error with getInt: " + e.getMessage());
162
              }
163
            }
164
          } catch (SQLException e) {
165
            System.out.println("Error with next: " + e.getMessage());
166
          }
167
        } catch (SQLException e) {
168
          System.out.println("Error with getrset: " + e.getMessage());
169
        }
170
        pstmt.close();
171
      } catch (SQLException e) {
172
        System.out.println("Error getting id: " + e.getMessage());
173
      }
174

  
175

  
176
      return element;
177
  }
178
}
0 179

  
src/edu/ucsb/nceas/metacat/DBSAXElement.java
15 15
import java.util.Hashtable;
16 16
import java.util.Enumeration;
17 17

  
18
public class DBSAXElement {
18
public class DBSAXElement extends BasicElement {
19 19

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

  
27 22
    public DBSAXElement (Connection conn, String tagname, 
28 23
                         long parent_id) {
24

  
25
      super(tagname, parent_id);
26

  
29 27
      this.conn = conn;
30

  
31
      this.tagname = tagname;
32
      this.parent_id = parent_id;
33
      content = new StringBuffer();
34
      attributes = new Hashtable(); 
35 28
      writeElementToDB();
36 29
    };
37 30
    
......
100 93
        return assigned_id;
101 94
    }
102 95

  
103
    // 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 id of this element */
115
    public long  getElementID() { return element_id; }
116

  
117
    /** Get the name of this element */
118
    public String  getTagName() { return tagname; }
119

  
120
    /** Get the attributes as a string */
121
    public String getAttributes() {
122
      StringBuffer buf = new StringBuffer();
123
      String attName = null;
124
      String attValue = null;
125

  
126
      Enumeration attList = attributes.keys();
127
      while (attList.hasMoreElements()) {
128
        attName = (String)attList.nextElement();
129
        attValue = (String)attributes.get(attName);
130
        buf.append(" ").append(attName).append("=").append(attValue);        
131
      }
132
      return buf.toString();      
133
    }
134

  
135 96
    /** Add a new attribute to this element, or set its value */
136 97
    public void setAttribute(String attName, String attValue) {
137 98
      if (attName != null) {
......
162 123
      }
163 124
    }
164 125

  
165
    /** Get an attribute value by name */
166
    public String getAttribute(String attName) {
167
      return (String)attributes.get(attName);
168
    }
169

  
170
    /** Append to the content of the element */
171
    public void appendContent(char[] cbuf, int start, int len) {
172
      this.content.append( cbuf, start, len );
173
    }
174

  
175
    /** Append to the content of the element */
176
    public void appendContent(String new_content) {
177
      this.content.append( new_content );
178
    }
179

  
180
    /** Get the content of the element */
181
    public String getContent() {
182
      return this.content.toString();
183
    }
184

  
185 126
    /** Write the element content to the db connection */
186 127
    public void writeContentToDB() {
187 128
        try {
......
200 141
          System.out.println(e.getMessage());
201 142
        }
202 143
    }
203

  
204 144
}
src/edu/ucsb/nceas/metacat/BasicElement.java
1
/**
2
 *        Name: BasicElement.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.io.IOException;
14
import java.util.Hashtable;
15
import java.util.Enumeration;
16

  
17
public class BasicElement {
18

  
19
    protected long		element_id;
20
    protected String		tagname;
21
    protected StringBuffer	content;
22
    protected long		parent_id;
23
    protected Hashtable		attributes;
24

  
25
    public BasicElement (String tagname, long parent_id) {
26
      this.tagname = tagname;
27
      this.parent_id = parent_id;
28
      content = new StringBuffer();
29
      attributes = new Hashtable(); 
30
    }
31
    
32
    public BasicElement (long element_id, String tagname, long parent_id) {
33
      this(tagname,parent_id);
34
      this.element_id = element_id;
35
    }
36

  
37
    // used by JTree to display this node
38
    public String toString ()
39
    {
40
	StringBuffer	value = new StringBuffer ();
41
	value.append ('<');
42
	value.append (getTagName ());
43
	value.append (getAttributes ().toString ());
44
	value.append ('>');
45
	return value.toString ();
46
    }
47

  
48
    /** Get the id of this element */
49
    public long  getElementID() { return element_id; }
50

  
51
    /** Get the name of this element */
52
    public String  getTagName() { return tagname; }
53

  
54
    /** Get the attributes as a string */
55
    public String getAttributes() {
56
      StringBuffer buf = new StringBuffer();
57
      String attName = null;
58
      String attValue = null;
59

  
60
      Enumeration attList = attributes.keys();
61
      while (attList.hasMoreElements()) {
62
        attName = (String)attList.nextElement();
63
        attValue = (String)attributes.get(attName);
64
        buf.append(" ").append(attName).append("=").append(attValue);        
65
      }
66
      return buf.toString();      
67
    }
68

  
69
    /** Add a new attribute to this element, or set its value */
70
    public void setAttribute(String attName, String attValue) {
71
      if (attName != null) {
72
        // Enter the attribute in the hash table
73
        attributes.put(attName, attValue);
74
 
75
      } else {
76
        System.err.println("Attribute name must not be null!");
77
      }
78
    }
79

  
80
    /** Get an attribute value by name */
81
    public String getAttribute(String attName) {
82
      return (String)attributes.get(attName);
83
    }
84

  
85
    /** Append to the content of the element */
86
    public void appendContent(char[] cbuf, int start, int len) {
87
      this.content.append( cbuf, start, len );
88
    }
89

  
90
    /** Append to the content of the element */
91
    public void appendContent(String new_content) {
92
      this.content.append( new_content );
93
    }
94

  
95
    /** Get the content of the element */
96
    public String getContent() {
97
      return this.content.toString();
98
    }
99

  
100
}
0 101

  
DBReader.java
1
/**
2
 *        Name: DBReader.java
3
 *     Purpose: A Class that creates an XML text document
4
 *              from a query to a relational DB containing a DOM representation
5
 * Institution: National Center for Ecological Analysis and Synthesis
6
 *   Copyright: 2000
7
 *     Authors: Matt Jones
8
 *
9
 *     Version: '$Id$'
10
 */
11

  
12
import java.io.*;
13
import java.net.URL;
14
import java.net.MalformedURLException;
15
import java.sql.*;
16
import java.util.Stack;
17

  
18
public class DBReader {
19

  
20
  static  String 	defaultDB = "jdbc:oracle:thin:@localhost:1521:test";
21
  private Connection	conn = null;
22

  
23
  static public void main(String[] args) {
24
     
25
     if (args.length < 3)
26
     {
27
        System.err.println("Wrong number of arguments!!!");
28
        System.err.println("USAGE: java DBReader " +
29
                           "<nodeid> <user> <password> [dbstring]");
30
        return;
31
     } else {
32
        try {
33
                    
34
          String nodeid = args[0];
35
          String user     = args[1];
36
          String password = args[2];
37
          String dbstring = null;
38

  
39
          if (args.length <= 3) {
40
            dbstring = defaultDB;
41
          } else {
42
            dbstring = args[3];
43
          }
44

  
45
          DBReader rd = new DBReader(user, password, dbstring);
46
          String xml = rd.readDocument(nodeid);
47
          System.out.println(xml);
48

  
49
        } catch (Exception e) {
50
          System.err.println("EXCEPTION HANDLING REQUIRED");
51
          System.err.println(e.getMessage());
52
          e.printStackTrace(System.err);
53
        }
54
     }
55
  }
56
  
57
  private DBReader( String user, String password, String dbstring) 
58
                  throws IOException, 
59
                         SQLException, 
60
                         ClassNotFoundException
61
  {
62
     // Open a connection to the database
63
     conn = openDBConnection(
64
                "oracle.jdbc.driver.OracleDriver",
65
                dbstring, user, password);
66

  
67
  }
68
  
69
  private Connection openDBConnection(String dbDriver, String connection,
70
                String user, String password)
71
                throws SQLException, ClassNotFoundException {
72
     // Load the Oracle JDBC driver
73
     Class.forName (dbDriver);
74

  
75
     // Connect to the database
76
     Connection conn = DriverManager.getConnection( connection, user, password);
77
     return conn;
78
  }
79

  
80
  public String readDocument(String nodeid) {
81
    StringBuffer doc = new StringBuffer();
82

  
83
    System.out.println("\nGetting document with nodeid: " + nodeid + "\n");
84
    BasicElement element = readNodeFromDB(nodeid);
85
    doc.append(element);
86

  
87
    return (doc.toString());
88
  }
89

  
90
  /** look up the assigned element id from DB connection */
91
  private BasicElement readNodeFromDB(String nodeid) {
92
      long element_id=0;
93
      long nodeparentid=0;
94
      String nodetype=null;
95
      String nodename=null;
96
      String nodedata=null;
97

  
98
      PreparedStatement pstmt;
99
      try {
100
        pstmt = 
101
          conn.prepareStatement("SELECT nodeid,nodeparentid,nodetype,
102
                  nodename,nodedata FROM xml_nodes WHERE nodeid = ?");
103
        // Bind the values to the query
104
        pstmt.setString(1, nodeid);
105

  
106
        pstmt.execute();
107
        try {
108
          ResultSet rs = pstmt.getResultSet();
109
          try {
110
            boolean tableHasRows = rs.next();
111
            if (tableHasRows) {
112
              try {
113
                element_id = rs.getLong(1);
114
                nodeparentid = rs.getLong(2);
115
                nodetype = rs.getString(3);
116
                nodename = rs.getString(4);
117
                nodedata = rs.getString(5);
118
              } catch (SQLException e) {
119
                System.out.println("Error with getInt: " + e.getMessage());
120
              }
121
            }
122
          } catch (SQLException e) {
123
            System.out.println("Error with next: " + e.getMessage());
124
          }
125
        } catch (SQLException e) {
126
          System.out.println("Error with getrset: " + e.getMessage());
127
        }
128
        pstmt.close();
129
      } catch (SQLException e) {
130
        System.out.println("Error getting id: " + e.getMessage());
131
      }
132

  
133
      if (nodetype.equals("ELEMENT")) {
134
        BasicElement element = 
135
                 new BasicElement(element_id,nodename,nodeparentid);
136
        element.appendContent(nodedata);
137
      }
138

  
139
      // Get a list of child nodes and recursively retrieve them as well
140
      try {
141
        pstmt = 
142
          conn.prepareStatement("SELECT nodeid,nodeparentid,nodetype, " +
143
                                "nodename,nodedata FROM xml_nodes " +
144
                                "WHERE nodeparentid = ?");
145
        // Bind the values to the query
146
        pstmt.setString(1, nodeid);
147

  
148
        pstmt.execute();
149
        try {
150
          ResultSet rs = pstmt.getResultSet();
151
          try {
152
            boolean tableHasRows = rs.next();
153
            if (tableHasRows) {
154
              try {
155
                element_id = rs.getLong(1);
156
                nodeparentid = rs.getLong(2);
157
                nodetype = rs.getString(3);
158
                nodename = rs.getString(4);
159
                nodedata = rs.getString(5);
160
              } catch (SQLException e) {
161
                System.out.println("Error with getInt: " + e.getMessage());
162
              }
163
            }
164
          } catch (SQLException e) {
165
            System.out.println("Error with next: " + e.getMessage());
166
          }
167
        } catch (SQLException e) {
168
          System.out.println("Error with getrset: " + e.getMessage());
169
        }
170
        pstmt.close();
171
      } catch (SQLException e) {
172
        System.out.println("Error getting id: " + e.getMessage());
173
      }
174

  
175

  
176
      return element;
177
  }
178
}
0 179

  
DBSAXElement.java
15 15
import java.util.Hashtable;
16 16
import java.util.Enumeration;
17 17

  
18
public class DBSAXElement {
18
public class DBSAXElement extends BasicElement {
19 19

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

  
27 22
    public DBSAXElement (Connection conn, String tagname, 
28 23
                         long parent_id) {
24

  
25
      super(tagname, parent_id);
26

  
29 27
      this.conn = conn;
30

  
31
      this.tagname = tagname;
32
      this.parent_id = parent_id;
33
      content = new StringBuffer();
34
      attributes = new Hashtable(); 
35 28
      writeElementToDB();
36 29
    };
37 30
    
......
100 93
        return assigned_id;
101 94
    }
102 95

  
103
    // 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 id of this element */
115
    public long  getElementID() { return element_id; }
116

  
117
    /** Get the name of this element */
118
    public String  getTagName() { return tagname; }
119

  
120
    /** Get the attributes as a string */
121
    public String getAttributes() {
122
      StringBuffer buf = new StringBuffer();
123
      String attName = null;
124
      String attValue = null;
125

  
126
      Enumeration attList = attributes.keys();
127
      while (attList.hasMoreElements()) {
128
        attName = (String)attList.nextElement();
129
        attValue = (String)attributes.get(attName);
130
        buf.append(" ").append(attName).append("=").append(attValue);        
131
      }
132
      return buf.toString();      
133
    }
134

  
135 96
    /** Add a new attribute to this element, or set its value */
136 97
    public void setAttribute(String attName, String attValue) {
137 98
      if (attName != null) {
......
162 123
      }
163 124
    }
164 125

  
165
    /** Get an attribute value by name */
166
    public String getAttribute(String attName) {
167
      return (String)attributes.get(attName);
168
    }
169

  
170
    /** Append to the content of the element */
171
    public void appendContent(char[] cbuf, int start, int len) {
172
      this.content.append( cbuf, start, len );
173
    }
174

  
175
    /** Append to the content of the element */
176
    public void appendContent(String new_content) {
177
      this.content.append( new_content );
178
    }
179

  
180
    /** Get the content of the element */
181
    public String getContent() {
182
      return this.content.toString();
183
    }
184

  
185 126
    /** Write the element content to the db connection */
186 127
    public void writeContentToDB() {
187 128
        try {
......
200 141
          System.out.println(e.getMessage());
201 142
        }
202 143
    }
203

  
204 144
}
BasicElement.java
1
/**
2
 *        Name: BasicElement.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.io.IOException;
14
import java.util.Hashtable;
15
import java.util.Enumeration;
16

  
17
public class BasicElement {
18

  
19
    protected long		element_id;
20
    protected String		tagname;
21
    protected StringBuffer	content;
22
    protected long		parent_id;
23
    protected Hashtable		attributes;
24

  
25
    public BasicElement (String tagname, long parent_id) {
26
      this.tagname = tagname;
27
      this.parent_id = parent_id;
28
      content = new StringBuffer();
29
      attributes = new Hashtable(); 
30
    }
31
    
32
    public BasicElement (long element_id, String tagname, long parent_id) {
33
      this(tagname,parent_id);
34
      this.element_id = element_id;
35
    }
36

  
37
    // used by JTree to display this node
38
    public String toString ()
39
    {
40
	StringBuffer	value = new StringBuffer ();
41
	value.append ('<');
42
	value.append (getTagName ());
43
	value.append (getAttributes ().toString ());
44
	value.append ('>');
45
	return value.toString ();
46
    }
47

  
48
    /** Get the id of this element */
49
    public long  getElementID() { return element_id; }
50

  
51
    /** Get the name of this element */
52
    public String  getTagName() { return tagname; }
53

  
54
    /** Get the attributes as a string */
55
    public String getAttributes() {
56
      StringBuffer buf = new StringBuffer();
57
      String attName = null;
58
      String attValue = null;
59

  
60
      Enumeration attList = attributes.keys();
61
      while (attList.hasMoreElements()) {
62
        attName = (String)attList.nextElement();
63
        attValue = (String)attributes.get(attName);
64
        buf.append(" ").append(attName).append("=").append(attValue);        
65
      }
66
      return buf.toString();      
67
    }
68

  
69
    /** Add a new attribute to this element, or set its value */
70
    public void setAttribute(String attName, String attValue) {
71
      if (attName != null) {
72
        // Enter the attribute in the hash table
73
        attributes.put(attName, attValue);
74
 
75
      } else {
76
        System.err.println("Attribute name must not be null!");
77
      }
78
    }
79

  
80
    /** Get an attribute value by name */
81
    public String getAttribute(String attName) {
82
      return (String)attributes.get(attName);
83
    }
84

  
85
    /** Append to the content of the element */
86
    public void appendContent(char[] cbuf, int start, int len) {
87
      this.content.append( cbuf, start, len );
88
    }
89

  
90
    /** Append to the content of the element */
91
    public void appendContent(String new_content) {
92
      this.content.append( new_content );
93
    }
94

  
95
    /** Get the content of the element */
96
    public String getContent() {
97
      return this.content.toString();
98
    }
99

  
100
}
0 101

  
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
 * Institution: National Center for Ecological Analysis and Synthesis
6
 *   Copyright: 2000
7
 *     Authors: Matt Jones
8
 *
9
 *     Version: '$Id$'
10
 */
11

  
12
//package project;
13

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

  
19
public class ReaderElement extends BasicElement {
20

  
21
    private Connection		conn;
22

  
23
    public ReaderElement (Connection conn, long nodeid) {
24

  
25
      this.conn = conn;
26
      //Lookup data for self
27
      String tagname = "foo";
28
      long parent_id = 999;
29
      super(tagname, parent_id);
30
      
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
      //}
40

  
41
    }
42
}
0 43

  
Makefile
2 2
SEP=:
3 3
#CPATH=../../xml.jar$(SEP)$(JAVA_HOME)/lib/classes.zip
4 4
CPATH=/home/httpd/servlets/xsql/lib/xmlparserv2.jar$(SEP)$(ORACLE_HOME)/jdbc/lib/classes111.zip$(SEP).
5
USER=jones
6
PW=kinkaj0u
5 7

  
6
default: orasax
8
default: reader
7 9

  
10
all: orasax reader
11

  
8 12
orasax:
9 13
	javac -classpath "$(CPATH)" \
10 14
		DBSAXWriter.java \
15
		BasicElement.java \
11 16
		DBSAXElement.java \
12 17
		DBSAXHandler.java
13 18
		
......
16 21
		DBWriter.java \
17 22
		DBElement.java
18 23
		
24
reader:
25
	javac -classpath "$(CPATH)" \
26
		DBReader.java \
27
		BasicElement.java
28

  
19 29
test:
20
	java -cp $(CPATH) DBSAXWriter test.xml jones kinkaj0u
30
	java -cp $(CPATH) DBSAXWriter test.xml $(USER) $(PW)
21 31

  
22 32
test1:
23
	java -cp $(CPATH) DBSAXWriter test.xml jones kinkaj0u \
24
		jdbc:oracle:thin:@24.237.22.245:1521:test
25

  
26
test2:
27
	java -cp $(CPATH) DBSAXWriter test.xml jones kinkaj0u \
33
	java -cp $(CPATH) DBSAXWriter test.xml $(USER) $(PW) \
28 34
		jdbc:oracle:thin:@localhost:1521:test
29 35

  
36
rdtest:
37
	java -cp $(CPATH) DBReader 1 $(USER) $(PW) 
38

  
30 39
clean:
31 40
	-rm -f *.class Log

Also available in: Unified diff