Revision 821
Added by bojilova over 23 years ago
src/edu/ucsb/nceas/metacat/DBSAXHandler.java | ||
---|---|---|
32 | 32 |
import java.io.StringReader; |
33 | 33 |
import java.util.Stack; |
34 | 34 |
import java.util.Vector; |
35 |
import java.util.Hashtable; |
|
35 | 36 |
import java.util.Enumeration; |
36 | 37 |
import java.util.EmptyStackException; |
37 | 38 |
|
... | ... | |
68 | 69 |
private Thread xmlIndex; |
69 | 70 |
private boolean endDocument = false; |
70 | 71 |
private int serverCode = 1; |
72 |
private Hashtable namespaces = new Hashtable(); |
|
71 | 73 |
|
72 | 74 |
private static final int MAXDATACHARS = 4000; |
73 | 75 |
// DOCTITLE attr cleared from the db |
... | ... | |
142 | 144 |
} |
143 | 145 |
} |
144 | 146 |
|
147 |
/** SAX Handler that is called at the start of Namespace */ |
|
148 |
public void startPrefixMapping(String prefix, String uri) |
|
149 |
throws SAXException |
|
150 |
{ |
|
151 |
MetaCatUtil.debugMessage("NAMESPACE"); |
|
152 |
|
|
153 |
namespaces.put(prefix, uri); |
|
154 |
} |
|
155 |
|
|
145 | 156 |
/** SAX Handler that is called at the start of each XML element */ |
146 | 157 |
public void startElement(String uri, String localName, |
147 | 158 |
String qName, Attributes atts) |
148 | 159 |
throws SAXException { |
149 |
MetaCatUtil.debugMessage("Start ELEMENT " + localName);
|
|
150 |
|
|
160 |
MetaCatUtil.debugMessage("Start ELEMENT " + qName);
|
|
161 |
|
|
151 | 162 |
DBSAXNode parentNode = null; |
152 | 163 |
DBSAXNode currentNode = null; |
153 | 164 |
|
... | ... | |
197 | 208 |
} |
198 | 209 |
|
199 | 210 |
// Create the current node representation |
200 |
currentNode = new DBSAXNode(conn, localName, parentNode,
|
|
211 |
currentNode = new DBSAXNode(conn, qName, parentNode,
|
|
201 | 212 |
currentDocument.getRootNodeID(),docid, |
202 | 213 |
currentDocument.getDoctype()); |
203 | 214 |
|
215 |
// Add all of the namespaces |
|
216 |
String prefix; |
|
217 |
String nsuri; |
|
218 |
Enumeration prefixes = namespaces.keys(); |
|
219 |
while ( prefixes.hasMoreElements() ) { |
|
220 |
prefix = (String)prefixes.nextElement(); |
|
221 |
nsuri = (String)namespaces.get(prefix); |
|
222 |
currentNode.setNamespace(prefix, nsuri, docid); |
|
223 |
} |
|
224 |
namespaces = null; |
|
225 |
namespaces = new Hashtable(); |
|
226 |
|
|
204 | 227 |
// Add all of the attributes |
205 | 228 |
for (int i=0; i<atts.getLength(); i++) { |
206 |
currentNode.setAttribute(atts.getLocalName(i), atts.getValue(i), docid);
|
|
229 |
currentNode.setAttribute(atts.getQName(i), atts.getValue(i), docid);
|
|
207 | 230 |
} |
208 | 231 |
|
209 | 232 |
// Add the node to the stack, so that any text data can be |
... | ... | |
309 | 332 |
// Write the content of the node to the database |
310 | 333 |
currentNode.writeChildNodeToDB("TEXT", null, data, docid); |
311 | 334 |
} |
312 |
|
|
313 |
// DOCTITLE attr cleared from the db |
|
314 |
// // write the title of document if there are tag <title> |
|
315 |
// if ( currentNode.getTagName().equals("title") ) { |
|
316 |
// if ( leftover > MAXTITLELEN ) |
|
317 |
// currentDocument.setTitle(new String(cbuf, start, MAXTITLELEN)); |
|
318 |
// else |
|
319 |
// currentDocument.setTitle(new String(cbuf, start, leftover)); |
|
320 |
// } |
|
321 | 335 |
} |
322 | 336 |
|
323 | 337 |
/** |
... | ... | |
372 | 386 |
/** SAX Handler that is called at the end of each XML element */ |
373 | 387 |
public void endElement(String uri, String localName, |
374 | 388 |
String qName) throws SAXException { |
375 |
MetaCatUtil.debugMessage("End ELEMENT " + localName);
|
|
389 |
MetaCatUtil.debugMessage("End ELEMENT " + qName);
|
|
376 | 390 |
|
377 | 391 |
// Get the node from the stack |
378 | 392 |
DBSAXNode currentNode = (DBSAXNode)nodeStack.pop(); |
... | ... | |
389 | 403 |
doctype = publicId; |
390 | 404 |
systemid = systemId; |
391 | 405 |
|
392 |
//System.out.println("Start DTD"); |
|
393 |
//System.out.println("DOCNAME: " + docname); |
|
394 |
//System.out.println("DOCTYPE: " + doctype); |
|
395 |
//System.out.println(" SYSID: " + systemid); |
|
396 |
|
|
397 | 406 |
MetaCatUtil.debugMessage("Start DTD"); |
398 | 407 |
MetaCatUtil.debugMessage("DOCNAME: " + docname); |
399 | 408 |
MetaCatUtil.debugMessage("DOCTYPE: " + doctype); |
... | ... | |
405 | 414 |
*/ |
406 | 415 |
public void endDTD() throws SAXException { |
407 | 416 |
|
408 |
//System.out.println("end DTD"); |
|
409 | 417 |
MetaCatUtil.debugMessage("end DTD"); |
410 | 418 |
} |
411 | 419 |
|
... | ... | |
471 | 479 |
public void attributeDecl(String eName, String aName, |
472 | 480 |
String type, String valueDefault, String value) |
473 | 481 |
throws org.xml.sax.SAXException { |
482 |
|
|
474 | 483 |
//System.out.println("ATTRIBUTEDECL: " + eName + " " |
475 | 484 |
// + aName + " " + type + " " + valueDefault + " " |
476 | 485 |
// + value); |
src/edu/ucsb/nceas/metacat/DocumentImpl.java | ||
---|---|---|
408 | 408 |
//", " + currentNode.nodetype + |
409 | 409 |
//", " + currentNode.nodename + |
410 | 410 |
//", " + currentNode.nodedata + ")]"); |
411 |
|
|
412 | 411 |
// Print the end tag for the previous node if needed |
413 | 412 |
// |
414 | 413 |
// This is determined by inspecting the parent nodeid for the |
... | ... | |
469 | 468 |
} else if (currentNode.nodetype.equals("ATTRIBUTE")) { |
470 | 469 |
out.print(" " + currentNode.nodename + "=\"" |
471 | 470 |
+ currentNode.nodedata + "\""); |
471 |
|
|
472 |
// Handle the NAMESPACE nodes |
|
473 |
} else if (currentNode.nodetype.equals("NAMESPACE")) { |
|
474 |
out.print(" xmlns:" + currentNode.nodename + "=\"" |
|
475 |
+ currentNode.nodedata + "\""); |
|
476 |
|
|
477 |
// Handle the TEXT nodes |
|
472 | 478 |
} else if (currentNode.nodetype.equals("TEXT")) { |
473 | 479 |
if (previousNodeWasElement) { |
474 | 480 |
out.print(">"); |
src/edu/ucsb/nceas/metacat/DBSAXNode.java | ||
---|---|---|
104 | 104 |
} |
105 | 105 |
|
106 | 106 |
// Bind the values to the query |
107 |
// NOT USED; USED DBAdapter.getUniqueID() instead |
|
108 |
//long nid = generateNodeID(); |
|
109 |
// NOT USED |
|
110 |
//let the db generate it: using identity key or through |
|
111 |
//the db trigger xml_nodes_before_insert using sequence |
|
112 |
//long nid = dbAdapter.getUniqueID(conn, "xml_nodes"); |
|
113 |
//pstmt.setLong(1, nid); |
|
114 | 107 |
pstmt.setString(1, nodetype); |
115 | 108 |
pstmt.setString(2, nodename); |
116 | 109 |
pstmt.setString(3, docid); |
... | ... | |
126 | 119 |
} else { |
127 | 120 |
pstmt.setLong(4, getRootNodeID()); |
128 | 121 |
pstmt.setLong(5, getNodeID()); |
129 |
// DON'T NEED to OVERRIDE THE meta_file_id TAGS with docid without rev |
|
130 |
// SINCE THE CLIENT INSERTS docids BEFORE SENDING THE doc TO METACAT |
|
131 |
//if ( nodetype == "TEXT" && getTagName().equals("meta_file_id") ) { |
|
132 |
// pstmt.setString(6, getDocID()); |
|
133 |
//} else { |
|
134 |
pstmt.setString(6, data); |
|
135 |
//} |
|
122 |
pstmt.setString(6, data); |
|
136 | 123 |
pstmt.setInt(7, incChildNum()); |
137 | 124 |
} |
138 | 125 |
} |
... | ... | |
252 | 239 |
} |
253 | 240 |
} |
254 | 241 |
|
242 |
/** Add a namespace to this node */ |
|
243 |
public void setNamespace(String prefix, String uri, String docid) |
|
244 |
throws SAXException { |
|
245 |
if (prefix != null) { |
|
246 |
// Enter the namespace in a hash table |
|
247 |
super.setNamespace(prefix, uri); |
|
248 |
// And enter the namespace in the database |
|
249 |
writeChildNodeToDB("NAMESPACE", prefix, uri, docid); |
|
250 |
} else { |
|
251 |
System.err.println("Namespace prefix must not be null!"); |
|
252 |
throw new SAXException("Namespace prefix must not be null!"); |
|
253 |
} |
|
254 |
} |
|
255 |
|
|
255 | 256 |
/** |
256 | 257 |
* NOT USED |
257 | 258 |
* Update the node index (xml_index) for this node by generating |
src/edu/ucsb/nceas/metacat/BasicNode.java | ||
---|---|---|
41 | 41 |
private long rootnode_id; |
42 | 42 |
private String doc_id; |
43 | 43 |
private Hashtable attributes; |
44 |
private Hashtable namespace; |
|
44 | 45 |
private int childNum; |
45 | 46 |
private int nodeIndex; |
46 | 47 |
private String nodeType; |
... | ... | |
50 | 51 |
public BasicNode () { |
51 | 52 |
children = new Vector(); |
52 | 53 |
attributes = new Hashtable(); |
54 |
namespace = new Hashtable(); |
|
53 | 55 |
this.childNum = 0; |
54 | 56 |
} |
55 | 57 |
|
... | ... | |
197 | 199 |
return (String)attributes.get(attName); |
198 | 200 |
} |
199 | 201 |
|
202 |
/** Add a namespace to this node */ |
|
203 |
public void setNamespace(String prefix, String uri) { |
|
204 |
if (prefix != null) { |
|
205 |
// Enter the namespace in the hash table |
|
206 |
namespace.put(prefix, uri); |
|
207 |
} else { |
|
208 |
System.err.println("Namespace prefix must not be null!"); |
|
209 |
} |
|
210 |
} |
|
211 |
|
|
212 |
/** Get an uri of the namespace prefix */ |
|
213 |
public String getNamespace(String prefix) { |
|
214 |
return (String)namespace.get(prefix); |
|
215 |
} |
|
216 |
|
|
200 | 217 |
/** Get nodeIndex of the node */ |
201 | 218 |
public int getNodeIndex() { |
202 | 219 |
return this.nodeIndex; |
Also available in: Unified diff
added support for Namespaces.
Metacat now can store and retrieve XML documents with Namespaces.
Namespace is stored as separate record in xml_nodes table with type "NAMESPACE"
where nodename is the prefix and nodedata is the uri of the namespace.