All Downloads are FREE. Search and download functionalities are using the official Maven repository.

juzu.impl.common.Lexers Maven / Gradle / Ivy

/*
 * Copyright 2013 eXo Platform SAS
 *
 * Licensed 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 juzu.impl.common;

import juzu.request.RequestParameter;

import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * Gather various lexers here.
 *
 * @author Julien Viet
 */
public class Lexers {

  /** The name validator. */
  static final Pattern NAME_VALIDATOR = Pattern.compile("(?!\\.)" + "[^/]+" + "(? 0) {
                  return parsePath(mode, base, padding, path, off + 3, size - 1);
                } else if (padding > 0) {
                  return parsePath(mode, base, padding - 1, path, off + 3, size);
                } else {
                  throw new IllegalArgumentException("Invalid path");
                }
              default:
                throw new AssertionError("Should not be here");
            }
          }
          for (int i = off;i < pos;i++) {
            if (path.charAt(i) == '.') {
              throw new IllegalArgumentException("No '.' allowed here");
            }
          }
          String[] ret = parsePath(mode, base, padding, path, pos + 1, size + 1);
          if (ret[at] == null) {
            ret[at] = path.substring(off, pos);
          }
          return ret;
        }
      }
    }
    else {
      String[] ret = new String[padding + size + 2];
      System.arraycopy(base, 0, ret, 0, padding);
      ret[at] = "";
      return ret;
    }
  }

  /**
   * Parse a dot separated name.
   *
   * @param s the sequence
   * @param from the from index
   * @param end the end index
   * @return the parsed identifiers
   * @throws IllegalArgumentException
   */
  public static String[] parseName(CharSequence s, int from, int end) throws IllegalArgumentException {
    if (from < 0) {
      throw new IllegalArgumentException("From bound " + from + " cannot be negative");
    }
    if (from > end) {
      throw new IllegalArgumentException("From bound " + from + " cannot be greater than the end bound " + end);
    }
    if (from == end) {
      return Name.EMPTY_STRING_ARRAY;
    } else {
      return parseName(0, s, from, end);
    }
  }

  private static String[] parseName(int size, CharSequence s, int from, int end) {
    int next = Tools.indexOf(s, '.', from, end);
    String[] identifiers;
    if (next < 0) {
      if (from == end) {
        throw new IllegalArgumentException("Empty segment");
      }
      identifiers = new String[size + 1];
      identifiers[size] = s.subSequence(from, end).toString();
    } else {
      if (next == from) {
        throw new IllegalArgumentException("Empty segment");
      }
      identifiers = parseName(size + 1, s, next + 1, end);
      identifiers[size] = s.subSequence(from, next).toString();
    }
    return identifiers;
  }

  public static Map parseQuery(String s) {
    return parseQuery(s, 0, s.length());
  }

  public static Map parseQuery(CharSequence s, int from, int to) {
    Map parameters = Collections.emptyMap();
    Iterator parser = queryParser(s, from, to);
    while (parser.hasNext()) {
      RequestParameter current = parser.next();
      if (parameters.isEmpty()) {
        parameters = new HashMap();
      }
      RequestParameter parameter = parameters.get(current.getName());
      if (parameter != null) {
        current = parameter.append(current);
      }
      parameters.put(current.getName(), current);
    }
    return parameters;
  }

  public static Iterator queryParser(final CharSequence s) {
    return queryParser(s, 0, s.length());

  }

  public static Iterator queryParser(final CharSequence s, final int from, final int to) {

    return new AbstractParameterParser(s, from, to) {
      @Override
      protected String safeDecodeName(String s) {
        return PercentCodec.RFC3986_QUERY_PARAM_NAME.safeDecode(s);
      }

      @Override
      protected String safeDecodeValue(String s) {
        return PercentCodec.RFC3986_QUERY_PARAM_VALUE.safeDecode(s);
      }
    }.iterator();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy