Project

General

Profile

1 562 berkley
/**
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$'
9
 *     '$Date$'
10
 * '$Revision$'
11 669 jones
 *
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 562 berkley
 */
26
27
package edu.ucsb.nceas.metacat;
28
29 5104 leinfelder
import java.util.Stack;
30 562 berkley
import java.util.Vector;
31
import java.util.Hashtable;
32
33
import org.xml.sax.Attributes;
34
import org.xml.sax.SAXException;
35
import org.xml.sax.helpers.DefaultHandler;
36
37 5099 daigle
import edu.ucsb.nceas.metacat.accesscontrol.AccessControlInterface;
38 5098 daigle
import edu.ucsb.nceas.metacat.accesscontrol.AccessControlList;
39
import edu.ucsb.nceas.metacat.accesscontrol.XMLAccessDAO;
40 5090 daigle
41 562 berkley
/**
42 5099 daigle
 * A Class implementing callback methods for the SAX parser to
43 562 berkley
 * call when processing the XML messages from the replication handler
44
 */
45 5099 daigle
public class DocInfoHandler extends DefaultHandler {
46
47
	private Hashtable<String, String> _docinfo = new Hashtable<String, String>();
48
	private String _currentTag = null;
49
	private XMLAccessDAO _currentAccessDAO = null;
50
	private String _accessPermOrder;
51
	private String _docId;
52 5103 daigle
	private boolean _inPrincipal = false;
53
	private boolean _inPermission = false;
54 5104 leinfelder
	private boolean _inAllow = false;
55
	private boolean _inDeny = false;
56 562 berkley
57 5099 daigle
	private Vector<XMLAccessDAO> xmlAccessDAOList = new Vector<XMLAccessDAO>();
58 5104 leinfelder
	private Stack<XMLAccessDAO> xmlAccessDAOStack = new Stack<XMLAccessDAO>();
59 5440 berkley
60
	private String chars = "";
61 4419 leinfelder
62 5099 daigle
	public DocInfoHandler() {
63
	}
64
65
	public DocInfoHandler(String docId) {
66
		_docId = docId;
67
	}
68 562 berkley
69 5099 daigle
   /**
70
	 * capture the name of the tag.
71
	 */
72
	public void startElement(String uri, String localName, String qName,
73
			Attributes attributes) throws SAXException {
74 5440 berkley
	    chars = "";
75 5099 daigle
		_currentTag = localName;
76
		if (_currentTag.equals("access")) {
77
			if (_accessPermOrder == null) {
78
				_accessPermOrder = attributes.getValue("order");
79
			}
80 5104 leinfelder
		} else if (_currentTag.equals(AccessControlInterface.ALLOW)) {
81
			_inAllow = true;
82
		} else if (_currentTag.equals(AccessControlInterface.DENY)) {
83
			_inDeny = true;
84
		} else if (_currentTag.equals(AccessControlInterface.PERMISSION)) {
85
			_inPermission = true;
86
		} else if (_currentTag.equals(AccessControlInterface.PRINCIPAL)) {
87
			// new principal means new DAO for storing it
88
			_inPrincipal = true;
89 5099 daigle
			_currentAccessDAO = new XMLAccessDAO();
90
			_currentAccessDAO.setDocId(_docId);
91
			_currentAccessDAO.setPermOrder(_accessPermOrder);
92 5104 leinfelder
			if (_inAllow) {
93
				_currentAccessDAO.setPermType(AccessControlInterface.ALLOW);
94
			}
95
			if (_inDeny) {
96
				_currentAccessDAO.setPermType(AccessControlInterface.DENY);
97
			}
98 5099 daigle
		}
99
	}
100 4486 daigle
101 5099 daigle
	public void endElement(String uri, String localName, String qName)
102
			throws SAXException {
103 5440 berkley
104
	    _docinfo.put(_currentTag, chars);
105
106
        if (_currentTag.equals(AccessControlInterface.PRINCIPAL) && _inPrincipal) {
107
            if (_currentAccessDAO != null) {
108
                _currentAccessDAO.setPrincipalName(chars);
109
            }
110
        } else if (_currentTag.equals(AccessControlInterface.PERMISSION) && _inPermission) {
111
            if (_currentAccessDAO != null) {
112
                String permString = chars;
113
                Long permLong = Long.valueOf(AccessControlList.intValue(permString));
114
                //add this permission for each DAO in the stack
115
                for (int i = 0; i < xmlAccessDAOStack.size(); i++) {
116
                    xmlAccessDAOStack.get(i).addPermission(permLong);
117
                }
118
            }
119
        }
120 5104 leinfelder
121
		if (localName.equals(AccessControlInterface.ALLOW)) {
122
			_inAllow = false;
123
		} else if (localName.equals(AccessControlInterface.DENY)) {
124
			_inDeny = false;
125 5103 daigle
		} else if (_currentTag.equals(AccessControlInterface.PRINCIPAL)) {
126
			_inPrincipal = false;
127 5104 leinfelder
			xmlAccessDAOStack.push(_currentAccessDAO);
128 5103 daigle
		} else if (_currentTag.equals(AccessControlInterface.PERMISSION)) {
129
			_inPermission = false;
130 5099 daigle
		}
131 5104 leinfelder
		//end of a section for allow/deny
132
		if (
133
			localName.equals(AccessControlInterface.ALLOW)
134
			||
135
			localName.equals(AccessControlInterface.DENY)
136
			) {
137
			//get all the DAOs for this section and add them to the overall list
138
			xmlAccessDAOList.addAll(xmlAccessDAOStack);
139
			xmlAccessDAOStack.clear();
140
		}
141 5099 daigle
	}
142 4419 leinfelder
143 562 berkley
  /**
144 5099 daigle
	 * put the content and the name of the tag into the hashtable. the name of
145
	 * the tag is the key.
146
	 */
147 562 berkley
  public void characters(char[] ch, int start, int length) throws SAXException
148
  {
149 5440 berkley
    chars += new String(ch, start, length);
150 562 berkley
  }
151
152 4486 daigle
  public Hashtable<String,String> getDocInfo()
153 562 berkley
  {
154 5099 daigle
    return _docinfo;
155 562 berkley
  }
156 4486 daigle
157 5098 daigle
  public Vector<XMLAccessDAO> getAccessControlList() {
158
	  return xmlAccessDAOList;
159 4486 daigle
  }
160 562 berkley
}