Example #1
0
  @Test
  public void testNoIterations() throws SyntaxException {

    // Create the loop.
    Operation op =
        new Foreach(
            null,
            new SetValue(null, "k", new Operation[] {}),
            new SetValue(null, "v", new Operation[] {}),
            new HashResource(),
            BooleanProperty.TRUE);

    // Run the operation.
    Context context = new CompileTimeContext();
    Element r1 = op.execute(context);

    // Check the value.
    assertTrue(r1 instanceof Undef);

    // Check side-effects of the loop.
    Element k = context.getLocalVariable("k");
    Element v = context.getLocalVariable("v");
    assertTrue(k instanceof Undef);
    assertTrue(v instanceof Undef);
  }
Example #2
0
  public static HashResource createRootElement(String rootElement) throws SyntaxException {

    if (rootElement == null || "".equals(rootElement.trim())) {

      return new HashResource();

    } else {

      try {

        PanParser parser = new PanParser(new StringReader(rootElement));
        ASTOperation ast = parser.dml();
        Operation dml = PanParserAstUtils.astToDml(ast, true);
        return (HashResource) dml.execute(new CompileTimeContext());

      } catch (SyntaxException e) {
        throw SyntaxException.create(null, MSG_INVALID_SYNTAX_ROOT_ELEMENT, e.getMessage());
      } catch (ClassCastException e) {
        throw SyntaxException.create(null, MSG_INVALID_TYPE_FOR_ROOT_ELEMENT);
      }
    }
  }
Example #3
0
  public boolean executeDmlValidationBlock(Operation dml, Element self) throws ValidationException {

    Element result = null;

    // Store the old local variables and iterators. Needed because structure
    // templates need to keep the state across template.execute() calls.
    LocalVariableMap oldVariables = createLocalVariableMap(null);
    IteratorMap oldIterators = createIteratorMap();

    // Initialize the SELF reference to the local self value.
    SelfHolder selfHolder = new ReadOnlySelfHolder(self);
    initializeSelfHolder(selfHolder);

    try {

      result = dml.execute(this);

    } catch (ReturnValueException rve) {

      result = rve.getElement();

    } catch (EvaluationException ee) {

      File objectFile = (objectTemplate != null) ? objectTemplate.source : null;
      ValidationException ve =
          ValidationException.create(MSG_VALIDATION_FAILED_BECAUSE_OF_EXCEPTION);
      ve.setObjectTemplate(objectFile);
      ve.initCause(ee);
      throw ve;

    } finally {
      clearSelf();

      restoreLocalVariableMap(oldVariables);
      restoreIteratorMap(oldIterators);
    }

    try {
      BooleanProperty bresult = (BooleanProperty) result;
      return bresult.getValue().booleanValue();
    } catch (ClassCastException cce) {
      File objectFile = (objectTemplate != null) ? objectTemplate.source : null;
      ValidationException ve =
          ValidationException.create(
              MSG_INVALID_VALIDATION_FUNCTION_RETURN_TYPE, result.getTypeAsString());
      throw ve.setObjectTemplate(objectFile);
    }
  }
Example #4
0
  public Element executeDmlBlock(Operation dml) {
    Element result = null;

    // Store the old local variables and iterators. Needed because structure
    // templates need to keep the state across template.execute() calls.
    LocalVariableMap oldVariables = createLocalVariableMap(null);
    IteratorMap oldIterators = createIteratorMap();

    // If the TEMPLATE variable is already set, then don't set the value to
    // the current template. This can happen when executing structure
    // templates and we want to keep the outermost value.
    boolean setTemplate = (getGlobalVariable(TPL_VAR) == null);

    // Set the value of TEMPLATE to the current template. Be careful,
    // compile time execution doesn't set the current template. In this
    // case, don't set the global variable.
    if (setTemplate) {
      Template current = getCurrentTemplate();
      if (current != null) {
        StringProperty tname = StringProperty.getInstance(current.name);
        setGlobalVariable(TPL_VAR, tname, true);
      } else {
        setTemplate = false;
      }
    }

    // Run the DML block. Making sure to always restore the previous
    // variables and iterators.
    try {
      result = dml.execute(this);
    } catch (ReturnValueException rve) {
      result = rve.getElement();
    } finally {

      // Remove TEMPLATE variable if we set it above.
      if (setTemplate) {
        removeGlobalVariable(TPL_VAR);
      }

      // Restore the saved local variables and iterators.
      restoreLocalVariableMap(oldVariables);
      restoreIteratorMap(oldIterators);
    }

    return result;
  }