当前位置: 首页>>代码示例>>Java>>正文


Java StringData类代码示例

本文整理汇总了Java中com.google.template.soy.data.restricted.StringData的典型用法代码示例。如果您正苦于以下问题:Java StringData类的具体用法?Java StringData怎么用?Java StringData使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


StringData类属于com.google.template.soy.data.restricted包,在下文中一共展示了StringData类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: compareString

import com.google.template.soy.data.restricted.StringData; //导入依赖的package包/类
/** Determines if the operand's string form can be equality-compared with a string. */
public static boolean compareString(String string, SoyValue other) {
  // This follows similarly to the Javascript specification, to ensure similar operation
  // over Javascript and Java: http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3
  if (other instanceof StringData || other instanceof SanitizedContent) {
    return string.equals(other.toString());
  }
  if (other instanceof NumberData) {
    try {
      // Parse the string as a number.
      return Double.parseDouble(string) == other.numberValue();
    } catch (NumberFormatException nfe) {
      // Didn't parse as a number.
      return false;
    }
  }
  return false;
}
 
开发者ID:google,项目名称:closure-templates,代码行数:19,代码来源:SharedRuntime.java

示例2: convertPrimitiveDataToExpr

import com.google.template.soy.data.restricted.StringData; //导入依赖的package包/类
/**
 * Converts a primitive data object into a primitive expression node.
 *
 * @param primitiveData The primitive data object to convert. Must not be undefined.
 * @param location The node's source location.
 * @return The resulting primitive expression node.
 */
@Nullable
public static PrimitiveNode convertPrimitiveDataToExpr(
    PrimitiveData primitiveData, SourceLocation location) {
  if (primitiveData instanceof StringData) {
    return new StringNode(primitiveData.stringValue(), location);
  } else if (primitiveData instanceof BooleanData) {
    return new BooleanNode(primitiveData.booleanValue(), location);
  } else if (primitiveData instanceof IntegerData) {
    // NOTE: We only support numbers in the range of JS [MIN_SAFE_INTEGER, MAX_SAFE_INTEGER]
    if (!IntegerNode.isInRange(primitiveData.longValue())) {
      return null;
    } else {
      return new IntegerNode(primitiveData.longValue(), location);
    }
  } else if (primitiveData instanceof FloatData) {
    return new FloatNode(primitiveData.floatValue(), location);
  } else if (primitiveData instanceof NullData) {
    return new NullNode(location);
  } else {
    throw new IllegalArgumentException();
  }
}
 
开发者ID:google,项目名称:closure-templates,代码行数:30,代码来源:InternalValueUtils.java

示例3: convertPrimitiveExprToData

import com.google.template.soy.data.restricted.StringData; //导入依赖的package包/类
/**
 * Converts a primitive expression node into a primitive data object.
 *
 * @param primitiveNode The primitive expression node to convert.
 * @return The resulting primitive data object.
 */
public static PrimitiveData convertPrimitiveExprToData(PrimitiveNode primitiveNode) {

  if (primitiveNode instanceof StringNode) {
    return StringData.forValue(((StringNode) primitiveNode).getValue());
  } else if (primitiveNode instanceof BooleanNode) {
    return BooleanData.forValue(((BooleanNode) primitiveNode).getValue());
  } else if (primitiveNode instanceof IntegerNode) {
    return IntegerData.forValue(((IntegerNode) primitiveNode).getValue());
  } else if (primitiveNode instanceof FloatNode) {
    return FloatData.forValue(((FloatNode) primitiveNode).getValue());
  } else if (primitiveNode instanceof NullNode) {
    return NullData.INSTANCE;
  } else {
    throw new IllegalArgumentException();
  }
}
 
开发者ID:google,项目名称:closure-templates,代码行数:23,代码来源:InternalValueUtils.java

示例4: getEscapingDirectiveAsFunction

