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-01-25 15:02:25 -0800 (Mon, 25 Jan 2010) $'
11
 * '$Revision: 5200 $'
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("Doc ID " + docid + " was deleted and cannot be updated."));
435
					} else {
436
						throw (new SAXException("Doc ID " + docid + " was not found and cannot be updated.")); 
437
					}
438
				} catch (Exception e) {
439
					throw (new SAXException("Error in EMLSaxHandler.startElement "
440
							+ action, e));
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 = null;
1143
			int leftover = len;
1144
			int offset = start;
1145
			boolean moredata = true;
1146

    
1147
			// This loop deals with the case where there are more characters
1148
			// than can fit in a single database text field (limit is
1149
			// MAXDATACHARS). If the text to be inserted exceeds MAXDATACHARS,
1150
			// write a series of nodes that are MAXDATACHARS long, and then the
1151
			// final node contains the remainder
1152
			while (moredata) {
1153
				if (leftover > MAXDATACHARS) {
1154
					data = new String(cbuf, offset, MAXDATACHARS);
1155
					leftover -= MAXDATACHARS;
1156
					offset += MAXDATACHARS;
1157
				} else {
1158
					data = new String(cbuf, offset, leftover);
1159
					moredata = false;
1160
				}
1161

    
1162
				// compare whitespace in access top module
1163
				if (processTopLevelAccess && needToCheckAccessModule) {
1164
					compareWhiteSpace(currentUnchangeableAccessModuleNodeStack, data,
1165
							UPDATEACCESSERROR);
1166
				}
1167
				// Write the content of the node to the database
1168
				if (needToCheckAccessModule
1169
						&& (processAdditionalAccess || processOtherAccess || processTopLevelAccess)) {
1170
					// stored the pull out nodes into storedNode stack
1171
					NodeRecord nodeElement = new NodeRecord(-2, -2, -2, "TEXT", null,
1172
							null, MetacatUtil.normalize(data));
1173
					storedAccessNodeStack.push(nodeElement);
1174

    
1175
				}
1176
				endNodeId = currentNode.writeChildNodeToDB("TEXT", null, data, docid);
1177
			}
1178
		} else {
1179
			// This is inline data write to file directly
1180
			StringBuffer inlineWhiteSpace = new StringBuffer(new String(cbuf, start, len));
1181
			writeInlineDataIntoFile(inlineDataFileWriter, inlineWhiteSpace);
1182
		}
1183

    
1184
	}
1185

    
1186
	/* Compare whitespace from xml and db */
1187
	private void compareWhiteSpace(Stack<NodeRecord> nodeStack, String string,
1188
			String error) throws SAXException {
1189
		NodeRecord node = null;
1190
		try {
1191
			node = nodeStack.pop();
1192
		} catch (EmptyStackException ee) {
1193
			logMetacat.error("the stack is empty for whitespace data");
1194
			throw new SAXException(error);
1195
		}
1196
		if (!node.getNodeType().equals("TEXT") || !string.equals(node.getNodeData())) {
1197
			logMetacat.error("Inconsistence happened: ");
1198
			logMetacat.error("current node type from xml is WHITESPACE TEXT");
1199
			logMetacat.error("node type from stack: " + node.getNodeType());
1200
			logMetacat.error("current node data from xml is: " + string);
1201
			logMetacat.error("node data from stack: " + node.getNodeData());
1202
			logMetacat.error("node is from stack: " + node.getNodeId());
1203
			throw new SAXException(error);
1204
		}// if
1205
	}
1206

    
1207
	/** SAX Handler that receives notification of end of the document */
1208
	public void endDocument() throws SAXException {
1209
		logMetacat.debug("end Document");
1210
		// There are some unchangable subtree didn't be compare
1211
		// This maybe cause user change the subtree id
1212
		if (!super.getIsRevisionDoc()) {
1213
			// write access rule to db
1214
			writeAccessRuleToDB();
1215
			// delete relation table
1216
			deleteRelations();
1217
			// write relations
1218
			for (int i = 0; i < onlineDataFileIdInRelationVector.size(); i++) {
1219
				String id = onlineDataFileIdInRelationVector.elementAt(i);
1220
				writeOnlineDataFileIdIntoRelationTable(id);
1221
			}
1222
		}
1223
	}
1224

    
1225
	/* The method to write all access rule into db */
1226
	private void writeAccessRuleToDB() throws SAXException {
1227
		// Delete old permssion
1228
		deletePermissionsInAccessTable(docid);
1229
		// write top leve access rule
1230
		writeTopLevelAccessRuleToDB();
1231
		// write additional access rule
1232
		// writeAdditionalAccessRuleToDB();
1233
		writeAdditionalAccessRulesToDB();
1234
	}// writeAccessRuleToDB
