Project

General

Profile

« Previous | Next » 

Revision 18

Added by Matt Jones over 24 years ago

Partially functional SAS DB loader.

View differences:

DBSAXHandler.java
13 13

  
14 14
import java.sql.*;
15 15
import java.util.Stack;
16
import java.util.EmptyStackException;
16 17

  
17 18
// Extensions to the SAX Interfaces for Namespace support.
18 19
import oracle.xml.parser.v2.DefaultXMLDocumentHandler;
......
31 32

  
32 33
   public DBSAXHandler(Connection conn)
33 34
   {
34
      System.out.println("\nINITIALIZING HANDLER....\n");
35

  
36 35
      this.conn = conn;
37 36

  
38 37
      // Create the stack for keeping track of element context
......
55 54
      String localName;
56 55
      String nsName;
57 56
      String expName;
57
      DBSAXElement parentElement;
58 58
      DBSAXElement currentElement;
59 59

  
60 60
      qName = name.getQualifiedName();
......
62 62
      nsName = name.getNamespace();
63 63
      expName = name.getExpandedName();
64 64
      
65
      // Get a reference to the parent element for the id
66
      long parent_id;
67
      try {
68
        parentElement = (DBSAXElement)elementStack.peek();
69
        parent_id = parentElement.getElementID();
70
      } catch (EmptyStackException e) {
71
        parent_id = 0;
72
      }
73

  
65 74
      // Create the current element representation
66
      currentElement = new DBSAXElement(conn, 1, localName, false, 0);
75
      currentElement = new DBSAXElement(conn, localName, parent_id);
67 76
      System.out.println("Element created:" + currentElement.getTagName());
68 77

  
69 78
      // Add all of the attributes
......
91 100
         System.out.println("                 Type:" + type);
92 101
         System.out.println("                Value:" + value);
93 102
         System.out.println();
94

  
95 103
      }      
96 104

  
97 105
      // Add the element to the stack, so that any text data can be 
......
118 126
      // Use the methods getQualifiedName(), getLocalName(), getNamespace()
119 127
      // and getExpandedName() in NSName interface to get Namespace
120 128
      // information.
121

  
122 129
      String expName = name.getExpandedName();
123 130

  
124
      // Remove the element from the stack
131
      // Get the element from the stack
125 132
      DBSAXElement currentElement = (DBSAXElement)elementStack.pop();
133
      
134
      // Write the content of the element to the database
135
      currentElement.writeContentToDB();
126 136

  
127 137
      System.out.println("Final content: " + currentElement.getContent());
128 138
      System.out.println("Closing element: " + expName);
src/edu/ucsb/nceas/metacat/DBSAXHandler.java
13 13

  
14 14
import java.sql.*;
15 15
import java.util.Stack;
16
import java.util.EmptyStackException;
16 17

  
17 18
// Extensions to the SAX Interfaces for Namespace support.
18 19
import oracle.xml.parser.v2.DefaultXMLDocumentHandler;
......
31 32

  
32 33
   public DBSAXHandler(Connection conn)
33 34
   {
34
      System.out.println("\nINITIALIZING HANDLER....\n");
35

  
36 35
      this.conn = conn;
37 36

  
38 37
      // Create the stack for keeping track of element context
......
55 54
      String localName;
56 55
      String nsName;
57 56
      String expName;
57
      DBSAXElement parentElement;
58 58
      DBSAXElement currentElement;
59 59

  
60 60
      qName = name.getQualifiedName();
......
62 62
      nsName = name.getNamespace();
63 63
      expName = name.getExpandedName();
64 64
      
65
      // Get a reference to the parent element for the id
66
      long parent_id;
67
      try {
68
        parentElement = (DBSAXElement)elementStack.peek();
69
        parent_id = parentElement.getElementID();
70
      } catch (EmptyStackException e) {
71
        parent_id = 0;
72
      }
73

  
65 74
      // Create the current element representation
66
      currentElement = new DBSAXElement(conn, 1, localName, false, 0);
75
      currentElement = new DBSAXElement(conn, localName, parent_id);
67 76
      System.out.println("Element created:" + currentElement.getTagName());
68 77

  
69 78
      // Add all of the attributes
......
91 100
         System.out.println("                 Type:" + type);
92 101
         System.out.println("                Value:" + value);
93 102
         System.out.println();
94

  
95 103
      }      
