commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d..@apache.org
Subject cvs commit: jakarta-commons-sandbox/http/src/java/org/apache/commons/http HttpHeaderTokenizer.java
Date Fri, 11 Apr 2003 21:13:10 GMT
dlr         2003/04/11 14:13:10

  Added:       http/src/java/org/apache/commons/http
                        HttpHeaderTokenizer.java
  Log:
  * http/src/java/org/apache/commons/http/HttpHeaderTokenizer.java
    Parses the HTTP headers (such as Accept-Language, Accept-Charset,
    etc.) as per section 14.4 of RFC 2068 (HTTP 1.1 header field
    definitions).
  
    More useful placed elsewhere?
  
  Revision  Changes    Path
  1.1                  jakarta-commons-sandbox/http/src/java/org/apache/commons/http/HttpHeaderTokenizer.java
  
  Index: HttpHeaderTokenizer.java
  ===================================================================
  package org.apache.commons.http;
  
  /*
   * Copyright (c) 2003 The Apache Jakarta Project.  All rights reserved.
   */
  
  import java.util.ArrayList;
  import java.util.Collections;
  import java.util.Iterator;
  import java.util.Locale;
  import java.util.NoSuchElementException;
  import java.util.StringTokenizer;
  
  /**
   * Parses the HTTP headers (such as <code>Accept-Language</code>,
   * <code>Accept-Charset</code>, etc.) as per section 14.4 of RFC 2068
   * (HTTP 1.1 header field definitions).
   *
   * @author <a href="mailto:dlr@collab.net">Daniel Rall</a>
   */
  public class HttpHeaderTokenizer
      implements Iterator
  {
      /**
       * Separates elements of the HTTP header.
       */
      private static final String ELEMENT_SEPARATOR = ",";
  
      /**
       * Separates element value from quality within elements.
       */
      private static final char QUALITY_SEPARATOR = ';';
  
      /**
       * The default quality value for our HeaderElement object.
       */
      private static final Float DEFAULT_QUALITY = new Float(1.0f);
  
      /**
       * The parsed {@link #HeaderElement} objects.
       */
      private ArrayList headerElements = new ArrayList(3);
  
      /**
       * Parses the (possibly multi-element) header.
       *
       * @param header The header.  For example, an
       * <code>Accept-Language</code> header might look like <code>en,
       * es;q=0.8, zh-TW;q=0.1</code>.
       */
      public HttpHeaderTokenizer(String header)
      {
          StringTokenizer tok = new StringTokenizer(header, ELEMENT_SEPARATOR);
          while (tok.hasMoreTokens())
          {
              HeaderElement headerElement = new HeaderElement();
              String element = tok.nextToken().trim();
              int index;
  
              // Record and cut off any quality value that comes after a
              // semi-colon.
              if ( (index = element.indexOf(QUALITY_SEPARATOR)) != -1 )
              {
                  String q = element.substring(index);
                  element = element.substring(0, index);
                  if ( (index = q.indexOf('=')) != -1 )
                  {
                      try
                      {
                          headerElement.quality =
                              Float.valueOf(q.substring(index + 1));
                      }
                      catch (NumberFormatException useDefault)
                      {
                      }
                  }
              }
  
              elementValueParsed(headerElement, element.trim());
  
              headerElements.add(headerElement);
          }
  
          // Sort by quality in descending order.
          Collections.sort(headerElements, Collections.reverseOrder());
      }
  
      /**
       * Callback for a parsed element value.  Override this method to
       * perform custom element value handling.
       *
       * @param element The header element object to set the value for.
       * @param value The element's value.
       */
      protected void elementValueParsed(HeaderElement element, String value)
      {
          element.value = value;
      }
  
      /**
       * @return Whether there are more headerElements.
       */
      public boolean hasNext()
      {
          return !headerElements.isEmpty();
      }
  
      /**
       * Creates a <code>Locale</code> from the next element of the HTTP
       * header.
       *
       * @return The next highest-rated <code>Locale</code>.
       * @throws NoSuchElementException No more headerElements.
       */
      public Object next()
      {
          if (headerElements.isEmpty())
          {
              throw new NoSuchElementException();
          }
          return ((HeaderElement) headerElements.remove(0)).value;
      }
  
      /**
       * Not implemented.
       */
      public final void remove()
      {
          throw new UnsupportedOperationException(getClass().getName() +
                                                  " does not support remove()");
      }
  
      /**
       * Data structure representing an element of the HTTP header.
       */
      protected class HeaderElement implements Comparable
      {
          /**
           * This element's value.
           */
          protected Object value;
  
          /**
           * The quality of our locale (as values approach
           * <code>1.0</code>, they indicate increased user preference).
           */
          protected Float quality = DEFAULT_QUALITY;
  
          public final int compareTo(Object headerElement)
          {
              return quality.compareTo(((HeaderElement) headerElement).quality);
          }
  
          public String toString()
          {
              return (value != null ? value.toString() : null);
          }
      }
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message