1235

    
1236
	/* The method to write top level access rule into db. */
1237
	private void writeTopLevelAccessRuleToDB() throws SAXException {
1238
		// for top document level
1239
		AccessSection accessSection = topLevelAccessControlMap.get(docid);
1240
		boolean top = true;
1241
		String subSectionId = null;
1242
		if (accessSection != null) {
1243
			AccessSection accessSectionObj = accessSection;
1244

    
1245
			// if accessSection is not null and is not reference
1246
			if (accessSectionObj.getReferences() == null) {
1247
				// write the top level access module into xml_accesssubtree to
1248
				// store info and then when update to check if the user can
1249
				// update it or not
1250
				deleteAccessSubTreeRecord(docid);
1251
				writeAccessSubTreeIntoDB(accessSectionObj, TOPLEVEL);
1252

    
1253
				// write access section into xml_access table
1254
				writeGivenAccessRuleIntoDB(accessSectionObj, top, subSectionId);
1255
				// write online data file into xml_access too.
1256
				// for (int i= 0; i <onlineDataFileIdInTopAccessVector.size();
1257
				// i++)
1258
				// {
1259
				// String id = onlineDataFileIdInTopAccessVector.elementAt(i);
1260
				// writeAccessRuleForRelatedDataFileIntoDB(accessSectionObj,
1261
				// id);
1262
				// }
1263

    
1264
			} else {
1265

    
1266
				// this is a reference and go trough the vector which contains
1267
				// all access object
1268
				String referenceId = accessSectionObj.getReferences();
1269
				boolean findAccessObject = false;
1270
				logMetacat.debug("referered id for top access: " + referenceId);
1271
				for (int i = 0; i < accessObjectList.size(); i++) {
1272
					AccessSection accessObj = accessObjectList.elementAt(i);
1273
					String accessObjId = accessObj.getSubTreeId();
1274
					if (referenceId != null && accessObj != null
1275
							&& referenceId.equals(accessObjId)) {
1276
						// make sure the user didn't change any thing in this
1277
						// access moduel
1278
						// too if user doesn't have all permission
1279
						if (needToCheckAccessModule) {
1280

    
1281
							Stack<NodeRecord> newStack = accessObj
1282
									.getStoredTmpNodeStack();
1283
							// revise order
1284
							newStack = DocumentUtil.reviseStack(newStack);
1285
							// go throught the vector of
1286
							// unChangeableAccessSubtreevector
1287
							// and find the one whose id is as same as
1288
							// referenceid
1289
							AccessSection oldAccessObj = getAccessSectionFromUnchangableAccessVector(referenceId);
1290
							// if oldAccessObj is null something is wrong
1291
							if (oldAccessObj == null) {
1292
								throw new SAXException(UPDATEACCESSERROR);
1293
							}// if
1294
							else {
1295
								// Get the node stack from old access obj
1296
								Stack<NodeRecord> oldStack = oldAccessObj
1297
										.getSubTreeNodeStack();
1298
								compareNodeStacks(newStack, oldStack);
1299
							}// else
1300
						}// if
1301
						// write accessobject into db
1302
						writeGivenAccessRuleIntoDB(accessObj, top, subSectionId);
1303
						// write online data file into xml_access too.
1304
						// for (int j= 0; j
1305
						// <onlineDataFileIdInTopAccessVector.size(); j++)
1306
						// {
1307
						// String id =
1308
						// onlineDataFileIdInTopAccessVector.elementAt(j);
1309
						// writeAccessRuleForRelatedDataFileIntoDB(accessSectionObj,
1310
						// id);
1311
						// }
1312

    
1313
						// write the reference access into xml_accesssubtree
1314
						// too write the top level access module into
1315
						// xml_accesssubtree to store info and then when update
1316
						// to check if the user can update it or not
1317
						deleteAccessSubTreeRecord(docid);
1318
						writeAccessSubTreeIntoDB(accessSectionObj, TOPLEVEL);
1319
						writeAccessSubTreeIntoDB(accessObj, SUBTREELEVEL);
1320
						findAccessObject = true;
1321
						break;
1322
					}
1323
				}// for
1324
				// if we couldn't find an access subtree id for this reference
1325
				// id
1326
				if (!findAccessObject) {
1327
					throw new SAXException("The referenceid: " + referenceId
1328
							+ " is not access subtree");
1329
				}// if
1330
			}// else
1331

    
1332
		}// if
1333
		else {
1334
			// couldn't find a access section object
1335
			logMetacat.warn("couldn't find access control for document: " + docid);
1336
		}
1337

    
1338
	}// writeTopLevelAccessRuletoDB
