示例#1
0
  @Override
  protected ParseResult parseNonEmptyToken(LoadContext context, DatasetVariable dv, String value) {
    int pipeLoc = value.indexOf(Constants.PIPE);
    if (pipeLoc == -1) {
      return new ParseResult.Fail(
          getTokenName() + " expected 2 pipe delimited arguments, found no pipe: " + value);
    }
    if (pipeLoc != value.lastIndexOf(Constants.PIPE)) {
      return new ParseResult.Fail(
          getTokenName() + " expected only 2 pipe delimited arguments, found: " + value);
    }
    String fullscope = value.substring(0, pipeLoc);
    String fvName = value.substring(pipeLoc + 1);
    String format;
    String varName;
    int equalLoc = fvName.indexOf('=');
    if (equalLoc != fvName.lastIndexOf('=')) {
      return new ParseResult.Fail(
          getTokenName() + " expected only 2 equal delimited arguments, found: " + value);
    }
    if (equalLoc == -1) {
      // Defaults to NUMBER
      format = "NUMBER";
      varName = fvName;
    } else {
      format = fvName.substring(0, equalLoc);
      varName = fvName.substring(equalLoc + 1);
    }
    if (dv.getDisplayName() != null) {
      return new ParseResult.Fail(getTokenName() + " must be the first token on the line");
    }

    VariableContext varContext = context.getVariableContext();
    FormatManager<?> formatManager;
    try {
      formatManager = context.getReferenceContext().getFormatManager(format);
    } catch (IllegalArgumentException e) {
      return new ParseResult.Fail(
          getTokenName()
              + " does not support format "
              + format
              + ", found in "
              + value
              + " due to "
              + e.getMessage());
    }
    LegalScope lvs = varContext.getScope(fullscope);

    if (!DatasetVariable.isLegalName(varName)) {
      return new ParseResult.Fail(varName + " is not a valid variable name");
    }
    boolean legal = varContext.assertLegalVariableID(lvs, formatManager, varName);
    if (!legal) {
      Set<LegalScope> known = varContext.getKnownLegalScopes(varName);
      StringBuilder sb = new StringBuilder();
      for (LegalScope v : known) {
        sb.append(v.getName());
        sb.append(", ");
      }
      return new ParseResult.Fail(
          getTokenName()
              + " found a var defined in incompatible variable scopes: "
              + varName
              + " was requested in "
              + fullscope
              + " but was previously in "
              + sb.toString(),
          context);
    }
    dv.setName(varName);
    dv.setFormat(format);
    dv.setScopeName(fullscope);
    return ParseResult.SUCCESS;
  }