import com.google.template.soy.data.restricted.StringData; //导入依赖的package包/类
public Function<String, String> getEscapingDirectiveAsFunction(String name) {
  final SoyJavaPrintDirective printDirective = soyJavaDirectivesMap.get(name);
  if (printDirective == null) {
    throw new IllegalStateException(
        "Failed to find Soy print directive with name '" + name + "'");
  }
  if (!printDirective.getValidArgsSizes().contains(0)) {
    throw new IllegalStateException(
        "Soy print directive with name '" + name + "' is not an escaping directive");
  }
  // TODO(lukes): this adapter is lame.  there should just be a way to get the print directive to
  // hand us an escaper or a function rather than writing this adapter.
  return new Function<String, String>() {
    @Override
    public String apply(String input) {
      return printDirective
          .applyForJava(StringData.forValue(input), ImmutableList.<SoyValue>of())
          .stringValue();
    }
  };
}
 
开发者ID:google,项目名称:closure-templates,代码行数:22,代码来源:RenderContext.java

示例5: render

import com.google.template.soy.data.restricted.StringData; //导入依赖的package包/类
@Override
public RenderResult render(LoggingAdvisingAppendable appendable, RenderContext context)
    throws IOException {
  RenderResult result = delegate.render(buffer, context);
  if (result.isDone()) {
    SoyValue resultData =
        kind == null
            ? StringData.forValue(buffer.toString())
            : UnsafeSanitizedContentOrdainer.ordainAsSafe(buffer.toString(), kind);
    for (SoyJavaPrintDirective directive : directives) {
      resultData = directive.applyForJava(resultData, ImmutableList.<SoyValue>of());
    }
    appendable.append(resultData.coerceToString());
  }
  return result;
}
 
开发者ID:google,项目名称:closure-templates,代码行数:17,代码来源:JbcSrcRuntime.java

示例6: getResolvedValue

import com.google.template.soy.data.restricted.StringData; //导入依赖的package包/类
private SoyString getResolvedValue() {
  SoyString local = resolvedValue;
  if (local == null) {
    if (buffer != null) {
      String string = buffer.toString();
      // This drops logs, but that is sometimes necessary.  We should make sure this only happens
      // when it has to by making sure that renderAndResolve is used for all printing usecases
      if (contentKind != null) {
        local = UnsafeSanitizedContentOrdainer.ordainAsSafe(string, contentKind);
      } else {
        local = StringData.forValue(string);
      }
      resolvedValue = local;
    } else {
      throw new AssertionError("getResolvedValue() should only be called if the value isDone.");
    }
  }
  return local;
}
 
开发者ID:google,项目名称:closure-templates,代码行数:20,代码来源:DetachableContentProvider.java

示例7: applyForJava

import com.google.template.soy.data.restricted.StringData; //导入依赖的package包/类
@Override
public SoyValue applyForJava(SoyValue value, List<SoyValue> args) {
  ULocale uLocale = I18nUtils.parseULocale(localeStringProvider.get());
  String formatType = args.isEmpty() ? DEFAULT_FORMAT : args.get(0).stringValue();
  String numbersKeyword = "local";
  if (args.size() > 1) {
    // A keyword for ULocale was passed (like 'native', for instance, to use native characters).
    numbersKeyword = args.get(1).stringValue();
  }

  // Minimum and maximum fraction digits. If only one value was specified, use as both min and max
  // (i.e. significant digits after the decimal point).
  Integer minFractionDigits =
      args.size() > 2 ? Integer.valueOf((int) args.get(2).numberValue()) : null;
  Integer maxFractionDigits =
      args.size() > 3 ? Integer.valueOf((int) args.get(3).numberValue()) : minFractionDigits;

  double number = value.numberValue();
  return StringData.forValue(
      I18NDirectivesRuntime.formatNum(
          uLocale, number, formatType, numbersKeyword, minFractionDigits, maxFractionDigits));
}
 
开发者ID:google,项目名称:closure-templates,代码行数:23,代码来源:FormatNumDirective.java

示例8: getConstantOrNull

import com.google.template.soy.data.restricted.StringData; //导入依赖的package包/类
/** Returns the value of the given expression if it's constant, else returns null. */
static SoyValue getConstantOrNull(ExprNode expr) {

  switch (expr.getKind()) {
    case NULL_NODE:
      return NullData.INSTANCE;
    case BOOLEAN_NODE:
      return BooleanData.forValue(((BooleanNode) expr).getValue());
    case INTEGER_NODE:
      return IntegerData.forValue(((IntegerNode) expr).getValue());
    case FLOAT_NODE:
      return FloatData.forValue(((FloatNode) expr).getValue());
    case STRING_NODE:
      return StringData.forValue(((StringNode) expr).getValue());
    case GLOBAL_NODE:
      GlobalNode global = (GlobalNode) expr;
      if (global.isResolved()) {
        return getConstantOrNull(global.getValue());
      }
      return null;
    default:
      return null;
  }
}
 