1339

    
1340
	/* Given a subtree id and find the responding access section */
1341
	private AccessSection getAccessSectionFromUnchangableAccessVector(String id) {
1342
		AccessSection result = null;
1343
		// Makse sure the id
1344
		if (id == null || id.equals("")) {
1345
			return result;
1346
		}
1347
		// go throught vector and find the list
1348
		for (int i = 0; i < unChangeableAccessSubTreeVector.size(); i++) {
1349
			AccessSection accessObj = unChangeableAccessSubTreeVector.elementAt(i);
1350
			if (accessObj.getSubTreeId() != null && (accessObj.getSubTreeId()).equals(id)) {
1351
				result = accessObj;
1352
			}// if
1353
		}// for
1354
		return result;
1355
	}// getAccessSectionFromUnchangableAccessVector
1356

    
1357
	/* Compare two node stacks to see if they are same */
1358
	private void compareNodeStacks(Stack<NodeRecord> stack1, Stack<NodeRecord> stack2)
1359
			throws SAXException {
1360
		// make sure stack1 and stack2 are not empty
1361
		if (stack1.isEmpty() || stack2.isEmpty()) {
1362
			logMetacat.error("Because stack is empty!");
1363
			throw new SAXException(UPDATEACCESSERROR);
1364
		}
1365
		// go throw two stacks and compare every element
1366
		while (!stack1.isEmpty()) {
1367
			// Pop an element from stack1
1368
			NodeRecord record1 = stack1.pop();
1369
			// Pop an element from stack2(stack 2 maybe empty)
1370
			NodeRecord record2 = null;
1371
			try {
1372
				record2 = stack2.pop();
1373
			} catch (EmptyStackException ee) {
1374
				logMetacat.error("Node stack2 is empty but stack1 isn't!");
1375
				throw new SAXException(UPDATEACCESSERROR);
1376
			}
1377
			// if two records are not same throw a exception
1378
			if (!record1.contentEquals(record2)) {
1379
				logMetacat.error("Two records from new and old stack are not " + "same!");
1380
				throw new SAXException(UPDATEACCESSERROR);
1381
			}// if
1382
		}// while
1383

    
1384
		// now stack1 is empty and we should make sure stack2 is empty too
1385
		if (!stack2.isEmpty()) {
1386
			logMetacat
1387
					.error("stack2 still has some elements while stack " + "is empty! ");
1388
			throw new SAXException(UPDATEACCESSERROR);
1389
		}// if
1390
	}// comparingNodeStacks
1391

    
1392
	/* The method to write additional access rule into db. */
1393
	private void writeAdditionalAccessRulesToDB() throws SAXException {
1394
		
1395
		// Iterate through every distribution and write access sections for data and inline
1396
		// types to the database
1397
		for (DistributionSection distributionSection : allDistributionSections) {			
1398
			// we're only interested in data and inline distributions
1399
			int distributionType = distributionSection.getDistributionType();
1400
			if (distributionType == DistributionSection.DATA_DISTRIBUTION
1401
					|| distributionType == DistributionSection.INLINE_DATA_DISTRIBUTION) {
1402
				AccessSection accessSection = distributionSection.getAccessSection();
1403
				
1404
				// If the distribution doesn't have an access section, we continue.
1405
				if (accessSection == null) {
1406
					continue;		
1407
				} 
1408
				
1409
				// We want to check file permissions for all online data updates and inserts, or for 
1410
				// inline updates.
1411
//				if (distributionType == DistributionSection.DATA_DISTRIBUTION
1412
//						|| (distributionType == DistributionSection.INLINE_DATA_DISTRIBUTION && action == "UPDATE")) {
1413

    
1414
				if (distributionType == DistributionSection.DATA_DISTRIBUTION) {
1415
					try {
1416
						PermissionController controller = new PermissionController(
1417
								distributionSection.getDataFileName(), false);
1418
						if (AccessionNumber.accNumberUsed(docid)
1419
								&& !controller.hasPermission(user, groups, "WRITE")) {
1420
							throw new SAXException(UPDATEACCESSERROR);
1421
						}
1422
					} catch (SQLException sqle) {
1423
						throw new SAXException(
1424
								"Database error checking user permissions: "
1425
										+ sqle.getMessage());
1426
					} catch (Exception e) {
1427
						throw new SAXException(
1428
								"General error checking user permissions: "
1429
										+ e.getMessage());
1430
					}
1431
				} else if (distributionType == DistributionSection.INLINE_DATA_DISTRIBUTION && action == "UPDATE") {
1432
					try {
1433
						PermissionController controller = new PermissionController(
1434
								docid, false);
1435

    
1436
						if (!controller.hasPermission(user, groups, "WRITE")) {
1437
							throw new SAXException(UPDATEACCESSERROR);
1438
						}
1439
					} catch (SQLException sqle) {
1440
						throw new SAXException(
1441
								"Database error checking user permissions: "
1442
										+ sqle.getMessage());
1443
					} catch (Exception e) {
1444
						throw new SAXException(
1445
								"General error checking user permissions: "
1446
										+ e.getMessage());
1447
					}
1448
				}
1449
				
1450
				String subSectionId = Integer.toString(distributionSection.getDistributionId());
1451
				writeGivenAccessRuleIntoDB(accessSection, false, subSectionId);
1452
			}
1453

    
1454
		}
1455
		
1456
		
1457
	}
