001: /* ===========================================================================
002: * $RCSfile: Section.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: * An RFC822 section is a list of 'header's (tag/value pairs).
027: *
028: * @author Mark Welsh
029: */
030: public class Section {
031: // Constants -------------------------------------------------------------
032:
033: // Fields ----------------------------------------------------------------
034: private Vector headers;
035:
036: // Class Methods ---------------------------------------------------------
037:
038: // Instance Methods ------------------------------------------------------
039: /** Blank section. */
040: public Section() {
041: headers = new Vector();
042: }
043:
044: /** Append a header to this section. */
045: public void add(Header header) {
046: headers.addElement(header);
047: }
048:
049: /** Append a header to this section. */
050: public void add(String tag, String value) {
051: add(new Header(tag, value));
052: }
053:
054: /** Return an Enumeration of headers. */
055: public Enumeration elements() {
056: return headers.elements();
057: }
058:
059: /** Does the section contain a header matching the specified one? */
060: public boolean hasHeader(Header queryHeader) {
061: if (queryHeader != null) {
062: for (Enumeration enm = headers.elements(); enm
063: .hasMoreElements();) {
064: Header header = (Header) enm.nextElement();
065: if (queryHeader.equals(header)) {
066: return true;
067: }
068: }
069: }
070: return false;
071: }
072:
073: /** Find a header matching the specified tag, or null if none. */
074: public Header findTag(String tag) {
075: // Check params
076: if (tag == null)
077: return null;
078:
079: // For now, do linear search of headers
080: for (Enumeration enm = headers.elements(); enm
081: .hasMoreElements();) {
082: Header header = (Header) enm.nextElement();
083: if (tag.equals(header.getTag())) {
084: // Found
085: return header;
086: }
087: }
088:
089: // Not found
090: return null;
091: }
092:
093: /** Print String rep of this object to a java.io.Writer. */
094: public void writeString(Writer writer) throws IOException {
095: for (Enumeration enm = headers.elements(); enm
096: .hasMoreElements();) {
097: ((Header) enm.nextElement()).writeString(writer);
098: }
099: writer.write("\015\012");
100: }
101:
102: /** Return String rep of this object. */
103: public String toString() {
104: StringBuffer sb = new StringBuffer();
105: for (Enumeration enm = headers.elements(); enm
106: .hasMoreElements();) {
107: sb.append(((Header) enm.nextElement()).toString());
108: sb.append("\015\012");
109: }
110: sb.append("\015\012");
111: return sb.toString();
112: }
113: }
|