Project

General

Profile

1
/**
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
 *
9
 *   '$Author: daigle $'
10
 *     '$Date: 2010-02-03 13:29:30 -0800 (Wed, 03 Feb 2010) $'
11
 * '$Revision: 5208 $'
12
 *
13
 * This program is free software; you can redistribute it and/or modify
14
 * it under the terms of the GNU General Public License as published by
15
 * the Free Software Foundation; either version 2 of the License, or
16
 * (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU General Public License
24
 * along with this program; if not, write to the Free Software
25
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
 */
27

    
28
package edu.ucsb.nceas.metacat;
29

    
30
import java.io.BufferedReader;
31
import java.io.File;
32
import java.io.FileReader;
33
import java.io.FileWriter;
34
import java.io.IOException;
35
import java.io.Reader;
36
import java.sql.PreparedStatement;
37
import java.sql.ResultSet;
38
import java.sql.SQLException;
39
import java.sql.Statement;
40
import java.util.EmptyStackException;
41
import java.util.Enumeration;
42
import java.util.Hashtable;
43
import java.util.Stack;
44
import java.util.Vector;
45

    
46
import org.apache.log4j.Logger;
47
import org.xml.sax.Attributes;
48
import org.xml.sax.SAXException;
49

    
50
import edu.ucsb.nceas.metacat.accesscontrol.AccessControlInterface;
51
import edu.ucsb.nceas.metacat.accesscontrol.AccessRule;
52
import edu.ucsb.nceas.metacat.accesscontrol.AccessSection;
53
import edu.ucsb.nceas.metacat.database.DBConnection;
54
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
55
import edu.ucsb.nceas.metacat.properties.PropertyService;
56
import edu.ucsb.nceas.metacat.util.DocumentUtil;
57
import edu.ucsb.nceas.metacat.util.MetacatUtil;
58
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
59

    
60
/**
61
 * A database aware Class implementing callback methods for the SAX parser to
62
 * call when processing the XML stream and generating events
63
 */