1458

    
1459
	/* Write a given access rule into db */
1460
	private void writeGivenAccessRuleIntoDB(AccessSection accessSection,
1461
			boolean topLevel, String subSectionId) throws SAXException {
1462
		if (accessSection == null) {
1463
			throw new SAXException("The access object is null");
1464
		}
1465

    
1466
		String permOrder = accessSection.getPermissionOrder();
1467
		String sql = null;
1468
		PreparedStatement pstmt = null;
1469
		if (topLevel) {
1470
			sql = "INSERT INTO xml_access (docid, principal_name, permission, "
1471
					+ "perm_type, perm_order, accessfileid) VALUES "
1472
					+ " (?, ?, ?, ?, ?, ?)";
1473
		} else {
1474
			sql = "INSERT INTO xml_access (docid,principal_name, "
1475
					+ "permission, perm_type, perm_order, accessfileid, subtreeid"
1476
					+ ") VALUES" + " (?, ?, ?, ?, ?, ?, ?)";
1477
		}
1478
		try {
1479

    
1480
			pstmt = connection.prepareStatement(sql);
1481
			// Increase DBConnection usage count
1482
			connection.increaseUsageCount(1);
1483
			// Bind the values to the query
1484
			pstmt.setString(6, docid);
1485
			logMetacat.debug("Accessfileid in accesstable: " + docid);
1486
			pstmt.setString(5, permOrder);
1487
			logMetacat.debug("PermOder in accesstable: " + permOrder);
1488
			// if it is not top level, set subsection id
1489
			if (topLevel) {
1490
				pstmt.setString(1, docid);
1491
				logMetacat.debug("Docid in accesstable: " + docid);
1492
			}
1493
			if (!topLevel) {
1494
				pstmt.setString(1, accessSection.getDataFileName());
1495
				logMetacat.debug("Docid in accesstable: " + inlineDataFileName);
1496

    
1497
				// for subtree should specify the
1498
				if (subSectionId == null) {
1499
					throw new SAXException("The subsection is null");
1500
				}
1501

    
1502
				pstmt.setString(7, subSectionId);
1503
				logMetacat.debug("SubSectionId in accesstable: " + subSectionId);
1504
			}
1505

    
1506
			Vector<AccessRule> accessRules = accessSection.getAccessRules();
1507
			// go through every rule
1508
			for (int i = 0; i < accessRules.size(); i++) {
1509
				AccessRule rule = accessRules.elementAt(i);
1510
				String permType = rule.getPermissionType();
1511
				int permission = rule.getPermission();
1512
				pstmt.setInt(3, permission);
1513
				logMetacat.debug("permission in accesstable: " + permission);
1514
				pstmt.setString(4, permType);
1515
				logMetacat.debug("Permtype in accesstable: " + permType);
1516
				// go through every principle in rule
1517
				Vector<String> nameVector = rule.getPrincipal();
1518
				for (int j = 0; j < nameVector.size(); j++) {
1519
					String prName = nameVector.elementAt(j);
1520
					pstmt.setString(2, prName);
1521
					logMetacat.debug("Principal in accesstable: " + prName);
1522
					logMetacat.debug("running sql: " + pstmt.toString());
1523
					pstmt.execute();
1524
				}// for
1525
			}// for
1526
			pstmt.close();
1527
		}// try
1528
		catch (SQLException e) {
1529
			throw new SAXException("EMLSAXHandler.writeAccessRuletoDB(): "
1530
					+ e.getMessage());
1531
		}// catch
1532
		finally {
1533
			try {
1534
				pstmt.close();
1535
			} catch (SQLException ee) {
1536
				throw new SAXException("EMLSAXHandler.writeAccessRuletoDB(): "
1537
						+ ee.getMessage());
1538
			}
1539
		}// finally
1540

    
1541
	}// writeGivenAccessRuleIntoDB
