Project

General

Profile

« Previous | Next » 

Revision 135

Added by Matt Jones over 24 years ago

rearranged API for incrementing the node index within the parent node

View differences:

src/edu/ucsb/nceas/metacat/DBSAXHandler.java
31 31
 */
32 32
public class DBSAXHandler extends DefaultXMLDocumentHandler {
33 33

  
34
   static int elementNo = 0;
35
   static String docname = null;
36
   private String doctype;
37
   private String systemid;
34
   static  int 		elementNo = 0;
35
   static  String 	docname = null;
36
   private String 	doctype;
37
   private String 	systemid;
38 38
   private boolean 	debug 	= false;
39 39
   private boolean 	stackCreated = false;
40 40
   private Stack 	elementStack;
......
62 62
      System.out.println("start Document");
63 63
    }
64 64
    // Create the document node represantation as root
65
    DBSAXElement documentNode = new DBSAXElement(conn, docname, 0, 0);
65
    //DBSAXElement documentNode = new DBSAXElement(conn, docname, 0, 0);
66
    DBSAXElement documentNode = new DBSAXElement(conn, docname);
66 67
    // Add the element to the stack, so that any text data can be 
67 68
    // added as it is encountered
68 69
    elementStack.push(documentNode);
......
114 115
      String localName;
115 116
      String nsName;
116 117
      String expName;
117
      DBSAXElement parentElement;
118
      DBSAXElement parentElement = null;
118 119
      DBSAXElement currentElement;
119 120

  
120 121
      qName = name.getQualifiedName();
......
124 125
      
125 126
      elementNo++;
126 127
      // Get a reference to the parent element for the id
127
      long parent_id;
128
      int nodeIndex;
129 128
      try {
130 129
        parentElement = (DBSAXElement)elementStack.peek();
131
        parent_id = parentElement.getNodeID();
132
        nodeIndex = parentElement.incChildNum();
133 130
      } catch (EmptyStackException e) {
134
        parent_id = 0;
135
        nodeIndex = 0;
136 131
      }
137 132

  
138 133
      // Document representation that points to the root document node
139 134
      if (elementNo == 1) {
140 135
        // If no DOCTYPE declaration: docname = root element name 
141
        if (docname == null)
136
        if (docname == null) {
142 137
            docname = localName;
143
        else if (doctype == null)
138
        } else if (doctype == null) {
144 139
            doctype = DBEntityResolver.doctype;
140
        }
145 141
        DBSAXElement documentNode = (DBSAXElement)elementStack.peek();
146 142
        documentNode.writeNodename(docname);
147 143
        new DBSAXDocument(conn, documentNode.getNodeID(), docname, doctype);
148 144
      }      
145

  
149 146
      // Create the current element representation
150
      currentElement = new DBSAXElement(conn, localName, parent_id, nodeIndex);
147
      currentElement = new DBSAXElement(conn, localName, parentElement);
151 148

  
152 149
      // Add all of the attributes
153 150
      for (int i=0; i<atts.getLength(); i++)
......
180 177
   /** SAX Handler that is called for each XML text node */
181 178
   public void characters(char[] cbuf, int start, int len) {
182 179
      DBSAXElement currentElement = (DBSAXElement)elementStack.peek();
183
      int nodeIndex = currentElement.incChildNum();
184 180
      String data = new String(cbuf, start, len);
185 181

  
186 182
      // Write the content of the element to the database
187
      currentElement.writeChildNodeToDB("TEXT", null, data, nodeIndex);
183
      currentElement.writeChildNodeToDB("TEXT", null, data);
188 184
   }
189 185

  
190 186
   /** 
......
201 197
    */
202 198
   public void comment(String data) throws SAXException {
203 199
      DBSAXElement currentElement = (DBSAXElement)elementStack.peek();
204
      int nodeIndex = currentElement.incChildNum();
205
      currentElement.writeChildNodeToDB("COMMENT", null, data, nodeIndex);
200
      currentElement.writeChildNodeToDB("COMMENT", null, data);
206 201
   }
207 202

  
208 203
   /** 
......
212 207
   public void processingInstruction(String target, String data) 
213 208
          throws SAXException {
214 209
      DBSAXElement currentElement = (DBSAXElement)elementStack.peek();
215
      int nodeIndex = currentElement.incChildNum();
216
      currentElement.writeChildNodeToDB("PI", target, data, nodeIndex);
210
      currentElement.writeChildNodeToDB("PI", target, data);
217 211
   }
218 212

  
219 213
   /** SAX Handler that is called at the end of each XML element */
src/edu/ucsb/nceas/metacat/DBSAXElement.java
24 24
  private Connection		conn;
