Beispiel #1
0
  void accept(Environment env) throws IOException, TemplateException {
    TemplateModel node = targetNode == null ? null : targetNode.getAsTemplateModel(env);
    TemplateModel nss = namespaces == null ? null : namespaces.getAsTemplateModel(env);
    if (namespaces instanceof StringLiteral) {
      nss = env.importLib(((TemplateScalarModel) nss).getAsString(), null);
    } else if (namespaces instanceof ListLiteral) {
      nss = ((ListLiteral) namespaces).evaluateStringsToNamespaces(env);
    }
    if (node != null && !(node instanceof TemplateNodeModel)) {
      throw new TemplateException(
          "Expecting an XML node here, for expression: "
              + targetNode
              + ", found a: "
              + node.getClass().getName(),
          env);
    }
    if (nss != null) {
      if (nss instanceof TemplateHashModel) {
        SimpleSequence ss = new SimpleSequence(1);
        ss.add(nss);
        nss = ss;
      } else if (!(nss instanceof TemplateSequenceModel)) {
        throw new TemplateException("Expecting a sequence of namespaces after 'using'", env);
      }
    }

    env.recurse((TemplateNodeModel) node, (TemplateSequenceModel) nss);
  }
  public void execute(
      Environment env, Map map, TemplateModel[] loopVars, TemplateDirectiveBody body)
      throws TemplateException, IOException {

    Iterator<?> it = map.entrySet().iterator();
    String name = "";
    String id = "";
    String clazz = "";
    String defaultvalue = "";
    String nid = "";
    String disabled = "";
    String type = "";
    String noselect = "";
    int typeid = 0;
    boolean plantext = false;
    while (it.hasNext()) {
      Map.Entry<?, ?> entry = (Map.Entry) it.next();
      String paramName = entry.getKey().toString();
      TemplateModel paramValue = (TemplateModel) entry.getValue();
      if (paramName.equals("plantext")) {
        if (!(paramValue instanceof TemplateBooleanModel)) {
          throw new TemplateModelException(
              "The \"" + plantext + "\" parameter " + "must be a boolean.");
        }
        plantext = ((TemplateBooleanModel) paramValue).getAsBoolean();
      }
      if (paramName.equals("name")) {
        name = paramValue.toString();
      } else if (paramName.equals("id")) {
        id = paramValue.toString();
      } else if (paramName.equals("class")) {
        clazz = paramValue.toString();
      } else if (paramName.equals("default")) {
        defaultvalue = paramValue.toString();
      } else if (paramName.equals("nid")) {
        nid = paramValue.toString();
      } else if (paramName.equals("disabled")) {
        disabled = paramValue.toString();
      } else if (paramName.equals("noselect")) {
        noselect = paramValue.toString();
      } else if (paramName.equals("type")) {
        type = paramValue.toString();
      } else if (paramName.equals("typeid")) {
        if (!(paramValue instanceof TemplateNumberModel)) {
          throw new TemplateModelException("The \"typeid\" parameter must be a number.");
        }

        typeid = ((TemplateNumberModel) paramValue).getAsNumber().intValue();
      }
    }
    String result = "";
    if (plantext)
      result = plaintext(name, id, clazz, defaultvalue, typeid, nid, disabled, type, noselect);
    else {
      result = html(name, id, clazz, defaultvalue, typeid, nid, disabled, type, noselect);
    }
    Writer out = env.getOut();
    out.write(result);
  }
 public Collection<Entity<?>> convert(TemplateHashModelEx hash, String... ignore) {
   List<Entity<?>> entities = new LinkedList<Entity<?>>();
   List<String> ignoreList = Arrays.asList(ignore);
   TemplateCollectionModel keys;
   try {
     keys = hash.keys();
     for (TemplateModelIterator iterator = keys.iterator(); iterator.hasNext(); ) {
       TemplateModel model = iterator.next();
       String key = model.toString();
       if (!ignoreList.contains(key)) {
         entities.add(new SimpleEntityModel(key, hash.get(key).toString()));
       }
     }
     Collections.sort(entities, new EntityComparator());
     return entities;
   } catch (TemplateModelException e) {
     throw new RuntimeException(e);
   }
 }
Beispiel #4
0
 static Number getNumber(TemplateModel model, Expression expr, Environment env)
     throws TemplateException {
   if (model instanceof TemplateNumberModel) {
     return getNumber((TemplateNumberModel) model, expr, env);
   } else if (model == null) {
     throw new InvalidReferenceException(expr + " is undefined.", env);
   } else {
     throw new NonNumericalException(
         expr + " is not a number, it is " + model.getClass().getName(), env);
   }
 }