64
public class Eml210SAXHandler extends DBSAXHandler implements AccessControlInterface {
65

    
66
	private boolean processTopLevelAccess = false;
67

    
68
	private boolean processAdditionalAccess = false;
69

    
70
	private boolean processOtherAccess = false;
71

    
72
	private AccessSection accessObject = null;
73

    
74
	private AccessRule accessRule = null;
75

    
76
	private Vector<AccessSection> accessObjectList = new Vector<AccessSection>(); // store
77
																					// every
78
																					// access
79
																					// rule
80

    
81
	private Hashtable<String, AccessSection> topLevelAccessControlMap = new Hashtable<String, AccessSection>();
82

    
83
	private Hashtable<String, AccessSection> additionalAccessControlMap = new Hashtable<String, AccessSection>();// store
84

    
85
	// subtree access for single additionalmetacat
86
	private Vector<Hashtable<String, AccessSection>> additionalAccessMapList = new Vector<Hashtable<String, AccessSection>>();// store
87
																																// maps
88
																																// for
89

    
90
	// every additionalmetadata
91
	private Vector<String> describesId = new Vector<String>(); // store the ids
92
																// in
93

    
94
	// additionalmetadata/describes
95
	private Stack<SubTree> subTreeInfoStack = new Stack<SubTree>();
96

    
97
	private Vector<SubTree> subTreeList = new Vector<SubTree>();// store the
98
																// final subtree
99
	
100
	private boolean needToCheckAccessModule = false;
101

    
102
	private Vector<AccessSection> unChangeableAccessSubTreeVector = new Vector<AccessSection>();
103

    
104
	private Stack<NodeRecord> currentUnchangeableAccessModuleNodeStack = new Stack<NodeRecord>();
105

    
106
	private AccessSection topAccessSection;
107

    
108
	// we need an another stack to store the access node which we pull out just
109
	// from xml. If a reference happened, we can use the stack the compare nodes
110
	private Stack<NodeRecord> storedAccessNodeStack = new Stack<NodeRecord>();
111

    
112
	// vector stored the data file id which will be write into relation table
113
	private Vector<String> onlineDataFileIdInRelationVector = new Vector<String>();
114

    
115
	// vector stored the data file id which will be write top access rules to
116
	// access table
117
	private Vector<String> onlineDataFileIdInTopAccessVector = new Vector<String>();
118

    
119
	// Indicator of inline data
120
	private boolean handleInlineData = false;
121

    
122
	private Hashtable<String, String> inlineDataNameSpace = null;
123

    
124
	private FileWriter inlineDataFileWriter = null;
125

    
126
	private String inlineDataFileName = null;
127

    
128
	DistributionSection currentDistributionSection = null;
129

    
130
	Vector<DistributionSection> allDistributionSections = new Vector<DistributionSection>();
131

    
132
	// This variable keeps a counter of each distribution element. This index
133
	// will be used to name the inline data file that gets written to disk, and to
134
	// strip inline data from the metadata document on file if the user does not have
135
	// read access.
136
	private int distributionIndex = 0;
137

    
138
	// private String distributionOnlineFileName = null;
139

    
140
	// This is used to delete inline files if the xml does not parse correctly.
141
	private Vector<String> inlineFileIdList = new Vector<String>();
142

    
143
	// Constant
144
	private static final String EML = "eml";
145

    
146
	private static final String DISTRIBUTION = "distribution";
147

    
148
	private static final String ORDER = "order";
149

    
150
	private static final String ID = "id";
151

    
152
	private static final String REFERENCES = "references";
153

    
154
	public static final String INLINE = "inline";
155

    
156
	private static final String ONLINE = "online";
157

    
158
	private static final String URL = "url";
159

    
160
	// private static final String PERMISSIONERROR = "User tried to update a
161
	// subtree "
162
	// + "when they don't have write permission!";
163

    
164
	private static final String UPDATEACCESSERROR = "User tried to update an "
165
			+ "access module when they don't have \"ALL\" permission!";
166

    
167
	private static final String TOPLEVEL = "top";
168

    
169
	private static final String SUBTREELEVEL = "subtree";
170

    
171
	private static final String RELATION = "Provides info for";
172

    
173
	private Logger logMetacat = Logger.getLogger(Eml210SAXHandler.class);
174

    
175
	/**
176
	 * Construct an instance of the handler class In this constructor, user can
177
	 * specify the version need to update
178
	 * 
179
	 * @param conn
180
	 *            the JDBC connection to which information is written
181
	 * @param action -
182
	 *            "INSERT" or "UPDATE"
183
	 * @param docid
184
	 *            to be inserted or updated into JDBC connection
185
	 * @param revision,
186
	 *            the user specified the revision need to be update
187
	 * @param user
188
	 *            the user connected to MetaCat servlet and owns the document
189
	 * @param groups
190
	 *            the groups to which user belongs
191
	 * @param pub
192
	 *            flag for public "read" access on document
193
	 * @param serverCode
194
	 *            the serverid from xml_replication on which this document
195
	 *            resides.
196
	 * 
197
	 */
198
	public Eml210SAXHandler(DBConnection conn, String action, String docid,
199
			String revision, String user, String[] groups, String pub, int serverCode,
200
			String createDate, String updateDate) throws SAXException {
201
		super(conn, action, docid, revision, user, groups, pub, serverCode, createDate,
202
				updateDate);
203
		// Get the unchangeable subtrees (user doesn't have write permission)
204
		try {
205
			PermissionController control = new PermissionController(docid
206
					+ PropertyService.getProperty("document.accNumSeparator") + revision);
207
			// unChangableSubTreeHash = getUnchangableSubTree(control, user,
208
			// groups);
209

    
210
			// If the action is update and user doesn't have "ALL" permission
211
			// we need to check if user can update access subtree			
212
			if (!control.hasPermission(user, groups, AccessControlInterface.ALLSTRING)
213
					&& action != null && action.equals("UPDATE")) {
214
				needToCheckAccessModule = true;
215
				unChangeableAccessSubTreeVector = getAccessSubTreeListFromDB();
216
			}
217

    
218
		} catch (Exception e) {
219
			throw new SAXException(e.getMessage());
220
		}
221
	}
222

    
223
	/*
224
	 * Get the subtree node info from xml_accesssubtree table
225
	 */
226
	private Vector<AccessSection> getAccessSubTreeListFromDB() throws Exception {
227
		Vector<AccessSection> result = new Vector<AccessSection>();
228
		PreparedStatement pstmt = null;
229
		ResultSet rs = null;
230
		String sql = "SELECT controllevel, subtreeid, startnodeid, endnodeid "
231
				+ "FROM xml_accesssubtree WHERE docid like ? "
232
				+ "ORDER BY startnodeid ASC";
233

    
234
		try {
235

    
236
			pstmt = connection.prepareStatement(sql);
237
			// Increase DBConnection usage count
238
			connection.increaseUsageCount(1);
239
			// Bind the values to the query
240
			pstmt.setString(1, docid);
241
			pstmt.execute();
242

    
243
			// Get result set
244
			rs = pstmt.getResultSet();
245
			while (rs.next()) {
246
				String level = rs.getString(1);
247
				String sectionId = rs.getString(2);
248
				long startNodeId = rs.getLong(3);
249
				long endNodeId = rs.getLong(4);
250
				// create a new access section
251
				AccessSection accessObj = new AccessSection();
252
				accessObj.setControlLevel(level);
253
				accessObj.setDocId(docid);
254
				accessObj.setSubTreeId(sectionId);
255
				accessObj.setStartNodeId(startNodeId);
256
				accessObj.setEndNodeId(endNodeId);
257
				Stack<NodeRecord> nodeStack = accessObj.getSubTreeNodeStack();
258
				accessObj.setSubTreeNodeStack(nodeStack);
259
				// add this access obj into vector
260
				result.add(accessObj);
261
				// Get the top level access subtree control
262
				if (level != null && level.equals(TOPLEVEL)) {
263
					topAccessSection = accessObj;
264
				}
265
			}
266
			pstmt.close();
267
		}// try
268
		catch (SQLException e) {
269
			throw new SAXException("EMLSAXHandler.getAccessSubTreeListFromDB(): "
270
					+ e.getMessage());
271
		}// catch
272
		finally {
273
			try {
274
				pstmt.close();
275
			} catch (SQLException ee) {
276
				throw new SAXException("EMLSAXHandler.getAccessSubTreeListFromDB(): "
277
						+ ee.getMessage());
278
			}
279
		}// finally
280
		return result;
281
	}
282

    
283
	/** SAX Handler that is called at the start of each XML element */
284
	public void startElement(String uri, String localName, String qName, Attributes atts)
285
			throws SAXException {
286
		// for element <eml:eml...> qname is "eml:eml", local name is "eml"
287
		// for element <acl....> both qname and local name is "eml"
288
		// uri is namesapce
289
		logMetacat.debug("Start ELEMENT(qName) " + qName);
290
		logMetacat.debug("Start ELEMENT(localName) " + localName);
291
		logMetacat.debug("Start ELEMENT(uri) " + uri);
292

    
293
		DBSAXNode parentNode = null;
294
		DBSAXNode currentNode = null;
295

    
296
		if (!handleInlineData) {
297
			// Get a reference to the parent node for the id
298
			try {
299
				parentNode = (DBSAXNode) nodeStack.peek();
300
			} catch (EmptyStackException e) {
301
				parentNode = null;
302
			}
303

    
304
			// start handle inline data
305
			if (localName.equals(INLINE)) {
306
				handleInlineData = true;
307
				// initialize namespace hash for in line data
308
				inlineDataNameSpace = new Hashtable<String, String>();
309
				// initialize file writer
310
				String docidWithoutRev = DocumentUtil.getDocIdFromString(docid);
311
				String seperator = ".";
312
				try {
313
					seperator = PropertyService.getProperty("document.accNumSeparator");
314
				} catch (PropertyNotFoundException pnfe) {
315
					logMetacat.error("Could not fing property 'accNumSeparator'. "
316
							+ "Setting separator to '.': " + pnfe.getMessage());
317
				}
318
				// the new file name will look like docid.rev.2
319
				inlineDataFileName = docidWithoutRev + seperator + revision + seperator
320
						+ distributionIndex;
321
				inlineDataFileWriter = createInlineDataFileWriter(inlineDataFileName);
322
				// put the inline file id into a vector. If upload failed,
323
				// metacat will delete the inline data file
324
				inlineFileIdList.add(inlineDataFileName);
325
				
326
				currentDistributionSection.setDistributionType(DistributionSection.INLINE_DATA_DISTRIBUTION);
327
				currentDistributionSection.setDataFileName(inlineDataFileName);
328

    
329
			}
330

    
331
			// If hit a text node, we need write this text for current's parent
332
			// node This will happen if the element is mixed
333
			if (hitTextNode && parentNode != null) {
334

    
335
				// compare top level access module
336
				if (processTopLevelAccess && needToCheckAccessModule) {
337
					compareTextNode(currentUnchangeableAccessModuleNodeStack, textBuffer,
338
							UPDATEACCESSERROR);
339
				}
340

    
341
				if (needToCheckAccessModule
342
						&& (processAdditionalAccess || processOtherAccess || processTopLevelAccess)) {
343
					// stored the pull out nodes into storedNode stack
344
					NodeRecord nodeElement = new NodeRecord(-2, -2, -2, "TEXT", null,
345
							null, MetacatUtil.normalize(textBuffer.toString()));
346
					storedAccessNodeStack.push(nodeElement);
347

    
348
				}
349

    
350
				// write the textbuffer into db for parent node.
351
				endNodeId = writeTextForDBSAXNode(endNodeId, textBuffer, parentNode);
352
				// rest hitTextNode
353
				hitTextNode = false;
354
				// reset textbuffer
355
				textBuffer = null;
356
				textBuffer = new StringBuffer();
357

    
358
			}
359

    
360
			// Document representation that points to the root document node
361
			if (atFirstElement) {
362
				atFirstElement = false;
363
				// If no DOCTYPE declaration: docname = root element
364
				// doctype = root element name or name space
365
				if (docname == null) {
366
					docname = localName;
367
					// if uri isn't null doctype = uri(namespace)
368
					// othewise root element
369
					if (uri != null && !(uri.trim()).equals("")) {
370
						doctype = uri;
371
					} else {
372
						doctype = docname;
373
					}
374
					logMetacat.debug("DOCNAME-a: " + docname);
375
					logMetacat.debug("DOCTYPE-a: " + doctype);
376
				} else if (doctype == null) {
377
					// because docname is not null and it is declared in dtd
378
					// so could not be in schema, no namespace
379
					doctype = docname;
380
					logMetacat.debug("DOCTYPE-b: " + doctype);
381
				}
382
				rootNode.writeNodename(docname);
383
				try {
384
					// for validated XML Documents store a reference to XML DB
385
					// Catalog. Because this is select statement and it needn't
386
					// roll back if insert document action failed. In order to
387
					// decrease DBConnection usage count, we get a new
388
					// dbconnection from pool String catalogid = null;
389
					DBConnection dbConn = null;
390
					int serialNumber = -1;
391

    
392
					try {
393
						// Get dbconnection
394
						dbConn = DBConnectionPool
395
								.getDBConnection("DBSAXHandler.startElement");
396
						serialNumber = dbConn.getCheckOutSerialNumber();
397

    
398
						Statement stmt = dbConn.createStatement();
399
						ResultSet rs = stmt
400
								.executeQuery("SELECT catalog_id FROM xml_catalog "
401
										+ "WHERE entry_type = 'Schema' "
402
										+ "AND public_id = '" + doctype + "'");
403
						boolean hasRow = rs.next();
404
						if (hasRow) {
405
							catalogid = rs.getString(1);
406
						}
407
						stmt.close();
408
					}// try
409
					finally {
410
						// Return dbconnection
411
						DBConnectionPool.returnDBConnection(dbConn, serialNumber);
412
					}// finally
413

    
414
					// create documentImpl object by the constructor which can
415
					// specify the revision
416
					if (!super.getIsRevisionDoc()) {
417
						currentDocument = new DocumentImpl(connection, rootNode
418
								.getNodeID(), docname, doctype, docid, revision, action,
419
								user, this.pub, catalogid, this.serverCode, createDate,
420
								updateDate);
421
					}
422

    
423
				} catch (McdbDocNotFoundException mdnfe) {
424
					Vector<Integer> revList = null;
425
					
426
					try {
427
						revList = DBUtil.getRevListFromRevisionTable(docid);
428
					} catch (SQLException sqle) {
429
						logMetacat.error("SQL error when trying to get rev list for doc " + docid + " : " + sqle.getMessage());
430
						throw (new SAXException("Doc ID " + docid + " was not found and cannot be updated.")); 
431
					}
432
					
433
					if (revList.size() > 0) {
434
						throw (new SAXException("EML210SaxHandler.startElement - Doc ID " + docid + " was deleted and cannot be updated."));
435
					} else {
436
						throw (new SAXException("EML210SaxHandler.startElement - Doc ID " + docid + " was not found and cannot be updated.")); 
437
					}
438
				} catch (Exception e) {
439
                    throw (new SAXException("EML210SaxHandler.startElement - error with action " + 
440
                    		action + " : " + e.getMessage()));
441
				}
442
			}
443

    
444
			// Create the current node representation
445
			currentNode = new DBSAXNode(connection, qName, localName, parentNode,
446
					rootNode.getNodeID(), docid, doctype);
447
			// Use a local variable to store the element node id
448
			// If this element is a start point of subtree(section), it will be
449
			// stored otherwise, it will be discarded
450
			long startNodeId = currentNode.getNodeID();
451
			// Add all of the namespaces
452
			String prefix = null;
453
			String nsuri = null;
454
			Enumeration<String> prefixes = namespaces.keys();
455
			while (prefixes.hasMoreElements()) {
456
				prefix = prefixes.nextElement();
457
				nsuri = namespaces.get(prefix);
458
				endNodeId = currentNode.setNamespace(prefix, nsuri, docid);
459
			}
460

    
461
			// Add all of the attributes
462
			for (int i = 0; i < atts.getLength(); i++) {
463
				String attributeName = atts.getQName(i);
464
				String attributeValue = atts.getValue(i);
465
				endNodeId = currentNode
466
						.setAttribute(attributeName, attributeValue, docid);
467

    
468
				// To handle name space and schema location if the attribute
469
				// name is xsi:schemaLocation. If the name space is in not
470
				// in catalog table it will be registered.
471
				if (attributeName != null
472
						&& attributeName.indexOf(MetaCatServlet.SCHEMALOCATIONKEYWORD) != -1) {
473
					SchemaLocationResolver resolver = new SchemaLocationResolver(
474
							attributeValue);
475
					resolver.resolveNameSpace();
476

    
477
				} else if (attributeName != null && attributeName.equals(ID)) {
478

    
479
				}
480
			}// for
481

    
482
			// handle access stuff
483
			if (localName.equals(ACCESS)) {
484
				if (parentNode.getTagName().equals(EML)) {
485
					processTopLevelAccess = true;
486
				} else if (parentNode.getTagName() == DISTRIBUTION) {
487
					processAdditionalAccess = true;
488
				} else {
489
					// process other access embedded into resource level
490
					// module
491
					processOtherAccess = true;
492
				}
493
				// create access object
494
				accessObject = new AccessSection();
495
				// set permission order
496
				String permOrder = currentNode.getAttribute(ORDER);
497
				accessObject.setPermissionOrder(permOrder);
498
				// set access id
499
				String accessId = currentNode.getAttribute(ID);
500
				accessObject.setSubTreeId(accessId);
501
				accessObject.setStartNodeId(startNodeId);
502
				accessObject.setDocId(docid);
503
				if (processAdditionalAccess) {
504
					accessObject.setDataFileName(inlineDataFileName);
505
				}
506

    
507
				// load top level node stack to
508
				// currentUnchangableAccessModuleNodeStack
509
				if (processTopLevelAccess && needToCheckAccessModule) {
510
					// get the node stack for
511
					currentUnchangeableAccessModuleNodeStack = topAccessSection
512
							.getSubTreeNodeStack();
513
				}
514
			} else if (localName.equals(DISTRIBUTION)) {
515
				distributionIndex++;
516
				currentDistributionSection = new DistributionSection(distributionIndex);
517

    
518
				// handle subtree info
519
				SubTree subTree = new SubTree();
520
				// set sub tree id
521
				subTree.setSubTreeId(String.valueOf(distributionIndex));
522
				// set sub tree start element name
523
				subTree.setStartElementName(currentNode.getTagName());
524
				// set start node number
525
				subTree.setStartNodeId(startNodeId);
526
				// add to stack, but it didn't get end node id yet
527
				subTreeInfoStack.push(subTree);
528
			}
529
			// Set up a access rule for allow
530
			else if (parentNode.getTagName() != null
531
					&& (parentNode.getTagName()).equals(ACCESS)
532
					&& localName.equals(ALLOW)) {
533

    
534
				accessRule = new AccessRule();
535

    
536
				// set permission type "allow"
537
				accessRule.setPermissionType(ALLOW);
538

    
539
			}
540
			// set up an access rule for deny
541
			else if (parentNode.getTagName() != null
542
					&& (parentNode.getTagName()).equals(ACCESS) && localName.equals(DENY)) {
543
				accessRule = new AccessRule();
544
				// set permission type "allow"
545
				accessRule.setPermissionType(DENY);
546
			}
547

    
548
			// Add the node to the stack, so that any text data can be
549
			// added as it is encountered
550
			nodeStack.push(currentNode);
551
			// Add the node to the vector used by thread for writing XML Index
552
			nodeIndex.addElement(currentNode);
553

    
554
			// compare top access level module
555
			if (processTopLevelAccess && needToCheckAccessModule) {
556
				compareElementNameSpaceAttributes(
557
						currentUnchangeableAccessModuleNodeStack, namespaces, atts,
558
						localName, UPDATEACCESSERROR);
559

    
560
			}
561

    
562
			// store access module element and attributes into stored stack
563
			if (needToCheckAccessModule
564
					&& (processAdditionalAccess || processOtherAccess || processTopLevelAccess)) {
565
				// stored the pull out nodes into storedNode stack
566
				NodeRecord nodeElement = new NodeRecord(-2, -2, -2, "ELEMENT", localName,
567
						prefix, MetacatUtil.normalize(null));
568
				storedAccessNodeStack.push(nodeElement);
569
				for (int i = 0; i < atts.getLength(); i++) {
570
					String attributeName = atts.getQName(i);
571
					String attributeValue = atts.getValue(i);
572
					NodeRecord nodeAttribute = new NodeRecord(-2, -2, -2, "ATTRIBUTE",
573
							attributeName, null, MetacatUtil.normalize(attributeValue));
574
					storedAccessNodeStack.push(nodeAttribute);
575
				}
576

    
577
			}
578

    
579
			// reset name space
580
			namespaces = null;
581
			namespaces = new Hashtable<String, String>();
582
		}// not inline data
583
		else {
584
			// we don't buffer the inline data in characters() method
585
			// so start character don't need to hand text node.
586

    
587
			// inline data may be the xml format.
588
			StringBuffer inlineElements = new StringBuffer();
589
			inlineElements.append("<").append(qName);
590
			// append attributes
591
			for (int i = 0; i < atts.getLength(); i++) {
592
				String attributeName = atts.getQName(i);
593
				String attributeValue = atts.getValue(i);
594
				inlineElements.append(" ");
595
				inlineElements.append(attributeName);
596
				inlineElements.append("=\"");
597
				inlineElements.append(attributeValue);
598
				inlineElements.append("\"");
599
			}
600
			// append namespace
601
			String prefix = null;
602
			String nsuri = null;
603
			Enumeration<String> prefixes = inlineDataNameSpace.keys();
604
			while (prefixes.hasMoreElements()) {
605
				prefix = prefixes.nextElement();
606
				nsuri = namespaces.get(prefix);
607
				inlineElements.append(" ");
608
				inlineElements.append("xmlns:");
609
				inlineElements.append(prefix);
610
				inlineElements.append("=\"");
611
				inlineElements.append(nsuri);
612
				inlineElements.append("\"");
613
			}
614
			inlineElements.append(">");
615
			// reset inline data name space
616
			inlineDataNameSpace = null;
617
			inlineDataNameSpace = new Hashtable<String, String>();
618
			// write inline data into file
619
			logMetacat.debug("the inline element data is: " + inlineElements.toString());
620
			writeInlineDataIntoFile(inlineDataFileWriter, inlineElements);
621
		}// else
622

    
623
	}
624

    
625
	private void compareElementNameSpaceAttributes(
626
			Stack<NodeRecord> unchangeableNodeStack,
627
			Hashtable<String, String> nameSpaces, Attributes attributes,
628
			String localName, String error) throws SAXException {
629
		// Get element subtree node stack (element node)
630
		NodeRecord elementNode = null;
631
		try {
632
			elementNode = unchangeableNodeStack.pop();
633
		} catch (EmptyStackException ee) {
634
			logMetacat.error("Node stack is empty for element data");
635
			throw new SAXException(error);
636
		}
637
		logMetacat.debug("current node type from xml is ELEMENT");
638
		logMetacat.debug("node type from stack: " + elementNode.getNodeType());
639
		logMetacat.debug("node name from xml document: " + localName);
640
		logMetacat.debug("node name from stack: " + elementNode.getNodeName());
641
		logMetacat.debug("node data from stack: " + elementNode.getNodeData());
642
		logMetacat.debug("node id is: " + elementNode.getNodeId());
643
		// if this node is not element or local name not equal or name space
644
		// not equals, throw an exception
645
		if (!elementNode.getNodeType().equals("ELEMENT")
646
				|| !localName.equals(elementNode.getNodeName()))
647
		// (uri != null && !uri.equals(elementNode.getNodePrefix())))
648
		{
649
			logMetacat.error("Inconsistence happened: ");
650
			logMetacat.error("current node type from xml is ELEMENT");
651
			logMetacat.error("node type from stack: " + elementNode.getNodeType());
652
			logMetacat.error("node name from xml document: " + localName);
653
			logMetacat.error("node name from stack: " + elementNode.getNodeName());
654
			logMetacat.error("node data from stack: " + elementNode.getNodeData());
655
			logMetacat.error("node id is: " + elementNode.getNodeId());
656
			throw new SAXException(error);
657
		}
658

    
659
		// compare namespace
660
		Enumeration<String> nameEn = nameSpaces.keys();
661
		while (nameEn.hasMoreElements()) {
662
			// Get namespacke node stack (element node)
663
			NodeRecord nameNode = null;
664
			try {
665
				nameNode = unchangeableNodeStack.pop();
666
			} catch (EmptyStackException ee) {
667
				logMetacat.error("Node stack is empty for namespace data");
668
				throw new SAXException(error);
669
			}
670

    
671
			String prefixName = nameEn.nextElement();
672
			String nameSpaceUri = nameSpaces.get(prefixName);
673
			if (!nameNode.getNodeType().equals("NAMESPACE")
674
					|| !prefixName.equals(nameNode.getNodeName())
675
					|| !nameSpaceUri.equals(nameNode.getNodeData())) {
676
				logMetacat.error("Inconsistence happened: ");
677
				logMetacat.error("current node type from xml is NAMESPACE");
678
				logMetacat.error("node type from stack: " + nameNode.getNodeType());
679
				logMetacat.error("current node name from xml is: " + prefixName);
680
				logMetacat.error("node name from stack: " + nameNode.getNodeName());
681
				logMetacat.error("current node data from xml is: " + nameSpaceUri);
682
				logMetacat.error("node data from stack: " + nameNode.getNodeData());
683
				logMetacat.error("node id is: " + nameNode.getNodeId());
684
				throw new SAXException(error);
685
			}
686

    
687
		}// while
688

    
689
		// compare attributes
690
		for (int i = 0; i < attributes.getLength(); i++) {
691
			NodeRecord attriNode = null;
692
			try {
693
				attriNode = unchangeableNodeStack.pop();
694

    
695
			} catch (EmptyStackException ee) {
696
				logMetacat.error("Node stack is empty for attribute data");
697
				throw new SAXException(error);
698
			}
699
			String attributeName = attributes.getQName(i);
700
			String attributeValue = attributes.getValue(i);
701
			logMetacat.debug("current node type from xml is ATTRIBUTE ");
702
			logMetacat.debug("node type from stack: " + attriNode.getNodeType());
703
			logMetacat.debug("current node name from xml is: " + attributeName);
704
			logMetacat.debug("node name from stack: " + attriNode.getNodeName());
705
			logMetacat.debug("current node data from xml is: " + attributeValue);
706
			logMetacat.debug("node data from stack: " + attriNode.getNodeData());
707
			logMetacat.debug("node id  is: " + attriNode.getNodeId());
708

    
709
			if (!attriNode.getNodeType().equals("ATTRIBUTE")
710
					|| !attributeName.equals(attriNode.getNodeName())
711
					|| !attributeValue.equals(attriNode.getNodeData())) {
712
				logMetacat.error("Inconsistence happened: ");
713
				logMetacat.error("current node type from xml is ATTRIBUTE ");
714
				logMetacat.error("node type from stack: " + attriNode.getNodeType());
715
				logMetacat.error("current node name from xml is: " + attributeName);
716
				logMetacat.error("node name from stack: " + attriNode.getNodeName());
717
				logMetacat.error("current node data from xml is: " + attributeValue);
718
				logMetacat.error("node data from stack: " + attriNode.getNodeData());
719
				logMetacat.error("node is: " + attriNode.getNodeId());
720
				throw new SAXException(error);
721
			}
722
		}// for
723

    
724
	}
725

    
726
	/* method to compare current text node and node in db */
727
	private void compareTextNode(Stack<NodeRecord> nodeStack, StringBuffer text,
728
			String error) throws SAXException {
729
		NodeRecord node = null;
730
		// get node from current stack
731
		try {
732
			node = nodeStack.pop();
733
		} catch (EmptyStackException ee) {
734
			logMetacat.error("Node stack is empty for text data in startElement");
735
			throw new SAXException(error);
736
		}
737
		logMetacat.debug("current node type from xml is TEXT in start element");
738
		logMetacat.debug("node type from stack: " + node.getNodeType());
739
		logMetacat.debug("current node data from xml is: " + text.toString());
740
		logMetacat.debug("node data from stack: " + node.getNodeData());
741
		logMetacat.debug("node name from stack: " + node.getNodeName());
742
		logMetacat.debug("node is: " + node.getNodeId());
743
		if (!node.getNodeType().equals("TEXT")
744
				|| !(text.toString()).equals(node.getNodeData())) {
745
			logMetacat.error("Inconsistence happened: ");
746
			logMetacat.error("current node type from xml is TEXT in start element");
747
			logMetacat.error("node type from stack: " + node.getNodeType());
748
			logMetacat.error("current node data from xml is: " + text.toString());
749
			logMetacat.error("node data from stack: " + node.getNodeData());
750
			logMetacat.error("node name from stack: " + node.getNodeName());
751
			logMetacat.error("node is: " + node.getNodeId());
752
			throw new SAXException(error);
753
		}// if
754
	}
755

    
756
	/** SAX Handler that is called for each XML text node */
757
	public void characters(char[] cbuf, int start, int len) throws SAXException {
758
		logMetacat.debug("CHARACTERS");
759
		if (!handleInlineData) {
760
			// buffer all text nodes for same element. This is for if text was
761
			// split into different nodes
762
			textBuffer.append(new String(cbuf, start, len));
763
			// set hittextnode true
764
			hitTextNode = true;
765
			// if text buffer .size is greater than max, write it to db.
766
			// so we can save memory
767
			if (textBuffer.length() > MAXDATACHARS) {
768
				logMetacat.debug("Write text into DB in charaters"
769
						+ " when text buffer size is greater than maxmum number");
770
				DBSAXNode currentNode = (DBSAXNode) nodeStack.peek();
771
				endNodeId = writeTextForDBSAXNode(endNodeId, textBuffer, currentNode);
772
				textBuffer = null;
773
				textBuffer = new StringBuffer();
774
			}
775
		} else {
776
			// this is inline data and write file system directly
777
			// we don't need to buffer it.
778
			StringBuffer inlineText = new StringBuffer();
779
			inlineText.append(new String(cbuf, start, len));
780
			logMetacat.debug("The inline text data write into file system: "
781
					+ inlineText.toString());
782
			writeInlineDataIntoFile(inlineDataFileWriter, inlineText);
783
		}
784
	}
785

    
786
	/** SAX Handler that is called at the end of each XML element */
787
	public void endElement(String uri, String localName, String qName)
788
			throws SAXException {
789
		logMetacat.debug("End ELEMENT " + qName);
790

    
791
		if (localName.equals(INLINE) && handleInlineData) {
792
			// Get the node from the stack
793
			DBSAXNode currentNode = (DBSAXNode) nodeStack.pop();
794
			logMetacat.debug("End of inline data");
795
			// close file writer
796
			try {
797
				inlineDataFileWriter.close();
798
				handleInlineData = false;
799
			} catch (IOException ioe) {
800
				throw new SAXException(ioe.getMessage());
801
			}
802

    
803
			// write put inline data file name into text buffer (without path)
804
			textBuffer = new StringBuffer(inlineDataFileName);
805
			// write file name into db
806
			endNodeId = writeTextForDBSAXNode(endNodeId, textBuffer, currentNode);
807
			// reset textbuff
808
			textBuffer = null;
809
			textBuffer = new StringBuffer();
810
			return;
811
		}
812

    
813
		if (!handleInlineData) {
814
			// Get the node from the stack
815
			DBSAXNode currentNode = (DBSAXNode) nodeStack.pop();
816
			String currentTag = currentNode.getTagName();
817

    
818
			// If before the end element, the parser hit text nodes and store
819
			// them into the buffer, write the buffer to data base. The reason
820
			// we put write database here is for xerces some time split text
821
			// node
822
			if (hitTextNode) {
823
				// get access value
824
				String data = null;
825
				// add principal
826
				if (currentTag.equals(PRINCIPAL) && accessRule != null) {
827
					data = (textBuffer.toString()).trim();
828
					accessRule.addPrincipal(data);
829

    
830
				} else if (currentTag.equals(PERMISSION) && accessRule != null) {
831
					data = (textBuffer.toString()).trim();
832
					// we combine different a permission into one value
833
					int permission = accessRule.getPermission();
834
					// add permission
835
					if (data.toUpperCase().equals(READSTRING)) {
836
						permission = permission | READ;
837
					} else if (data.toUpperCase().equals(WRITESTRING)) {
838
						permission = permission | WRITE;
839
					} else if (data.toUpperCase().equals(CHMODSTRING)) {
840
						permission = permission | CHMOD;
841
					} else if (data.toUpperCase().equals(ALLSTRING)) {
842
						permission = permission | ALL;
843
					}
844
					accessRule.setPermission(permission);
845
				} else if (currentTag.equals(REFERENCES)
846
						&& (processTopLevelAccess || processAdditionalAccess || processOtherAccess)) {
847
					// get reference
848
					data = (textBuffer.toString()).trim();
849
					// put reference id into accessSection
850
					accessObject.setReferences(data);
851

    
852
				} else if (currentTag.equals(URL)) {
853
					// handle online data, make sure its'parent is online
854
					DBSAXNode parentNode = (DBSAXNode) nodeStack.peek();
855
					if (parentNode != null && parentNode.getTagName() != null
856
							&& parentNode.getTagName().equals(ONLINE)) {
857
						// if online data is in local metacat, add it to the
858
						// vector
859
						data = (textBuffer.toString()).trim();
860
						handleOnlineUrlDataFile(data);
861
						/*
862
						 * if (data != null && (data.indexOf(MetacatUtil
863
						 * .getProperty("httpserver")) != -1 || data
864
						 * .indexOf(MetacatUtil .getProperty("server")) != -1)) { //
865
						 * Get docid from url String dataId = MetacatUtil
866
						 * .getDocIdWithRevFromOnlineURL(data); // add to vector
867
						 * onlineDataFileIdVector.add(dataId); }//if
868
						 */
869
					}// if
870
				}// else if
871
				// write text to db if it is not inline data
872
				// if (!localName.equals(INLINE))
873
				{
874
					logMetacat.debug("Write text into DB in End Element");
875

    
876
					// compare top level access module
877
					if (processTopLevelAccess && needToCheckAccessModule) {
878
						compareTextNode(currentUnchangeableAccessModuleNodeStack,
879
								textBuffer, UPDATEACCESSERROR);
880
					}
881
					// write text node into db
882
					endNodeId = writeTextForDBSAXNode(endNodeId, textBuffer, currentNode);
883
				}
884
				if (needToCheckAccessModule
885
						&& (processAdditionalAccess || processOtherAccess || processTopLevelAccess)) {
886
					// stored the pull out nodes into storedNode stack
887
					NodeRecord nodeElement = new NodeRecord(-2, -2, -2, "TEXT", null,
888
							null, MetacatUtil.normalize(textBuffer.toString()));
889
					storedAccessNodeStack.push(nodeElement);
890

    
891
				}
892
			}// if
893

    
894
			// set hitText false
895
			hitTextNode = false;
896
			// reset textbuff
897
			textBuffer = null;
898
			textBuffer = new StringBuffer();
899

    
900
			// hand sub stree stuff
901
			if (!subTreeInfoStack.empty()) {
902
				SubTree tree = subTreeInfoStack.peek();// get last
903
				// subtree
904
				if (tree != null && tree.getStartElementName() != null
905
						&& (tree.getStartElementName()).equals(currentTag)) {
906
					// find the end of sub tree and set the end node id
907
					tree.setEndNodeId(endNodeId);
908
					// add the subtree into the final store palace
909
					subTreeList.add(tree);
910
					// get rid of it from stack
911
					subTreeInfoStack.pop();
912
				}// if
913
			}// if
914

    
915
			// access stuff
916
			if (currentTag.equals(ALLOW) || currentTag.equals(DENY)) {
917
				// finish parser access rule and assign it to new one
918
				AccessRule newRule = accessRule;
919
				// add the new rule to access section object
920
				accessObject.addAccessRule(newRule);
921
				// reset access rule
922
				accessRule = null;
923
			} else if (currentTag.equals(ACCESS)) {
924
				// finish parsing an access section and assign it to new one
925
				DBSAXNode parentNode = (DBSAXNode) nodeStack.peek();
926

    
927
				accessObject.setEndNodeId(endNodeId);
928

    
929
				if (parentNode != null && parentNode.getTagName() != null
930
						&& parentNode.getTagName().equals(DISTRIBUTION)) {
931
					describesId.add(String.valueOf(distributionIndex));
932
					currentDistributionSection.setAccessSection(accessObject);
933
				}
934

    
935
				AccessSection newAccessObject = accessObject;
936

    
937
				if (newAccessObject != null) {
938

    
939
					// add the accessSection into a vector to store it
940
					// if it is not a reference, need to store it
941
					if (newAccessObject.getReferences() == null) {
942

    
943
						newAccessObject.setStoredTmpNodeStack(storedAccessNodeStack);
944
						accessObjectList.add(newAccessObject);
945
					}
946
					if (processTopLevelAccess) {
947

    
948
						// top level access control will handle whole document
949
						// -docid
950
						topLevelAccessControlMap.put(docid, newAccessObject);
951
						// reset processtopleveraccess tag
952

    
953
					}// if
954
					else if (processAdditionalAccess) {
955
						// for additional control put everything in describes
956
						// value
957
						// and access object into hash
958
						for (int i = 0; i < describesId.size(); i++) {
959

    
960
							String subId = describesId.elementAt(i);
961
							if (subId != null) {
962
								additionalAccessControlMap.put(subId, newAccessObject);
963
							}// if
964
						}// for
965
						// add this hashtable in to vector
966

    
967
						additionalAccessMapList.add(additionalAccessControlMap);
968
						// reset this hashtable in order to store another
969
						// additional
970
						// accesscontrol
971
						additionalAccessControlMap = null;
972
						additionalAccessControlMap = new Hashtable<String, AccessSection>();
973
					}// if
974

    
975
				}// if
976
				// check if access node stack is empty after parsing top access
977
				// module
978

    
979
				if (needToCheckAccessModule && processTopLevelAccess
980
						&& !currentUnchangeableAccessModuleNodeStack.isEmpty()) {
981

    
982
					logMetacat.error("Access node stack is not empty after "
983
							+ "parsing access subtree");
984
					throw new SAXException(UPDATEACCESSERROR);
985

    
986
				}
987
				// reset access section object
988

    
989
				accessObject = null;
990

    
991
				// reset tmp stored node stack
992
				storedAccessNodeStack = null;
993
				storedAccessNodeStack = new Stack<NodeRecord>();
994

    
995
				// reset flag
996
				processAdditionalAccess = false;
997
				processTopLevelAccess = false;
998
				processOtherAccess = false;
999

    
1000
			} else if (currentTag.equals(DISTRIBUTION)) {
1001
				// If the current Distribution is inline or data and it doesn't have an access section
1002
				// we use the top level access section (if it exists)
1003
				if ((currentDistributionSection.getDistributionType() == DistributionSection.DATA_DISTRIBUTION 
1004
						|| currentDistributionSection.getDistributionType() == DistributionSection.INLINE_DATA_DISTRIBUTION)
1005
						&& currentDistributionSection.getAccessSection() == null
1006
						&& topLevelAccessControlMap.size() > 0) {
1007
					
1008
					AccessSection accessSection = new AccessSection();
1009
					accessSection.setDocId(docid);	
1010
					AccessSection topLevelAccess = topLevelAccessControlMap.get(docid);
1011
					accessSection.setPermissionOrder(topLevelAccess.getPermissionOrder());
1012
					Vector<AccessRule> accessRuleList = topLevelAccess.getAccessRules();
1013
					for (AccessRule accessRule : accessRuleList) {
1014
						accessSection.addAccessRule(accessRule);
1015
					}
1016
					currentDistributionSection.setAccessSection(accessSection);
1017
				} 
1018
				if (currentDistributionSection.getAccessSection() != null) {
1019
					currentDistributionSection.getAccessSection().setDataFileName(currentDistributionSection.getDataFileName());
1020
				}
1021
				allDistributionSections.add(currentDistributionSection);
1022
				currentDistributionSection = null;
1023
				describesId = null;
1024
				describesId = new Vector<String>();
1025
			}
1026
		} else {
1027
			// this is in inline part
1028
			StringBuffer endElement = new StringBuffer();
1029
			endElement.append("</");
1030
			endElement.append(qName);
1031
			endElement.append(">");
1032
			logMetacat.debug("inline endElement: " + endElement.toString());
1033
			writeInlineDataIntoFile(inlineDataFileWriter, endElement);
1034
		}
1035
	}
1036

    
1037
	/**
1038
	 * SAX Handler that receives notification of comments in the DTD
1039
	 */
1040
	public void comment(char[] ch, int start, int length) throws SAXException {
1041
		logMetacat.debug("COMMENT");
1042
		if (!handleInlineData) {
1043
			if (!processingDTD) {
1044
				DBSAXNode currentNode = (DBSAXNode) nodeStack.peek();
1045
				String str = new String(ch, start, length);
1046

    
1047
				// compare top level access module
1048
				if (processTopLevelAccess && needToCheckAccessModule) {
1049
					compareCommentNode(currentUnchangeableAccessModuleNodeStack, str,
1050
							UPDATEACCESSERROR);
1051
				}
1052
				endNodeId = currentNode.writeChildNodeToDB("COMMENT", null, str, docid);
1053
				if (needToCheckAccessModule
1054
						&& (processAdditionalAccess || processOtherAccess || processTopLevelAccess)) {
1055
					// stored the pull out nodes into storedNode stack
1056
					NodeRecord nodeElement = new NodeRecord(-2, -2, -2, "COMMENT", null,
1057
							null, MetacatUtil.normalize(str));
1058
					storedAccessNodeStack.push(nodeElement);
1059

    
1060
				}
1061
			}
1062
		} else {
1063
			// inline data comment
1064
			StringBuffer inlineComment = new StringBuffer();
1065
			inlineComment.append("<!--");
1066
			inlineComment.append(new String(ch, start, length));
1067
			inlineComment.append("-->");
1068
			logMetacat.debug("inline data comment: " + inlineComment.toString());
1069
			writeInlineDataIntoFile(inlineDataFileWriter, inlineComment);
1070
		}
1071
	}
1072

    
1073
	/* Compare comment from xml and db */
1074
	private void compareCommentNode(Stack<NodeRecord> nodeStack, String string,
1075
			String error) throws SAXException {
1076
		NodeRecord node = null;
1077
		try {
1078
			node = nodeStack.pop();
1079
		} catch (EmptyStackException ee) {
1080
			logMetacat.error("the stack is empty for comment data");
1081
			throw new SAXException(error);
1082
		}
1083
		logMetacat.debug("current node type from xml is COMMENT");
1084
		logMetacat.debug("node type from stack: " + node.getNodeType());
1085
		logMetacat.debug("current node data from xml is: " + string);
1086
		logMetacat.debug("node data from stack: " + node.getNodeData());
1087
		logMetacat.debug("node is from stack: " + node.getNodeId());
1088
		// if not consistent terminate program and throw a exception
1089
		if (!node.getNodeType().equals("COMMENT") || !string.equals(node.getNodeData())) {
1090
			logMetacat.error("Inconsistence happened: ");
1091
			logMetacat.error("current node type from xml is COMMENT");
1092
			logMetacat.error("node type from stack: " + node.getNodeType());
1093
			logMetacat.error("current node data from xml is: " + string);
1094
			logMetacat.error("node data from stack: " + node.getNodeData());
1095
			logMetacat.error("node is from stack: " + node.getNodeId());
1096
			throw new SAXException(error);
1097
		}// if
1098
	}
1099

    
1100
	/**
1101
	 * SAX Handler called once for each processing instruction found: node that
1102
	 * PI may occur before or after the root element.
1103
	 */
1104
	public void processingInstruction(String target, String data) throws SAXException {
1105
		logMetacat.debug("PI");
1106
		if (!handleInlineData) {
1107
			DBSAXNode currentNode = (DBSAXNode) nodeStack.peek();
1108
			endNodeId = currentNode.writeChildNodeToDB("PI", target, data, docid);
1109
		} else {
1110
			StringBuffer inlinePI = new StringBuffer();
1111
			inlinePI.append("<?");
1112
			inlinePI.append(target);
1113
			inlinePI.append(" ");
1114
			inlinePI.append(data);
1115
			inlinePI.append("?>");
1116
			logMetacat.debug("inline data pi is: " + inlinePI.toString());
1117
			writeInlineDataIntoFile(inlineDataFileWriter, inlinePI);
1118
		}
1119
	}
1120

    
1121
	/** SAX Handler that is called at the start of Namespace */
1122
	public void startPrefixMapping(String prefix, String uri) throws SAXException {
1123
		logMetacat.debug("NAMESPACE");
1124
		if (!handleInlineData) {
1125
			namespaces.put(prefix, uri);
1126
		} else {
1127
			inlineDataNameSpace.put(prefix, uri);
1128
		}
1129
	}
1130

    
1131
	/**
1132
	 * SAX Handler that is called for each XML text node that is Ignorable white
1133
	 * space
1134
	 */
1135
	public void ignorableWhitespace(char[] cbuf, int start, int len) throws SAXException {
1136
		// When validation is turned "on", white spaces are reported here
1137
		// When validation is turned "off" white spaces are not reported here,
1138
		// but through characters() callback
1139
		logMetacat.debug("IGNORABLEWHITESPACE");
1140
		if (!handleInlineData) {
1141
			DBSAXNode currentNode = (DBSAXNode) nodeStack.peek();
1142
				String data = new String(cbuf, start, len);
1143
				// compare whitespace in access top module
1144
				if (processTopLevelAccess && needToCheckAccessModule) {
1145
					compareWhiteSpace(currentUnchangeableAccessModuleNodeStack, data,
1146
							UPDATEACCESSERROR);
1147
				}
1148
				// Write the content of the node to the database
1149
				if (needToCheckAccessModule
1150
						&& (processAdditionalAccess || processOtherAccess || processTopLevelAccess)) {
1151
					// stored the pull out nodes into storedNode stack
1152
					NodeRecord nodeElement = new NodeRecord(-2, -2, -2, "TEXT", null,
1153
							null, MetacatUtil.normalize(data));
1154
					storedAccessNodeStack.push(nodeElement);
1155

    
1156
				}
1157
				endNodeId = currentNode.writeChildNodeToDB("TEXT", null, data, docid);
1158
		} else {
1159
			// This is inline data write to file directly
1160
			StringBuffer inlineWhiteSpace = new StringBuffer(new String(cbuf, start, len));
1161
			writeInlineDataIntoFile(inlineDataFileWriter, inlineWhiteSpace);
1162
		}
1163

    
1164
	}
1165

    
1166
	/* Compare whitespace from xml and db */
1167
	private void compareWhiteSpace(Stack<NodeRecord> nodeStack, String string,
1168
			String error) throws SAXException {
1169
		NodeRecord node = null;
1170
		try {
1171
			node = nodeStack.pop();
1172
		} catch (EmptyStackException ee) {
1173
			logMetacat.error("the stack is empty for whitespace data");
1174
			throw new SAXException(error);
1175
		}
1176
		if (!node.getNodeType().equals("TEXT") || !string.equals(node.getNodeData())) {
1177
			logMetacat.error("Inconsistence happened: ");
1178
			logMetacat.error("current node type from xml is WHITESPACE TEXT");
1179
			logMetacat.error("node type from stack: " + node.getNodeType());
1180
			logMetacat.error("current node data from xml is: " + string);
1181
			logMetacat.error("node data from stack: " + node.getNodeData());
1182
			logMetacat.error("node is from stack: " + node.getNodeId());
1183
			throw new SAXException(error);
1184
		}// if
1185
	}
1186

    
1187
	/** SAX Handler that receives notification of end of the document */
1188
	public void endDocument() throws SAXException {
1189
		logMetacat.debug("end Document");
1190
		// There are some unchangable subtree didn't be compare
1191
		// This maybe cause user change the subtree id
1192
		if (!super.getIsRevisionDoc()) {
1193
			// write access rule to db
1194
			writeAccessRuleToDB();
1195
			// delete relation table
1196
			deleteRelations();
1197
			// write relations
1198
			for (int i = 0; i < onlineDataFileIdInRelationVector.size(); i++) {
1199
				String id = onlineDataFileIdInRelationVector.elementAt(i);
1200
				writeOnlineDataFileIdIntoRelationTable(id);
1201
			}
1202
		}
1203
	}
1204

    
1205
	/* The method to write all access rule into db */
1206
	private void writeAccessRuleToDB() throws SAXException {
1207
		// Delete old permssion
1208
		deletePermissionsInAccessTable(docid);
1209
		// write top leve access rule
1210
		writeTopLevelAccessRuleToDB();
1211
		// write additional access rule
1212
		// writeAdditionalAccessRuleToDB();
1213
		writeAdditionalAccessRulesToDB();
1214
	}// writeAccessRuleToDB
1215

    
1216
	/* The method to write top level access rule into db. */
1217
	private void writeTopLevelAccessRuleToDB() throws SAXException {
1218
		// for top document level
1219
		AccessSection accessSection = topLevelAccessControlMap.get(docid);
1220
		boolean top = true;
1221
		String subSectionId = null;
1222
		if (accessSection != null) {
1223
			AccessSection accessSectionObj = accessSection;
1224

    
1225
			// if accessSection is not null and is not reference
1226
			if (accessSectionObj.getReferences() == null) {
1227
				// write the top level access module into xml_accesssubtree to
1228
				// store info and then when update to check if the user can
1229
				// update it or not
1230
				deleteAccessSubTreeRecord(docid);
1231
				writeAccessSubTreeIntoDB(accessSectionObj, TOPLEVEL);
1232

    
1233
				// write access section into xml_access table
1234
				writeGivenAccessRuleIntoDB(accessSectionObj, top, subSectionId);
1235
				// write online data file into xml_access too.
1236
				// for (int i= 0; i <onlineDataFileIdInTopAccessVector.size();
1237
				// i++)
1238
				// {
1239
				// String id = onlineDataFileIdInTopAccessVector.elementAt(i);
1240
				// writeAccessRuleForRelatedDataFileIntoDB(accessSectionObj,
1241
				// id);
1242
				// }
1243

    
1244
			} else {
1245

    
1246
				// this is a reference and go trough the vector which contains
1247
				// all access object
1248
				String referenceId = accessSectionObj.getReferences();
1249
				boolean findAccessObject = false;
1250
				logMetacat.debug("referered id for top access: " + referenceId);
1251
				for (int i = 0; i < accessObjectList.size(); i++) {
1252
					AccessSection accessObj = accessObjectList.elementAt(i);
1253
					String accessObjId = accessObj.getSubTreeId();
1254
					if (referenceId != null && accessObj != null
1255
							&& referenceId.equals(accessObjId)) {
1256
						// make sure the user didn't change any thing in this
1257
						// access moduel
1258
						// too if user doesn't have all permission
1259
						if (needToCheckAccessModule) {
1260

    
1261
							Stack<NodeRecord> newStack = accessObj
1262
									.getStoredTmpNodeStack();
1263
							// revise order
1264
							newStack = DocumentUtil.reviseStack(newStack);
1265
							// go throught the vector of
1266
							// unChangeableAccessSubtreevector
1267
							// and find the one whose id is as same as
1268
							// referenceid
1269
							AccessSection oldAccessObj = getAccessSectionFromUnchangableAccessVector(referenceId);
1270
							// if oldAccessObj is null something is wrong
1271
							if (oldAccessObj == null) {
1272
								throw new SAXException(UPDATEACCESSERROR);
1273
							}// if
1274
							else {
1275
								// Get the node stack from old access obj
1276
								Stack<NodeRecord> oldStack = oldAccessObj
1277
										.getSubTreeNodeStack();
1278
								compareNodeStacks(newStack, oldStack);
1279
							}// else
1280
						}// if
1281
						// write accessobject into db
1282
						writeGivenAccessRuleIntoDB(accessObj, top, subSectionId);
1283
						// write online data file into xml_access too.
1284
						// for (int j= 0; j
1285
						// <onlineDataFileIdInTopAccessVector.size(); j++)
1286
						// {
1287
						// String id =
1288
						// onlineDataFileIdInTopAccessVector.elementAt(j);
1289
						// writeAccessRuleForRelatedDataFileIntoDB(accessSectionObj,
1290
						// id);
1291
						// }
1292

    
1293
						// write the reference access into xml_accesssubtree
1294
						// too write the top level access module into
1295
						// xml_accesssubtree to store info and then when update
1296
						// to check if the user can update it or not
1297
						deleteAccessSubTreeRecord(docid);
1298
						writeAccessSubTreeIntoDB(accessSectionObj, TOPLEVEL);
1299
						writeAccessSubTreeIntoDB(accessObj, SUBTREELEVEL);
1300
						findAccessObject = true;
1301
						break;
1302
					}
1303
				}// for
1304
				// if we couldn't find an access subtree id for this reference
1305
				// id
1306
				if (!findAccessObject) {
1307
					throw new SAXException("The referenceid: " + referenceId
1308
							+ " is not access subtree");
1309
				}// if
1310
			}// else
1311

    
1312
		}// if
1313
		else {
1314
			// couldn't find a access section object
1315
			logMetacat.warn("couldn't find access control for document: " + docid);
1316
		}
1317

    
1318
	}// writeTopLevelAccessRuletoDB
1319

    
1320
	/* Given a subtree id and find the responding access section */
1321
	private AccessSection getAccessSectionFromUnchangableAccessVector(String id) {
1322
		AccessSection result = null;
1323
		// Makse sure the id
1324
		if (id == null || id.equals("")) {
1325
			return result;
1326
		}
1327
		// go throught vector and find the list
1328
		for (int i = 0; i < unChangeableAccessSubTreeVector.size(); i++) {
1329
			AccessSection accessObj = unChangeableAccessSubTreeVector.elementAt(i);
1330
			if (accessObj.getSubTreeId() != null && (accessObj.getSubTreeId()).equals(id)) {
1331
				result = accessObj;
1332
			}// if
1333
		}// for
1334
		return result;
1335
	}// getAccessSectionFromUnchangableAccessVector
1336

    
1337
	/* Compare two node stacks to see if they are same */
1338
	private void compareNodeStacks(Stack<NodeRecord> stack1, Stack<NodeRecord> stack2)
1339
			throws SAXException {
1340
		// make sure stack1 and stack2 are not empty
1341
		if (stack1.isEmpty() || stack2.isEmpty()) {
1342
			logMetacat.error("Because stack is empty!");
1343
			throw new SAXException(UPDATEACCESSERROR);
1344
		}
1345
		// go throw two stacks and compare every element
1346
		while (!stack1.isEmpty()) {
1347
			// Pop an element from stack1
1348
			NodeRecord record1 = stack1.pop();
1349
			// Pop an element from stack2(stack 2 maybe empty)
1350
			NodeRecord record2 = null;
1351
			try {
1352
				record2 = stack2.pop();
1353
			} catch (EmptyStackException ee) {
1354
				logMetacat.error("Node stack2 is empty but stack1 isn't!");
1355
				throw new SAXException(UPDATEACCESSERROR);
1356
			}
1357
			// if two records are not same throw a exception
1358
			if (!record1.contentEquals(record2)) {
1359
				logMetacat.error("Two records from new and old stack are not " + "same!");
1360
				throw new SAXException(UPDATEACCESSERROR);
1361
			}// if
1362
		}// while
1363

    
1364
		// now stack1 is empty and we should make sure stack2 is empty too
1365
		if (!stack2.isEmpty()) {
1366
			logMetacat
1367
					.error("stack2 still has some elements while stack " + "is empty! ");
1368
			throw new SAXException(UPDATEACCESSERROR);
1369
		}// if
1370
	}// comparingNodeStacks
1371

    
1372
	/* The method to write additional access rule into db. */
1373
	private void writeAdditionalAccessRulesToDB() throws SAXException {
1374
		
1375
		// Iterate through every distribution and write access sections for data and inline
1376
		// types to the database
1377
		for (DistributionSection distributionSection : allDistributionSections) {			
1378
			// we're only interested in data and inline distributions
1379
			int distributionType = distributionSection.getDistributionType();
1380
			if (distributionType == DistributionSection.DATA_DISTRIBUTION
1381
					|| distributionType == DistributionSection.INLINE_DATA_DISTRIBUTION) {
1382
				AccessSection accessSection = distributionSection.getAccessSection();
1383
				
1384
				// If the distribution doesn't have an access section, we continue.
1385
				if (accessSection == null) {
1386
					continue;		
1387
				} 
1388
				
1389
				// We want to check file permissions for all online data updates and inserts, or for 
1390
				// inline updates.
1391
//				if (distributionType == DistributionSection.DATA_DISTRIBUTION
1392
//						|| (distributionType == DistributionSection.INLINE_DATA_DISTRIBUTION && action == "UPDATE")) {
1393

    
1394
				if (distributionType == DistributionSection.DATA_DISTRIBUTION) {
1395
					try {
1396
						PermissionController controller = new PermissionController(
1397
								distributionSection.getDataFileName(), false);
1398
						if (AccessionNumber.accNumberUsed(docid)
1399
								&& !controller.hasPermission(user, groups, "WRITE")) {
1400
							throw new SAXException(UPDATEACCESSERROR);
1401
						}
1402
					} catch (SQLException sqle) {
1403
						throw new SAXException(
1404
								"Database error checking user permissions: "
1405
										+ sqle.getMessage());
1406
					} catch (Exception e) {
1407
						throw new SAXException(
1408
								"General error checking user permissions: "
1409
										+ e.getMessage());
1410
					}
1411
				} else if (distributionType == DistributionSection.INLINE_DATA_DISTRIBUTION && action == "UPDATE") {
1412
					try {
1413
						PermissionController controller = new PermissionController(
1414
								docid, false);
1415

    
1416
						if (!controller.hasPermission(user, groups, "WRITE")) {
1417
							throw new SAXException(UPDATEACCESSERROR);
1418
						}
1419
					} catch (SQLException sqle) {
1420
						throw new SAXException(
1421
								"Database error checking user permissions: "
1422
										+ sqle.getMessage());
1423
					} catch (Exception e) {
1424
						throw new SAXException(
1425
								"General error checking user permissions: "
1426
										+ e.getMessage());
1427
					}
1428
				}
1429
				
1430
				String subSectionId = Integer.toString(distributionSection.getDistributionId());
1431
				writeGivenAccessRuleIntoDB(accessSection, false, subSectionId);
1432
			}
1433

    
1434
		}
1435
		
1436
		
1437
	}
1438

    
1439
	/* Write a given access rule into db */
1440
	private void writeGivenAccessRuleIntoDB(AccessSection accessSection,
1441
			boolean topLevel, String subSectionId) throws SAXException {
1442
		if (accessSection == null) {
1443
			throw new SAXException("The access object is null");
1444
		}
1445

    
1446
		String permOrder = accessSection.getPermissionOrder();
1447
		String sql = null;
1448
		PreparedStatement pstmt = null;
1449
		if (topLevel) {
1450
			sql = "INSERT INTO xml_access (docid, principal_name, permission, "
1451
					+ "perm_type, perm_order, accessfileid) VALUES "
1452
					+ " (?, ?, ?, ?, ?, ?)";
1453
		} else {
1454
			sql = "INSERT INTO xml_access (docid,principal_name, "
1455
					+ "permission, perm_type, perm_order, accessfileid, subtreeid"
1456
					+ ") VALUES" + " (?, ?, ?, ?, ?, ?, ?)";
1457
		}
1458
		try {
1459

    
1460
			pstmt = connection.prepareStatement(sql);
1461
			// Increase DBConnection usage count
1462
			connection.increaseUsageCount(1);
1463
			// Bind the values to the query
1464
			pstmt.setString(6, docid);
1465
			logMetacat.debug("Accessfileid in accesstable: " + docid);
1466
			pstmt.setString(5, permOrder);
1467
			logMetacat.debug("PermOder in accesstable: " + permOrder);
1468
			// if it is not top level, set subsection id
1469
			if (topLevel) {
1470
				pstmt.setString(1, docid);
1471
				logMetacat.debug("Docid in accesstable: " + docid);
1472
			}
1473
			if (!topLevel) {
1474
				pstmt.setString(1, accessSection.getDataFileName());
1475
				logMetacat.debug("Docid in accesstable: " + inlineDataFileName);
1476

    
1477
				// for subtree should specify the
1478
				if (subSectionId == null) {
1479
					throw new SAXException("The subsection is null");
1480
				}
1481

    
1482
				pstmt.setString(7, subSectionId);
1483
				logMetacat.debug("SubSectionId in accesstable: " + subSectionId);
1484
			}
1485

    
1486
			Vector<AccessRule> accessRules = accessSection.getAccessRules();
1487
			// go through every rule
1488
			for (int i = 0; i < accessRules.size(); i++) {
1489
				AccessRule rule = accessRules.elementAt(i);
1490
				String permType = rule.getPermissionType();
1491
				int permission = rule.getPermission();
1492
				pstmt.setInt(3, permission);
1493
				logMetacat.debug("permission in accesstable: " + permission);
1494
				pstmt.setString(4, permType);
1495
				logMetacat.debug("Permtype in accesstable: " + permType);
1496
				// go through every principle in rule
1497
				Vector<String> nameVector = rule.getPrincipal();
1498
				for (int j = 0; j < nameVector.size(); j++) {
1499
					String prName = nameVector.elementAt(j);
1500
					pstmt.setString(2, prName);
1501
					logMetacat.debug("Principal in accesstable: " + prName);
1502
					logMetacat.debug("running sql: " + pstmt.toString());
1503
					pstmt.execute();
1504
				}// for
1505
			}// for
1506
			pstmt.close();
1507
		}// try
1508
		catch (SQLException e) {
1509
			throw new SAXException("EMLSAXHandler.writeAccessRuletoDB(): "
1510
					+ e.getMessage());
1511
		}// catch
1512
		finally {
1513
			try {
1514
				pstmt.close();
1515
			} catch (SQLException ee) {
1516
				throw new SAXException("EMLSAXHandler.writeAccessRuletoDB(): "
1517
						+ ee.getMessage());
1518
			}
1519
		}// finally
1520

    
1521
	}// writeGivenAccessRuleIntoDB
1522

    
1523
	/* Write a gaven access rule into db */
1524
	private void writeAccessRuleForRelatedDataFileIntoDB(AccessSection accessSection,
1525
			String dataId) throws SAXException {
1526
		if (accessSection == null) {
1527
			throw new SAXException("The access object is null");
1528
		}
1529
		// get rid of rev from dataId
1530
		// dataId = MetacatUtil.getDocIdFromString(dataId);
1531
		String permOrder = accessSection.getPermissionOrder();
1532
		String sql = null;
1533
		PreparedStatement pstmt = null;
1534
		sql = "INSERT INTO xml_access (docid, principal_name, permission, "
1535
				+ "perm_type, perm_order, accessfileid) VALUES " + " (?, ?, ?, ?, ?, ?)";
1536

    
1537
		try {
1538

    
1539
			pstmt = connection.prepareStatement(sql);
1540
			// Increase DBConnection usage count
1541
			connection.increaseUsageCount(1);
1542
			// Bind the values to the query
1543
			pstmt.setString(1, dataId);
1544
			logMetacat.debug("Docid in accesstable: " + docid);
1545
			pstmt.setString(6, docid);
1546
			logMetacat.debug("Accessfileid in accesstable: " + docid);
1547
			pstmt.setString(5, permOrder);
1548
			logMetacat.debug("PermOder in accesstable: " + permOrder);
1549
			// if it is not top level, set subsection id
1550

    
1551
			Vector<AccessRule> accessRules = accessSection.getAccessRules();
1552
			// go through every rule
1553
			for (int i = 0; i < accessRules.size(); i++) {
1554
				AccessRule rule = accessRules.elementAt(i);
1555
				String permType = rule.getPermissionType();
1556
				int permission = rule.getPermission();
1557
				pstmt.setInt(3, permission);
1558
				logMetacat.debug("permission in accesstable: " + permission);
1559
				pstmt.setString(4, permType);
1560
				logMetacat.debug("Permtype in accesstable: " + permType);
1561
				// go through every principle in rule
1562
				Vector<String> nameVector = rule.getPrincipal();
1563
				for (int j = 0; j < nameVector.size(); j++) {
1564
					String prName = nameVector.elementAt(j);
1565
					pstmt.setString(2, prName);
1566
					logMetacat.debug("Principal in accesstable: " + prName);
1567
					logMetacat.debug("running sql: " + pstmt.toString());
1568
					pstmt.execute();
1569
				}// for
1570
			}// for
1571
			pstmt.close();
1572
		}// try
1573
		catch (SQLException e) {
1574
			throw new SAXException("EMLSAXHandler.writeAccessRuletoDB(): "
1575
					+ e.getMessage());
1576
		}// catch
1577
		finally {
1578
			try {
1579
				pstmt.close();
1580
			} catch (SQLException ee) {
1581
				throw new SAXException("EMLSAXHandler.writeAccessRuletoDB(): "
1582
						+ ee.getMessage());
1583
			}
1584
		}// finally
1585

    
1586
	}// writeAccessRuleForRalatedDataFileIntoDB
1587

    
1588
	/* Delete from db all permission for resources related to @aclid if any. */
1589
	private void deletePermissionsInAccessTable(String aclid) throws SAXException {
1590
		Statement stmt = null;
1591
		try {
1592
			// delete all acl records for resources related to @aclid if any
1593
			stmt = connection.createStatement();
1594
			// Increase DBConnection usage count
1595
			connection.increaseUsageCount(1);
1596
			logMetacat.debug("running sql: DELETE FROM xml_access WHERE accessfileid = '"
1597
					+ aclid + "'");
1598
			stmt.execute("DELETE FROM xml_access WHERE accessfileid = '" + aclid + "'");
1599

    
1600
		} catch (SQLException e) {
1601
			throw new SAXException(e.getMessage());
1602
		} finally {
1603
			try {
1604
				stmt.close();
1605
			} catch (SQLException ee) {
1606
				throw new SAXException(ee.getMessage());
1607
			}
1608
		}
1609
	}// deletePermissionsInAccessTable
1610

    
1611
	/*
1612
	 * In order to make sure only usr has "all" permission can update access
1613
	 * subtree in eml document we need to keep access subtree info in
1614
	 * xml_accesssubtree table, such as docid, version, startnodeid, endnodeid
1615
	 */
1616
	private void writeAccessSubTreeIntoDB(AccessSection accessSection, String level)
1617
			throws SAXException {
1618
		if (accessSection == null) {
1619
			throw new SAXException("The access object is null");
1620
		}
1621

    
1622
		String sql = null;
1623
		PreparedStatement pstmt = null;
1624
		sql = "INSERT INTO xml_accesssubtree (docid, rev, controllevel, "
1625
				+ "subtreeid, startnodeid, endnodeid) VALUES " + " (?, ?, ?, ?, ?, ?)";
1626
		try {
1627

    
1628
			pstmt = connection.prepareStatement(sql);
1629
			// Increase DBConnection usage count
1630
			connection.increaseUsageCount(1);
1631
			long startNodeId = accessSection.getStartNodeId();
1632
			long endNodeId = accessSection.getEndNodeId();
1633
			String sectionId = accessSection.getSubTreeId();
1634
			// Bind the values to the query
1635
			pstmt.setString(1, docid);
1636
			logMetacat.debug("Docid in access-subtreetable: " + docid);
1637
			pstmt.setLong(2, (new Long(revision)).longValue());
1638
			logMetacat.debug("rev in accesssubtreetable: " + revision);
1639
			pstmt.setString(3, level);
1640
			logMetacat.debug("contorl level in access-subtree table: " + level);
1641
			pstmt.setString(4, sectionId);
1642
			logMetacat.debug("Subtree id in access-subtree table: " + sectionId);
1643
			pstmt.setLong(5, startNodeId);
1644
			logMetacat.debug("Start node id is: " + startNodeId);
1645
			pstmt.setLong(6, endNodeId);
1646
			logMetacat.debug("End node id is: " + endNodeId);
1647
			logMetacat.debug("running sql: " + pstmt.toString());
1648
			pstmt.execute();
1649
			pstmt.close();
1650
		}// try
1651
		catch (SQLException e) {
1652
			throw new SAXException("EMLSAXHandler.writeAccessSubTreeIntoDB(): "
1653
					+ e.getMessage());
1654
		}// catch
1655
		finally {
1656
			try {
1657
				pstmt.close();
1658
			} catch (SQLException ee) {
1659
				throw new SAXException("EMLSAXHandler.writeAccessSubTreeIntoDB(): "
1660
						+ ee.getMessage());
1661
			}
1662
		}// finally
1663

    
1664
	}// writeAccessSubtreeIntoDB
1665

    
1666
	/* Delete every access subtree record from xml_accesssubtree. */
1667
	private void deleteAccessSubTreeRecord(String docId) throws SAXException {
1668
		Statement stmt = null;
1669
		try {
1670
			// delete all acl records for resources related to @aclid if any
1671
			stmt = connection.createStatement();
1672
			// Increase DBConnection usage count
1673
			connection.increaseUsageCount(1);
1674
			logMetacat.debug("running sql: DELETE FROM xml_accesssubtree WHERE docid = '"
1675
					+ docId + "'");
1676
			stmt.execute("DELETE FROM xml_accesssubtree WHERE docid = '" + docId + "'");
1677

    
1678
		} catch (SQLException e) {
1679
			throw new SAXException(e.getMessage());
1680
		} finally {
1681
			try {
1682
				stmt.close();
1683
			} catch (SQLException ee) {
1684
				throw new SAXException(ee.getMessage());
1685
			}
1686
		}
1687
	}// deleteAccessSubTreeRecord
1688

    
1689
	// open a file writer for writing inline data to file
1690
	private FileWriter createInlineDataFileWriter(String fileName) throws SAXException {
1691
		FileWriter writer = null;
1692
		String path;
1693
		try {
1694
			path = PropertyService.getProperty("application.inlinedatafilepath");
1695
		} catch (PropertyNotFoundException pnfe) {
1696
			throw new SAXException(pnfe.getMessage());
1697
		}
1698
		/*
1699
		 * File inlineDataDirectory = new File(path);
1700
		 */
1701
		String newFile = path + "/" + fileName;
1702
		logMetacat.debug("inline file name: " + newFile);
1703
		try {
1704
			// true means append
1705
			writer = new FileWriter(newFile, true);
1706
		} catch (IOException ioe) {
1707
			throw new SAXException(ioe.getMessage());
1708
		}
1709
		return writer;
1710
	}
1711

    
1712
	// write inline data into file system and return file name(without path)
1713
	private void writeInlineDataIntoFile(FileWriter writer, StringBuffer data)
1714
			throws SAXException {
1715
		try {
1716
			writer.write(data.toString());
1717
			writer.flush();
1718
		} catch (Exception e) {
1719
			throw new SAXException(e.getMessage());
1720
		}
1721
	}
1722

    
1723
	/*
1724
	 * In eml2, the inline data wouldn't store in db, it store in file system
1725
	 * The db stores file name(without path). We got the old file name from db
1726
	 * and compare to the new in line data file
1727
	 */
1728
	public boolean compareInlineDataFiles(String oldFileName, String newFileName)
1729
			throws McdbException {
1730
		// this method need to be testing
1731
		boolean same = true;
1732
		String data = null;
1733
		try {
1734
			String path = PropertyService.getProperty("application.inlinedatafilepath");
1735
			// the new file name will look like path/docid.rev.2
1736
			File inlineDataDirectory = new File(path);
1737
			File oldDataFile = new File(inlineDataDirectory, oldFileName);
1738
			File newDataFile = new File(inlineDataDirectory, newFileName);
1739

    
1740
			FileReader oldFileReader = new FileReader(oldDataFile);
1741
			BufferedReader oldStringReader = new BufferedReader(oldFileReader);
1742
			FileReader newFileReader = new FileReader(newDataFile);
1743
			BufferedReader newStringReader = new BufferedReader(newFileReader);
1744
			// read first line of data
1745
			String oldString = oldStringReader.readLine();
1746
			String newString = newStringReader.readLine();
1747

    
1748
			// at the end oldstring will be null
1749
			while (oldString != null) {
1750
				oldString = oldStringReader.readLine();
1751
				newString = newStringReader.readLine();
1752
				if (!oldString.equals(newString)) {
1753
					same = false;
1754
					break;
1755
				}
1756
			}
1757

    
1758
			// if oldString is null but newString is not null, they are same
1759
			if (same) {
1760
				if (newString != null) {
1761
					same = false;
1762
				}
1763
			}
1764

    
1765
		} catch (Exception e) {
1766
			throw new McdbException(e.getMessage());
1767
		}
1768
		logMetacat.debug("the inline data retrieved from file: " + data);
1769
		return same;
1770
	}
1771

    
1772
	// if xml file failed to upload, we need to call this method to delete
1773
	// the inline data already in file system
1774
	public void deleteInlineFiles() throws SAXException {
1775
		if (!inlineFileIdList.isEmpty()) {
1776
			for (int i = 0; i < inlineFileIdList.size(); i++) {
1777
				String fileName = inlineFileIdList.elementAt(i);
1778
				deleteInlineDataFile(fileName);
1779
			}
1780
		}
1781
	}
1782

    
1783
	/* delete the inline data file */
1784
	private void deleteInlineDataFile(String fileName) throws SAXException {
1785
		String path;
1786
		try {
1787
			path = PropertyService.getProperty("application.inlinedatafilepath");
1788
		} catch (PropertyNotFoundException pnfe) {
1789
			throw new SAXException("Could not find inline data file path: "
1790
					+ pnfe.getMessage());
1791
		}
1792
		File inlineDataDirectory = new File(path);
1793
		File newFile = new File(inlineDataDirectory, fileName);
1794
		newFile.delete();
1795

    
1796
	}
1797

    
1798
	/*
1799
	 * In eml2, the inline data wouldn't store in db, it store in file system
1800
	 * The db stores file name(without path).
1801
	 */
1802
	public static Reader readInlineDataFromFileSystem(String fileName)
1803
			throws McdbException {
1804
		// BufferedReader stringReader = null;
1805
		FileReader fileReader = null;
1806
		try {
1807
			String path = PropertyService.getProperty("application.inlinedatafilepath");
1808
			// the new file name will look like path/docid.rev.2
1809
			File inlineDataDirectory = new File(path);
1810
			File dataFile = new File(inlineDataDirectory, fileName);
1811

    
1812
			fileReader = new FileReader(dataFile);
1813
			// stringReader = new BufferedReader(fileReader);
1814
		} catch (Exception e) {
1815
			throw new McdbException(e.getMessage());
1816
		}
1817
		// return stringReader;
1818
		return fileReader;
1819
	}
1820

    
1821
	/* Delete relations */
1822
	private void deleteRelations() throws SAXException {
1823
		PreparedStatement pStmt = null;
1824
		String sql = "DELETE FROM xml_relation where docid =?";
1825
		try {
1826
			pStmt = connection.prepareStatement(sql);
1827
			// bind variable
1828
			pStmt.setString(1, docid);
1829
			// execute query
1830
			pStmt.execute();
1831
			pStmt.close();
1832
		}// try
1833
		catch (SQLException e) {
1834
			throw new SAXException("EMLSAXHandler.deleteRelations(): " + e.getMessage());
1835
		}// catch
1836
		finally {
1837
			try {
1838
				pStmt.close();
1839
			}// try
1840
			catch (SQLException ee) {
1841
				throw new SAXException("EMLSAXHandler.deleteRelations: "
1842
						+ ee.getMessage());
1843
			}// catch
1844
		}// finally
1845
	}
1846

    
1847
	/*
1848
	 * Write an online data file id into xml_relation table. The dataId
1849
	 * shouldnot have the revision
1850
	 */
1851
	private void writeOnlineDataFileIdIntoRelationTable(String dataId)
1852
			throws SAXException {
1853
		PreparedStatement pStmt = null;
1854
		String sql = "INSERT into xml_relation (docid, packagetype, subject, "
1855
				+ "relationship, object) values (?, ?, ?, ?, ?)";
1856
		try {
1857
			pStmt = connection.prepareStatement(sql);
1858
			// bind variable
1859
			pStmt.setString(1, docid);
1860
			pStmt.setString(2, DocumentImpl.EML2_1_0NAMESPACE);
1861
			pStmt.setString(3, docid);
1862
			pStmt.setString(4, RELATION);
1863
			pStmt.setString(5, dataId);
1864
			// execute query
1865
			pStmt.execute();
1866
			pStmt.close();
1867
		}// try
1868
		catch (SQLException e) {
1869
			throw new SAXException(
1870
					"EMLSAXHandler.writeOnlineDataFileIdIntoRelationTable(): "
1871
							+ e.getMessage());
1872
		}// catch
1873
		finally {
1874
			try {
1875
				pStmt.close();
1876
			}// try
1877
			catch (SQLException ee) {
1878
				throw new SAXException(
1879
						"EMLSAXHandler.writeOnlineDataFileIdIntoRelationTable(): "
1880
								+ ee.getMessage());
1881
			}// catch
1882
		}// finally
1883

    
1884
	}// writeOnlineDataFileIdIntoRelationTable
1885

    
1886
	/*
1887
	 * This method will handle data file in online url. If the data file is in
1888
	 * ecogrid protocol, then the datafile identifier(without rev)should be put
1889
	 * into onlineDataFileRelationVector. The docid in this vector will be
1890
	 * insert into xml_relation table in endDocument(). If the data file doesn't
1891
	 * exsit in xml_documents or xml_revision table, or the user has all
1892
	 * permission to the data file if the docid already existed, the data file
1893
	 * id (without rev)will be put into onlineDataFileTopAccessVector. The top
1894
	 * access rules specified in this eml document will apply to the data file.
1895
	 * NEED to do: We should also need to implement http and ftp. Those external
1896
	 * files should be download and assign a data file id to it.
1897
	 */
1898
	private void handleOnlineUrlDataFile(String url) throws SAXException {
1899
		logMetacat.warn("The url is " + url);
1900

    
1901
		if (currentDistributionSection == null) {
1902
			throw new SAXException("Trying to set the online file name for a null"
1903
					+ " distribution section");
1904
		}
1905

    
1906
		// if the url is not in ecogrid protocol, null will be returned
1907
		String accessionNumber = DocumentUtil.getAccessionNumberFromEcogridIdentifier(url);
1908
		if (accessionNumber == null) {
1909
			// the accession number is null if the url does not references a
1910
			// local data file (url would start with "ecogrid://"
1911
			currentDistributionSection
1912
					.setDistributionType(DistributionSection.ONLINE_DATA_DISTRIBUTION);
1913
		} else {
1914
			// handle ecogrid protocol
1915
			// get rid of revision number to get the docid.
1916
			String docid = DocumentUtil.getDocIdFromAccessionNumber(accessionNumber);
1917

    
1918
			currentDistributionSection
1919
					.setDistributionType(DistributionSection.DATA_DISTRIBUTION);
1920
			currentDistributionSection.setDataFileName(docid);
1921

    
1922
			// distributionOnlineFileName = docid;
1923
			onlineDataFileIdInRelationVector.add(docid);
1924
			try {				
1925
				if (!AccessionNumber.accNumberUsed(docid)) {
1926
					onlineDataFileIdInTopAccessVector.add(docid);
1927
				} else {
1928
					PermissionController controller = new PermissionController(accessionNumber);				
1929
					if (controller.hasPermission(user, groups,AccessControlInterface.ALLSTRING)) {
1930
						onlineDataFileIdInTopAccessVector.add(docid);
1931
					} else {
1932
						throw new SAXException(UPDATEACCESSERROR);
1933
					}
1934
				} 
1935
			}// try
1936
			catch (Exception e) {
1937
				logMetacat.error("Eorr in "
1938
								+ "Eml210SAXHanlder.handleOnlineUrlDataFile is "
1939
								+ e.getMessage());
1940
				throw new SAXException(e.getMessage());
1941
			}
1942
		}
1943
	}
1944
}
(33-33/61)