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.automate;
027:
028: import org.xml.sax.Attributes;
029: import org.xml.sax.SAXException;
030: import org.xml.sax.helpers.DefaultHandler;
031:
032: import java.io.IOException;
033: import java.io.InputStream;
034:
035: import java.util.HashMap;
036: import java.util.Map;
037:
038: import javax.xml.parsers.ParserConfigurationException;
039: import javax.xml.parsers.SAXParser;
040: import javax.xml.parsers.SAXParserFactory;
041:
042: /**
043: *
044: *
045: * @author $author$
046: * @version $Revision: 1.13 $
047: */
048: public class AutomationConfiguration {
049: private HashMap remoteIdentifications = new HashMap();
050:
051: /**
052: * Creates a new AutomationConfiguration object.
053: *
054: * @param in
055: *
056: * @throws IOException
057: * @throws SAXException
058: * @throws ParserConfigurationException
059: */
060: public AutomationConfiguration(InputStream in) throws IOException,
061: SAXException, ParserConfigurationException {
062: SAXParserFactory saxFactory = SAXParserFactory.newInstance();
063: SAXParser saxParser = saxFactory.newSAXParser();
064: saxParser.parse(in, new AutomationConfigurationSAXHandler());
065: }
066:
067: /**
068: *
069: *
070: * @return
071: */
072: public Map getRemoteIdentifications() {
073: return remoteIdentifications;
074: }
075:
076: /**
077: *
078: *
079: * @param args
080: */
081: public static void main(String[] args) {
082: try {
083: } catch (Exception e) {
084: e.printStackTrace();
085: }
086: }
087:
088: private class AutomationConfigurationSAXHandler extends
089: DefaultHandler {
090: private String AUTOMATION_ELEMENT = "Automation";
091: private String REMOTEID_ELEMENT = "RemoteIdentification";
092: private String AUTHORIZEDKEYSFORMAT_ELEMENT = "AuthorizedKeysFormat";
093: private String RULE_ELEMENT = "Rule";
094: private String STARTSWITH_ATTRIBUTE = "startsWith";
095: private String CONTAINS_ATTRIBUTE = "contains";
096: private String DEFAULTNAME_ATTRIBUTE = "defaultName";
097: private String NAME_ATTRIBUTE = "name";
098: private String IMPLEMENTATION_ATTRIBUTE = "implementationClass";
099: private String PRIORITY_ATTRIBUTE = "priority";
100: private String DEFAULTPATH_ATTRIBUTE = "defaultPath";
101: private String currentElement = null;
102: private RemoteIdentification currentRID = null;
103:
104: public void startElement(String uri, String localName,
105: String qname, Attributes attrs) throws SAXException {
106: if (currentElement == null) {
107: if (!qname.equals(AUTOMATION_ELEMENT)) {
108: throw new SAXException("Unexpected root element <"
109: + qname + ">");
110: }
111: } else {
112: if (currentElement.equals(AUTOMATION_ELEMENT)) {
113: if (qname.equals(REMOTEID_ELEMENT)) {
114: String defaultName = attrs
115: .getValue(DEFAULTNAME_ATTRIBUTE);
116:
117: if (defaultName == null) {
118: throw new SAXException(
119: DEFAULTNAME_ATTRIBUTE
120: + " attribute must be specified");
121: }
122:
123: currentRID = new RemoteIdentification(
124: defaultName);
125: } else {
126: throw new SAXException("Unexpected element <"
127: + qname + ">");
128: }
129: } else if (currentElement.equals(REMOTEID_ELEMENT)) {
130: if (qname.equals(RULE_ELEMENT)) {
131: String startsWith = attrs
132: .getValue(STARTSWITH_ATTRIBUTE);
133: String contains = attrs
134: .getValue(CONTAINS_ATTRIBUTE);
135: String name = attrs.getValue(NAME_ATTRIBUTE);
136: String priority = attrs
137: .getValue(PRIORITY_ATTRIBUTE);
138:
139: try {
140: RemoteIdentificationRule rule = new RemoteIdentificationRule();
141:
142: if (startsWith != null) {
143: rule.addExpression(
144: STARTSWITH_ATTRIBUTE,
145: startsWith);
146: }
147:
148: if (contains != null) {
149: rule.addExpression(CONTAINS_ATTRIBUTE,
150: contains);
151: }
152:
153: if (name != null) {
154: rule.setName(name);
155: }
156:
157: try {
158: if (priority != null) {
159: rule.setPriority(Integer
160: .parseInt(priority));
161: }
162: } catch (NumberFormatException ex1) {
163: throw new SAXException(
164: "Failed to parse priority value! value="
165: + priority);
166: }
167:
168: currentRID.addRule(rule);
169: } catch (UnsupportedRuleException ure) {
170: throw new SAXException(ure.getMessage());
171: }
172: } else if (qname
173: .equals(AUTHORIZEDKEYSFORMAT_ELEMENT)) {
174: String implementationClass = attrs
175: .getValue(IMPLEMENTATION_ATTRIBUTE);
176: String defaultPath = attrs
177: .getValue(DEFAULTPATH_ATTRIBUTE);
178:
179: if (implementationClass == null) {
180: throw new SAXException(
181: IMPLEMENTATION_ATTRIBUTE
182: + " attribute is required");
183: }
184:
185: try {
186: currentRID.setAuthorizedKeysFormat(Class
187: .forName(implementationClass));
188: currentRID
189: .setAuthorizedKeysDefaultPath(defaultPath);
190: } catch (ClassNotFoundException ex) {
191: throw new SAXException(ex.getMessage());
192: }
193: } else {
194: throw new SAXException("Unexpected element <"
195: + qname + ">");
196: }
197: } else {
198: throw new SAXException("Unexpected element <"
199: + qname + ">");
200: }
201: }
202:
203: currentElement = qname;
204: }
205:
206: public void endElement(String uri, String localName,
207: String qname) throws SAXException {
208: if (currentElement != null) {
209: if (!currentElement.equals(qname)) {
210: throw new SAXException(
211: "Unexpected end element found <" + qname
212: + ">");
213: }
214:
215: if (currentElement.equals(REMOTEID_ELEMENT)) {
216: if (currentRID.getRules().size() > 0) {
217: remoteIdentifications.put(currentRID
218: .getDefaultName(), currentRID);
219: } else {
220: throw new SAXException("<" + REMOTEID_ELEMENT
221: + "> "
222: + " requires at least one child <"
223: + RULE_ELEMENT + "> element!");
224: }
225:
226: currentElement = AUTOMATION_ELEMENT;
227: } else if (currentElement.equals(RULE_ELEMENT)) {
228: currentElement = REMOTEID_ELEMENT;
229: } else if (currentElement
230: .equals(AUTHORIZEDKEYSFORMAT_ELEMENT)) {
231: currentElement = REMOTEID_ELEMENT;
232: } else if (currentElement.equals(AUTOMATION_ELEMENT)) {
233: currentElement = null;
234: } else {
235: throw new SAXException("Unexpected end element <"
236: + qname + ">");
237: }
238: }
239: }
240: }
241: }
|