com.github.mustachejava.codes.ValueCode Java Examples

The following examples show how to use com.github.mustachejava.codes.ValueCode. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MustacheHelper.java    From helidon-build-tools with Apache License 2.0 6 votes vote down vote up
@Override
public MustacheVisitor createMustacheVisitor() {
    return new DefaultMustacheVisitor(this) {
        @Override
        public void value(TemplateContext tc, String variable, boolean encoded) {
            list.add(new ValueCode(tc, df, variable, encoded) {
                @Override
                public Writer execute(Writer writer, List<Object> scopes) {
                    try {
                        final Object object = get(scopes);
                        if (object instanceof NotEncoded) {
                            writer.write(oh.stringify(object));
                            return appendText(run(writer, scopes));
                        } else {
                            return super.execute(writer, scopes);
                        }
                    } catch (Exception e) {
                        throw new MustacheException("Failed to get value for " + name, e, tc);
                    }
                }
            });
        }
    };
}
 
Example #2
Source File: AbstractProperty.java    From hesperides with GNU General Public License v3.0 6 votes vote down vote up
public static List<AbstractProperty> extractPropertiesFromStringContent(String content) {
    List<AbstractProperty> properties = new ArrayList<>();
    Mustache mustache = getMustacheInstanceFromStringContent(content);
    for (Code code : mustache.getCodes()) {
        if (code instanceof ValueCode) {
            String propertyDefinition = code.getName();
            Property property = Property.extractProperty(propertyDefinition);
            if (property != null) {
                properties.add(property);
            }
        } else if (code instanceof IterableCode) {
            IterableProperty iterableProperty = IterableProperty.extractIterablePropertyFromMustacheCode((IterableCode) code);
            properties.add(iterableProperty);
        }
    }
    return properties;
}
 
Example #3
Source File: TemplateUtils.java    From dcos-commons with Apache License 2.0 5 votes vote down vote up
@Override
protected synchronized Wrapper getWrapper(String name, List<Object> scopes) {
  Wrapper wrapper = super.getWrapper(name, scopes);
  // This should only do anything when the template param is e.g. "{{hello}}", not "{{#hello}}hi{{/hello}}".
  // The latter case implies an expectation that the value will sometimes be unset. We can determine the
  // situation based on the code type:
  // - "{{hello}}" = ValueCode <-- check for this case
  // - "{{#hello}}{{/hello}}" = IterableCode
  // - "{{^hello}}{{/hello}}" = NotIterableCode
  // - etc... "{{>partial}}", "{{!comment}}"
  if (code instanceof ValueCode && wrapper instanceof MissingWrapper) {
    missingValues.add(new MissingValue(name, tc.line()));
  }
  return wrapper;
}
 
Example #4
Source File: IterableProperty.java    From hesperides with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Méthode récursive permettant d'extraire les propriétés et les propriétés itérables contenues dans une propriété itérable.
 *
 * @param code
 * @return
 */
public static IterableProperty extractIterablePropertyFromMustacheCode(IterableCode code) {
    String name = code.getName();
    Set<AbstractProperty> properties = new HashSet<>();

    for (Code childCode : code.getCodes()) {
        if (childCode instanceof ValueCode) {
            properties.add(Property.extractProperty(childCode.getName()));
        } else if (childCode instanceof IterableCode) {
            properties.add(extractIterablePropertyFromMustacheCode((IterableCode) childCode));
        }
    }
    return new IterableProperty(name, new ArrayList<>(properties));
}