1542

    
1543
	/* Write a gaven access rule into db */
1544
	private void writeAccessRuleForRelatedDataFileIntoDB(AccessSection accessSection,
1545
			String dataId) throws SAXException {
1546
		if (accessSection == null) {
1547
			throw new SAXException("The access object is null");
1548
		}
1549
		// get rid of rev from dataId
1550
		// dataId = MetacatUtil.getDocIdFromString(dataId);
1551
		String permOrder = accessSection.getPermissionOrder();
1552
		String sql = null;
1553
		PreparedStatement pstmt = null;
1554
		sql = "INSERT INTO xml_access (docid, principal_name, permission, "
1555
				+ "perm_type, perm_order, accessfileid) VALUES " + " (?, ?, ?, ?, ?, ?)";
1556

    
1557
		try {
1558

    
1559
			pstmt = connection.prepareStatement(sql);
1560
			// Increase DBConnection usage count
1561
			connection.increaseUsageCount(1);
1562
			// Bind the values to the query
1563
			pstmt.setString(1, dataId);
1564
			logMetacat.debug("Docid in accesstable: " + docid);
1565
			pstmt.setString(6, docid);
1566
			logMetacat.debug("Accessfileid in accesstable: " + docid);
1567
			pstmt.setString(5, permOrder);
1568
			logMetacat.debug("PermOder in accesstable: " + permOrder);
1569
			// if it is not top level, set subsection id
1570

    
1571
			Vector<AccessRule> accessRules = accessSection.getAccessRules();
1572
			// go through every rule
1573
			for (int i = 0; i < accessRules.size(); i++) {
1574
				AccessRule rule = accessRules.elementAt(i);
1575
				String permType = rule.getPermissionType();
1576
				int permission = rule.getPermission();
1577
				pstmt.setInt(3, permission);
1578
				logMetacat.debug("permission in accesstable: " + permission);
1579
				pstmt.setString(4, permType);
1580
				logMetacat.debug("Permtype in accesstable: " + permType);
1581
				// go through every principle in rule
1582
				Vector<String> nameVector = rule.getPrincipal();
1583
				for (int j = 0; j < nameVector.size(); j++) {
1584
					String prName = nameVector.elementAt(j);
1585
					pstmt.setString(2, prName);
1586
					logMetacat.debug("Principal in accesstable: " + prName);
1587
					logMetacat.debug("running sql: " + pstmt.toString());
1588
					pstmt.execute();
1589
				}// for
1590
			}// for
1591
			pstmt.close();
1592
		}// try
1593
		catch (SQLException e) {
1594
			throw new SAXException("EMLSAXHandler.writeAccessRuletoDB(): "
1595
					+ e.getMessage());
1596
		}// catch
1597
		finally {
1598
			try {
1599
				pstmt.close();
1600
			} catch (SQLException ee) {
1601
				throw new SAXException("EMLSAXHandler.writeAccessRuletoDB(): "
1602
						+ ee.getMessage());
1603
			}
1604
		}// finally
1605

    
1606
	}// writeAccessRuleForRalatedDataFileIntoDB
1607

    
1608
	/* Delete from db all permission for resources related to @aclid if any. */
1609
	private void deletePermissionsInAccessTable(String aclid) throws SAXException {
1610
		Statement stmt = null;
1611
		try {
1612
			// delete all acl records for resources related to @aclid if any
1613
			stmt = connection.createStatement();
1614
			// Increase DBConnection usage count
1615
			connection.increaseUsageCount(1);
1616
			logMetacat.debug("running sql: DELETE FROM xml_access WHERE accessfileid = '"
1617
					+ aclid + "'");
1618
			stmt.execute("DELETE FROM xml_access WHERE accessfileid = '" + aclid + "'");
1619

    
1620
		} catch (SQLException e) {
1621
			throw new SAXException(e.getMessage());
1622
		} finally {
1623
			try {
1624
				stmt.close();
1625
			} catch (SQLException ee) {
1626
				throw new SAXException(ee.getMessage());
1627
			}
1628
		}
1629
	}// deletePermissionsInAccessTable
1630

    
1631
	/*
1632
	 * In order to make sure only usr has "all" permission can update access
1633
	 * subtree in eml document we need to keep access subtree info in
1634
	 * xml_accesssubtree table, such as docid, version, startnodeid, endnodeid
1635
	 */
1636
	private void writeAccessSubTreeIntoDB(AccessSection accessSection, String level)
