001: /* ===========================================================================
002: * $RCSfile: Header.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.lang.Math;
023: import java.io.*;
024: import java.util.*;
025:
026: /**
027: * An RFC822 'header' is a 'tag' / 'value' pair.
028: *
029: * @author Mark Welsh
030: */
031: public class Header {
032: // Constants -------------------------------------------------------------
033: /* Maximum length of header line in a section, before break to next line */
034: private static final int MAX_HEADER_LINE_LENGTH = 70;
035:
036: // Fields ----------------------------------------------------------------
037: private String tag;
038: private String value;
039:
040: // Class Methods ---------------------------------------------------------
041: /** Parse a header from the specified String. */
042: public static Header parse(String line) {
043: Header header = null;
044: if (line != null) {
045: int pos = line.indexOf(':');
046: if (pos != -1) {
047: header = new Header(line.substring(0, pos).trim(), line
048: .substring(pos + 1).trim());
049: }
050: }
051: return header;
052: }
053:
054: // Instance Methods ------------------------------------------------------
055: /** Ctor. */
056: public Header(String tag, String value) {
057: this .tag = (tag == null ? "" : tag);
058: this .value = (value == null ? "" : value);
059: }
060:
061: /** Return the tag. */
062: public String getTag() {
063: return tag;
064: }
065:
066: /** Return the value. */
067: public String getValue() {
068: return value;
069: }
070:
071: /** Test equality of headers. */
072: public boolean equals(Object o) {
073: if (o instanceof Header) {
074: Header header = (Header) o;
075: if (header.getTag().equals(getTag())
076: && header.getValue().equals(getValue())) {
077: return true;
078: }
079: }
080: return false;
081: }
082:
083: /** Print String rep of this object to a java.io.Writer. */
084: public void writeString(Writer writer) throws IOException {
085: String prefix = getTag() + ": ";
086: String value = getValue();
087: for (int index = 0; index < value.length(); prefix = " ") // continuation lines are prefixed with single space
088: {
089: int start = index;
090: // Compute length of value that can be appended to this line
091: index += Math.min(value.length() - index,
092: MAX_HEADER_LINE_LENGTH - prefix.length());
093: // Write tag or continuation space, (part of) value, EOL
094: writer.write(prefix + value.substring(start, index)
095: + "\015\012");
096: }
097: }
098:
099: /** Return String rep of this object. */
100: public String toString() {
101: return getTag() + ": " + getValue();
102: }
103: }
|