Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You under the Apache License, Version 2.0
   * (the "License"); you may not use this file except in compliance with
   * the License.  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.apache.tomcat.util.http.fileupload;
 
 import java.util.Map;

A simple parser intended to parse sequences of name/value pairs. Parameter values are exptected to be enclosed in quotes if they contain unsafe characters, such as '=' characters or separators. Parameter values are optional and can be omitted.

param1 = value; param2 = "anything goes; really"; param3

Author(s):
Oleg Kalnichevski
 
 
 public class ParameterParser {
    
String to be parsed.
 
     private char[] chars = null;

    
Current position in the string.
 
     private int pos = 0;

    
Maximum position in the string.
 
     private int len = 0;

    
Start of a token.
 
     private int i1 = 0;

    
End of a token.
 
     private int i2 = 0;

    
Whether names stored in the map should be converted to lower case.
 
     private boolean lowerCaseNames = false;

    
Default ParameterParser constructor.
 
     public ParameterParser() {
         super();
     }

    
Are there any characters left to parse?

Returns:
true if there are unparsed characters, false otherwise.
 
     private boolean hasChar() {
         return this. < this.;
     }

    
A helper method to process the parsed token. This method removes leading and trailing blanks as well as enclosing quotation marks, when necessary.

Parameters:
quoted true if quotation marks are expected, false otherwise.
Returns:
the token
 
     private String getToken(boolean quoted) {
         // Trim leading white spaces
         while (( < ) && (Character.isWhitespace([]))) {
             ++;
         }
         // Trim trailing white spaces
         while (( > ) && (Character.isWhitespace([ - 1]))) {
            --;
        }
        // Strip away quotation marks if necessary
        if (quoted) {
            if ((( - ) >= 2)
                && ([] == '"')
                && ([ - 1] == '"')) {
                ++;
                --;
            }
        }
        String result = null;
        if ( > ) {
            result = new String( - );
        }
        return result;
    }

    
Tests if the given character is present in the array of characters.

Parameters:
ch the character to test for presense in the array of characters
charray the array of characters to test against
Returns:
true if the character is present in the array of characters, false otherwise.
    private boolean isOneOf(char chfinal char[] charray) {
        boolean result = false;
        for (int i = 0; i < charray.lengthi++) {
            if (ch == charray[i]) {
                result = true;
                break;
            }
        }
        return result;
    }

    
Parses out a token until any of the given terminators is encountered.

Parameters:
terminators the array of terminating characters. Any of these characters when encountered signify the end of the token
Returns:
the token
    private String parseToken(final char[] terminators) {
        char ch;
         = ;
         = ;
        while (hasChar()) {
            ch = [];
            if (isOneOf(chterminators)) {
                break;
            }
            ++;
            ++;
        }
        return getToken(false);
    }

    
Parses out a token until any of the given terminators is encountered outside the quotation marks.

Parameters:
terminators the array of terminating characters. Any of these characters when encountered outside the quotation marks signify the end of the token
Returns:
the token
    private String parseQuotedToken(final char[] terminators) {
        char ch;
         = ;
         = ;
        boolean quoted = false;
        boolean charEscaped = false;
        while (hasChar()) {
            ch = [];
            if (!quoted && isOneOf(chterminators)) {
                break;
            }
            if (!charEscaped && ch == '"') {
                quoted = !quoted;
            }
            charEscaped = (!charEscaped && ch == '\\');
            ++;
            ++;
        }
        return getToken(true);
    }

    
Returns true if parameter names are to be converted to lower case when name/value pairs are parsed.

Returns:
true if parameter names are to be converted to lower case when name/value pairs are parsed. Otherwise returns false
    public boolean isLowerCaseNames() {
        return this.;
    }

    
Sets the flag if parameter names are to be converted to lower case when name/value pairs are parsed.

Parameters:
b true if parameter names are to be converted to lower case when name/value pairs are parsed. false otherwise.
    public void setLowerCaseNames(boolean b) {
        this. = b;
    }

    
Extracts a map of name/value pairs from the given string. Names are expected to be unique. Multiple separators may be specified and the earliest found in the input string is used.

Parameters:
str the string that contains a sequence of name/value pairs
separators the name/value pairs separators
Returns:
a map of name/value pairs
    public Map<String,Stringparse(final String strchar[] separators) {
        if (separators == null || separators.length == 0) {
            return new HashMap<String,String>();
        }
        char separator = separators[0];
        if (str != null) {
            int idx = str.length();
            for (int i = 0;  i < separators.length;  i++) {
                int tmp = str.indexOf(separators[i]);
                if (tmp != -1) {
                    if (tmp < idx) {
                        idx = tmp;
                        separator = separators[i];
                    }
                }
            }
        }
        return parse(strseparator);
    }

    
Extracts a map of name/value pairs from the given string. Names are expected to be unique.

Parameters:
str the string that contains a sequence of name/value pairs
separator the name/value pairs separator
Returns:
a map of name/value pairs
    public Map<String,Stringparse(final String strchar separator) {
        if (str == null) {
            return new HashMap<String,String>();
        }
        return parse(str.toCharArray(), separator);
    }

    
Extracts a map of name/value pairs from the given array of characters. Names are expected to be unique.

Parameters:
inputChars the array of characters that contains a sequence of name/value pairs
separator the name/value pairs separator
Returns:
a map of name/value pairs
    public Map<String,Stringparse(final char[] inputCharschar separator) {
        if (inputChars == null) {
            return new HashMap<String,String>();
        }
        return parse(inputChars, 0, inputChars.lengthseparator);
    }

    
Extracts a map of name/value pairs from the given array of characters. Names are expected to be unique.

Parameters:
inputChars the array of characters that contains a sequence of name/value pairs
offset - the initial offset.
length - the length.
separator the name/value pairs separator
Returns:
a map of name/value pairs
    public Map<String,Stringparse(
        final char[] inputChars,
        int offset,
        int length,
        char separator) {
        if (inputChars == null) {
            return new HashMap<String,String>();
        }
        HashMap<String,Stringparams = new HashMap<String,String>();
        this. = inputChars;
        this. = offset;
        this. = length;
        String paramName = null;
        String paramValue = null;
        while (hasChar()) {
            paramName = parseToken(new char[] {
                    '='separator });
            paramValue = null;
            if (hasChar() && ([] == '=')) {
                ++; // skip '='
                paramValue = parseQuotedToken(new char[] {
                        separator });
            }
            if (hasChar() && ([] == separator)) {
                ++; // skip separator
            }
            if ((paramName != null) && (paramName.length() > 0)) {
                if (this.) {
                    paramName = paramName.toLowerCase(.);
                }
                params.put(paramNameparamValue);
            }
        }
        return params;
    }
New to GrepCode? Check out our FAQ X