1637
			throws SAXException {
1638
		if (accessSection == null) {
1639
			throw new SAXException("The access object is null");
1640
		}
1641

    
1642
		String sql = null;
1643
		PreparedStatement pstmt = null;
1644
		sql = "INSERT INTO xml_accesssubtree (docid, rev, controllevel, "
1645
				+ "subtreeid, startnodeid, endnodeid) VALUES " + " (?, ?, ?, ?, ?, ?)";
1646
		try {
1647

    
1648
			pstmt = connection.prepareStatement(sql);
1649
			// Increase DBConnection usage count
1650
			connection.increaseUsageCount(1);
1651
			long startNodeId = accessSection.getStartNodeId();
1652
			long endNodeId = accessSection.getEndNodeId();
1653
			String sectionId = accessSection.getSubTreeId();
1654
			// Bind the values to the query
1655
			pstmt.setString(1, docid);
1656
			logMetacat.debug("Docid in access-subtreetable: " + docid);
1657
			pstmt.setLong(2, (new Long(revision)).longValue());
1658
			logMetacat.debug("rev in accesssubtreetable: " + revision);
1659
			pstmt.setString(3, level);
1660
			logMetacat.debug("contorl level in access-subtree table: " + level);
1661
			pstmt.setString(4, sectionId);
1662
			logMetacat.debug("Subtree id in access-subtree table: " + sectionId);
1663
			pstmt.setLong(5, startNodeId);
1664
			logMetacat.debug("Start node id is: " + startNodeId);
1665
			pstmt.setLong(6, endNodeId);
1666
			logMetacat.debug("End node id is: " + endNodeId);
1667
			logMetacat.debug("running sql: " + pstmt.toString());
1668
			pstmt.execute();
1669
			pstmt.close();
1670
		}// try
1671
		catch (SQLException e) {
1672
			throw new SAXException("EMLSAXHandler.writeAccessSubTreeIntoDB(): "
1673
					+ e.getMessage());
1674
		}// catch
1675
		finally {
1676
			try {
1677
				pstmt.close();
1678
			} catch (SQLException ee) {
1679
				throw new SAXException("EMLSAXHandler.writeAccessSubTreeIntoDB(): "
1680
						+ ee.getMessage());
1681
			}
1682
		}// finally
1683

    
1684
	}// writeAccessSubtreeIntoDB
1685

    
1686
	/* Delete every access subtree record from xml_accesssubtree. */
1687
	private void deleteAccessSubTreeRecord(String docId) throws SAXException {
1688
		Statement stmt = null;
1689
		try {
1690
			// delete all acl records for resources related to @aclid if any
1691
			stmt = connection.createStatement();
1692
			// Increase DBConnection usage count
1693
			connection.increaseUsageCount(1);
1694
			logMetacat.debug("running sql: DELETE FROM xml_accesssubtree WHERE docid = '"
1695
					+ docId + "'");
1696
			stmt.execute("DELETE FROM xml_accesssubtree WHERE docid = '" + docId + "'");
1697

    
1698
		} catch (SQLException e) {
1699
			throw new SAXException(e.getMessage());
1700
		} finally {
1701
			try {
1702
				stmt.close();
1703
			} catch (SQLException ee) {
1704
				throw new SAXException(ee.getMessage());
1705
			}
1706
		}
1707
	}// deleteAccessSubTreeRecord
1708

    
1709
	// open a file writer for writing inline data to file
1710
	private FileWriter createInlineDataFileWriter(String fileName) throws SAXException {
1711
		FileWriter writer = null;
1712
		String path;
1713
		try {
1714
			path = PropertyService.getProperty("application.inlinedatafilepath");
1715
		} catch (PropertyNotFoundException pnfe) {
1716
			throw new SAXException(pnfe.getMessage());
1717
		}
1718
		/*
1719
		 * File inlineDataDirectory = new File(path);
1720
		 */
1721
		String newFile = path + "/" + fileName;
1722
		logMetacat.debug("inline file name: " + newFile);
1723
		try {
1724
			// true means append
1725
			writer = new FileWriter(newFile, true);
1726
		} catch (IOException ioe) {
1727
			throw new SAXException(ioe.getMessage());
1728
		}
1729
		return writer;
1730
	}
1731

    
1732
	// write inline data into file system and return file name(without path)
1733
	private void writeInlineDataIntoFile(FileWriter writer, StringBuffer data)
1734
			throws SAXException {
1735
		try {
1736
			writer.write(data.toString());
1737
			writer.flush();
1738
		} catch (Exception e) {
1739
			throw new SAXException(e.getMessage());
1740
		}
1741
	}
1742

    
1743
	/*
1744
	 * In eml2, the inline data wouldn't store in db, it store in file system
1745
	 * The db stores file name(without path). We got the old file name from db
1746
	 * and compare to the new in line data file
1747
	 */
