View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.impl.cookie;
29  
30  import java.util.ArrayList;
31  import java.util.BitSet;
32  import java.util.List;
33  
34  import org.apache.http.HeaderElement;
35  import org.apache.http.NameValuePair;
36  import org.apache.http.ParseException;
37  import org.apache.http.annotation.Immutable;
38  import org.apache.http.message.BasicHeaderElement;
39  import org.apache.http.message.BasicNameValuePair;
40  import org.apache.http.message.ParserCursor;
41  import org.apache.http.message.TokenParser;
42  import org.apache.http.util.Args;
43  import org.apache.http.util.CharArrayBuffer;
44  
45  /**
46   *
47   * @since 4.0
48   */
49  @Immutable
50  public class NetscapeDraftHeaderParser {
51  
52      public final static NetscapeDraftHeaderParser DEFAULT = new NetscapeDraftHeaderParser();
53  
54      private final static char PARAM_DELIMITER                = ';';
55  
56      // IMPORTANT!
57      // These private static variables must be treated as immutable and never exposed outside this class
58      private static final BitSet TOKEN_DELIMS = TokenParser.INIT_BITSET('=', PARAM_DELIMITER);
59      private static final BitSet VALUE_DELIMS = TokenParser.INIT_BITSET(PARAM_DELIMITER);
60  
61      private final TokenParser tokenParser;
62  
63      public NetscapeDraftHeaderParser() {
64          super();
65          this.tokenParser = TokenParser.INSTANCE;
66      }
67  
68      public HeaderElement parseHeader(
69              final CharArrayBuffer buffer,
70              final ParserCursor cursor) throws ParseException {
71          Args.notNull(buffer, "Char array buffer");
72          Args.notNull(cursor, "Parser cursor");
73          final NameValuePair nvp = parseNameValuePair(buffer, cursor);
74          final List<NameValuePair> params = new ArrayList<NameValuePair>();
75          while (!cursor.atEnd()) {
76              final NameValuePair param = parseNameValuePair(buffer, cursor);
77              params.add(param);
78          }
79          return new BasicHeaderElement(
80                  nvp.getName(),
81                  nvp.getValue(), params.toArray(new NameValuePair[params.size()]));
82      }
83  
84      private NameValuePair parseNameValuePair(
85              final CharArrayBuffer buffer, final ParserCursor cursor) {
86          final String name = tokenParser.parseToken(buffer, cursor, TOKEN_DELIMS);
87          if (cursor.atEnd()) {
88              return new BasicNameValuePair(name, null);
89          }
90          final int delim = buffer.charAt(cursor.getPos());
91          cursor.updatePos(cursor.getPos() + 1);
92          if (delim != '=') {
93              return new BasicNameValuePair(name, null);
94          }
95          final String value = tokenParser.parseToken(buffer, cursor, VALUE_DELIMS);
96          if (!cursor.atEnd()) {
97              cursor.updatePos(cursor.getPos() + 1);
98          }
99          return new BasicNameValuePair(name, value);
100     }
101 
102 }