001: /* ===========================================================================
002: * $RCSfile: SectionList.java,v $
003: * ===========================================================================
004: *
005: * RetroGuard -- an obfuscation package for Java classfiles.
006: *
007: * Copyright (c) 1998-2006 Mark Welsh (markw@retrologic.com)
008: *
009: * This program can be redistributed and/or modified under the terms of the
010: * Version 2 of the GNU General Public License as published by the Free
011: * Software Foundation.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: */
019:
020: package COM.rl.util.rfc822;
021:
022: import java.io.*;
023: import java.util.*;
024:
025: /**
026: * A list of RFC822 sections, usually representing a file.
027: *
028: * @author Mark Welsh
029: */
030: public class SectionList {
031: // Constants -------------------------------------------------------------
032:
033: // Fields ----------------------------------------------------------------
034: private Vector sections;
035:
036: // Class Methods ---------------------------------------------------------
037:
038: // Instance Methods ------------------------------------------------------
039: /** Construct with no initial sections. */
040: public SectionList() {
041: sections = new Vector();
042: }
043:
044: /** Parse the stream, appending the sections found there to our list. */
045: public void parse(InputStream in) {
046: // Wrap the stream for text reading
047: BufferedReader reader = new BufferedReader(
048: new InputStreamReader(in));
049:
050: // Read until end of file
051: String line;
052: Section section = null;
053: try {
054: while ((line = reader.readLine()) != null) {
055: // If at end of section, close section
056: if (section != null && line.indexOf(':') == -1) {
057: add(section);
058: section = null;
059: }
060:
061: // If at start of section, skip non-header lines
062: if (section == null && line.indexOf(':') == -1) {
063: continue;
064: }
065:
066: // Start or continue section
067: if (section == null) {
068: section = new Section();
069: }
070:
071: // Read header, potentially split over multiple lines
072: boolean done = false;
073: while (!done) {
074: // Mark for reset, read a line, and...
075: reader.mark(80);
076: String nextLine = reader.readLine();
077: if (nextLine == null) // stop on EOF, or...
078: {
079: done = true;
080: } else if (nextLine.indexOf(' ') == 0) // append, or...
081: {
082: line = line.concat(nextLine.substring(1));
083: } else // new section, so reset in preparation for read.
084: {
085: reader.reset();
086: done = true;
087: }
088: }
089: // Parse the header and add to this section
090: section.add(Header.parse(line));
091: }
092: } catch (IOException e) {
093: // Unexpected EOF during readLine() can cause this.
094: // Just terminate the read quietly.
095: return;
096: }
097: }
098:
099: /** Add a Section to the list. */
100: public void add(Section section) {
101: sections.addElement(section);
102: }
103:
104: /** Return an Enumeration of sections. */
105: public Enumeration elements() {
106: return sections.elements();
107: }
108:
109: /** Find the first section in the list containing the matching header. */
110: public Section find(String tag, String value) {
111: return find(new Header(tag, value));
112: }
113:
114: /** Find the first section in the list containing the matching header. */
115: public Section find(Header header) {
116: for (Enumeration enm = elements(); enm.hasMoreElements();) {
117: Section section = (Section) enm.nextElement();
118: if (section.hasHeader(header)) {
119: return section;
120: }
121: }
122: return null;
123: }
124:
125: /** Print String rep of this object to a java.io.Writer. */
126: public void writeString(Writer writer) throws IOException {
127: for (Enumeration enm = elements(); enm.hasMoreElements();) {
128: ((Section) enm.nextElement()).writeString(writer);
129: }
130: }
131:
132: /** Return String rep of this object. */
133: public String toString() {
134: StringBuffer sb = new StringBuffer();
135: for (Enumeration enm = elements(); enm.hasMoreElements();) {
136: sb.append(((Section) enm.nextElement()).toString());
137: }
138: return sb.toString();
139: }
140: }
|