Skip navigation links
com.x5.template

Class Chunk

    • Nested Class Summary

      • Nested classes/interfaces inherited from interface java.util.Map

        java.util.Map.Entry<K,V>
    • Constructor Summary

      Constructors 
      Constructor and Description
      Chunk() 
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method and Description
      protected java.lang.Object _resolveTagValue(SnippetTag tag, int depth, boolean ignoreParentContext) 
      void addData(DataCapsule smartObj)
      Smart objects implementing DataCapsule can provide their own legend of available tags and which methods to call for exporting the tag data.
      void addData(DataCapsule smartObj, java.lang.String altPrefix)
      Two smart objects of the same type in a single template? No problem.
      void addProtocol(ContentSource src) 
      void append(Chunk toAdd)
      Add a Chunk on to the end of a Chunk's "template" -- this "child" Chunk won't get it's .toString() invoked until the parent Chunk's tags are replaced, ie when the parent Chunk's .toString() method is invoked.
      void append(Snippet toAdd) 
      void append(java.lang.String toAdd)
      Add a String on to the end a Chunk's template.
      static java.lang.Object boxIfAlienObject(java.lang.Object o) 
      void clear() 
      boolean containsKey(java.lang.Object key) 
      boolean containsValue(java.lang.Object value) 
      java.util.Set<java.util.Map.Entry<java.lang.String,java.lang.Object>> entrySet() 
      boolean equals(java.lang.Object o) 
      static java.lang.String findAndReplace(java.lang.String toSearch, java.lang.String find, java.lang.String replace)
      Useful utility function.
      java.lang.Object get(java.lang.Object key) 
      ChunkFactory getChunkFactory() 
      ChunkLocale getLocale() 
      java.util.Map<java.lang.String,java.lang.Object> getTagsTable()
      Retrieve all find-and-replace rules.
      java.lang.Object getTagValue(java.lang.String tagName)
      Retrieves a tag replacement rule.
      java.lang.String getTemplateOrigin() 
      ContentSource getTemplateSet() 
      int hashCode() 
      boolean hasValue(java.lang.String tagName) 
      boolean isEmpty() 
      java.util.Set<java.lang.String> keySet() 
      java.lang.String makeTag(java.lang.String tagName) 
      java.lang.Object put(java.lang.String key, java.lang.Object value) 
      void putAll(java.util.Map t) 
      java.lang.Object remove(java.lang.Object key) 
      void render(java.io.PrintStream out) 
      void render(java.io.Writer out) 
      void render(java.io.Writer out, Chunk context) 
      void resetTags()
      Clears all tag replacement rules.
      void resetTemplate()
      Clears template
      protected java.lang.String resolveBackticks(java.lang.String lookupName, int depth) 
      protected java.lang.Object resolveTagValue(SnippetTag tag, int depth) 
      protected java.lang.Object resolveTagValue(SnippetTag tag, int depth, java.lang.String origin) 
      protected java.lang.Object resolveTagValue(java.lang.String tagName, int depth) 
      void set(java.lang.String tagName)
      For convenience, sets a flag to "TRUE" - to reverse, call unset("flag")
      void set(java.lang.String tagName, boolean value)
      For convenience, calls .set(tagName, "TRUE") for true values and .unset(tagName) for false values -- ie since the string "FALSE" would evaluate to true in a template if-expression.
      void set(java.lang.String tagName, char tagValue)
      For convenience, auto-converts char to String and creates tag replacement rule.
      void set(java.lang.String tagName, Chunk tagValue)
      Creates a find-and-replace rule for tag replacement.
      void set(java.lang.String tagName, int tagValue)
      For convenience, auto-converts int to String and creates tag replacement rule.
      void set(java.lang.String tagName, long tagValue)
      For convenience, auto-converts long to String and creates tag replacement rule.
      void set(java.lang.String tagName, java.lang.Object tagValue)
      Convenience method, chains to set(String s, Object o, String ifNull)
      void set(java.lang.String tagName, java.lang.Object tagValue, java.lang.String ifNull)
      Create a tag replacement rule, supplying a default value in case the value passed is null.
      void set(java.lang.String tagName, java.lang.String tagValue)
      Creates a find-and-replace rule for tag replacement.
      void set(java.lang.String tagName, java.lang.StringBuffer tagValue)
      For convenience, auto-converts StringBuffer to String and creates tag replacement rule.
      void set(java.lang.String tagName, java.lang.StringBuilder tagValue)
      For convenience, auto-converts StringBuilder to String and creates tag replacement rule.
      void setChunkFactory(ChunkFactory factory) 
      void setErrorHandling(boolean renderErrs, java.io.PrintStream err) 
      void setLiteral(java.lang.String tagName, java.lang.String literalValue)
      setLiteral() tag values will render verbatim, so even if the value contains tags/specials they will not be expanded.
      void setLocale(ChunkLocale chunkLocale) 
      void setLocale(java.util.Locale javaLocale) 
      void setLocale(java.lang.String localeCode) 
      void setMultiple(Chunk copyFrom)
      Adds multiple find-and-replace rules using all rules from the passed Chunk.
      void setMultiple(java.util.Map<java.lang.String,java.lang.Object> rules)
      Adds multiple find-and-replace rules using all entries in the Hashtable.
      void setOrDelete(java.lang.String tagName, java.lang.Object tagValue)
      Careful, setOrDelete will DELETE a previous value for the tag at this level if passed a null value.
      void setToBean(java.lang.String tagName, java.lang.Object bean)
      Make bean properties available to template
      void setToBean(java.lang.String tagName, java.lang.Object bean, java.lang.String ifNull)
      Make bean properties available to template
      int size() 
      boolean stillNeeds(java.lang.String tagName) 
      java.lang.String toString()
      Apply all tag replacement rules recursively and return template contents with translated tags.
      java.lang.String toString(Chunk context) 
      void unset(java.lang.String tagName)
      unset("tag") deletes the named tag expansion rule from the ruleset.
      java.util.Collection<java.lang.Object> values() 
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.Map

        compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
    • Constructor Detail

      • Chunk

        public Chunk()
    • Method Detail

      • setChunkFactory

        public void setChunkFactory(ChunkFactory factory)
      • append

        public void append(Snippet toAdd)
      • append

        public void append(java.lang.String toAdd)
        Add a String on to the end a Chunk's template.
      • append

        public void append(Chunk toAdd)
        Add a Chunk on to the end of a Chunk's "template" -- this "child" Chunk won't get it's .toString() invoked until the parent Chunk's tags are replaced, ie when the parent Chunk's .toString() method is invoked. The child does NOT get cloned in this call so if you append a chunk, alter its tags and append it again, you'll see it in its final state twice after the template expansion. To keep incremental changes, use append(child.toString()) instead.
      • set

        public void set(java.lang.String tagName,
                        java.lang.String tagValue)
        Creates a find-and-replace rule for tag replacement. Overwrites any previous rules for this tagName. Do not include the tag boundary markers in the tagName, ie GOOD: set("this", "that") BAD: set("{$this}", "that")
        Parameters:
        tagName - will be ignored if null.
        tagValue - will be translated to the empty String if null -- use setOrDelete() instead of set() if you don't need/want this behavior.
      • set

        public void set(java.lang.String tagName,
                        Chunk tagValue)
        Creates a find-and-replace rule for tag replacement. See remarks at append(Chunk c).
        Parameters:
        tagName - will be ignored if null.
        tagValue - will be translated to the empty String if null.
        See Also:
        append(Chunk)
      • set

        public void set(java.lang.String tagName,
                        java.lang.Object tagValue)
        Convenience method, chains to set(String s, Object o, String ifNull)
      • setOrDelete

        public void setOrDelete(java.lang.String tagName,
                                java.lang.Object tagValue)
        Careful, setOrDelete will DELETE a previous value for the tag at this level if passed a null value. This is a way around the standard behavior which interprets null values as the empty string. The default is to use the empty string for null object/strings. setOrDelete provides an alternate option, leaving the tag "unset" in case the value is null, which allows the template to provide its own default value.
      • setLiteral

        public void setLiteral(java.lang.String tagName,
                               java.lang.String literalValue)
        setLiteral() tag values will render verbatim, so even if the value contains tags/specials they will not be expanded. However, if the final engine output is passed back into the chunk processor as a static string (for example, using chunk to pre-generate table rows that are destined for placement in another chunk), it will not be protected from the engine in that second pass. To prevent re-processing higher up the chain, encase your string in {% literal %}...{% endliteral %} tags, with the tradeoff that they will appear in your final output. This is by design, so the literal will be preserved even after multiple passes through the engine. Typical workaround: ... Or, just be super-careful to use setLiteral() again when placing pre-processed output into higher-level chunks.
        Parameters:
        tagName -
        literalValue -
      • set

        public void set(java.lang.String tagName,
                        java.lang.Object tagValue,
                        java.lang.String ifNull)
        Create a tag replacement rule, supplying a default value in case the value passed is null. If both the tagValue and the fallback are null, the rule created will resolve all instances of the tag to the string "NULL"
        Parameters:
        tagName - tag to replace
        tagValue - replacement value -- no-op unless this is of type String or Chunk.
        ifNull - fallback replacement value in case tagValue is null
      • setToBean

        public void setToBean(java.lang.String tagName,
                              java.lang.Object bean)
        Make bean properties available to template
      • setToBean

        public void setToBean(java.lang.String tagName,
                              java.lang.Object bean,
                              java.lang.String ifNull)
        Make bean properties available to template
      • set

        public void set(java.lang.String tagName)
        For convenience, sets a flag to "TRUE" - to reverse, call unset("flag")
      • set

        public void set(java.lang.String tagName,
                        int tagValue)
        For convenience, auto-converts int to String and creates tag replacement rule. Overwrites any existing rule with this tagName.
      • set

        public void set(java.lang.String tagName,
                        char tagValue)
        For convenience, auto-converts char to String and creates tag replacement rule. Overwrites any existing rule with this tagName.
      • set

        public void set(java.lang.String tagName,
                        long tagValue)
        For convenience, auto-converts long to String and creates tag replacement rule. Overwrites any existing rule with this tagName.
      • set

        public void set(java.lang.String tagName,
                        java.lang.StringBuilder tagValue)
        For convenience, auto-converts StringBuilder to String and creates tag replacement rule. Overwrites any existing rule with this tagName.
      • set

        public void set(java.lang.String tagName,
                        boolean value)
        For convenience, calls .set(tagName, "TRUE") for true values and .unset(tagName) for false values -- ie since the string "FALSE" would evaluate to true in a template if-expression.
      • set

        public void set(java.lang.String tagName,
                        java.lang.StringBuffer tagValue)
        For convenience, auto-converts StringBuffer to String and creates tag replacement rule. Overwrites any existing rule with this tagName.
      • unset

        public void unset(java.lang.String tagName)
        unset("tag") deletes the named tag expansion rule from the ruleset.
        Parameters:
        tagName -
      • hasValue

        public boolean hasValue(java.lang.String tagName)
        Returns:
        true if a rule exists for this tagName, otherwise false. Returns false if tagName is null.
      • stillNeeds

        public boolean stillNeeds(java.lang.String tagName)
        Returns:
        true if a rule does not yet exist for this tagName, otherwise false. Returns false if tagName is null.
      • toString

        public java.lang.String toString()
        Apply all tag replacement rules recursively and return template contents with translated tags. Rules and original template pieces remain intact, so toString can be called several times, modifying rules between each invocation to produce a slightly different output each time.
        Overrides:
        toString in class java.lang.Object
        Returns:
        A String with all template pieces assembled and all known tags recursively resolved.
      • toString

        public java.lang.String toString(Chunk context)
      • render

        public void render(java.io.PrintStream out)
                    throws java.io.IOException
        Throws:
        java.io.IOException
      • render

        public void render(java.io.Writer out)
                    throws java.io.IOException
        Throws:
        java.io.IOException
      • render

        public void render(java.io.Writer out,
                           Chunk context)
                    throws java.io.IOException
        Throws:
        java.io.IOException
      • getTagValue

        public java.lang.Object getTagValue(java.lang.String tagName)
        Retrieves a tag replacement rule. getTagValue() responds outside the context of recursive tag replacement, so the return value may include unresolved tags. To iterate up the ancestor chain, use get() instead.
        Returns:
        The Chunk or Snippet etc. that this tag will resolve to, or null if no rule yet exists.
      • resolveBackticks

        protected java.lang.String resolveBackticks(java.lang.String lookupName,
                                                    int depth)
      • resolveTagValue

        protected java.lang.Object resolveTagValue(SnippetTag tag,
                                                   int depth,
                                                   java.lang.String origin)
      • resolveTagValue

        protected java.lang.Object resolveTagValue(SnippetTag tag,
                                                   int depth)
      • _resolveTagValue

        protected java.lang.Object _resolveTagValue(SnippetTag tag,
                                                    int depth,
                                                    boolean ignoreParentContext)
      • boxIfAlienObject

        public static java.lang.Object boxIfAlienObject(java.lang.Object o)
      • resolveTagValue

        protected java.lang.Object resolveTagValue(java.lang.String tagName,
                                                   int depth)
      • resetTags

        public void resetTags()
        Clears all tag replacement rules.
      • clear

        public void clear()
        Specified by:
        clear in interface java.util.Map<java.lang.String,java.lang.Object>
      • resetTemplate

        public void resetTemplate()
        Clears template
      • containsKey

        public boolean containsKey(java.lang.Object key)
        Specified by:
        containsKey in interface java.util.Map<java.lang.String,java.lang.Object>
      • containsValue

        public boolean containsValue(java.lang.Object value)
        Specified by:
        containsValue in interface java.util.Map<java.lang.String,java.lang.Object>
      • entrySet

        public java.util.Set<java.util.Map.Entry<java.lang.String,java.lang.Object>> entrySet()
        Specified by:
        entrySet in interface java.util.Map<java.lang.String,java.lang.Object>
      • equals

        public boolean equals(java.lang.Object o)
        Specified by:
        equals in interface java.util.Map<java.lang.String,java.lang.Object>
        Overrides:
        equals in class java.lang.Object
      • get

        public java.lang.Object get(java.lang.Object key)
        Specified by:
        get in interface java.util.Map<java.lang.String,java.lang.Object>
      • hashCode

        public int hashCode()
        Specified by:
        hashCode in interface java.util.Map<java.lang.String,java.lang.Object>
        Overrides:
        hashCode in class java.lang.Object
      • isEmpty

        public boolean isEmpty()
        Specified by:
        isEmpty in interface java.util.Map<java.lang.String,java.lang.Object>
      • keySet

        public java.util.Set<java.lang.String> keySet()
        Specified by:
        keySet in interface java.util.Map<java.lang.String,java.lang.Object>
      • put

        public java.lang.Object put(java.lang.String key,
                                    java.lang.Object value)
        Specified by:
        put in interface java.util.Map<java.lang.String,java.lang.Object>
      • remove

        public java.lang.Object remove(java.lang.Object key)
        Specified by:
        remove in interface java.util.Map<java.lang.String,java.lang.Object>
      • putAll

        public void putAll(java.util.Map t)
        Specified by:
        putAll in interface java.util.Map<java.lang.String,java.lang.Object>
      • size

        public int size()
        Specified by:
        size in interface java.util.Map<java.lang.String,java.lang.Object>
      • values

        public java.util.Collection<java.lang.Object> values()
        Specified by:
        values in interface java.util.Map<java.lang.String,java.lang.Object>
      • setMultiple

        public void setMultiple(java.util.Map<java.lang.String,java.lang.Object> rules)
        Adds multiple find-and-replace rules using all entries in the Hashtable. Replaces an existing rule if tagNames collide.
      • setMultiple

        public void setMultiple(Chunk copyFrom)
        Adds multiple find-and-replace rules using all rules from the passed Chunk. Replaces any existing rules with the same tagName.
      • getTagsTable

        public java.util.Map<java.lang.String,java.lang.Object> getTagsTable()
        Retrieve all find-and-replace rules. Alterations to the returned Hashtable WILL AFFECT the tag replacement rules of the Chunk directly. Does not return a clone.
        Returns:
        a Hashtable containing the Chunk's find-and-replace rules.
      • addData

        public void addData(DataCapsule smartObj)
        Smart objects implementing DataCapsule can provide their own legend of available tags and which methods to call for exporting the tag data. NB: set() won't unwrap its data until "runtime" ie when toString() is called. addData() is not like that -- the exported values are copied and frozen when the addData() call is made. No pointer to the DataCapsule is kept.
        Parameters:
        smartObj -
      • addData

        public void addData(DataCapsule smartObj,
                            java.lang.String altPrefix)
        Two smart objects of the same type in a single template? No problem. Provide a unique altPrefix for each one to avoid tag namespace collisions. Note: these two calls are now equivalent: chunk.addData(capsule,"x"); chunk.set("x",capsule); The second form is preferred, so this method may get deprecated.
        Parameters:
        smartObj -
        altPrefix -
      • makeTag

        public java.lang.String makeTag(java.lang.String tagName)
      • setErrorHandling

        public void setErrorHandling(boolean renderErrs,
                                     java.io.PrintStream err)
      • setLocale

        public void setLocale(java.lang.String localeCode)
      • setLocale

        public void setLocale(java.util.Locale javaLocale)
      • setLocale

        public void setLocale(ChunkLocale chunkLocale)
      • findAndReplace

        public static java.lang.String findAndReplace(java.lang.String toSearch,
                                                      java.lang.String find,
                                                      java.lang.String replace)
        Useful utility function. An efficient find-and-replace-all algorithm for simple cases when regexp would be overkill. IMO they should have included this with String.
        Parameters:
        toSearch - text body.
        find - text to search for.
        replace - text to insert in place of "find" -- defaults to empty String if null is passed.
        Returns:
        a new String based on input with all instances of "find" replaced with "replace"
      • getTemplateOrigin

        public java.lang.String getTemplateOrigin()

Copyright © 2019. All Rights Reserved.