Project

General

Profile

« Previous | Next » 

Revision 203

Merged in substantial changes to DBWriter and associated classes and to
the MetaCatServlet in order to accomodate the new UPDATE and DELETE
functions. The command line tools and the parameters for the
servlet have changed substantially.

View differences:

DBSAXHandler.java
1 1
/**
2
 *      Name: DBSAXHandler.java
3
 *   Purpose: A Class that handles the SAX XML events as they
4
 *            are generated from XML documents
5
 * Copyright: 2000 Regents of the University of California and the
6
 *            National Center for Ecological Analysis and Synthesis
7
 *   Authors: Matt Jones
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that handles the SAX XML events as they
4
 *             are generated from XML documents
5
 *  Copyright: 2000 Regents of the University of California and the
6
 *             National Center for Ecological Analysis and Synthesis
7
 *    Authors: Matt Jones, Jivka Bojilova
8 8
 *
9
 *   Version: '$Id$'
9
 *   '$Author$'
10
 *     '$Date$'
11
 * '$Revision$'
10 12
 */
11 13

  
12 14
package edu.ucsb.nceas.metacat;
......
32 34
   static  String 	docname = null;
33 35
   private String 	doctype;
34 36
   private String 	systemid;
35
   private boolean 	debug 	= false;
36 37
   private boolean 	stackCreated = false;
37 38
   private Stack 	nodeStack;
38 39
   private Connection	conn = null;
39 40
   private DBSAXDocument currentDocument;
40 41
   private DBSAXNode    rootNode;
42
   private String       action = null;
43
   private String       docid = null;
41 44

  
42 45
   /** Construct an instance of the handler class 
43 46
    *
......
54 57
     }
55 58
   }
56 59
 
60
   /** Construct an instance of the handler class 
61
    *
62
    * @param conn the JDBC connection to which information is written
63
    */
64
   public DBSAXHandler(Connection conn, String action, String docid) {
65
     this(conn);
66
     this.action = action;
67
     this.docid = docid;
68
   }
69

  
57 70
   /** SAX Handler that receives notification of beginning of the document */