1748
	public boolean compareInlineDataFiles(String oldFileName, String newFileName)
1749
			throws McdbException {
1750
		// this method need to be testing
1751
		boolean same = true;
1752
		String data = null;
1753
		try {
1754
			String path = PropertyService.getProperty("application.inlinedatafilepath");
1755
			// the new file name will look like path/docid.rev.2
1756
			File inlineDataDirectory = new File(path);
1757
			File oldDataFile = new File(inlineDataDirectory, oldFileName);
1758
			File newDataFile = new File(inlineDataDirectory, newFileName);
1759

    
1760
			FileReader oldFileReader = new FileReader(oldDataFile);
1761
			BufferedReader oldStringReader = new BufferedReader(oldFileReader);
1762
			FileReader newFileReader = new FileReader(newDataFile);
1763
			BufferedReader newStringReader = new BufferedReader(newFileReader);
1764
			// read first line of data
1765
			String oldString = oldStringReader.readLine();
1766
			String newString = newStringReader.readLine();
1767

    
1768
			// at the end oldstring will be null
1769
			while (oldString != null) {
1770
				oldString = oldStringReader.readLine();
1771
				newString = newStringReader.readLine();
1772
				if (!oldString.equals(newString)) {
1773
					same = false;
1774
					break;
1775
				}
1776
			}
1777

    
1778
			// if oldString is null but newString is not null, they are same
1779
			if (same) {
1780
				if (newString != null) {
1781
					same = false;
1782
				}
1783
			}
1784

    
1785
		} catch (Exception e) {
1786
			throw new McdbException(e.getMessage());
1787
		}
1788
		logMetacat.debug("the inline data retrieved from file: " + data);
1789
		return same;
1790
	}
1791

    
1792
	// if xml file failed to upload, we need to call this method to delete
1793
	// the inline data already in file system
1794
	public void deleteInlineFiles() throws SAXException {
1795
		if (!inlineFileIdList.isEmpty()) {
1796
			for (int i = 0; i < inlineFileIdList.size(); i++) {
1797
				String fileName = inlineFileIdList.elementAt(i);
1798
				deleteInlineDataFile(fileName);
1799
			}
1800
		}
1801
	}
1802

    
1803
	/* delete the inline data file */
1804
	private void deleteInlineDataFile(String fileName) throws SAXException {
1805
		String path;
1806
		try {
1807
			path = PropertyService.getProperty("application.inlinedatafilepath");
1808
		} catch (PropertyNotFoundException pnfe) {
1809
			throw new SAXException("Could not find inline data file path: "
1810
					+ pnfe.getMessage());
1811
		}
1812
		File inlineDataDirectory = new File(path);
1813
		File newFile = new File(inlineDataDirectory, fileName);
1814
		newFile.delete();
1815

    
1816
	}
1817

    
1818
	/*
1819
	 * In eml2, the inline data wouldn't store in db, it store in file system
1820
	 * The db stores file name(without path).
1821
	 */
1822
	public static Reader readInlineDataFromFileSystem(String fileName)
1823
			throws McdbException {
1824
		// BufferedReader stringReader = null;
1825
		FileReader fileReader = null;
1826
		try {
1827
			String path = PropertyService.getProperty("application.inlinedatafilepath");
1828
			// the new file name will look like path/docid.rev.2
1829
			File inlineDataDirectory = new File(path);
1830
			File dataFile = new File(inlineDataDirectory, fileName);
1831

    
1832
			fileReader = new FileReader(dataFile);
1833
			// stringReader = new BufferedReader(fileReader);
1834
		} catch (Exception e) {
1835
			throw new McdbException(e.getMessage());
1836
		}
1837
		// return stringReader;
1838
		return fileReader;
1839
	}
1840

    
1841
	/* Delete relations */
1842
	private void deleteRelations() throws SAXException {
1843
		PreparedStatement pStmt = null;
1844
		String sql = "DELETE FROM xml_relation where docid =?";
1845
		try {
1846
			pStmt = connection.prepareStatement(sql);
1847
			// bind variable
1848
			pStmt.setString(1, docid);
1849
			// execute query
1850
			pStmt.execute();
1851
			pStmt.close();
1852
		}// try
1853
		catch (SQLException e) {
1854
			throw new SAXException("EMLSAXHandler.deleteRelations(): " + e.getMessage());
1855
		}// catch
1856
		finally {
1857
			try {
1858
				pStmt.close();
1859
			}// try
1860
			catch (SQLException ee) {
1861
				throw new SAXException("EMLSAXHandler.deleteRelations: "
1862
						+ ee.getMessage());
1863
			}// catch
1864
		}// finally
1865
	}
