Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package org.xillium.base.text;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.util.*;
import java.util.regex.*;
import org.xillium.base.*;
import org.xillium.base.beans.Beans;
import org.xillium.base.beans.Strings;
/**
* This is a macro processor that handles 2 types of macro expansions in a text markup.
*
*
Reference expansion - insertion of another markup snippet with its own embedded parameters and references
*
*
* Dynamic data are provided by either a functor or an open object.
*
*/
public class Macro {
private static final Pattern PARAMETER = Pattern.compile("\\{([^{}@:-]+)(?::-([^{}@]+))?\\}");
private static final Pattern REFERENCE = Pattern.compile("\\{([^{}@]+)?@([^{}@]+)@([^{}@]+)?\\}");
/**
* Expands a text markup by resolving embedded parameters and references to other text markups, with the help of a companion
* open object.
*
* A reference pointing to another markup in the resources is marked up as {@code {PREFIX@MARKUP(ARGS):MEMBER:ALTERN(ARGS)@SUFFIX}},
* where
*
*
{@code PREFIX} and {@code SUFFIX} are optional pieces of text to be placed before and after the markup insertion.
*
{@code MARKUP} is a required element, which gives the name of the markup to be expanded recursively.
*
{@code MEMBER} gives the name of the data member within the companion object to be used as the companion object for the
* recursive expansion of the markup. If this name is "-", the current companion object is reused instead. If omitted, the
* value of {@code MARKUP} is used as this name.
*
{@code ALTERN} is an optional element, which gives the name of an alternative markup in the case the data member has no
* value.
*
{@code (ARGS)} is an optional list of positional arguments to be passed to the markup, which refers to such arguments using
* positional argument parameters {@code {1}}, {@code {2}}, etc.
*
*
* @param resources a collection of named text resources
* @param name the name of the text markup to be expanded
* @param object an object providing values to the parameters, which will be wrapped if not an open object already
* @throws IllegalArgumentException if any reference to a text markup cannot be resolved
* @throws NullPointerException if any reference to a data member cannot be resolved and the data member is required in the
* subsequence markup expansion
* @return the fully expanded text
*/
public static String expand(Map resources, final String name, final Object object, String[] args) {
String text = resources.get(name);
if (text == null) {
throw new IllegalArgumentException("Unknown text resource '" + name + '\'');
}
// use of namespace
int slash = name.indexOf('/');
String namespace = slash > -1 ? name.substring(0, slash) : null;
Object data = object == null ? null : ((object instanceof Open) ? object : new Open.Wrapper(Strings.toString(object)));
// expand all parameters first
text = expand(text, data, args);
while (true) {
Matcher matcher = REFERENCE.matcher(text);
if (matcher.find()) {
StringBuilder sb = new StringBuilder();
int top = 0;
List