58 71
   public void startDocument() throws SAXException {
59
     if (debug) {
60
       System.out.println("start Document");
61
     }
72
     MetaCatUtil.debugMessage("start Document");
73

  
62 74
     // Create the document node representation as root
63 75
     rootNode = new DBSAXNode(conn, docname);
64 76
     // Add the node to the stack, so that any text data can be 
......
69 81
   /** SAX Handler that receives notification of end of the document */
70 82
   public void endDocument() throws SAXException {
71 83
     currentDocument.setTitleFromChildElement();
72
     if (debug) {
73
       System.out.println("end Document");
84
     MetaCatUtil.debugMessage("end Document");
85
     if ((docid != null) && (!docid.equals(currentDocument.getDocID()))) {
86
       throw (new SAXException("New document ID generated:",
87
              new AccessionNumberException(currentDocument.getDocID())));
88
     } else {
89
       throw (new SAXException("New document ID generated:",
90
              new AccessionNumberException(currentDocument.getDocID())));
74 91
     }
75 92
   }
76 93

  
......
78 95
   public void startElement(String uri, String localName,
79 96
                            String qName, Attributes atts) 
80 97
               throws SAXException {
81
     if (debug) {
82
       System.out.println("Start ELEMENT " + localName);
83
     }
98
     MetaCatUtil.debugMessage("Start ELEMENT " + localName);
84 99

  
85
      DBSAXNode parentNode = null;
86
      DBSAXNode currentNode = null;
100
     DBSAXNode parentNode = null;
101
     DBSAXNode currentNode = null;
87 102

  
88
      elementNo++;
89
      // Get a reference to the parent node for the id
90
      try {
91
        parentNode = (DBSAXNode)nodeStack.peek();
92
      } catch (EmptyStackException e) {
93
      }
103
     elementNo++;
104
     // Get a reference to the parent node for the id
105
     try {
106
       parentNode = (DBSAXNode)nodeStack.peek();
107
     } catch (EmptyStackException e) {
108
     }
94 109

  
95
      // Document representation that points to the root document node
96
      if (elementNo == 1) {
97
        // If no DOCTYPE declaration: docname = root element name 
98
        if (docname == null) {
99
            docname = localName;
100
        } else if (doctype == null) {
101
            doctype = DBEntityResolver.doctype;
102
        }
103
        rootNode.writeNodename(docname);
104
        rootNode.writeRootNodeID(rootNode.getNodeID());
105
        currentDocument = new DBSAXDocument(conn, rootNode.getNodeID(), 
106
                                            docname, doctype);
107
        rootNode.writeDocID(currentDocument.getDocID());
108
      }      
110
     // Document representation that points to the root document node
111
     if (elementNo == 1) {
112
       // If no DOCTYPE declaration: docname = root element name 
113
       if (docname == null) {
114
         docname = localName;
115
         MetaCatUtil.debugMessage("DOCNAME: " + docname);
116
       } else if (doctype == null) {
117
         doctype = DBEntityResolver.doctype;
118
         MetaCatUtil.debugMessage("DOCTYPE: " + doctype);
119
       }
120
       rootNode.writeNodename(docname);
121
       rootNode.writeRootNodeID(rootNode.getNodeID());
122
       try {
123
         currentDocument = new DBSAXDocument(conn, rootNode.getNodeID(), 
124
                                           docname, doctype, docid, action);
125
         } catch (AccessionNumberException ane) {
126
           throw (new SAXException("Error with" + action, ane));
127
         }
128
       rootNode.writeDocID(currentDocument.getDocID());
129
     }      
109 130

  
110
      // Create the current node representation
111
      currentNode = new DBSAXNode(conn, localName, parentNode, 
112
                                  rootNode, currentDocument);
131
     // Create the current node representation
132
     currentNode = new DBSAXNode(conn, localName, parentNode, 
133
                                 rootNode, currentDocument);
113 134

  
114
      // Add all of the attributes
115
      for (int i=0; i<atts.getLength(); i++) {
116
         currentNode.setAttribute(atts.getLocalName(i), atts.getValue(i));
117
      }      
135
     // Add all of the attributes
136
     for (int i=0; i<atts.getLength(); i++) {
137
       currentNode.setAttribute(atts.getLocalName(i), atts.getValue(i));
138
     }      
118 139

  
119
      // Add the node to the stack, so that any text data can be 
120
      // added as it is encountered
121
      nodeStack.push(currentNode);
122
   }
140
     // Add the node to the stack, so that any text data can be 
141
     // added as it is encountered
142
     nodeStack.push(currentNode);
143
  }
123 144

  
124 145
   /** SAX Handler that is called for each XML text node */
125 146
   public void characters(char[] cbuf, int start, int len) {
126
     if (debug) {
127
       System.out.println("CHARACTERS");
128
     }
147
     MetaCatUtil.debugMessage("CHARACTERS");
129 148
     DBSAXNode currentNode = (DBSAXNode)nodeStack.peek();
130 149
     String data = new String(cbuf, start, len);
131 150

  
......
138 157
    * white space
139 158
    */
140 159
   public void ignorableWhitespace(char[] cbuf, int start, int len) {
141
     if (debug) {
142
       System.out.println("IGNORABLEWHITESPACE");
143
     }
160
     MetaCatUtil.debugMessage("IGNORABLEWHITESPACE");
144 161
   }
145 162

  
146 163
   /** 
......
149 166
    */
150 167
/*
151 168
   public void comment(String data) throws SAXException {
152
     if (debug) {
153
       System.out.println("COMMENT");
154
     }
155
      DBSAXNode currentNode = (DBSAXNode)nodeStack.peek();
156
      currentNode.writeChildNodeToDB("COMMENT", null, data);
169
     MetaCatUtil.debugMessage("COMMENT");
170
     DBSAXNode currentNode = (DBSAXNode)nodeStack.peek();
171
     currentNode.writeChildNodeToDB("COMMENT", null, data);
157 172
   }
158 173
*/
159 174
   /** 
......
162 177
    */
163 178
   public void processingInstruction(String target, String data) 
164 179
          throws SAXException {
165
     if (debug) {
166
       System.out.println("PI");
167
     }
180
     MetaCatUtil.debugMessage("PI");
168 181
     DBSAXNode currentNode = (DBSAXNode)nodeStack.peek();
169 182
     currentNode.writeChildNodeToDB("PI", target, data);
170 183
   }
......
172 185
   /** SAX Handler that is called at the end of each XML element */
173 186
   public void endElement(String uri, String localName,
174 187
                          String qName) throws SAXException {
175
     if (debug) {
176
       System.out.println("End ELEMENT " + localName);
177
     }
188
     MetaCatUtil.debugMessage("End ELEMENT " + localName);
178 189

  
179 190
     // Get the node from the stack
180 191
     DBSAXNode currentNode = (DBSAXNode)nodeStack.pop();
......
191 202
     doctype = publicId;
192 203
     systemid = systemId;
193 204

  
194
     if (debug) {
195
       System.out.println("Start DOCTYPE");
196
       System.out.println("DOCNAME: " + docname);
197
       System.out.println("DOCTYPE: " + doctype);
198
       System.out.println("  SYSID: " + systemid);
199
     }
205
     MetaCatUtil.debugMessage("Start DOCTYPE");
206
     MetaCatUtil.debugMessage("DOCNAME: " + docname);
207
     MetaCatUtil.debugMessage("DOCTYPE: " + doctype);
208
     MetaCatUtil.debugMessage("  SYSID: " + systemid);
200 209
   }
201 210

  
202 211
   /** 
203 212
    * SAX Handler that receives notification of end of DTD 
204 213
    */
205 214
   public void endDTD() throws SAXException {
206
     if (debug) {
207
       System.out.println("end DOCTYPE");
208
     }
215
     MetaCatUtil.debugMessage("end DOCTYPE");
209 216
   }
210 217

  
211 218
   /** 
212 219
    * SAX Handler that receives notification of comments in the DTD
213 220
    */
214 221
   public void comment(char[] ch, int start, int length) throws SAXException {
215
     if (debug) {
216
       System.out.println("COMMENT");
217
     }
222
     MetaCatUtil.debugMessage("COMMENT");
218 223
     DBSAXNode currentNode = (DBSAXNode)nodeStack.peek();
219 224
     currentNode.writeChildNodeToDB("COMMENT", null, new String(ch));
220 225
   }
......
223 228
    * SAX Handler that receives notification of the start of CDATA sections
224 229
    */
225 230
   public void startCDATA() throws SAXException {
226
     if (debug) {
227
       System.out.println("start CDATA");
228
     }
231
     MetaCatUtil.debugMessage("start CDATA");
229 232
   }
230 233

  
231 234
   /** 
232 235
    * SAX Handler that receives notification of the end of CDATA sections
233 236
    */
234 237
   public void endCDATA() throws SAXException {
235
     if (debug) {
236
       System.out.println("end CDATA");
237
     }
238
     MetaCatUtil.debugMessage("end CDATA");
238 239
   }
239 240

  
240 241
   /** 
241 242
    * SAX Handler that receives notification of the start of entities
242 243
    */
243 244
   public void startEntity(String name) throws SAXException {
244
     if (debug) {
245
       System.out.println("start ENTITY");
246
     }
245
     MetaCatUtil.debugMessage("start ENTITY");
247 246
   }
248 247

  
249 248
   /** 
250 249
    * SAX Handler that receives notification of the end of entities
251 250
    */
252 251
   public void endEntity(String name) throws SAXException {
253
     if (debug) {
254
       System.out.println("end ENTITY");
255
     }
252
     MetaCatUtil.debugMessage("end ENTITY");
256 253
   }
257 254

  
258 255
   /** 
......
260 257
    */
261 258
   public void elementDecl(String name, String model)
262 259
                        throws org.xml.sax.SAXException {
263
     if (debug) {
264
       System.out.println("ELEMENTDECL");
265
     }
260
     MetaCatUtil.debugMessage("ELEMENTDECL");
266 261
   }
267 262

  
268 263
   /** 
......
271 266
   public void attributeDecl(String eName, String aName,
272 267
                        String type, String valueDefault, String value)
273 268
                        throws org.xml.sax.SAXException {
274
     if (debug) {
275
       System.out.println("ATTRIBUTEDECL");
276
     }
269
     MetaCatUtil.debugMessage("ATTRIBUTEDECL");
277 270
   }
278 271

  
279 272
   /** 
......
281 274
    */
282 275
   public void internalEntityDecl(String name, String value)
283 276
                        throws org.xml.sax.SAXException {
284
     if (debug) {
285
       System.out.println("INTERNENTITYDECL");
286
     }
277
     MetaCatUtil.debugMessage("INTERNENTITYDECL");
287 278
   }
288 279

  
289 280
   /** 
......
292 283
   public void externalEntityDecl(String name, String publicId,
293 284
                        String systemId)
294 285
                        throws org.xml.sax.SAXException {
295
     if (debug) {
296
       System.out.println("EXTERNENTITYDECL");
297
     }
286
     MetaCatUtil.debugMessage("EXTERNENTITYDECL");
298 287
   }
299 288

  
300 289

  
301 290
}
291

  
292
/**
293
 * '$Log$
294
 * 'Revision 1.27.2.6  2000/06/26 02:02:20  jones
295
 * 'Continued fixing problems with exception handling that deals
296
 * 'with INSERT and UPDATE actions and the docid passed to DBWriter
297
 * '
298
 * 'Revision 1.27.2.5  2000/06/26 00:51:06  jones
299
 * 'If docid passed to DBWriter.write() is not unique, classes now generate
300
 * 'an AccessionNumberException containing the new docid generated as a
301
 * 'replacement.  The docid is then extracted from the exception and
302
 * 'returned to the calling application for user feedback or client processing.
303
 * '
304
 * 'Revision 1.27.2.4  2000/06/25 23:38:16  jones
305
 * 'Added RCSfile keyword
306
 * '
307
 * 'Revision 1.27.2.3  2000/06/25 23:34:17  jones
308
 * 'Changed documentation formatting, added log entries at bottom of source files
309
 * ''
310
 */

Also available in: Unified diff