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: leinfelder $'
9
 *     '$Date: 2011-03-23 12:51:40 -0700 (Wed, 23 Mar 2011) $'
10
 * '$Revision: 6016 $'
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.Stack;
30
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
import edu.ucsb.nceas.metacat.accesscontrol.AccessControlInterface;
38
import edu.ucsb.nceas.metacat.accesscontrol.AccessControlList;
39
import edu.ucsb.nceas.metacat.accesscontrol.XMLAccessDAO;
40

    
41
/** 
42
 * A Class implementing callback methods for the SAX parser to
43
 * call when processing the XML messages from the replication handler
44
 */
45
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 _accessFileId;
52
	private String _subTreeId;
53
	private String _docId;
54
	private boolean _inPrincipal = false;
55
	private boolean _inPermission = false;
56
	private boolean _inAllow = false;
57
	private boolean _inDeny = false;
58
  
59
	private Vector<XMLAccessDAO> xmlAccessDAOList = new Vector<XMLAccessDAO>();
60
	private Stack<XMLAccessDAO> xmlAccessDAOStack = new Stack<XMLAccessDAO>();
61
	
62
	private String chars = "";
63
  
64
	public DocInfoHandler() {
65
	}
66

    
67
	public DocInfoHandler(String docId) {
68
		_docId = docId;
69
	}
70
  
71
   /**
72
	 * capture the name of the tag.
73
	 */
74
	public void startElement(String uri, String localName, String qName,
75
			Attributes attributes) throws SAXException {
76
	    chars = "";
77
		_currentTag = localName;
78
		if (_currentTag.equals("access")) {
79
			if (_accessPermOrder == null) {
80
				_accessPermOrder = attributes.getValue("order");			
81
			}
82
			if (_accessFileId == null) {
83
				_accessFileId = attributes.getValue("accessfileid");			
84
			}
85
			if (_subTreeId == null) {
86
				_subTreeId = attributes.getValue("subtreeid");			
87
			}
88
		} else if (_currentTag.equals(AccessControlInterface.ALLOW)) {
89
			_inAllow = true;
90
		} else if (_currentTag.equals(AccessControlInterface.DENY)) {
91
			_inDeny = true;
92
		} else if (_currentTag.equals(AccessControlInterface.PERMISSION)) {
93
			_inPermission = true;			
94
		} else if (_currentTag.equals(AccessControlInterface.PRINCIPAL)) {
95
			// new principal means new DAO for storing it
96
			_inPrincipal = true;
97
			_currentAccessDAO = new XMLAccessDAO();
98
			_currentAccessDAO.setDocId(_docId);
99
			_currentAccessDAO.setPermOrder(_accessPermOrder);
100
			_currentAccessDAO.setAccessFileId(_accessFileId);
101
			_currentAccessDAO.setSubTreeId(_subTreeId);
102
			_currentAccessDAO.setDocId(_docId);
103
			if (_inAllow) {
104
				_currentAccessDAO.setPermType(AccessControlInterface.ALLOW);
105
			}
106
			if (_inDeny) {
107
				_currentAccessDAO.setPermType(AccessControlInterface.DENY);
108
			}
109
		}
110
	}
111

    
112
	public void endElement(String uri, String localName, String qName)
113
			throws SAXException {
114
			    
115
	    _docinfo.put(_currentTag, chars);
116
    	
117
        if (_currentTag.equals(AccessControlInterface.PRINCIPAL) && _inPrincipal) {
118
            if (_currentAccessDAO != null) {
119
                _currentAccessDAO.setPrincipalName(chars);
120
            }
121
        } else if (_currentTag.equals(AccessControlInterface.PERMISSION) && _inPermission) {
122
            if (_currentAccessDAO != null) {
123
                String permString = chars;
124
                Long permLong = Long.valueOf(AccessControlList.intValue(permString));
125
                //add this permission for each DAO in the stack
126
                for (int i = 0; i < xmlAccessDAOStack.size(); i++) {
127
                    xmlAccessDAOStack.get(i).addPermission(permLong);
128
                }
129
            }
130
        }
131
		
132
		if (localName.equals(AccessControlInterface.ALLOW)) {
133
			_inAllow = false;
134
		} else if (localName.equals(AccessControlInterface.DENY)) {
135
			_inDeny = false;
136
		} else if (_currentTag.equals(AccessControlInterface.PRINCIPAL)) {
137
			_inPrincipal = false;
138
			xmlAccessDAOStack.push(_currentAccessDAO);
139
		} else if (_currentTag.equals(AccessControlInterface.PERMISSION)) {
140
			_inPermission = false;
141
		}
142
		//end of a section for allow/deny
143
		if (
144
			localName.equals(AccessControlInterface.ALLOW)
145
			||
146
			localName.equals(AccessControlInterface.DENY)
147
			) {
148
			//get all the DAOs for this section and add them to the overall list
149
			xmlAccessDAOList.addAll(xmlAccessDAOStack);
150
			xmlAccessDAOStack.clear();
151
		}
152
	}
153
  
154
  /**
155
	 * put the content and the name of the tag into the hashtable. the name of
156
	 * the tag is the key.
157
	 */
158
  public void characters(char[] ch, int start, int length) throws SAXException
159
  {
160
    chars += new String(ch, start, length);
161
  }
162
  
163
  public Hashtable<String,String> getDocInfo()
164
  {
165
    return _docinfo;
166
  }
167
  
168
  public Vector<XMLAccessDAO> getAccessControlList() {
169
	  return xmlAccessDAOList;
170
  }
171
}
(25-25/65)