1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/messaging/saaj/packaging/mime/internet/HeaderTokenizer.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,382 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +/* 1.30 + * @(#)HeaderTokenizer.java 1.9 02/03/27 1.31 + */ 1.32 + 1.33 + 1.34 + 1.35 +package com.sun.xml.internal.messaging.saaj.packaging.mime.internet; 1.36 + 1.37 + 1.38 +/** 1.39 + * This class tokenizes RFC822 and MIME headers into the basic 1.40 + * symbols specified by RFC822 and MIME. <p> 1.41 + * 1.42 + * This class handles folded headers (ie headers with embedded 1.43 + * CRLF SPACE sequences). The folds are removed in the returned 1.44 + * tokens. 1.45 + * 1.46 + * @version 1.9, 02/03/27 1.47 + * @author John Mani 1.48 + */ 1.49 + 1.50 +public class HeaderTokenizer { 1.51 + 1.52 + /** 1.53 + * The Token class represents tokens returned by the 1.54 + * HeaderTokenizer. 1.55 + */ 1.56 + public static class Token { 1.57 + 1.58 + private int type; 1.59 + private String value; 1.60 + 1.61 + /** 1.62 + * Token type indicating an ATOM. 1.63 + */ 1.64 + public static final int ATOM = -1; 1.65 + 1.66 + /** 1.67 + * Token type indicating a quoted string. The value 1.68 + * field contains the string without the quotes. 1.69 + */ 1.70 + public static final int QUOTEDSTRING = -2; 1.71 + 1.72 + /** 1.73 + * Token type indicating a comment. The value field 1.74 + * contains the comment string without the comment 1.75 + * start and end symbols. 1.76 + */ 1.77 + public static final int COMMENT = -3; 1.78 + 1.79 + /** 1.80 + * Token type indicating end of input. 1.81 + */ 1.82 + public static final int EOF = -4; 1.83 + 1.84 + /** 1.85 + * Constructor. 1.86 + * @param type Token type 1.87 + * @param value Token value 1.88 + */ 1.89 + public Token(int type, String value) { 1.90 + this.type = type; 1.91 + this.value = value; 1.92 + } 1.93 + 1.94 + /** 1.95 + * Return the type of the token. If the token represents a 1.96 + * delimiter or a control character, the type is that character 1.97 + * itself, converted to an integer. Otherwise, it's value is 1.98 + * one of the following: 1.99 + * <ul> 1.100 + * <li><code>ATOM</code> A sequence of ASCII characters 1.101 + * delimited by either SPACE, CTL, "(", <"> or the 1.102 + * specified SPECIALS 1.103 + * <li><code>QUOTEDSTRING</code> A sequence of ASCII characters 1.104 + * within quotes 1.105 + * <li><code>COMMENT</code> A sequence of ASCII characters 1.106 + * within "(" and ")". 1.107 + * <li><code>EOF</code> End of header 1.108 + * </ul> 1.109 + */ 1.110 + public int getType() { 1.111 + return type; 1.112 + } 1.113 + 1.114 + /** 1.115 + * Returns the value of the token just read. When the current 1.116 + * token is a quoted string, this field contains the body of the 1.117 + * string, without the quotes. When the current token is a comment, 1.118 + * this field contains the body of the comment. 1.119 + * 1.120 + * @return token value 1.121 + */ 1.122 + public String getValue() { 1.123 + return value; 1.124 + } 1.125 + } 1.126 + 1.127 + private String string; // the string to be tokenized 1.128 + private boolean skipComments; // should comments be skipped ? 1.129 + private String delimiters; // delimiter string 1.130 + private int currentPos; // current parse position 1.131 + private int maxPos; // string length 1.132 + private int nextPos; // track start of next Token for next() 1.133 + private int peekPos; // track start of next Token for peek() 1.134 + 1.135 + /** 1.136 + * RFC822 specials 1.137 + */ 1.138 + public final static String RFC822 = "()<>@,;:\\\"\t .[]"; 1.139 + 1.140 + /** 1.141 + * MIME specials 1.142 + */ 1.143 + public final static String MIME = "()<>@,;:\\\"\t []/?="; 1.144 + 1.145 + // The EOF Token 1.146 + private final static Token EOFToken = new Token(Token.EOF, null); 1.147 + 1.148 + /** 1.149 + * Constructor that takes a rfc822 style header. 1.150 + * 1.151 + * @param header The rfc822 header to be tokenized 1.152 + * @param delimiters Set of delimiter characters 1.153 + * to be used to delimit ATOMS. These 1.154 + * are usually <code>RFC822</code> or 1.155 + * <code>MIME</code> 1.156 + * @param skipComments If true, comments are skipped and 1.157 + * not returned as tokens 1.158 + */ 1.159 + public HeaderTokenizer(String header, String delimiters, 1.160 + boolean skipComments) { 1.161 + string = (header == null) ? "" : header; // paranoia ?! 1.162 + this.skipComments = skipComments; 1.163 + this.delimiters = delimiters; 1.164 + currentPos = nextPos = peekPos = 0; 1.165 + maxPos = string.length(); 1.166 + } 1.167 + 1.168 + /** 1.169 + * Constructor. Comments are ignored and not returned as tokens 1.170 + * 1.171 + * @param header The header that is tokenized 1.172 + * @param delimiters The delimiters to be used 1.173 + */ 1.174 + public HeaderTokenizer(String header, String delimiters) { 1.175 + this(header, delimiters, true); 1.176 + } 1.177 + 1.178 + /** 1.179 + * Constructor. The RFC822 defined delimiters - RFC822 - are 1.180 + * used to delimit ATOMS. Also comments are skipped and not 1.181 + * returned as tokens 1.182 + */ 1.183 + public HeaderTokenizer(String header) { 1.184 + this(header, RFC822); 1.185 + } 1.186 + 1.187 + /** 1.188 + * Parses the next token from this String. <p> 1.189 + * 1.190 + * Clients sit in a loop calling next() to parse successive 1.191 + * tokens until an EOF Token is returned. 1.192 + * 1.193 + * @return the next Token 1.194 + * @exception ParseException if the parse fails 1.195 + */ 1.196 + public Token next() throws ParseException { 1.197 + Token tk; 1.198 + 1.199 + currentPos = nextPos; // setup currentPos 1.200 + tk = getNext(); 1.201 + nextPos = peekPos = currentPos; // update currentPos and peekPos 1.202 + return tk; 1.203 + } 1.204 + 1.205 + /** 1.206 + * Peek at the next token, without actually removing the token 1.207 + * from the parse stream. Invoking this method multiple times 1.208 + * will return successive tokens, until <code>next()</code> is 1.209 + * called. <p> 1.210 + * 1.211 + * @return the next Token 1.212 + * @exception ParseException if the parse fails 1.213 + */ 1.214 + public Token peek() throws ParseException { 1.215 + Token tk; 1.216 + 1.217 + currentPos = peekPos; // setup currentPos 1.218 + tk = getNext(); 1.219 + peekPos = currentPos; // update peekPos 1.220 + return tk; 1.221 + } 1.222 + 1.223 + /** 1.224 + * Return the rest of the Header. 1.225 + * 1.226 + * @return String rest of header. null is returned if we are 1.227 + * already at end of header 1.228 + */ 1.229 + public String getRemainder() { 1.230 + return string.substring(nextPos); 1.231 + } 1.232 + 1.233 + /* 1.234 + * Return the next token starting from 'currentPos'. After the 1.235 + * parse, 'currentPos' is updated to point to the start of the 1.236 + * next token. 1.237 + */ 1.238 + private Token getNext() throws ParseException { 1.239 + // If we're already at end of string, return EOF 1.240 + if (currentPos >= maxPos) 1.241 + return EOFToken; 1.242 + 1.243 + // Skip white-space, position currentPos beyond the space 1.244 + if (skipWhiteSpace() == Token.EOF) 1.245 + return EOFToken; 1.246 + 1.247 + char c; 1.248 + int start; 1.249 + boolean filter = false; 1.250 + 1.251 + c = string.charAt(currentPos); 1.252 + 1.253 + // Check or Skip comments and position currentPos 1.254 + // beyond the comment 1.255 + while (c == '(') { 1.256 + // Parsing comment .. 1.257 + int nesting; 1.258 + for (start = ++currentPos, nesting = 1; 1.259 + nesting > 0 && currentPos < maxPos; 1.260 + currentPos++) { 1.261 + c = string.charAt(currentPos); 1.262 + if (c == '\\') { // Escape sequence 1.263 + currentPos++; // skip the escaped character 1.264 + filter = true; 1.265 + } else if (c == '\r') 1.266 + filter = true; 1.267 + else if (c == '(') 1.268 + nesting++; 1.269 + else if (c == ')') 1.270 + nesting--; 1.271 + } 1.272 + if (nesting != 0) 1.273 + throw new ParseException("Unbalanced comments"); 1.274 + 1.275 + if (!skipComments) { 1.276 + // Return the comment, if we are asked to. 1.277 + // Note that the comment start & end markers are ignored. 1.278 + String s; 1.279 + if (filter) // need to go thru the token again. 1.280 + s = filterToken(string, start, currentPos-1); 1.281 + else 1.282 + s = string.substring(start,currentPos-1); 1.283 + 1.284 + return new Token(Token.COMMENT, s); 1.285 + } 1.286 + 1.287 + // Skip any whitespace after the comment. 1.288 + if (skipWhiteSpace() == Token.EOF) 1.289 + return EOFToken; 1.290 + c = string.charAt(currentPos); 1.291 + } 1.292 + 1.293 + // Check for quoted-string and position currentPos 1.294 + // beyond the terminating quote 1.295 + if (c == '"') { 1.296 + for (start = ++currentPos; currentPos < maxPos; currentPos++) { 1.297 + c = string.charAt(currentPos); 1.298 + if (c == '\\') { // Escape sequence 1.299 + currentPos++; 1.300 + filter = true; 1.301 + } else if (c == '\r') 1.302 + filter = true; 1.303 + else if (c == '"') { 1.304 + currentPos++; 1.305 + String s; 1.306 + 1.307 + if (filter) 1.308 + s = filterToken(string, start, currentPos-1); 1.309 + else 1.310 + s = string.substring(start,currentPos-1); 1.311 + 1.312 + return new Token(Token.QUOTEDSTRING, s); 1.313 + } 1.314 + } 1.315 + throw new ParseException("Unbalanced quoted string"); 1.316 + } 1.317 + 1.318 + // Check for SPECIAL or CTL 1.319 + if (c < 040 || c >= 0177 || delimiters.indexOf(c) >= 0) { 1.320 + currentPos++; // re-position currentPos 1.321 + char ch[] = new char[1]; 1.322 + ch[0] = c; 1.323 + return new Token((int)c, new String(ch)); 1.324 + } 1.325 + 1.326 + // Check for ATOM 1.327 + for (start = currentPos; currentPos < maxPos; currentPos++) { 1.328 + c = string.charAt(currentPos); 1.329 + // ATOM is delimited by either SPACE, CTL, "(", <"> 1.330 + // or the specified SPECIALS 1.331 + if (c < 040 || c >= 0177 || c == '(' || c == ' ' || 1.332 + c == '"' || delimiters.indexOf(c) >= 0) 1.333 + break; 1.334 + } 1.335 + return new Token(Token.ATOM, string.substring(start, currentPos)); 1.336 + } 1.337 + 1.338 + // Skip SPACE, HT, CR and NL 1.339 + private int skipWhiteSpace() { 1.340 + char c; 1.341 + for (; currentPos < maxPos; currentPos++) 1.342 + if (((c = string.charAt(currentPos)) != ' ') && 1.343 + (c != '\t') && (c != '\r') && (c != '\n')) 1.344 + return currentPos; 1.345 + return Token.EOF; 1.346 + } 1.347 + 1.348 + /* Process escape sequences and embedded LWSPs from a comment or 1.349 + * quoted string. 1.350 + */ 1.351 + private static String filterToken(String s, int start, int end) { 1.352 + StringBuffer sb = new StringBuffer(); 1.353 + char c; 1.354 + boolean gotEscape = false; 1.355 + boolean gotCR = false; 1.356 + 1.357 + for (int i = start; i < end; i++) { 1.358 + c = s.charAt(i); 1.359 + if (c == '\n' && gotCR) { 1.360 + // This LF is part of an unescaped 1.361 + // CRLF sequence (i.e, LWSP). Skip it. 1.362 + gotCR = false; 1.363 + continue; 1.364 + } 1.365 + 1.366 + gotCR = false; 1.367 + if (!gotEscape) { 1.368 + // Previous character was NOT '\' 1.369 + if (c == '\\') // skip this character 1.370 + gotEscape = true; 1.371 + else if (c == '\r') // skip this character 1.372 + gotCR = true; 1.373 + else // append this character 1.374 + sb.append(c); 1.375 + } else { 1.376 + // Previous character was '\'. So no need to 1.377 + // bother with any special processing, just 1.378 + // append this character 1.379 + sb.append(c); 1.380 + gotEscape = false; 1.381 + } 1.382 + } 1.383 + return sb.toString(); 1.384 + } 1.385 +}