1866

    
1867
	/*
1868
	 * Write an online data file id into xml_relation table. The dataId
1869
	 * shouldnot have the revision
1870
	 */
1871
	private void writeOnlineDataFileIdIntoRelationTable(String dataId)
1872
			throws SAXException {
1873
		PreparedStatement pStmt = null;
1874
		String sql = "INSERT into xml_relation (docid, packagetype, subject, "
1875
				+ "relationship, object) values (?, ?, ?, ?, ?)";
1876
		try {
1877
			pStmt = connection.prepareStatement(sql);
1878
			// bind variable
1879
			pStmt.setString(1, docid);
1880
			pStmt.setString(2, DocumentImpl.EML2_1_0NAMESPACE);
1881
			pStmt.setString(3, docid);
1882
			pStmt.setString(4, RELATION);
1883
			pStmt.setString(5, dataId);
1884
			// execute query
1885
			pStmt.execute();
1886
			pStmt.close();
1887
		}// try
1888
		catch (SQLException e) {
1889
			throw new SAXException(
1890
					"EMLSAXHandler.writeOnlineDataFileIdIntoRelationTable(): "
1891
							+ e.getMessage());
1892
		}// catch
1893
		finally {
1894
			try {
1895
				pStmt.close();
1896
			}// try
1897
			catch (SQLException ee) {
1898
				throw new SAXException(
1899
						"EMLSAXHandler.writeOnlineDataFileIdIntoRelationTable(): "
1900
								+ ee.getMessage());
1901
			}// catch
1902
		}// finally
1903

    
1904
	}// writeOnlineDataFileIdIntoRelationTable
1905

    
1906
	/*
1907
	 * This method will handle data file in online url. If the data file is in
1908
	 * ecogrid protocol, then the datafile identifier(without rev)should be put
1909
	 * into onlineDataFileRelationVector. The docid in this vector will be
1910
	 * insert into xml_relation table in endDocument(). If the data file doesn't
1911
	 * exsit in xml_documents or xml_revision table, or the user has all
1912
	 * permission to the data file if the docid already existed, the data file
1913
	 * id (without rev)will be put into onlineDataFileTopAccessVector. The top
1914
	 * access rules specified in this eml document will apply to the data file.
1915
	 * NEED to do: We should also need to implement http and ftp. Those external
1916
	 * files should be download and assign a data file id to it.
1917
	 */
1918
	private void handleOnlineUrlDataFile(String url) throws SAXException {
1919
		logMetacat.warn("The url is " + url);
1920

    
1921
		if (currentDistributionSection == null) {
1922
			throw new SAXException("Trying to set the online file name for a null"
1923
					+ " distribution section");
1924
		}
1925

    
1926
		// if the url is not in ecogrid protocol, null will be returned
1927
		String accessionNumber = DocumentUtil.getAccessionNumberFromEcogridIdentifier(url);
1928
		if (accessionNumber == null) {
1929
			// the accession number is null if the url does not references a
1930
			// local data file (url would start with "ecogrid://"
1931
			currentDistributionSection
1932
					.setDistributionType(DistributionSection.ONLINE_DATA_DISTRIBUTION);
1933
		} else {
1934
			// handle ecogrid protocol
1935
			// get rid of revision number to get the docid.
1936
			String docid = DocumentUtil.getDocIdFromAccessionNumber(accessionNumber);
1937

    
1938
			currentDistributionSection
1939
					.setDistributionType(DistributionSection.DATA_DISTRIBUTION);
1940
			currentDistributionSection.setDataFileName(docid);
1941

    
1942
			// distributionOnlineFileName = docid;
1943
			onlineDataFileIdInRelationVector.add(docid);
1944
			try {				
1945
				if (!AccessionNumber.accNumberUsed(docid)) {
1946
					onlineDataFileIdInTopAccessVector.add(docid);
1947
				} else {
1948
					PermissionController controller = new PermissionController(accessionNumber);				
1949
					if (controller.hasPermission(user, groups,AccessControlInterface.ALLSTRING)) {
1950
						onlineDataFileIdInTopAccessVector.add(docid);
1951
					} else {
1952
						throw new SAXException(UPDATEACCESSERROR);
1953
					}
1954
				} 
1955
			}// try
1956
			catch (Exception e) {
1957
				logMetacat.error("Eorr in "
1958
								+ "Eml210SAXHanlder.handleOnlineUrlDataFile is "
1959
								+ e.getMessage());
1960
				throw new SAXException(e.getMessage());
1961
			}
1962
		}
1963
	}
1964
}
(33-33/60)