示例#1
0
文件: Set.java 项目: tengstrand/Laja
  private void setContextToMacroLocalContextIfVariableExistsInParamaterList() {
    AttributeRef attributeRef = target.getAttributeRef();

    if (attributeRef != null) {
      String variableToSet = attributeRef.getVariableName();

      if (macro != null && macro.getParameters().contains(variableToSet)) {
        context = macro.getLocalContext();
      } else if (target.isNamespaceRef()) {
        throw new RuntimeException("Setting namespaces not implemented.");
      }
    }
  }
示例#2
0
文件: Set.java 项目: tengstrand/Laja
  public Object generate() {
    if (target.isNamespaceRef()) {
      // e.g: #set ($x = $y)
      throw new InterpretationException(
          source,
          indexInSource,
          "Not possible to set namespace variable $" + target.getNamespaceName());
    }
    if (target.isMethodRef()) {
      // e.g: #set (a() = 123)
      throw new InterpretationException(
          source, indexInSource, "Not possible to set a method, function or macro");
    }
    if (!target.hasAttributes()) {
      // e.g: #set (x = "1") or #set ($n.x = "1")
      data.setAttribute(target.getVariableName(), target.getSpecifiedContext(context));
    } else {
      // e.g: #set (x.a = "1") or #set ($n.x.a = "1") or #set (a.b.c.d = 1)
      AttributeRefs targetAttributeRefs = target.getAttributesRefs();

      AttributeRef targetAttributeRef = targetAttributeRefs.getLastAttribute();
      String targetAttributeName = targetAttributeRef.getName();

      Object targetObject = target.getContext().evaluate(target.getAttributeRef());

      if (targetObject == null) {
        throw new InterpretationException(
            source,
            indexInSource,
            "Can not set value, the target was null '" + target.getAttributeRef().getName() + "'");
      }

      targetAttributeRefs = targetAttributeRefs.createAttributesSkipLast();

      if (targetAttributeRefs.hasAttributes()) {
        targetObject =
            targetAttributeRefs.evaluateNextAttributes(target.getContext(), targetObject);
      }

      Context targetContext = contextFactory.createContext(targetObject, targetAttributeRef);
      data.setAttribute(targetAttributeName, targetContext);
    }
    return null;
  }