Beispiel #5
0
 /*
  * WARNING! This algorithm is duplicated in SequenceBuiltins.modelsEqual.
  * Thus, if you update this method, then you have to update that too!
  */
 boolean isTrue(Environment env) throws TemplateException {
   TemplateModel ltm = left.getAsTemplateModel(env);
   TemplateModel rtm = right.getAsTemplateModel(env);
   if (env != null && env.isClassicCompatible()) {
     if (ltm == null) {
       ltm = TemplateScalarModel.EMPTY_STRING;
     }
     if (rtm == null) {
       rtm = TemplateScalarModel.EMPTY_STRING;
     }
   }
   assertNonNull(ltm, left, env);
   assertNonNull(rtm, right, env);
   int comp = 0;
   if (ltm instanceof TemplateNumberModel && rtm instanceof TemplateNumberModel) {
     Number first = EvaluationUtil.getNumber((TemplateNumberModel) ltm, left, env);
     Number second = EvaluationUtil.getNumber((TemplateNumberModel) rtm, right, env);
     ArithmeticEngine ae =
         env != null ? env.getArithmeticEngine() : getTemplate().getArithmeticEngine();
     comp = ae.compareNumbers(first, second);
   } else if (ltm instanceof TemplateDateModel && rtm instanceof TemplateDateModel) {
     TemplateDateModel ltdm = (TemplateDateModel) ltm;
     TemplateDateModel rtdm = (TemplateDateModel) rtm;
     int ltype = ltdm.getDateType();
     int rtype = rtdm.getDateType();
     if (ltype != rtype) {
       throw new TemplateException(
           "Can not compare dates of different type. Left date is of "
               + TemplateDateModel.TYPE_NAMES.get(ltype)
               + " type, right date is of "
               + TemplateDateModel.TYPE_NAMES.get(rtype)
               + " type.",
           env);
     }
     if (ltype == TemplateDateModel.UNKNOWN) {
       throw new TemplateException("Left date is of UNKNOWN type, and can not be compared.", env);
     }
     if (rtype == TemplateDateModel.UNKNOWN) {
       throw new TemplateException("Right date is of UNKNOWN type, and can not be compared.", env);
     }
     Date first = EvaluationUtil.getDate(ltdm, left, env);
     Date second = EvaluationUtil.getDate(rtdm, right, env);
     comp = first.compareTo(second);
   } else if (ltm instanceof TemplateScalarModel && rtm instanceof TemplateScalarModel) {
     if (operation != EQUALS && operation != NOT_EQUALS) {
       throw new TemplateException("Can not use operator " + opString + " on string values.", env);
     }
     String first = EvaluationUtil.getString((TemplateScalarModel) ltm, left, env);
     String second = EvaluationUtil.getString((TemplateScalarModel) rtm, right, env);
     comp = env.getCollator().compare(first, second);
   } else if (ltm instanceof TemplateBooleanModel && rtm instanceof TemplateBooleanModel) {
     if (operation != EQUALS && operation != NOT_EQUALS) {
       throw new TemplateException(
           "Can not use operator " + opString + " on boolean values.", env);
     }
     boolean first = ((TemplateBooleanModel) ltm).getAsBoolean();
     boolean second = ((TemplateBooleanModel) rtm).getAsBoolean();
     comp = (first ? 1 : 0) - (second ? 1 : 0);
   }
   // Here we handle compatibility issues
   else if (env.isClassicCompatible()) {
     String first = left.getStringValue(env);
     String second = right.getStringValue(env);
     comp = env.getCollator().compare(first, second);
   } else {
     throw new TemplateException(
         "The only legal comparisons are between two numbers, two strings, or two dates.\n"
             + "Left  hand operand is a "
             + ltm.getClass().getName()
             + "\n"
             + "Right hand operand is a "
             + rtm.getClass().getName()
             + "\n",
         env);
   }
   switch (operation) {
     case EQUALS:
       return comp == 0;
     case NOT_EQUALS:
       return comp != 0;
     case LESS_THAN:
       return comp < 0;
     case GREATER_THAN:
       return comp > 0;
     case LESS_THAN_EQUALS:
       return comp <= 0;
     case GREATER_THAN_EQUALS:
       return comp >= 0;
     default:
       throw new TemplateException("unknown operation", env);
   }
 }