开发者ID:google,项目名称:closure-templates,代码行数:25,代码来源:SimplifyExprVisitor.java

示例9: visitMsgFallbackGroupNode

import com.google.template.soy.data.restricted.StringData; //导入依赖的package包/类
@Override
protected void visitMsgFallbackGroupNode(MsgFallbackGroupNode node) {
  if (assistantForMsgs == null) {
    assistantForMsgs = new RenderVisitorAssistantForMsgs(this, msgBundle);
  }
  if (!node.getEscapingDirectives().isEmpty()) {
    // The entire message needs to be escaped, so we need to render to a temporary buffer.
    // Fortunately, for most messages (in HTML context) this is unnecessary.
    pushOutputBuf(new StringBuilder());
  }
  assistantForMsgs.visitForUseByMaster(node);
  if (!node.getEscapingDirectives().isEmpty()) {
    // Escape the entire message with the required directives.
    SoyValue wholeMsg = StringData.forValue(popOutputBuf().toString());
    for (SoyPrintDirective directive : node.getEscapingDirectives()) {
      wholeMsg = applyDirective(directive, wholeMsg, ImmutableList.<SoyValue>of(), node);
    }
    append(currOutputBuf, wholeMsg.stringValue());
  }
}
 
开发者ID:google,项目名称:closure-templates,代码行数:21,代码来源:RenderVisitor.java

示例10: visitCssFunction

import com.google.template.soy.data.restricted.StringData; //导入依赖的package包/类
private SoyValue visitCssFunction(FunctionNode node) {
  List<SoyValue> children = visitChildren(node);
  String selector = Iterables.getLast(children).stringValue();

  String renamedSelector = cssRenamingMap.get(selector);
  if (renamedSelector == null) {
    renamedSelector = selector;
  }

  if (node.numChildren() == 1) {
    return StringData.forValue(renamedSelector);
  } else {
    String fullSelector = children.get(0).stringValue() + "-" + renamedSelector;
    return StringData.forValue(fullSelector);
  }
}
 
开发者ID:google,项目名称:closure-templates,代码行数:17,代码来源:EvalVisitor.java

示例11: computeForJava

import com.google.template.soy.data.restricted.StringData; //导入依赖的package包/类
@Override
public SoyValue computeForJava(List<SoyValue> args) {
  SoyValue arg0 = args.get(0);
  SoyValue arg1 = args.get(1);

  Preconditions.checkArgument(
      arg0 instanceof StringData || arg0 instanceof SanitizedContent,
      "First argument to strIndexOf() function is not StringData or SanitizedContent: %s",
      arg0);

  Preconditions.checkArgument(
      arg1 instanceof StringData || arg1 instanceof SanitizedContent,
      "Second argument to strIndexOf() function is not StringData or SanitizedContent: %s",
      arg1);

  String strArg0 = arg0.coerceToString();
  String strArg1 = arg1.coerceToString();

  return IntegerData.forValue(strArg0.indexOf(strArg1));
}
 
开发者ID:google,项目名称:closure-templates,代码行数:21,代码来源:StrIndexOfFunction.java

示例12: changeNewlineToBr

import com.google.template.soy.data.restricted.StringData; //导入依赖的package包/类
public static SoyString changeNewlineToBr(SoyValue value) {
  String result = NEWLINE_PATTERN.matcher(coerceToString(value)).replaceAll("<br>");

  // Make sure to transmit the known direction, if any, to any downstream directive that may need
  // it, e.g. BidiSpanWrapDirective. Since a known direction is carried only by SanitizedContent,
  // and the transformation we make is only valid in HTML, we only transmit the direction when we
  // get HTML SanitizedContent.
  // TODO(user): Consider always returning HTML SanitizedContent.
  if (value instanceof SanitizedContent) {
    SanitizedContent sanitizedContent = (SanitizedContent) value;
    if (sanitizedContent.getContentKind() == ContentKind.HTML) {
      return UnsafeSanitizedContentOrdainer.ordainAsSafe(
          result, ContentKind.HTML, sanitizedContent.getContentDirection());
    }
  }
  return StringData.forValue(result);
}
 
