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.mru;
027:
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030:
031: import org.xml.sax.Attributes;
032: import org.xml.sax.SAXException;
033: import org.xml.sax.helpers.DefaultHandler;
034:
035: import java.io.File;
036: import java.io.IOException;
037: import java.io.InputStream;
038:
039: import java.util.Iterator;
040: import java.util.Stack;
041: import java.util.Vector;
042:
043: import javax.xml.parsers.ParserConfigurationException;
044: import javax.xml.parsers.SAXParser;
045: import javax.xml.parsers.SAXParserFactory;
046:
047: /**
048: *
049: *
050: * @author $author$
051: * @version $Revision: 1.15 $
052: */
053: public class MRUList extends java.util.Vector {
054: private static Log log = LogFactory.getLog(MRUList.class);
055: private static final String MRU_LIST_ELEMENT = "MRUList";
056: private static final String FILE_ELEMENT = "File";
057: private String currentElement = null;
058:
059: /**
060: * Creates a new MRUList object.
061: */
062: public MRUList() {
063: super ();
064: }
065:
066: /**
067: * Creates a new MRUList object.
068: *
069: * @param in
070: *
071: * @throws SAXException
072: * @throws ParserConfigurationException
073: * @throws IOException
074: */
075: public MRUList(InputStream in) throws SAXException,
076: ParserConfigurationException, IOException {
077: this ();
078: reload(in);
079: }
080:
081: /**
082: *
083: *
084: * @param in
085: *
086: * @throws SAXException
087: * @throws ParserConfigurationException
088: * @throws IOException
089: */
090: public void reload(InputStream in) throws SAXException,
091: ParserConfigurationException, IOException {
092: SAXParserFactory saxFactory = SAXParserFactory.newInstance();
093: SAXParser saxParser = saxFactory.newSAXParser();
094: saxParser.parse(in, new MRUSAXHandler());
095: }
096:
097: /**
098: *
099: *
100: * @return
101: */
102: public String toString() {
103: String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
104: xml += ("<!-- Most recently used -->\n<" + MRU_LIST_ELEMENT + ">\n");
105:
106: Iterator it = iterator();
107: File file = null;
108:
109: while (it.hasNext()) {
110: file = (File) it.next();
111: xml += (" " + "<" + FILE_ELEMENT + ">"
112: + file.getAbsolutePath() + "</" + FILE_ELEMENT + ">\n");
113: }
114:
115: xml += ("</" + MRU_LIST_ELEMENT + ">");
116:
117: return xml;
118: }
119:
120: private class MRUSAXHandler extends DefaultHandler {
121: private String MRU_LIST_ELEMENT = "MRUList";
122: private String FILE_ELEMENT = "File";
123: private File currentFile = null;
124: private Stack tags = new Stack();
125:
126: public void startElement(String uri, String localName,
127: String qname, Attributes attrs) throws SAXException {
128: ElementWrapper currentElement = (tags.size() == 0) ? null
129: : (ElementWrapper) tags.peek();
130:
131: if (currentElement == null) {
132: if (!qname.equals(MRU_LIST_ELEMENT)) {
133: throw new SAXException("Unexpected root element <"
134: + qname + ">");
135: }
136: } else {
137: if (currentElement.element.equals(MRU_LIST_ELEMENT)) {
138: if (qname.equals(FILE_ELEMENT)) {
139: } else {
140: throw new SAXException("Unexpected element <"
141: + qname + ">");
142: }
143: } else {
144: throw new SAXException("Unexpected element <"
145: + qname + ">");
146: }
147: }
148:
149: ElementWrapper w = new ElementWrapper(qname);
150: tags.push(w);
151: }
152:
153: public void characters(char[] ch, int start, int len)
154: throws SAXException {
155: ElementWrapper currentElement = (tags.size() == 0) ? null
156: : (ElementWrapper) tags.peek();
157:
158: if (currentElement != null) {
159: currentElement.text.append(new String(ch, start, len));
160: } else {
161: throw new SAXException("Unexpected text at " + start
162: + " for " + len);
163: }
164: }
165:
166: public void endElement(String uri, String localName,
167: String qname) throws SAXException {
168: ElementWrapper currentElement = (tags.size() == 0) ? null
169: : (ElementWrapper) tags.peek();
170:
171: if (currentElement != null) {
172: if (!currentElement.element.equals(qname)) {
173: throw new SAXException(
174: "Unexpected end element found <" + qname
175: + ">");
176: }
177:
178: if (currentElement.element.equals(FILE_ELEMENT)) {
179: MRUList.this .add(new File(currentElement.text
180: .toString()));
181: }
182:
183: tags.pop();
184: }
185: }
186: }
187:
188: public class ElementWrapper {
189: String element;
190: StringBuffer text;
191:
192: ElementWrapper(String element) {
193: this .element = element;
194: text = new StringBuffer();
195: }
196: }
197: }
|