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

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

There is a newer version: 1.2.0
Show newest version
/*
 * 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 java.util.regex.Pattern;

/**
 * Gather various lexers here.
 *
 * @author Julien Viet
 */
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
   */
  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;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy