001: /*
002: * SSHTools - Java SSH2 API
003: *
004: * Copyright (C) 2002-2003 Lee David Painter and Contributors.
005: *
006: * Contributions made by:
007: *
008: * Brett Smith
009: * Richard Pernavas
010: * Erwin Bolwidt
011: *
012: * This program is free software; you can redistribute it and/or
013: * modify it under the terms of the GNU General Public License
014: * as published by the Free Software Foundation; either version 2
015: * of the License, or (at your option) any later version.
016: *
017: * This program is distributed in the hope that it will be useful,
018: * but WITHOUT ANY WARRANTY; without even the implied warranty of
019: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: * GNU General Public License for more details.
021: *
022: * You should have received a copy of the GNU General Public License
023: * along with this program; if not, write to the Free Software
024: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
025: */
026: package com.sshtools.common.configuration;
027:
028: import org.xml.sax.*;
029: import org.xml.sax.helpers.*;
030:
031: import java.io.*;
032:
033: import java.util.*;
034:
035: import javax.xml.parsers.*;
036:
037: /**
038: *
039: *
040: * @author $author$
041: * @version $Revision: 1.12 $
042: */
043: public class Authorization extends DefaultHandler {
044: private static final String AUTHORIZEDKEYS_ELEMENT = "AuthorizedKeys";
045: private static final String KEY_ELEMENT = "Key";
046: private ArrayList authorizedKeys = new ArrayList();
047:
048: /**
049: * Creates a new Authorization object.
050: *
051: * @param in
052: *
053: * @throws SAXException
054: * @throws ParserConfigurationException
055: * @throws IOException
056: */
057: public Authorization(InputStream in) throws SAXException,
058: ParserConfigurationException, IOException {
059: SAXParserFactory saxFactory = SAXParserFactory.newInstance();
060: SAXParser saxParser = saxFactory.newSAXParser();
061: authorizedKeys.clear();
062: saxParser.parse(in, new AuthorizedKeysSAXHandler());
063: }
064:
065: /**
066: * Creates a new Authorization object.
067: */
068: public Authorization() {
069: // Creates an empty authorization file
070: }
071:
072: /**
073: *
074: *
075: * @return
076: */
077: public List getAuthorizedKeys() {
078: return (List) authorizedKeys.clone();
079: }
080:
081: /**
082: *
083: *
084: * @param keyfile
085: */
086: public void addKey(String keyfile) {
087: authorizedKeys.add(keyfile);
088: }
089:
090: /**
091: *
092: *
093: * @return
094: */
095: public String toString() {
096: String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
097: xml += "<!-- SSHTools Public Key Authorization File -->\n";
098: xml += ("<" + AUTHORIZEDKEYS_ELEMENT + ">\n");
099: xml += "<!-- Enter authorized public key elements here -->\n";
100:
101: Iterator it = authorizedKeys.iterator();
102:
103: while (it.hasNext()) {
104: xml += (" <" + KEY_ELEMENT + ">" + it.next().toString()
105: + "</" + KEY_ELEMENT + ">\n");
106: }
107:
108: xml += ("</" + AUTHORIZEDKEYS_ELEMENT + ">");
109:
110: return xml;
111: }
112:
113: class AuthorizedKeysSAXHandler extends DefaultHandler {
114: private String currentElement = null;
115:
116: public void startElement(String uri, String localName,
117: String qname, Attributes attrs) throws SAXException {
118: if (currentElement == null) {
119: if (!qname.equals(AUTHORIZEDKEYS_ELEMENT)) {
120: throw new SAXException("Unexpected root element "
121: + qname);
122: }
123: } else {
124: if (currentElement.equals(AUTHORIZEDKEYS_ELEMENT)) {
125: if (!qname.equals(KEY_ELEMENT)) {
126: throw new SAXException("Unexpected element "
127: + qname);
128: }
129: } else {
130: throw new SAXException("Unexpected element "
131: + qname);
132: }
133: }
134:
135: currentElement = qname;
136: }
137:
138: public void characters(char[] ch, int start, int length)
139: throws SAXException {
140: if (currentElement != null) {
141: if (currentElement.equals(KEY_ELEMENT)) {
142: String key = new String(ch, start, length);
143: authorizedKeys.add(key);
144: }
145: }
146: }
147:
148: public void endElement(String uri, String localName,
149: String qname) throws SAXException {
150: if (currentElement != null) {
151: if (!currentElement.equals(qname)) {
152: throw new SAXException(
153: "Unexpected end element found " + qname);
154: }
155:
156: if (currentElement.equals(KEY_ELEMENT)) {
157: currentElement = AUTHORIZEDKEYS_ELEMENT;
158: } else if (currentElement
159: .equals(AUTHORIZEDKEYS_ELEMENT)) {
160: currentElement = null;
161: } else {
162: throw new SAXException("Unexpected end element "
163: + qname);
164: }
165: }
166: }
167: }
168: }
|