开发者ID:google,项目名称:closure-templates,代码行数:18,代码来源:BasicDirectivesRuntime.java

示例13: insertWordBreaks

import com.google.template.soy.data.restricted.StringData; //导入依赖的package包/类
public static SoyString insertWordBreaks(SoyValue value, int maxCharsBetweenWordBreaks) {
  String result =
      new InsertWordBreaks(maxCharsBetweenWordBreaks).processString(coerceToString(value));

  // Make sure to transmit the known direction, if any, to any downstream directive that may need
  // it, e.g. BidiSpanWrapDirective. Since a known direction is carried only by SanitizedContent,
  // and the transformation we make is only valid in HTML, we only transmit the direction when we
  // get HTML SanitizedContent.
  // TODO(user): Consider always returning HTML SanitizedContent.
  if (value instanceof SanitizedContent) {
    SanitizedContent sanitizedContent = (SanitizedContent) value;
    if (sanitizedContent.getContentKind() == ContentKind.HTML) {
      return UnsafeSanitizedContentOrdainer.ordainAsSafe(
          result, ContentKind.HTML, sanitizedContent.getContentDirection());
    }
  }

  return StringData.forValue(result);
}
 
开发者ID:google,项目名称:closure-templates,代码行数:20,代码来源:BasicDirectivesRuntime.java

示例14: testCleanHtml

import com.google.template.soy.data.restricted.StringData; //导入依赖的package包/类
@Test
public void testCleanHtml() {
  assertEquals(
      UnsafeSanitizedContentOrdainer.ordainAsSafe("<em>foo</em>", ContentKind.HTML),
      Sanitizers.cleanHtml("<em>f<object>oo</em>"));
  assertEquals(
      UnsafeSanitizedContentOrdainer.ordainAsSafe("<em>foo</em>", ContentKind.HTML),
      Sanitizers.cleanHtml(StringData.forValue("<em>f<object>oo</em>")));
  assertEquals(
      UnsafeSanitizedContentOrdainer.ordainAsSafe("<em>foo</em>", ContentKind.HTML, Dir.LTR),
      Sanitizers.cleanHtml(
          UnsafeSanitizedContentOrdainer.ordainAsSafe("<em>f<object>oo</em>", ContentKind.CSS)));

  // Input of ContentKind.HTML is left alone.
  assertEquals(
      UnsafeSanitizedContentOrdainer.ordainAsSafe("<script>notevil()</script>", ContentKind.HTML),
      Sanitizers.cleanHtml(
          UnsafeSanitizedContentOrdainer.ordainAsSafe(
              "<script>notevil()</script>", ContentKind.HTML)));
}
 
开发者ID:google,项目名称:closure-templates,代码行数:21,代码来源:SanitizersTest.java

示例15: testPlus

import com.google.template.soy.data.restricted.StringData; //导入依赖的package包/类
@Test
public void testPlus() {
  assertEquals(3, plus(IntegerData.forValue(1), IntegerData.forValue(2)).integerValue());

  // N.B. coerced to float
  assertEquals(3.0, plus(FloatData.forValue(1), IntegerData.forValue(2)).numberValue(), 0.0);

  // coerced to string
  assertEquals("32", plus(StringData.forValue("3"), IntegerData.forValue(2)).stringValue());

  // SanitizedContent:
  assertEquals(
      "HelloWorld",
      plus(SanitizedContents.unsanitizedText("Hello"), SanitizedContents.unsanitizedText("World"))
          .stringValue());

  // Even arrays:
  SoyValueConverter converter = SoyValueConverter.UNCUSTOMIZED_INSTANCE;
  assertEquals(
      "[Hello][World]",
      plus(
              converter.convert(ImmutableList.of("Hello")).resolve(),
              converter.convert(ImmutableList.of("World")).resolve())
          .stringValue());
}
 
开发者ID:google,项目名称:closure-templates,代码行数:26,代码来源:SharedRuntimeTest.java


注:本文中的com.google.template.soy.data.restricted.StringData类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。