25 25

  
26 26
  /** 
27
   * Construct a new element instance
27
   * Construct a new node instance for DOCUMENT nodes
28 28
   *
29 29
   * @param conn the JDBC Connection to which all information is written
30
   * @param tagname the name of the element
31
   * @param parent_id the parent id number for this element
30
   * @param tagname the name of the node
32 31
   */
32
  public DBSAXElement (Connection conn, String tagname) {
33
    super(tagname);
34
    this.conn = conn;
35
    writeChildNodeToDB("DOCUMENT", tagname, null);
36
    setNodeID(getAssignedNodeID());
37
  }
38

  
39
  /** 
40
   * Construct a new node instance for ELEMENT nodes
41
   *
42
   * @param conn the JDBC Connection to which all information is written
43
   * @param tagname the name of the node
44
   * @param parentNode the parent node for this node being created
45
   */
33 46
  public DBSAXElement (Connection conn, String tagname, 
34
                       long parent_id, int nodeIndex) {
47
                       DBSAXElement parentNode) {
35 48

  
36
    super(tagname, parent_id, nodeIndex);
37

  
49
    super(tagname);
50
    setParentID(parentNode.getNodeID());
51
    setNodeIndex(parentNode.incChildNum());
38 52
    this.conn = conn;
39
    if (getParentID() != 0) {
40
      writeChildNodeToDB("ELEMENT", getTagName(), null, getNodeIndex());
41
    } else {
42
      writeChildNodeToDB("DOCUMENT", getTagName(), null, 0);
43
    }
44

  
53
    writeChildNodeToDB("ELEMENT", getTagName(), null);
45 54
    setNodeID(getAssignedNodeID());
46 55
  }
47 56
    
48 57
  /** creates SQL code and inserts new node into DB connection */
49 58
  public void writeChildNodeToDB(String nodetype, String nodename,
50
                                  String data, int nodeIndex) {
59
                                 String data) {
51 60
    try {
52 61
      PreparedStatement pstmt;
53 62
      if (nodetype == "DOCUMENT") {
......
69 78
      if (nodetype != "DOCUMENT") {
70 79
        if (nodetype == "ELEMENT") {
71 80
          pstmt.setLong(3, getParentID());
81
          pstmt.setString(4, data);
82
          pstmt.setInt(5, getNodeIndex());
72 83
        } else {
73 84
          pstmt.setLong(3, getNodeID());
85
          pstmt.setString(4, data);
86
          pstmt.setInt(5, incChildNum());
74 87
        }
75
        pstmt.setString(4, data);
76
        pstmt.setInt(5, nodeIndex);
77 88
      }
78 89
      // Do the insertion
79 90
      pstmt.execute();
......
81 92
    } catch (SQLException e) {
82 93
      System.err.println("Error inserting node: (" + nodetype + ", " +
83 94
                                                     nodename + ", " + 
84
                                                     data + ", " + 
85
                                                     nodeIndex + ")" );
86

  
95
                                                     data + ")" );
87 96
      System.err.println(e.getMessage());
88 97
    }
89 98
  }
......
137 146
      // Enter the attribute in the hash table
138 147
      super.setAttribute(attName, attValue);
139 148

  
140
      writeChildNodeToDB("ATTRIBUTE", attName, attValue, 0);
141 149
      // And enter the attribute in the database
150
      writeChildNodeToDB("ATTRIBUTE", attName, attValue);
142 151
    } else {
143 152
      System.err.println("Attribute name must not be null!");
144 153
    }
src/edu/ucsb/nceas/metacat/BasicNode.java
27 27
    private String      nodeType;
28 28
    private Vector	children;
29 29

  
30
    /** Construct a Basic Element */
30
    /** Construct a Basic Node */
31 31
    public BasicNode () {
32 32
      children = new Vector();
33 33
      attributes = new Hashtable(); 
34 34
      this.childNum = 0;
35 35
    }
36 36

  
37
    /** Construct a Basic Element 
37
    /** 
38
     * Construct a Basic Node 
38 39
     *
39 40
     * @param tagname the name of the node
41
     */
42
    public BasicNode (String tagname) {
43
      this();
44
      this.tagname = tagname;
45
    }
46

  
47
    /** 
48
     * Construct a Basic Node 
49
     *
50
     * @param tagname the name of the node
40 51
     * @param parent_id the id number of the parent node
41
     * @param nodeIndex - order of node among siblings in parent node
52
     * @param nodeIndex - index of node among siblings in parent node
42 53
     *                    Every node initializes childNum to 0 when 
43 54
     *                    created and has interface incChildNum
44 55
     *                    when new child is created
......
50 61
      this.nodeIndex = nodeIndex;
51 62
    }
52 63
    
53
    /** Construct a Basic Element 
64
    /** Construct a Basic Node 
54 65
     *
55 66
     * @param node_id the id number of the node
56 67
     * @param tagname the name of the node

Also available in: Unified diff