Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: a class to handle document info request xml strings
4
 *  Copyright: 2000 Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6
 *    Authors: Chad Berkley
7
 *
8
 *   '$Author: daigle $'
9
 *     '$Date: 2009-11-04 15:05:30 -0800 (Wed, 04 Nov 2009) $'
10
 * '$Revision: 5103 $'
11
 *
12
 * This program is free software; you can redistribute it and/or modify
13
 * it under the terms of the GNU General Public License as published by
14
 * the Free Software Foundation; either version 2 of the License, or
15
 * (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU General Public License
23
 * along with this program; if not, write to the Free Software
24
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
 */
26

    
27
package edu.ucsb.nceas.metacat;
28

    
29
import java.util.Vector;
30
import java.util.Hashtable;
31

    
32
import org.xml.sax.Attributes;
33
import org.xml.sax.SAXException;
34
import org.xml.sax.helpers.DefaultHandler;
35

    
36
import edu.ucsb.nceas.metacat.accesscontrol.AccessControlInterface;
37
import edu.ucsb.nceas.metacat.accesscontrol.AccessControlList;
38
import edu.ucsb.nceas.metacat.accesscontrol.XMLAccessDAO;
39

    
40
/** 
41
 * A Class implementing callback methods for the SAX parser to
42
 * call when processing the XML messages from the replication handler
43
 */
44
public class DocInfoHandler extends DefaultHandler {
45
	
46
	private Hashtable<String, String> _docinfo = new Hashtable<String, String>();
47
	private String _currentTag = null;
48
	private XMLAccessDAO _currentAccessDAO = null;
49
	private String _accessPermOrder;
50
	private String _docId;
51
	private boolean _inPrincipal = false;
52
	private boolean _inPermission = false;
53
  
54
	private Vector<XMLAccessDAO> xmlAccessDAOList = new Vector<XMLAccessDAO>();
55
  
56
	public DocInfoHandler() {
57
	}
58

    
59
	public DocInfoHandler(String docId) {
60
		_docId = docId;
61
	}
62
  
63
   /**
64
	 * capture the name of the tag.
65
	 */
66
	public void startElement(String uri, String localName, String qName,
67
			Attributes attributes) throws SAXException {
68
		_currentTag = localName;
69
		if (_currentTag.equals("access")) {
70
			if (_accessPermOrder == null) {
71
				_accessPermOrder = attributes.getValue("order");			
72
			}
73
			_currentAccessDAO = new XMLAccessDAO();
74
			_currentAccessDAO.setDocId(_docId);
75
			_currentAccessDAO.setPermOrder(_accessPermOrder);
76
		} else if (_currentTag.equals(AccessControlInterface.PRINCIPAL)) {
77
			_inPrincipal = true;
78
		} else if (_currentTag.equals(AccessControlInterface.PERMISSION)) {
79
			_inPermission = true;
80
		}
81
	}
82

    
83
	public void endElement(String uri, String localName, String qName)
84
			throws SAXException {
85
		if (localName.equals("access")) {			
86
			if (_currentAccessDAO != null) {
87
				xmlAccessDAOList.add(_currentAccessDAO);
88
			}			
89
			_currentAccessDAO = null;
90
		} else if (qName.equals(AccessControlInterface.ALLOW)) {
91
			if (_currentAccessDAO != null) {
92
				_currentAccessDAO.setPermType(AccessControlInterface.ALLOW);
93
			}
94
		} else if (qName.equals(AccessControlInterface.DENY)) {
95
			if (_currentAccessDAO != null) {
96
				_currentAccessDAO.setPermType(AccessControlInterface.DENY);
97
			}
98
		} else if (_currentTag.equals(AccessControlInterface.PRINCIPAL)) {
99
			_inPrincipal = false;
100
		} else if (_currentTag.equals(AccessControlInterface.PERMISSION)) {
101
			_inPermission = false;
102
		}
103
	}
104
  
105
  /**
106
	 * put the content and the name of the tag into the hashtable. the name of
107
	 * the tag is the key.
108
	 */
109
  public void characters(char[] ch, int start, int length) throws SAXException
110
  {
111
    _docinfo.put(_currentTag, new String(ch, start, length));
112
    	
113
    if (_currentTag.equals(AccessControlInterface.PRINCIPAL) && _inPrincipal) {
114
		if (_currentAccessDAO != null) {
115
			_currentAccessDAO.setPrincipalName(new String(ch, start, length));
116
		}
117
	} else if (_currentTag.equals(AccessControlInterface.PERMISSION) && _inPermission) {
118
		if (_currentAccessDAO != null) {
119
			String permString = new String(ch, start, length);
120
			Long permLong = Long.valueOf(AccessControlList.intValue(permString));
121
			_currentAccessDAO.addPermission(permLong);
122
		}
123
	}
124
  }
125
  
126
  public Hashtable<String,String> getDocInfo()
127
  {
128
    return _docinfo;
129
  }
130
  
131
  public Vector<XMLAccessDAO> getAccessControlList() {
132
	  return xmlAccessDAOList;
133
  }
134
}
(24-24/58)