コード例 #1
0
ファイル: AttrsNode.java プロジェクト: moio/jade4j
 public boolean isTerse(JadeTemplate template) {
   return isSelfClosing(template) && template.isTerse();
 }
コード例 #2
0
ファイル: AttrsNode.java プロジェクト: moio/jade4j
  private void addAttributesToMap(
      HashMap<String, String> newAttributes,
      ArrayList<String> classes,
      Attr attribute,
      JadeModel model,
      JadeTemplate template)
      throws ExpressionException {
    String name = attribute.getName();
    String key = name;
    boolean escaped = false;
    //        if ("class".equals(key)) {
    //          classes.push(attr.val);
    //          classEscaping.push(attr.escaped);
    //        } else if (isConstant(attr.val)) {
    //          if (buffer) {
    //            this.buffer(runtime.attr(key, toConstant(attr.val), escaped, this.terse));
    //          } else {
    //            var val = toConstant(attr.val);
    //            if (key === 'style') val = runtime.style(val);
    //            if (escaped && !(key.indexOf('data') === 0 && typeof val !== 'string')) {
    //              val = runtime.escape(val);
    //            }
    //            buf.push(utils.stringify(key) + ': ' + utils.stringify(val));
    //          }
    //        } else {
    //          if (buffer) {
    //            this.bufferExpression('jade.attr("' + key + '", ' + attr.val + ', ' +
    // utils.stringify(escaped) + ', ' + utils.stringify(this.terse) + ')');
    //          } else {
    //            var val = attr.val;
    //            if (key === 'style') {
    //              val = 'jade.style(' + val + ')';
    //            }
    //            if (escaped && !(key.indexOf('data') === 0)) {
    //              val = 'jade.escape(' + val + ')';
    //            } else if (escaped) {
    //              val = '(typeof (jade_interp = ' + val + ') == "string" ?
    // jade.escape(jade_interp) : jade_interp)';
    //            }
    //            buf.push(utils.stringify(key) + ': ' + val);
    //          }
    //        }

    String value = null;
    Object attributeValue = attribute.getValue();
    if ("class".equals(key)) {
      if (attributeValue instanceof String) {
        escaped = attribute.isEscaped();
        value = getInterpolatedAttributeValue(name, attributeValue, escaped, model, template);
      } else if (attributeValue instanceof ExpressionString) {
        escaped = ((ExpressionString) attributeValue).isEscape();
        Object expressionValue =
            evaluateExpression(
                (ExpressionString) attributeValue, model, template.getExpressionHandler());
        if (expressionValue != null && expressionValue.getClass().isArray()) {
          StringBuffer s = new StringBuffer("");
          boolean first = true;
          if (expressionValue instanceof int[]) {
            for (int o : (int[]) expressionValue) {
              if (!first) s.append(" ");
              s.append(o);
              first = false;
            }
          } else {
            for (Object o : (Object[]) expressionValue) {
              if (!first) s.append(" ");
              s.append(o.toString());
              first = false;
            }
          }
          value = s.toString();
        } else if (expressionValue != null && expressionValue instanceof Boolean) {
          if ((Boolean) expressionValue) value = expressionValue.toString();
        } else if (expressionValue != null) {
          value = expressionValue.toString();
        }
      }
      if (!StringUtils.isBlank(value)) classes.add(value);
      return;
      //        }else if("id".equals(key)){
      //            value = (String) attribute;
    } else if (attributeValue instanceof String) {
      escaped = attribute.isEscaped();
      value = getInterpolatedAttributeValue(name, attributeValue, escaped, model, template);
    } else if (attributeValue instanceof Boolean) {
      if ((Boolean) attributeValue) {
        value = name;
      } else {
        return;
      }
      if (template.isTerse()) {
        value = null;
      }
    } else if (attributeValue instanceof ExpressionString) {
      escaped = ((ExpressionString) attributeValue).isEscape();
      Object expressionValue =
          evaluateExpression(
              (ExpressionString) attributeValue, model, template.getExpressionHandler());
      if (expressionValue == null) {
        return;
      }
      // TODO: refactor
      if (expressionValue instanceof Boolean) {
        if ((Boolean) expressionValue) {
          value = name;
        } else {
          return;
        }
        if (template.isTerse()) {
          value = null;
        }
      } else {
        value = expressionValue.toString();
        value = StringEscapeUtils.escapeHtml4(value);
      }
    } else if (attributeValue instanceof String) {
      value = (String) attributeValue;
      //        } else {
      //            return "";
    }
    newAttributes.put(name, value);
  }