1 |
17
|
jones
|
/**
|
2 |
203
|
jones
|
* '$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 |
349
|
jones
|
* Release: @release@
|
9 |
17
|
jones
|
*
|
10 |
203
|
jones
|
* '$Author$'
|
11 |
|
|
* '$Date$'
|
12 |
|
|
* '$Revision$'
|
13 |
669
|
jones
|
*
|
14 |
|
|
* This program is free software; you can redistribute it and/or modify
|
15 |
|
|
* it under the terms of the GNU General Public License as published by
|
16 |
|
|
* the Free Software Foundation; either version 2 of the License, or
|
17 |
|
|
* (at your option) any later version.
|
18 |
|
|
*
|
19 |
|
|
* This program is distributed in the hope that it will be useful,
|
20 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
21 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
22 |
|
|
* GNU General Public License for more details.
|
23 |
|
|
*
|
24 |
|
|
* You should have received a copy of the GNU General Public License
|
25 |
|
|
* along with this program; if not, write to the Free Software
|
26 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
27 |
17
|
jones
|
*/
|
28 |
|
|
|
29 |
75
|
jones
|
package edu.ucsb.nceas.metacat;
|
30 |
51
|
jones
|
|
31 |
17
|
jones
|
import java.sql.*;
|
32 |
638
|
bojilova
|
import java.io.StringReader;
|
33 |
17
|
jones
|
import java.util.Stack;
|
34 |
471
|
bojilova
|
import java.util.Vector;
|
35 |
821
|
bojilova
|
import java.util.Hashtable;
|
36 |
471
|
bojilova
|
import java.util.Enumeration;
|
37 |
18
|
jones
|
import java.util.EmptyStackException;
|
38 |
17
|
jones
|
|
39 |
185
|
jones
|
import org.xml.sax.Attributes;
|
40 |
|
|
import org.xml.sax.SAXException;
|
41 |
204
|
jones
|
import org.xml.sax.SAXParseException;
|
42 |
186
|
jones
|
import org.xml.sax.ext.DeclHandler;
|
43 |
185
|
jones
|
import org.xml.sax.ext.LexicalHandler;
|
44 |
|
|
import org.xml.sax.helpers.DefaultHandler;
|
45 |
17
|
jones
|
|
46 |
1359
|
tao
|
/**
|
47 |
31
|
jones
|
* A database aware Class implementing callback bethods for the SAX parser to
|
48 |
|
|
* call when processing the XML stream and generating events
|
49 |
|
|
*/
|
50 |
1359
|
tao
|
public class DBSAXHandler extends DefaultHandler
|
51 |
471
|
bojilova
|
implements LexicalHandler, DeclHandler, Runnable {
|
52 |
17
|
jones
|
|
53 |
1396
|
tao
|
protected boolean atFirstElement;
|
54 |
243
|
jones
|
private boolean processingDTD;
|
55 |
1396
|
tao
|
protected String docname = null;
|
56 |
|
|
protected String doctype;
|
57 |
|
|
protected String systemid;
|
58 |
17
|
jones
|
private boolean stackCreated = false;
|
59 |
1396
|
tao
|
protected Stack nodeStack;
|
60 |
|
|
protected Vector nodeIndex;
|
61 |
|
|
protected DBConnection connection = null;
|
62 |
|
|
protected DocumentImpl currentDocument;
|
63 |
|
|
protected DBSAXNode rootNode;
|
64 |
|
|
protected String action = null;
|
65 |
|
|
protected String docid = null;
|
66 |
|
|
protected String revision = null;
|
67 |
|
|
protected String user = null;
|
68 |
|
|
protected String[] groups = null;
|
69 |
|
|
protected String pub = null;
|
70 |
|
|
protected Thread xmlIndex;
|
71 |
471
|
bojilova
|
private boolean endDocument = false;
|
72 |
1396
|
tao
|
protected int serverCode = 1;
|
73 |
|
|
protected Hashtable namespaces = new Hashtable();
|
74 |
1411
|
tao
|
protected boolean hitTextNode = false; // a flag to hit text node
|
75 |
|
|
// a buffer to keep all text nodes for same element
|
76 |
|
|
// it is for element was splited
|
77 |
|
|
protected StringBuffer textBuffer = new StringBuffer();
|
78 |
|
|
protected Stack textBufferStack = new Stack();
|
79 |
17
|
jones
|
|
80 |
1404
|
tao
|
protected static final int MAXDATACHARS = 4000;
|
81 |
1396
|
tao
|
protected static final long INDEXDELAY = 10000;
|
82 |
1415
|
tao
|
// methods writeChildNodeToDB, setAttribute, setNamespace,
|
83 |
|
|
// writeTextForDBSAXNode will increase endNodeId.
|
84 |
|
|
protected long endNodeId = -1; // The end node id for a substree
|
85 |
691
|
bojilova
|
// DOCTITLE attr cleared from the db
|
86 |
|
|
// private static final int MAXTITLELEN = 1000;
|
87 |
220
|
jones
|
|
88 |
1359
|
tao
|
/** Construct an instance of the handler class
|
89 |
31
|
jones
|
*
|
90 |
|
|
* @param conn the JDBC connection to which information is written
|
91 |
|
|
*/
|
92 |
1217
|
tao
|
public DBSAXHandler(DBConnection conn) {
|
93 |
|
|
this.connection = conn;
|
94 |
204
|
jones
|
this.atFirstElement = true;
|
95 |
243
|
jones
|
this.processingDTD = false;
|
96 |
17
|
jones
|
|
97 |
185
|
jones
|
// Create the stack for keeping track of node context
|
98 |
|
|
// if it doesn't already exist
|
99 |
|
|
if (!stackCreated) {
|
100 |
|
|
nodeStack = new Stack();
|
101 |
471
|
bojilova
|
nodeIndex = new Vector();
|
102 |
185
|
jones
|
stackCreated = true;
|
103 |
|
|
}
|
104 |
17
|
jones
|
}
|
105 |
1359
|
tao
|
|
106 |
|
|
/** Construct an instance of the handler class
|
107 |
203
|
jones
|
*
|
108 |
|
|
* @param conn the JDBC connection to which information is written
|
109 |
425
|
bojilova
|
* @param action - "INSERT" or "UPDATE"
|
110 |
|
|
* @param docid to be inserted or updated into JDBC connection
|
111 |
680
|
bojilova
|
* @param user the user connected to MetaCat servlet and owns the document
|
112 |
802
|
bojilova
|
* @param groups the groups to which user belongs
|
113 |
680
|
bojilova
|
* @param pub flag for public "read" access on document
|
114 |
|
|
* @param serverCode the serverid from xml_replication on which this document
|
115 |
|
|
* resides.
|
116 |
|
|
*
|
117 |
203
|
jones
|
*/
|
118 |
1359
|
tao
|
public DBSAXHandler(DBConnection conn, String action, String docid,
|
119 |
955
|
tao
|
String user, String[] groups, String pub, int serverCode)
|
120 |
425
|
bojilova
|
{
|
121 |
203
|
jones
|
this(conn);
|
122 |
|
|
this.action = action;
|
123 |
|
|
this.docid = docid;
|
124 |
425
|
bojilova
|
this.user = user;
|
125 |
802
|
bojilova
|
this.groups = groups;
|
126 |
680
|
bojilova
|
this.pub = pub;
|
127 |
|
|
this.serverCode = serverCode;
|
128 |
471
|
bojilova
|
this.xmlIndex = new Thread(this);
|
129 |
1359
|
tao
|
}
|
130 |
|
|
|
131 |
955
|
tao
|
/** Construct an instance of the handler class
|
132 |
|
|
* In this constructor, user can specify the version need to upadate
|
133 |
|
|
*
|
134 |
|
|
* @param conn the JDBC connection to which information is written
|
135 |
|
|
* @param action - "INSERT" or "UPDATE"
|
136 |
|
|
* @param docid to be inserted or updated into JDBC connection
|
137 |
|
|
* @param revision, the user specified the revision need to be update
|
138 |
|
|
* @param user the user connected to MetaCat servlet and owns the document
|
139 |
|
|
* @param groups the groups to which user belongs
|
140 |
|
|
* @param pub flag for public "read" access on document
|
141 |
|
|
* @param serverCode the serverid from xml_replication on which this document
|
142 |
|
|
* resides.
|
143 |
|
|
*
|
144 |
|
|
*/
|
145 |
1359
|
tao
|
public DBSAXHandler(DBConnection conn, String action, String docid,
|
146 |
955
|
tao
|
String revision, String user, String[] groups, String pub, int serverCode)
|
147 |
|
|
{
|
148 |
|
|
this(conn);
|
149 |
|
|
this.action = action;
|
150 |
|
|
this.docid = docid;
|
151 |
|
|
this.revision = revision;
|
152 |
|
|
this.user = user;
|
153 |
|
|
this.groups = groups;
|
154 |
|
|
this.pub = pub;
|
155 |
|
|
this.serverCode = serverCode;
|
156 |
|
|
this.xmlIndex = new Thread(this);
|
157 |
203
|
jones
|
}
|
158 |
1359
|
tao
|
|
159 |
72
|
bojilova
|
/** SAX Handler that receives notification of beginning of the document */
|
160 |
122
|
jones
|
public void startDocument() throws SAXException {
|
161 |
1062
|
tao
|
MetaCatUtil.debugMessage("start Document", 50);
|
162 |
203
|
jones
|
|
163 |
185
|
jones
|
// Create the document node representation as root
|
164 |
1217
|
tao
|
rootNode = new DBSAXNode(connection, this.docid);
|
165 |
1359
|
tao
|
// Add the node to the stack, so that any text data can be
|
166 |
185
|
jones
|
// added as it is encountered
|
167 |
|
|
nodeStack.push(rootNode);
|
168 |
72
|
bojilova
|
}
|
169 |
|
|
|
170 |
|
|
/** SAX Handler that receives notification of end of the document */
|
171 |
122
|
jones
|
public void endDocument() throws SAXException {
|
172 |
1062
|
tao
|
MetaCatUtil.debugMessage("end Document", 50);
|
173 |
471
|
bojilova
|
// Starting new thread for writing XML Index.
|
174 |
|
|
// It calls the run method of the thread.
|
175 |
|
|
try {
|
176 |
|
|
xmlIndex.start();
|
177 |
|
|
} catch (NullPointerException e) {
|
178 |
|
|
xmlIndex = null;
|
179 |
1359
|
tao
|
throw new
|
180 |
471
|
bojilova
|
SAXException("Problem with starting thread for writing XML Index. " +
|
181 |
|
|
e.getMessage());
|
182 |
|
|
}
|
183 |
72
|
bojilova
|
}
|
184 |
|
|
|
185 |
821
|
bojilova
|
/** SAX Handler that is called at the start of Namespace */
|
186 |
1359
|
tao
|
public void startPrefixMapping(String prefix, String uri)
|
187 |
821
|
bojilova
|
throws SAXException
|
188 |
|
|
{
|
189 |
1062
|
tao
|
MetaCatUtil.debugMessage("NAMESPACE", 50);
|
190 |
821
|
bojilova
|
|
191 |
|
|
namespaces.put(prefix, uri);
|
192 |
|
|
}
|
193 |
1359
|
tao
|
|
194 |
185
|
jones
|
/** SAX Handler that is called at the start of each XML element */
|
195 |
|
|
public void startElement(String uri, String localName,
|
196 |
1359
|
tao
|
String qName, Attributes atts)
|
197 |
185
|
jones
|
throws SAXException {
|
198 |
1389
|
tao
|
// for element <eml:eml...> qname is "eml:eml", local name is "eml"
|
199 |
|
|
// for element <acl....> both qname and local name is "eml"
|
200 |
|
|
// uri is namesapce
|
201 |
|
|
MetaCatUtil.debugMessage("Start ELEMENT(qName) " + qName, 50);
|
202 |
|
|
MetaCatUtil.debugMessage("Start ELEMENT(localName) " + localName, 50);
|
203 |
|
|
MetaCatUtil.debugMessage("Start ELEMENT(uri) " + uri, 50);
|
204 |
|
|
|
205 |
203
|
jones
|
DBSAXNode parentNode = null;
|
206 |
|
|
DBSAXNode currentNode = null;
|
207 |
17
|
jones
|
|
208 |
203
|
jones
|
// Get a reference to the parent node for the id
|
209 |
|
|
try {
|
210 |
|
|
parentNode = (DBSAXNode)nodeStack.peek();
|
211 |
|
|
} catch (EmptyStackException e) {
|
212 |
408
|
jones
|
parentNode = null;
|
213 |
203
|
jones
|
}
|
214 |
1411
|
tao
|
|
215 |
|
|
// If hit a text node, we need write this text for current's parent node
|
216 |
|
|
// This will happend if the element is mixted
|
217 |
|
|
if (hitTextNode && parentNode != null)
|
218 |
|
|
{
|
219 |
|
|
// write the textbuffer into db for parent node.
|
220 |
1415
|
tao
|
endNodeId = writeTextForDBSAXNode(textBuffer, parentNode);
|
221 |
1411
|
tao
|
// rest hitTextNode
|
222 |
|
|
hitTextNode =false;
|
223 |
|
|
// reset textbuffer
|
224 |
|
|
textBuffer = null;
|
225 |
|
|
textBuffer = new StringBuffer();
|
226 |
|
|
|
227 |
|
|
}
|
228 |
18
|
jones
|
|
229 |
203
|
jones
|
// Document representation that points to the root document node
|
230 |
1389
|
tao
|
if (atFirstElement)
|
231 |
|
|
{
|
232 |
204
|
jones
|
atFirstElement = false;
|
233 |
1389
|
tao
|
// If no DOCTYPE declaration: docname = root element
|
234 |
|
|
// doctype = root element name or name space
|
235 |
|
|
if (docname == null)
|
236 |
|
|
{
|
237 |
203
|
jones
|
docname = localName;
|
238 |
1389
|
tao
|
// if uri isn't null doctype = uri(namespace)
|
239 |
|
|
// othewise root element
|
240 |
|
|
if (uri != null && !(uri.trim()).equals(""))
|
241 |
|
|
{
|
242 |
|
|
doctype = uri;
|
243 |
|
|
}
|
244 |
|
|
else
|
245 |
|
|
{
|
246 |
|
|
doctype = docname;
|
247 |
|
|
}
|
248 |
|
|
MetaCatUtil.debugMessage("DOCNAME-a: " + docname, 30);
|
249 |
|
|
MetaCatUtil.debugMessage("DOCTYPE-a: " + doctype, 30);
|
250 |
|
|
}
|
251 |
|
|
else if (doctype == null)
|
252 |
|
|
{
|
253 |
|
|
// because docname is not null and it is declared in dtd
|
254 |
|
|
// so could not be in schema, no namespace
|
255 |
204
|
jones
|
doctype = docname;
|
256 |
1389
|
tao
|
MetaCatUtil.debugMessage("DOCTYPE-b: " + doctype, 30);
|
257 |
203
|
jones
|
}
|
258 |
|
|
rootNode.writeNodename(docname);
|
259 |
|
|
try {
|
260 |
697
|
bojilova
|
// for validated XML Documents store a reference to XML DB Catalog
|
261 |
1217
|
tao
|
// Because this is select statement and it needn't to roll back if
|
262 |
|
|
// insert document action fialed.
|
263 |
1359
|
tao
|
// In order to decrease DBConnection usage count, we get a new
|
264 |
1217
|
tao
|
// dbconnection from pool
|
265 |
697
|
bojilova
|
String catalogid = null;
|
266 |
1217
|
tao
|
DBConnection dbConn = null;
|
267 |
|
|
int serialNumber = -1;
|
268 |
1359
|
tao
|
|
269 |
697
|
bojilova
|
if ( systemid != null ) {
|
270 |
1217
|
tao
|
try
|
271 |
|
|
{
|
272 |
|
|
// Get dbconnection
|
273 |
|
|
dbConn=DBConnectionPool.getDBConnection
|
274 |
|
|
("DBSAXHandler.startElement");
|
275 |
|
|
serialNumber=dbConn.getCheckOutSerialNumber();
|
276 |
1359
|
tao
|
|
277 |
1217
|
tao
|
Statement stmt = dbConn.createStatement();
|
278 |
|
|
ResultSet rs = stmt.executeQuery(
|
279 |
697
|
bojilova
|
"SELECT catalog_id FROM xml_catalog " +
|
280 |
1359
|
tao
|
"WHERE entry_type = 'DTD' " +
|
281 |
697
|
bojilova
|
"AND public_id = '" + doctype + "'");
|
282 |
1217
|
tao
|
boolean hasRow = rs.next();
|
283 |
|
|
if ( hasRow ) {
|
284 |
|
|
catalogid = rs.getString(1);
|
285 |
|
|
}
|
286 |
|
|
stmt.close();
|
287 |
|
|
}//try
|
288 |
|
|
finally
|
289 |
|
|
{
|
290 |
|
|
// Return dbconnection
|
291 |
|
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
292 |
|
|
}//finally
|
293 |
697
|
bojilova
|
}
|
294 |
1359
|
tao
|
|
295 |
955
|
tao
|
//create documentImpl object by the constructor which can specify
|
296 |
|
|
//the revision
|
297 |
1359
|
tao
|
currentDocument = new DocumentImpl(connection, rootNode.getNodeID(),
|
298 |
|
|
docname, doctype, docid, revision, action, user,
|
299 |
697
|
bojilova
|
this.pub, catalogid, this.serverCode);
|
300 |
1359
|
tao
|
|
301 |
|
|
|
302 |
457
|
bojilova
|
} catch (Exception ane) {
|
303 |
1359
|
tao
|
throw (new SAXException("Error in DBSaxHandler.startElement " +
|
304 |
675
|
berkley
|
action, ane));
|
305 |
408
|
jones
|
}
|
306 |
1359
|
tao
|
}
|
307 |
135
|
jones
|
|
308 |
203
|
jones
|
// Create the current node representation
|
309 |
1217
|
tao
|
currentNode = new DBSAXNode(connection, qName, localName, parentNode,
|
310 |
457
|
bojilova
|
currentDocument.getRootNodeID(),docid,
|
311 |
|
|
currentDocument.getDoctype());
|
312 |
1359
|
tao
|
|
313 |
821
|
bojilova
|
// Add all of the namespaces
|
314 |
|
|
String prefix;
|
315 |
|
|
String nsuri;
|
316 |
|
|
Enumeration prefixes = namespaces.keys();
|
317 |
|
|
while ( prefixes.hasMoreElements() ) {
|
318 |
|
|
prefix = (String)prefixes.nextElement();
|
319 |
|
|
nsuri = (String)namespaces.get(prefix);
|
320 |
|
|
currentNode.setNamespace(prefix, nsuri, docid);
|
321 |
|
|
}
|
322 |
|
|
namespaces = null;
|
323 |
|
|
namespaces = new Hashtable();
|
324 |
|
|
|
325 |
203
|
jones
|
// Add all of the attributes
|
326 |
1396
|
tao
|
for (int i=0; i<atts.getLength(); i++)
|
327 |
|
|
{
|
328 |
|
|
String attributeName = atts.getQName(i);
|
329 |
|
|
String attributeValue = atts.getValue(i);
|
330 |
1415
|
tao
|
endNodeId =
|
331 |
|
|
currentNode.setAttribute(attributeName, attributeValue, docid);
|
332 |
1396
|
tao
|
|
333 |
|
|
// To handle name space and schema location if the attribute name is
|
334 |
|
|
// xsi:schemaLocation. If the name space is in not in catalog table
|
335 |
|
|
// it will be regeistered.
|
336 |
|
|
if (attributeName != null &&
|
337 |
|
|
attributeName.indexOf(MetaCatServlet.SCHEMALOCATIONKEYWORD) != -1)
|
338 |
|
|
{
|
339 |
|
|
SchemaLocationResolver resolver =
|
340 |
|
|
new SchemaLocationResolver(attributeValue);
|
341 |
|
|
resolver.resolveNameSpace();
|
342 |
|
|
|
343 |
|
|
}
|
344 |
1359
|
tao
|
}
|
345 |
17
|
jones
|
|
346 |
1359
|
tao
|
// Add the node to the stack, so that any text data can be
|
347 |
203
|
jones
|
// added as it is encountered
|
348 |
|
|
nodeStack.push(currentNode);
|
349 |
471
|
bojilova
|
// Add the node to the vector used by thread for writing XML Index
|
350 |
|
|
nodeIndex.addElement(currentNode);
|
351 |
|
|
|
352 |
203
|
jones
|
}
|
353 |
1359
|
tao
|
|
354 |
819
|
bojilova
|
/* The run method of xmlIndex thread. It writes XML Index for the document. */
|
355 |
471
|
bojilova
|
public void run () {
|
356 |
|
|
DBSAXNode currNode = null;
|
357 |
|
|
DBSAXNode prevNode = null;
|
358 |
1217
|
tao
|
DBConnection dbConn = null;
|
359 |
|
|
int serialNumber = -1;
|
360 |
638
|
bojilova
|
String doctype = currentDocument.getDoctype();
|
361 |
471
|
bojilova
|
int step = 0;
|
362 |
|
|
int counter = 0;
|
363 |
17
|
jones
|
|
364 |
471
|
bojilova
|
try {
|
365 |
1359
|
tao
|
|
366 |
471
|
bojilova
|
// Opening separate db connection for writing XML Index
|
367 |
1217
|
tao
|
dbConn=DBConnectionPool.getDBConnection("DBSAXHandler.run");
|
368 |
|
|
serialNumber=dbConn.getCheckOutSerialNumber();
|
369 |
|
|
dbConn.setAutoCommit(false);
|
370 |
1359
|
tao
|
|
371 |
899
|
berkley
|
//the following while loop construct checks to make sure that the docid
|
372 |
|
|
//of the document that we are trying to index is already
|
373 |
|
|
//in the xml_documents table. if this is not the case, the foreign
|
374 |
|
|
//key relationship between xml_documents and xml_index is temporarily
|
375 |
|
|
//broken causing multiple problems.
|
376 |
|
|
boolean inxmldoc = false;
|
377 |
1396
|
tao
|
long startTime = System.currentTimeMillis();
|
378 |
899
|
berkley
|
while(!inxmldoc)
|
379 |
|
|
{
|
380 |
|
|
String xmlDocumentsCheck = "select distinct docid from xml_documents";
|
381 |
1359
|
tao
|
PreparedStatement xmlDocCheck =
|
382 |
1217
|
tao
|
dbConn.prepareStatement(xmlDocumentsCheck);
|
383 |
|
|
// Increase usage count
|
384 |
|
|
dbConn.increaseUsageCount(1);
|
385 |
899
|
berkley
|
xmlDocCheck.execute();
|
386 |
|
|
ResultSet doccheckRS = xmlDocCheck.getResultSet();
|
387 |
|
|
boolean tableHasRows = doccheckRS.next();
|
388 |
|
|
Vector docids = new Vector();
|
389 |
1359
|
tao
|
while(tableHasRows)
|
390 |
899
|
berkley
|
{
|
391 |
|
|
docids.add(doccheckRS.getString(1).trim());
|
392 |
|
|
tableHasRows = doccheckRS.next();
|
393 |
|
|
}
|
394 |
1359
|
tao
|
|
395 |
899
|
berkley
|
for(int i=0; i<docids.size(); i++)
|
396 |
|
|
{
|
397 |
|
|
String d = ((String)docids.elementAt(i)).trim();
|
398 |
|
|
if(docid.trim().equals(d))
|
399 |
|
|
{
|
400 |
|
|
inxmldoc = true;
|
401 |
|
|
}
|
402 |
|
|
}
|
403 |
1396
|
tao
|
doccheckRS.close();
|
404 |
899
|
berkley
|
xmlDocCheck.close();
|
405 |
1396
|
tao
|
// make sure the while loop will be ended in reseaonable time
|
406 |
|
|
long stopTime = System.currentTimeMillis();
|
407 |
|
|
if ((stopTime - startTime) > INDEXDELAY)
|
408 |
|
|
{
|
409 |
|
|
throw new Exception("Couldn't find the docid for index build in" +
|
410 |
|
|
"reseaonable time!");
|
411 |
|
|
}
|
412 |
899
|
berkley
|
}
|
413 |
1359
|
tao
|
|
414 |
471
|
bojilova
|
// Going through the elements of the document and writing its Index
|
415 |
|
|
Enumeration nodes = nodeIndex.elements();
|
416 |
|
|
while ( nodes.hasMoreElements() ) {
|
417 |
|
|
currNode = (DBSAXNode)nodes.nextElement();
|
418 |
1217
|
tao
|
currNode.updateNodeIndex(dbConn, docid, doctype);
|
419 |
471
|
bojilova
|
}
|
420 |
1359
|
tao
|
|
421 |
|
|
|
422 |
1217
|
tao
|
dbConn.commit();
|
423 |
1359
|
tao
|
|
424 |
819
|
bojilova
|
//if this is a package file
|
425 |
1217
|
tao
|
String packagedoctype = MetaCatUtil.getOption("packagedoctype");
|
426 |
887
|
berkley
|
Vector packagedoctypes = new Vector();
|
427 |
1359
|
tao
|
|
428 |
887
|
berkley
|
packagedoctypes = MetaCatUtil.getOptionList(packagedoctype);
|
429 |
1359
|
tao
|
|
430 |
887
|
berkley
|
if ( packagedoctypes.contains(doctype) )
|
431 |
638
|
bojilova
|
{
|
432 |
819
|
bojilova
|
// write the package info to xml_relation table
|
433 |
1217
|
tao
|
RelationHandler rth = new RelationHandler(docid, dbConn);
|
434 |
819
|
bojilova
|
// from the relations get the access file id for that package
|
435 |
|
|
String aclid = rth.getAccessFileID(docid);
|
436 |
|
|
// if there are access file, write ACL for that package
|
437 |
|
|
if ( aclid != null ) {
|
438 |
1217
|
tao
|
runAccessControlList(dbConn, aclid);
|
439 |
645
|
bojilova
|
}
|
440 |
1359
|
tao
|
|
441 |
638
|
bojilova
|
}
|
442 |
1359
|
tao
|
|
443 |
1396
|
tao
|
|
444 |
1359
|
tao
|
|
445 |
471
|
bojilova
|
} catch (Exception e) {
|
446 |
|
|
try {
|
447 |
1217
|
tao
|
dbConn.rollback();
|
448 |
|
|
//dbconn.close();
|
449 |
471
|
bojilova
|
} catch (SQLException sqle) {}
|
450 |
1396
|
tao
|
MetaCatUtil.debugMessage("Error in DBSAXHandler.run " +
|
451 |
|
|
e.getMessage(), 30);
|
452 |
|
|
|
453 |
1217
|
tao
|
}
|
454 |
|
|
finally
|
455 |
|
|
{
|
456 |
|
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
457 |
1359
|
tao
|
}//finally
|
458 |
471
|
bojilova
|
}
|
459 |
1359
|
tao
|
|
460 |
819
|
bojilova
|
// It runs in xmlIndex thread. It writes ACL for a package.
|
461 |
1217
|
tao
|
private void runAccessControlList (DBConnection conn, String aclid)
|
462 |
819
|
bojilova
|
throws Exception
|
463 |
|
|
{
|
464 |
|
|
// read the access file from xml_nodes
|
465 |
|
|
// parse the access file and store the access info into xml_access
|
466 |
1359
|
tao
|
AccessControlList aclobj =
|
467 |
819
|
bojilova
|
new AccessControlList(conn, aclid, //new StringReader(xml),
|
468 |
|
|
user, groups, serverCode);
|
469 |
|
|
conn.commit();
|
470 |
|
|
}
|
471 |
471
|
bojilova
|
|
472 |
819
|
bojilova
|
|
473 |
460
|
bojilova
|
/** SAX Handler that is called for each XML text node */
|
474 |
1411
|
tao
|
public void characters(char[] cbuf, int start, int len) throws SAXException
|
475 |
|
|
{
|
476 |
1062
|
tao
|
MetaCatUtil.debugMessage("CHARACTERS", 50);
|
477 |
1411
|
tao
|
// buffer all text nodes for same element. This is for text was splited
|
478 |
|
|
// into different nodes
|
479 |
|
|
textBuffer.append(new String(cbuf, start,len));
|
480 |
|
|
// set hittextnode true
|
481 |
|
|
hitTextNode = true;
|
482 |
|
|
}
|
483 |
1359
|
tao
|
|
484 |
|
|
/**
|
485 |
660
|
bojilova
|
* SAX Handler that is called for each XML text node that is
|
486 |
|
|
* Ignorable white space
|
487 |
31
|
jones
|
*/
|
488 |
660
|
bojilova
|
public void ignorableWhitespace(char[] cbuf, int start, int len)
|
489 |
|
|
throws SAXException {
|
490 |
|
|
// When validation is turned "on", white spaces are reported here
|
491 |
|
|
// When validation is turned "off" white spaces are not reported here,
|
492 |
|
|
// but through characters() callback
|
493 |
1062
|
tao
|
MetaCatUtil.debugMessage("IGNORABLEWHITESPACE", 50);
|
494 |
660
|
bojilova
|
|
495 |
1359
|
tao
|
|
496 |
660
|
bojilova
|
DBSAXNode currentNode = (DBSAXNode)nodeStack.peek();
|
497 |
|
|
String data = null;
|
498 |
|
|
int leftover = len;
|
499 |
|
|
int offset = start;
|
500 |
|
|
boolean moredata = true;
|
501 |
1359
|
tao
|
|
502 |
|
|
// This loop deals with the case where there are more characters
|
503 |
|
|
// than can fit in a single database text field (limit is
|
504 |
660
|
bojilova
|
// MAXDATACHARS). If the text to be inserted exceeds MAXDATACHARS,
|
505 |
|
|
// write a series of nodes that are MAXDATACHARS long, and then the
|
506 |
|
|
// final node contains the remainder
|
507 |
|
|
while (moredata) {
|
508 |
|
|
if (leftover > MAXDATACHARS) {
|
509 |
|
|
data = new String(cbuf, offset, MAXDATACHARS);
|
510 |
|
|
leftover -= MAXDATACHARS;
|
511 |
|
|
offset += MAXDATACHARS;
|
512 |
|
|
} else {
|
513 |
|
|
data = new String(cbuf, offset, leftover);
|
514 |
|
|
moredata = false;
|
515 |
|
|
}
|
516 |
|
|
|
517 |
|
|
// Write the content of the node to the database
|
518 |
1415
|
tao
|
endNodeId = currentNode.writeChildNodeToDB("TEXT", null, data, docid);
|
519 |
660
|
bojilova
|
}
|
520 |
17
|
jones
|
}
|
521 |
|
|
|
522 |
1359
|
tao
|
/**
|
523 |
|
|
* SAX Handler called once for each processing instruction found:
|
524 |
122
|
jones
|
* node that PI may occur before or after the root element.
|
525 |
|
|
*/
|
526 |
1359
|
tao
|
public void processingInstruction(String target, String data)
|
527 |
122
|
jones
|
throws SAXException {
|
528 |
1062
|
tao
|
MetaCatUtil.debugMessage("PI", 50);
|
529 |
186
|
jones
|
DBSAXNode currentNode = (DBSAXNode)nodeStack.peek();
|
530 |
1415
|
tao
|
endNodeId = currentNode.writeChildNodeToDB("PI", target, data, docid);
|
531 |
92
|
bojilova
|
}
|
532 |
72
|
bojilova
|
|
533 |
31
|
jones
|
/** SAX Handler that is called at the end of each XML element */
|
534 |
185
|
jones
|
public void endElement(String uri, String localName,
|
535 |
|
|
String qName) throws SAXException {
|
536 |
1062
|
tao
|
MetaCatUtil.debugMessage("End ELEMENT " + qName, 50);
|
537 |
1411
|
tao
|
|
538 |
|
|
// write buffered text nodes into db (so no splited)
|
539 |
|
|
DBSAXNode currentNode = (DBSAXNode)nodeStack.peek();
|
540 |
|
|
|
541 |
|
|
// If before the end element, the parser hit text nodes and store them
|
542 |
|
|
// into the buffer, write the buffer to data base. The reason we put
|
543 |
|
|
// write database here is for xerces some time split text node
|
544 |
|
|
if (hitTextNode)
|
545 |
|
|
{
|
546 |
|
|
MetaCatUtil.debugMessage("Write text into DB in End Element", 50);
|
547 |
1415
|
tao
|
endNodeId = writeTextForDBSAXNode(textBuffer, currentNode);
|
548 |
1411
|
tao
|
}//if
|
549 |
|
|
|
550 |
|
|
//set hitText false
|
551 |
|
|
hitTextNode = false;
|
552 |
|
|
// reset textbuff
|
553 |
|
|
textBuffer = null;
|
554 |
|
|
textBuffer = new StringBuffer();
|
555 |
17
|
jones
|
|
556 |
185
|
jones
|
// Get the node from the stack
|
557 |
1411
|
tao
|
currentNode = (DBSAXNode)nodeStack.pop();
|
558 |
17
|
jones
|
}
|
559 |
|
|
|
560 |
185
|
jones
|
//
|
561 |
|
|
// the next section implements the LexicalHandler interface
|
562 |
|
|
//
|
563 |
|
|
|
564 |
|
|
/** SAX Handler that receives notification of DOCTYPE. Sets the DTD */
|
565 |
1359
|
tao
|
public void startDTD(String name, String publicId, String systemId)
|
566 |
185
|
jones
|
throws SAXException {
|
567 |
|
|
docname = name;
|
568 |
|
|
doctype = publicId;
|
569 |
|
|
systemid = systemId;
|
570 |
|
|
|
571 |
1359
|
tao
|
processingDTD = true;
|
572 |
|
|
|
573 |
1062
|
tao
|
MetaCatUtil.debugMessage("Start DTD", 50);
|
574 |
1359
|
tao
|
MetaCatUtil.debugMessage("Setting processingDTD to true", 50);
|
575 |
1062
|
tao
|
MetaCatUtil.debugMessage("DOCNAME: " + docname, 50);
|
576 |
|
|
MetaCatUtil.debugMessage("DOCTYPE: " + doctype, 50);
|
577 |
|
|
MetaCatUtil.debugMessage(" SYSID: " + systemid, 50);
|
578 |
185
|
jones
|
}
|
579 |
|
|
|
580 |
1359
|
tao
|
/**
|
581 |
|
|
* SAX Handler that receives notification of end of DTD
|
582 |
185
|
jones
|
*/
|
583 |
|
|
public void endDTD() throws SAXException {
|
584 |
1359
|
tao
|
|
585 |
1364
|
tao
|
processingDTD = false;
|
586 |
1359
|
tao
|
MetaCatUtil.debugMessage("Setting processingDTD to false", 50);
|
587 |
1062
|
tao
|
MetaCatUtil.debugMessage("end DTD", 50);
|
588 |
185
|
jones
|
}
|
589 |
|
|
|
590 |
1359
|
tao
|
/**
|
591 |
185
|
jones
|
* SAX Handler that receives notification of comments in the DTD
|
592 |
|
|
*/
|
593 |
|
|
public void comment(char[] ch, int start, int length) throws SAXException {
|
594 |
1062
|
tao
|
MetaCatUtil.debugMessage("COMMENT", 50);
|
595 |
660
|
bojilova
|
if ( !processingDTD ) {
|
596 |
|
|
DBSAXNode currentNode = (DBSAXNode)nodeStack.peek();
|
597 |
1415
|
tao
|
endNodeId =
|
598 |
1440
|
tao
|
currentNode.writeChildNodeToDB("COMMENT", null,
|
599 |
|
|
new String(ch, start, length), docid);
|
600 |
660
|
bojilova
|
}
|
601 |
185
|
jones
|
}
|
602 |
|
|
|
603 |
1359
|
tao
|
/**
|
604 |
185
|
jones
|
* SAX Handler that receives notification of the start of CDATA sections
|
605 |
|
|
*/
|
606 |
|
|
public void startCDATA() throws SAXException {
|
607 |
1062
|
tao
|
MetaCatUtil.debugMessage("start CDATA", 50);
|
608 |
185
|
jones
|
}
|
609 |
|
|
|
610 |
1359
|
tao
|
/**
|
611 |
185
|
jones
|
* SAX Handler that receives notification of the end of CDATA sections
|
612 |
|
|
*/
|
613 |
|
|
public void endCDATA() throws SAXException {
|
614 |
1062
|
tao
|
MetaCatUtil.debugMessage("end CDATA", 50);
|
615 |
185
|
jones
|
}
|
616 |
|
|
|
617 |
1359
|
tao
|
/**
|
618 |
185
|
jones
|
* SAX Handler that receives notification of the start of entities
|
619 |
|
|
*/
|
620 |
|
|
public void startEntity(String name) throws SAXException {
|
621 |
1062
|
tao
|
MetaCatUtil.debugMessage("start ENTITY: " + name, 50);
|
622 |
697
|
bojilova
|
//System.out.println("start ENTITY: " + name);
|
623 |
243
|
jones
|
if (name.equals("[dtd]")) {
|
624 |
|
|
processingDTD = true;
|
625 |
|
|
}
|
626 |
185
|
jones
|
}
|
627 |
|
|
|
628 |
1359
|
tao
|
/**
|
629 |
185
|
jones
|
* SAX Handler that receives notification of the end of entities
|
630 |
|
|
*/
|
631 |
|
|
public void endEntity(String name) throws SAXException {
|
632 |
1062
|
tao
|
MetaCatUtil.debugMessage("end ENTITY: " + name, 50);
|
633 |
697
|
bojilova
|
//System.out.println("end ENTITY: " + name);
|
634 |
243
|
jones
|
if (name.equals("[dtd]")) {
|
635 |
|
|
processingDTD = false;
|
636 |
|
|
}
|
637 |
185
|
jones
|
}
|
638 |
186
|
jones
|
|
639 |
1359
|
tao
|
/**
|
640 |
186
|
jones
|
* SAX Handler that receives notification of element declarations
|
641 |
|
|
*/
|
642 |
|
|
public void elementDecl(String name, String model)
|
643 |
|
|
throws org.xml.sax.SAXException {
|
644 |
697
|
bojilova
|
//System.out.println("ELEMENTDECL: " + name + " " + model);
|
645 |
1062
|
tao
|
MetaCatUtil.debugMessage("ELEMENTDECL: " + name + " " + model, 50);
|
646 |
186
|
jones
|
}
|
647 |
|
|
|
648 |
1359
|
tao
|
/**
|
649 |
186
|
jones
|
* SAX Handler that receives notification of attribute declarations
|
650 |
|
|
*/
|
651 |
|
|
public void attributeDecl(String eName, String aName,
|
652 |
|
|
String type, String valueDefault, String value)
|
653 |
|
|
throws org.xml.sax.SAXException {
|
654 |
821
|
bojilova
|
|
655 |
1359
|
tao
|
//System.out.println("ATTRIBUTEDECL: " + eName + " "
|
656 |
697
|
bojilova
|
// + aName + " " + type + " " + valueDefault + " "
|
657 |
|
|
// + value);
|
658 |
1359
|
tao
|
MetaCatUtil.debugMessage("ATTRIBUTEDECL: " + eName + " "
|
659 |
243
|
jones
|
+ aName + " " + type + " " + valueDefault + " "
|
660 |
1062
|
tao
|
+ value, 50);
|
661 |
186
|
jones
|
}
|
662 |
|
|
|
663 |
1359
|
tao
|
/**
|
664 |
186
|
jones
|
* SAX Handler that receives notification of internal entity declarations
|
665 |
|
|
*/
|
666 |
|
|
public void internalEntityDecl(String name, String value)
|
667 |
|
|
throws org.xml.sax.SAXException {
|
668 |
697
|
bojilova
|
//System.out.println("INTERNENTITYDECL: " + name + " " + value);
|
669 |
1062
|
tao
|
MetaCatUtil.debugMessage("INTERNENTITYDECL: " + name + " " + value, 50);
|
670 |
186
|
jones
|
}
|
671 |
|
|
|
672 |
1359
|
tao
|
/**
|
673 |
186
|
jones
|
* SAX Handler that receives notification of external entity declarations
|
674 |
|
|
*/
|
675 |
|
|
public void externalEntityDecl(String name, String publicId,
|
676 |
|
|
String systemId)
|
677 |
|
|
throws org.xml.sax.SAXException {
|
678 |
1359
|
tao
|
//System.out.println("EXTERNENTITYDECL: " + name + " " + publicId
|
679 |
697
|
bojilova
|
// + " " + systemId);
|
680 |
1359
|
tao
|
MetaCatUtil.debugMessage("EXTERNENTITYDECL: " + name + " " + publicId
|
681 |
1062
|
tao
|
+ " " + systemId, 50);
|
682 |
831
|
bojilova
|
// it processes other external entity, not the DTD;
|
683 |
|
|
// it doesn't signal for the DTD here
|
684 |
|
|
processingDTD = false;
|
685 |
186
|
jones
|
}
|
686 |
|
|
|
687 |
204
|
jones
|
//
|
688 |
|
|
// the next section implements the ErrorHandler interface
|
689 |
|
|
//
|
690 |
186
|
jones
|
|
691 |
1359
|
tao
|
/**
|
692 |
204
|
jones
|
* SAX Handler that receives notification of fatal parsing errors
|
693 |
|
|
*/
|
694 |
|
|
public void fatalError(SAXParseException exception) throws SAXException {
|
695 |
1062
|
tao
|
MetaCatUtil.debugMessage("FATALERROR", 50);
|
696 |
204
|
jones
|
throw (new SAXException("Fatal processing error.", exception));
|
697 |
|
|
}
|
698 |
|
|
|
699 |
1359
|
tao
|
/**
|
700 |
204
|
jones
|
* SAX Handler that receives notification of recoverable parsing errors
|
701 |
|
|
*/
|
702 |
|
|
public void error(SAXParseException exception) throws SAXException {
|
703 |
1062
|
tao
|
MetaCatUtil.debugMessage("ERROR", 50);
|
704 |
660
|
bojilova
|
throw (new SAXException("Processing error.", exception));
|
705 |
204
|
jones
|
}
|
706 |
|
|
|
707 |
1359
|
tao
|
/**
|
708 |
204
|
jones
|
* SAX Handler that receives notification of warnings
|
709 |
|
|
*/
|
710 |
|
|
public void warning(SAXParseException exception) throws SAXException {
|
711 |
1062
|
tao
|
MetaCatUtil.debugMessage("WARNING", 50);
|
712 |
660
|
bojilova
|
throw (new SAXException("Warning.", exception));
|
713 |
204
|
jones
|
}
|
714 |
|
|
|
715 |
1359
|
tao
|
//
|
716 |
204
|
jones
|
// Helper, getter and setter methods
|
717 |
|
|
//
|
718 |
1359
|
tao
|
|
719 |
204
|
jones
|
/**
|
720 |
|
|
* get the document name
|
721 |
|
|
*/
|
722 |
|
|
public String getDocname() {
|
723 |
|
|
return docname;
|
724 |
|
|
}
|
725 |
|
|
|
726 |
|
|
/**
|
727 |
|
|
* get the document processing state
|
728 |
|
|
*/
|
729 |
243
|
jones
|
public boolean processingDTD() {
|
730 |
|
|
return processingDTD;
|
731 |
204
|
jones
|
}
|
732 |
1411
|
tao
|
|
733 |
|
|
/* Method to write a text buffer for DBSAXNode*/
|
734 |
1415
|
tao
|
protected long writeTextForDBSAXNode(StringBuffer strBuffer, DBSAXNode node)
|
735 |
1411
|
tao
|
throws SAXException
|
736 |
|
|
{
|
737 |
1415
|
tao
|
long nodeId = -1;
|
738 |
1411
|
tao
|
// Check parameter
|
739 |
|
|
if ( strBuffer == null || node == null)
|
740 |
|
|
{
|
741 |
1415
|
tao
|
return nodeId;
|
742 |
1411
|
tao
|
}
|
743 |
|
|
boolean moredata = true;
|
744 |
|
|
String data = null;
|
745 |
|
|
int bufferSize = strBuffer.length();
|
746 |
|
|
int start = 0;
|
747 |
|
|
|
748 |
|
|
// if there are some cotent in buffer, write it
|
749 |
|
|
if (bufferSize > 0)
|
750 |
|
|
{
|
751 |
|
|
MetaCatUtil.debugMessage("Write text into DB", 50);
|
752 |
|
|
// This loop deals with the case where there are more characters
|
753 |
|
|
// than can fit in a single database text field (limit is
|
754 |
|
|
// MAXDATACHARS). If the text to be inserted exceeds MAXDATACHARS,
|
755 |
|
|
// write a series of nodes that are MAXDATACHARS long, and then the
|
756 |
|
|
// final node contains the remainder
|
757 |
|
|
while (moredata)
|
758 |
|
|
{
|
759 |
|
|
bufferSize = strBuffer.length();
|
760 |
|
|
if (bufferSize > MAXDATACHARS)
|
761 |
|
|
{
|
762 |
|
|
data = strBuffer.substring(start, MAXDATACHARS);
|
763 |
|
|
// cut the stringbuffer part that already written into db
|
764 |
|
|
strBuffer = strBuffer.delete(start, MAXDATACHARS);
|
765 |
|
|
}
|
766 |
|
|
else
|
767 |
|
|
{
|
768 |
|
|
data = strBuffer.substring(start, bufferSize);
|
769 |
|
|
moredata = false;
|
770 |
|
|
}
|
771 |
|
|
|
772 |
|
|
// Write the content of the node to the database
|
773 |
1415
|
tao
|
nodeId = node.writeChildNodeToDB("TEXT", null, data, docid);
|
774 |
1411
|
tao
|
}//while
|
775 |
|
|
}//if
|
776 |
1415
|
tao
|
return nodeId;
|
777 |
1411
|
tao
|
}
|
778 |
17
|
jones
|
}
|