96 104

  
97 105
      // Add the element to the stack, so that any text data can be 
......
118 126
      // Use the methods getQualifiedName(), getLocalName(), getNamespace()
119 127
      // and getExpandedName() in NSName interface to get Namespace
120 128
      // information.
121

  
122 129
      String expName = name.getExpandedName();
123 130

  
124
      // Remove the element from the stack
131
      // Get the element from the stack
125 132
      DBSAXElement currentElement = (DBSAXElement)elementStack.pop();
133
      
134
      // Write the content of the element to the database
135
      currentElement.writeContentToDB();
126 136

  
127 137
      System.out.println("Final content: " + currentElement.getContent());
128 138
      System.out.println("Closing element: " + expName);
src/edu/ucsb/nceas/metacat/DBSAXNode.java
20 20
    private long		element_id;
21 21
    private String		tagname;
22 22
    private StringBuffer	content;
23
    private boolean		isEmpty;
24 23
    private long		parent_id;
25 24
    private Hashtable		attributes;
26 25
    private Connection		conn;
27 26

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

  
33 32
      this.tagname = tagname;
34
      this.isEmpty = isEmpty;
35 33
      this.parent_id = parent_id;
36 34
      content = new StringBuffer();
37 35
      attributes = new Hashtable(); 
38 36
      writeElementToDB();
39

  
40
      System.out.println("******ELEMENT_ID: " + this.element_id);
41 37
    };
42 38
    
43 39
    private long assignElementID() {
......
111 107
	return value.toString ();
112 108
    }
113 109

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

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

  
......
157 156
    public String getContent() {
158 157
      return this.content.toString();
159 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

  
160 179
}
src/edu/ucsb/nceas/metacat/DBSAXElement.java
20 20
    private long		element_id;
21 21
    private String		tagname;
22 22
    private StringBuffer	content;
23
    private boolean		isEmpty;
24 23
    private long		parent_id;
25 24
    private Hashtable		attributes;
26 25
    private Connection		conn;
27 26

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

  
33 32
      this.tagname = tagname;
34
      this.isEmpty = isEmpty;
35 33
      this.parent_id = parent_id;
36 34
      content = new StringBuffer();
37 35
      attributes = new Hashtable(); 
38 36
      writeElementToDB();
39

  
40
      System.out.println("******ELEMENT_ID: " + this.element_id);
41 37
    };
42 38
    
43 39
    private long assignElementID() {
......
111 107
	return value.toString ();
112 108
    }
113 109

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

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

  
......
157 156
    public String getContent() {
158 157
      return this.content.toString();
159 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

  
160 179
}
DBSAXElement.java
20 20
    private long		element_id;
21 21
    private String		tagname;
22 22
    private StringBuffer	content;
23
    private boolean		isEmpty;
24 23
    private long		parent_id;
25 24
    private Hashtable		attributes;
26 25
    private Connection		conn;
27 26

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

  
33 32
      this.tagname = tagname;
34
      this.isEmpty = isEmpty;
35 33
      this.parent_id = parent_id;
36 34
      content = new StringBuffer();
37 35
      attributes = new Hashtable(); 
38 36
      writeElementToDB();
39

  
40
      System.out.println("******ELEMENT_ID: " + this.element_id);
41 37
    };
42 38
    
43 39
    private long assignElementID() {
......
111 107
	return value.toString ();
112 108
    }
113 109

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

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

  
......
157 156
    public String getContent() {
158 157
      return this.content.toString();
159 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

  
160 179
}

Also available in: Unified diff