Esempio n. 1
0
  /**
   * Parse out a list of {@link Definition} from the provided text description.
   *
   * <p>The format expected here is one definition per line; using the format
   * "name=...expression..".
   *
   * @param definition
   * @return List of definition
   */
  public static List<Definition> toDefinition(String definition) {
    List<Definition> list = new ArrayList<Definition>();
    HashSet<String> check = new HashSet<String>();

    // clean up cross platform differences of line feed
    String[] defs = splitDefinitions(definition);

    for (String line : defs) {
      int mark = line.indexOf("=");
      if (mark != -1) {
        String name = line.substring(0, mark).trim();
        String expressionDefinition = line.substring(mark + 1).trim();

        if (check.contains(name)) {
          throw new IllegalArgumentException("Attribute " + name + " defined more than once");
        }
        Expression expression;
        try {
          expression = ECQL.toExpression(expressionDefinition);
        } catch (CQLException e) {
          throw new IllegalArgumentException(
              "Unable to parse expression " + name + "=" + expressionDefinition + " " + e, e);
        }
        Definition def = new Definition();
        def.name = name;
        def.expression = expression;
        list.add(def);
        check.add(name); // to catch duplicates!
      }
    }
    return list;
  }