Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: javax/mail/internet/HeaderTokenizer.java


1   /**
2    *
3    * Copyright 2003-2004 The Apache Software Foundation
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  
18  package javax.mail.internet;
19  
20  /**
21   * @version $Rev: 54266 $ $Date: 2004-10-10 14:02:50 -0700 (Sun, 10 Oct 2004) $
22   */
23  public class HeaderTokenizer {
24      public static class Token {
25          // Constant values from J2SE 1.4 API Docs (Constant values)
26          public static final int ATOM = -1;
27          public static final int COMMENT = -3;
28          public static final int EOF = -4;
29          public static final int QUOTEDSTRING = -2;
30          private int _type;
31          private String _value;
32  
33          public Token(int type, String value) {
34              _type = type;
35              _value = value;
36          }
37  
38          public int getType() {
39              return _type;
40          }
41  
42          public String getValue() {
43              return _value;
44          }
45      }
46  
47      private static final Token EOF = new Token(Token.EOF, null);
48      // characters not allowed in MIME
49      public static final String MIME = "()<>@,;:\\\"\t []/?=";
50      // charaters not allowed in RFC822
51      public static final String RFC822 = "()<>@,;:\\\"\t .[]";
52      private static final String WHITE = " \t\n\r";
53      private String _delimiters;
54      private String _header;
55      private boolean _skip;
56      private int pos;
57  
58      public HeaderTokenizer(String header) {
59          this(header, RFC822);
60      }
61  
62      public HeaderTokenizer(String header, String delimiters) {
63          this(header, delimiters, true);
64      }
65  
66      public HeaderTokenizer(String header,
67                             String delimiters,
68                             boolean skipComments) {
69          _skip = skipComments;
70          _header = header;
71          _delimiters = delimiters;
72      }
73  
74      public String getRemainder() {
75          return _header.substring(pos);
76      }
77  
78      public Token next() throws ParseException {
79          return readToken();
80      }
81  
82      public Token peek() throws ParseException {
83          int start = pos;
84          try {
85              return readToken();
86          } finally {
87              pos = start;
88          }
89      }
90  
91      /**
92       * @return
93       */
94      private Token readAtomicToken() {
95          // skip to next delimiter
96          int start = pos;
97          while (++pos < _header.length()
98                  && _delimiters.indexOf(_header.charAt(pos)) == -1)
99              ;
100         return new Token(Token.ATOM, _header.substring(start, pos));
101     }
102 
103     private Token readToken() throws ParseException {
104         if (pos >= _header.length()) {
105             return EOF;
106         } else {
107             char c = _header.charAt(pos);
108             if (c == '(') {
109                 Token comment = readUntil(')', Token.COMMENT);
110                 if (_skip) {
111                     return readToken();
112                 } else {
113                     return comment;
114                 }
115             } else if (c == '\"') {
116                 return readUntil('\"', Token.QUOTEDSTRING);
117             } else if (WHITE.indexOf(c) != -1) {
118                 eatWhiteSpace();
119                 return readToken();
120             } else if (_delimiters.indexOf(c) != -1) {
121                 pos++;
122                 return new Token(Token.ATOM, String.valueOf(c));
123             } else {
124                 return readAtomicToken();
125             }
126         }
127     }
128 
129     /**
130      * @return
131      */
132     private Token readUntil(char end, int type) {
133         int start = ++pos;
134         // skip to end of comment/string
135         while (++pos < _header.length()
136                 && _header.charAt(pos) != end)
137             ;
138         String value = _header.substring(start, pos++);
139         return new Token(type, value);
140     }
141 
142     /**
143      * @return
144      */
145     private void eatWhiteSpace() {
146         // skip to end of whitespace
147         while (++pos < _header.length()
148                 && WHITE.indexOf(_header.charAt(pos)) != -1)
149             ;
150     }
151 }