Example usage for java.util.regex Matcher appendTail

List of usage examples for java.util.regex Matcher appendTail

Introduction

In this page you can find the example usage for java.util.regex Matcher appendTail.

Prototype

public StringBuilder appendTail(StringBuilder sb) 

Source Link

Document

Implements a terminal append-and-replace step.

Usage

From source file:de.escidoc.core.aa.business.UserAccountHandler.java

/**
 * replaces group-id-filter with resolved userIds.
 *
 * @param filter cql-filter//from  w  ww  .  j  a  va  2  s  .  c  om
 * @return Map with replaced cql-query (groupId replaced with userIds)
 * @throws InvalidSearchQueryException e
 * @throws SystemException             e
 */
private Map<String, String[]> fixCqlGroupFilter(final Map<String, String[]> filter)
        throws InvalidSearchQueryException, SystemException {

    Map<String, String[]> returnFilter = filter;
    final Object[] queryPartsObject = filter.get(Constants.SRU_PARAMETER_QUERY);
    if (queryPartsObject != null) {
        final String[] queryParts = new String[queryPartsObject.length];
        for (int i = 0; i < queryPartsObject.length; i++) {
            if (queryPartsObject[i] != null) {
                queryParts[i] = queryPartsObject[i].toString();
            }
        }
        boolean groupFilterFound = false;
        for (int i = 0; i < queryParts.length; i++) {
            final Matcher matcher = GROUP_FILTER_PATTERN.matcher(queryParts[i]);
            if (matcher.find()) {
                groupFilterFound = true;
                final Matcher groupFilterMatcher = GROUP_FILTER_PATTERN.matcher(queryParts[i]);
                final StringBuffer result = new StringBuffer("");
                while (groupFilterMatcher.find()) {
                    if (groupFilterMatcher.group(6).matches(".*?%.*")) {
                        throw new InvalidSearchQueryException("Wildcards not allowed in group-filter");
                    }
                    if (groupFilterMatcher.group(3) != null
                            && groupFilterMatcher.group(3).matches(">|<|<=|>=|<>")
                            || groupFilterMatcher.group(4) != null || groupFilterMatcher.group(5) != null) {
                        throw new InvalidSearchQueryException("non-supported relation in group-filter");
                    }
                    // get users for group
                    final StringBuilder replacement = new StringBuilder(" (");
                    try {
                        final Set<String> userIds = retrieveUsersForGroup(groupFilterMatcher.group(6));
                        // write user-cql-query
                        // and replace group-expression with it.
                        if (userIds != null && !userIds.isEmpty()) {
                            for (final String userId : userIds) {
                                if (replacement.length() > 2) {
                                    replacement.append(" or ");
                                }
                                replacement.append('\"');
                                replacement.append(Constants.FILTER_PATH_ID);
                                replacement.append("\"=").append(userId).append(' ');
                            }
                        } else {
                            throw new UserGroupNotFoundException("");
                        }
                    } catch (final UserGroupNotFoundException e) {
                        if (LOGGER.isWarnEnabled()) {
                            LOGGER.warn("Error on getting users for group.");
                        }
                        if (LOGGER.isDebugEnabled()) {
                            LOGGER.debug("Error on getting users for group.", e);
                        }
                        // if group has no users or group not found,
                        // write nonexisting user in query
                        replacement.append('\"');
                        replacement.append(Constants.FILTER_PATH_ID);
                        replacement.append("\"=").append("nonexistinguser").append(' ');
                    }

                    replacement.append(") ");
                    groupFilterMatcher.appendReplacement(result, replacement.toString());
                }
                groupFilterMatcher.appendTail(result);
                queryParts[i] = result.toString();
            }
        }
        if (groupFilterFound) {
            final Map<String, String[]> filter1 = new HashMap<String, String[]>();
            for (final Entry<String, String[]> entry : filter.entrySet()) {
                if (entry.getValue() != null) {
                    // noinspection RedundantCast
                    filter1.put(entry.getKey(), new String[((Object[]) entry.getValue()).length]);
                    // noinspection RedundantCast
                    for (int j = 0; j < ((Object[]) entry.getValue()).length; j++) {
                        filter1.get(entry.getKey())[j] = entry.getValue()[j];
                    }
                } else {
                    filter1.put(entry.getKey(), null);
                }
            }
            filter1.put(Constants.SRU_PARAMETER_QUERY, queryParts);
            returnFilter = filter1;
        }
    }
    return returnFilter;
}

From source file:org.apache.synapse.mediators.transform.PayloadFactoryMediator.java

/**
 * Replaces the payload format with SynapsePath arguments which are evaluated using getArgValues().
 *
 * @param format/*  w  w w.j  a  v a  2 s. com*/
 * @param result
 * @param synCtx
 */
private void replace(String format, StringBuffer result, MessageContext synCtx) {
    HashMap<String, String>[] argValues = getArgValues(synCtx);
    HashMap<String, String> replacement;
    Map.Entry<String, String> replacementEntry;
    String replacementValue = null;
    Matcher matcher;

    matcher = pattern.matcher(format);
    try {
        while (matcher.find()) {
            String matchSeq = matcher.group();
            int argIndex;
            try {
                argIndex = Integer.parseInt(matchSeq.substring(1, matchSeq.length()));
            } catch (NumberFormatException e) {
                argIndex = Integer.parseInt(matchSeq.substring(2, matchSeq.length() - 1));
            }
            replacement = argValues[argIndex - 1];
            replacementEntry = replacement.entrySet().iterator().next();
            if (mediaType.equals(JSON_TYPE) && inferReplacementType(replacementEntry).equals(XML_TYPE)) {
                // XML to JSON conversion here
                try {
                    replacementValue = "<jsonObject>" + replacementEntry.getKey() + "</jsonObject>";
                    OMElement omXML = AXIOMUtil.stringToOM(replacementValue);
                    // This is to replace \" with \\" and \\$ with \$. Because for Matcher, $ sign is
                    // a special character and for JSON " is a special character.
                    replacementValue = JsonUtil.toJsonString(omXML).toString()
                            .replaceAll(ESCAPE_DOUBLE_QUOTE_WITH_FIVE_BACK_SLASHES,
                                    ESCAPE_DOUBLE_QUOTE_WITH_NINE_BACK_SLASHES)
                            .replaceAll(ESCAPE_DOLLAR_WITH_TEN_BACK_SLASHES,
                                    ESCAPE_DOLLAR_WITH_SIX_BACK_SLASHES);
                } catch (XMLStreamException e) {
                    handleException(
                            "Error parsing XML for JSON conversion, please check your xPath expressions return valid XML: ",
                            synCtx);
                } catch (AxisFault e) {
                    handleException("Error converting XML to JSON", synCtx);
                }
            } else if (mediaType.equals(XML_TYPE) && inferReplacementType(replacementEntry).equals(JSON_TYPE)) {
                // JSON to XML conversion here
                try {
                    OMElement omXML = JsonUtil.toXml(IOUtils.toInputStream(replacementEntry.getKey()), false);
                    if (JsonUtil.isAJsonPayloadElement(omXML)) { // remove <jsonObject/> from result.
                        Iterator children = omXML.getChildElements();
                        String childrenStr = "";
                        while (children.hasNext()) {
                            childrenStr += (children.next()).toString().trim();
                        }
                        replacementValue = childrenStr;
                    } else { ///~
                        replacementValue = omXML.toString();
                    }
                    //replacementValue = omXML.toString();
                } catch (AxisFault e) {
                    handleException(
                            "Error converting JSON to XML, please check your JSON Path expressions return valid JSON: ",
                            synCtx);
                }
            } else {
                // No conversion required, as path evaluates to regular String.
                replacementValue = replacementEntry.getKey();
                // This is to replace " with \" and \\ with \\\\
                if (mediaType.equals(JSON_TYPE) && inferReplacementType(replacementEntry).equals(STRING_TYPE)
                        && (!replacementValue.startsWith("{") && !replacementValue.startsWith("["))) {
                    replacementValue = replacementValue
                            .replaceAll(Matcher.quoteReplacement("\\\\"),
                                    ESCAPE_BACK_SLASH_WITH_SIXTEEN_BACK_SLASHES)
                            .replaceAll("\"", ESCAPE_DOUBLE_QUOTE_WITH_TEN_BACK_SLASHES);
                } else if ((mediaType.equals(JSON_TYPE)
                        && inferReplacementType(replacementEntry).equals(JSON_TYPE))
                        && (!replacementValue.startsWith("{") && !replacementValue.startsWith("["))) {
                    // This is to handle only the string value
                    replacementValue = replacementValue.replaceAll("\"",
                            ESCAPE_DOUBLE_QUOTE_WITH_TEN_BACK_SLASHES);
                }
            }
            matcher.appendReplacement(result, replacementValue);
        }
    } catch (ArrayIndexOutOfBoundsException e) {
        log.error("#replace. Mis-match detected between number of formatters and arguments", e);
    }
    matcher.appendTail(result);
}

From source file:org.etudes.jforum.view.admin.ImportExportAction.java

/**
 * parse and replace emebeded references
 * @param message - message/*from   w  w w  .ja v  a 2 s.c  o  m*/
 * @param embeddedRef - embedded reference
 * @return - modified message
 */
private String parseAndReplacePostTextUrls(String message, String embeddedRef) {
    StringBuffer sb = new StringBuffer();

    Pattern p = getExportContentResourcePattern();

    Matcher m = p.matcher(message);
    while (m.find()) {
        if (m.groupCount() == 3) {
            String ref = m.group(3);

            String refPathWithFolders = ref.substring(ref.indexOf("/"));

            String embeddedRefPathWithFolders = embeddedRef.substring("/content/group/".length());
            embeddedRefPathWithFolders = embeddedRefPathWithFolders
                    .substring(embeddedRefPathWithFolders.indexOf("/"));

            String siteId = ToolManager.getCurrentPlacement().getContext();

            try {
                refPathWithFolders = URLDecoder.decode(refPathWithFolders, "UTF-8");
                //This is alreaded docoded
                //embeddedRefPathWithFolders = URLDecoder.decode(embeddedRefPathWithFolders, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                if (logger.isWarnEnabled())
                    logger.warn("parseAndReplacePostTextUrls: " + e);
            }

            if (refPathWithFolders.equalsIgnoreCase(embeddedRefPathWithFolders)) {
                m.appendReplacement(sb, Matcher.quoteReplacement(
                        m.group(1) + "=\"" + "embeded_jf_content/content/group" + refPathWithFolders + "\""));
            }

        }
    }
    m.appendTail(sb);

    return sb.toString();
}

From source file:org.etudes.jforum.view.admin.ImportExportAction.java

/**
 * parse embedded content resource urls/*from  ww  w  .j  a v a 2  s .c o m*/
 * 
 * @param message
 *          - message
 * @param parentPath
 *          - parent path
 * @param embeddedRefs
 *          - embedded references
 * @return
 *          - modifed content
 */
private String parseEmbeddedContentResourceUrls(String message, String parentPath, Set<String> embeddedRefs) {
    /*try
    {
       parentPath = URLDecoder.decode(parentPath, "UTF-8");
    }
    catch (UnsupportedEncodingException e)
    {
       if (logger.isWarnEnabled()) logger.warn("parseEmbeddedContentResourceUrls: " + e);
    }*/

    StringBuffer sb = new StringBuffer();

    Pattern p = getEmbeddedContentResourcePattern();

    Matcher m = p.matcher(message);
    while (m.find()) {
        if (m.groupCount() == 2) {
            String ref = m.group(2);

            if (logger.isDebugEnabled())
                logger.debug("parseEmbeddedContentResourceUrls Before : ref : " + ref);

            //check for the resource in the unzipped file before changing the url
            // embeddedRefs.add(ref);

            boolean excludeParentPath = false;

            // for relative paths
            if (ref.startsWith("..")) {
                ref = ref.substring(ref.lastIndexOf("..") + "..".length() + 1);
                excludeParentPath = true;
            }

            if (logger.isDebugEnabled())
                logger.debug("parseEmbeddedContentResourceUrls After : ref : " + ref);

            String siteId = ToolManager.getCurrentPlacement().getContext();

            if (excludeParentPath) {
                m.appendReplacement(sb, Matcher.quoteReplacement(
                        m.group(1) + "=\"/access/content/group/" + siteId + "/" + ref + "\""));
                embeddedRefs.add(ref);
            } else {
                m.appendReplacement(sb, Matcher.quoteReplacement(m.group(1) + "=\"/access/content/group/"
                        + siteId + Validator.escapeUrl(parentPath) + "/" + ref + "\""));
                embeddedRefs.add(parentPath + "/" + ref);
            }
        }
    }
    m.appendTail(sb);

    return sb.toString();
}

From source file:org.etudes.mneme.impl.AttachmentServiceImpl.java

/**
 * {@inheritDoc}/*from   w ww. j ava 2s  . c o  m*/
 */
protected String translateEmbeddedReferences(String data, Collection<Translation> translations,
        String parentRef) {
    if (data == null)
        return data;
    if (translations == null)
        return data;

    // pattern to find any src= or href= text
    // groups: 0: the whole matching text 1: src|href 2: the string in the quotes 3: the terminator character
    Pattern p = Pattern.compile("(src|href)[\\s]*=[\\s]*\"([^#\"]*)([#\"])",
            Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);

    Matcher m = p.matcher(data);
    StringBuffer sb = new StringBuffer();

    // process each "harvested" string (avoiding like strings that are not in src= or href= patterns)
    while (m.find()) {
        if (m.groupCount() == 3) {
            String ref = m.group(2);
            String terminator = m.group(3);

            if (ref != null)
                ref = ref.trim();

            // expand to a full reference if relative
            ref = adjustRelativeReference(ref, parentRef);

            // harvest any content hosting reference
            int index = ref.indexOf("/access/content/");
            if (index != -1) {
                // except for any in /user/ or /public/
                if (ref.indexOf("/access/content/user/") != -1) {
                    index = -1;
                } else if (ref.indexOf("/access/content/public/") != -1) {
                    index = -1;
                }
            }

            // harvest also the mneme docs references
            if (index == -1)
                index = ref.indexOf("/access/mneme/content/");

            if (index != -1) {
                // save just the reference part (i.e. after the /access);
                String normal = ref.substring(index + 7);

                // deal with %20, &amp;, and other encoded URL stuff
                normal = decodeUrl(normal);

                // translate the normal form
                String translated = normal;
                for (Translation translation : translations) {
                    translated = translation.translate(translated);
                }

                // URL encode translated
                String escaped = EscapeRefUrl.escapeUrl(translated);

                // if changed, replace
                if (!normal.equals(translated)) {
                    m.appendReplacement(sb, Matcher.quoteReplacement(
                            m.group(1) + "=\"" + ref.substring(0, index + 7) + escaped + terminator));
                }
            }
        }
    }

    m.appendTail(sb);
    return sb.toString();
}

From source file:org.etudes.jforum.view.admin.ImportExportAction.java

/**
 * parse export content resource reference urls
 * @param message//from w  ww  .j  ava 2s .  c o  m
 *             - messge   
 * @param ref
 *             - reference
 * @param parentPath
 *             - parent path
 * @return
 *             - modified content
 */
private String parseExportContentResourceUrls(String message, String ref, String parentPath) {
    ref = Validator.escapeUrl(ref);

    // file name with spaces doesn't have %20 for spaces
    // get file name
    /*This may not be needed as spaces have %20
     String fileName = ref.substring(ref.lastIndexOf("/") + 1);
            
    try
    {
       fileName = URLDecoder.decode(fileName, "UTF-8");
    }
    catch (UnsupportedEncodingException e)
    {
       if (logger.isWarnEnabled()) logger.warn("parseExportContentResourceUrls: " + e);
    }*/

    //ref = ref.substring(0, ref.lastIndexOf("/") + 1) + fileName;

    parentPath = Validator.escapeUrl(parentPath);

    StringBuffer sb = new StringBuffer();

    Pattern p = Pattern.compile("(src|href)[\\s]*=[\\s]*[\\\"'](/access" + ref + ")[\\\"']",
            Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.UNICODE_CASE);

    Matcher m = p.matcher(message);
    while (m.find()) {
        if (m.groupCount() == 2) {
            String refMatch = m.group(2);
            if (parentPath == null || parentPath.trim().length() == 0) {
                String siteId = ToolManager.getCurrentPlacement().getContext();
                refMatch = refMatch.substring(("/access/content/group/" + siteId).length() + 1);
            } else {

                if (refMatch.indexOf(parentPath) == -1) {
                    String siteId = ToolManager.getCurrentPlacement().getContext();
                    refMatch = refMatch.substring(("/access/content/group/" + siteId).length() + 1);

                    String pathRef[] = parentPath.split("/");

                    StringBuilder refPath = new StringBuilder();

                    for (int i = 0; i < (pathRef.length - 1); i++) {
                        refPath.append("../");
                    }
                    refMatch = refPath.toString() + refMatch;
                } else {
                    int index = refMatch.indexOf(parentPath);
                    refMatch = refMatch.substring(index + parentPath.length() + 1);
                }
            }

            /*String fileName1 = null;
            boolean escapeFilePath = false;
                    
            try
            {
               if (logger.isDebugEnabled()) logger.debug("parseExportContentResourceUrls: refMatch :"+ refMatch);
                              
               if (refMatch.lastIndexOf("/") != -1)
               {
                  fileName1 = refMatch.substring(refMatch.lastIndexOf("/")+1);
                  refMatch = refMatch.substring(0, refMatch.lastIndexOf("/")+1);
                          
                  if (logger.isDebugEnabled()) logger.debug("parseExportContentResourceUrls: refMatch sub string :"+ refMatch);
                          
                  fileName1 = URLDecoder.decode(fileName1, "UTF-8");
                  escapeFilePath = true;
               }
            }
            catch (UnsupportedEncodingException e)
            {
               if (logger.isWarnEnabled()) logger.warn("parseExportContentResourceUrls: " + e);
            }
                    
            if (escapeFilePath)
            {
               m.appendReplacement(sb, Matcher.quoteReplacement(m.group(1)+ "=\""+ refMatch + fileName1 +"\""));
            }
            else
               m.appendReplacement(sb, Matcher.quoteReplacement(m.group(1)+ "=\""+ refMatch + "\""));*/
            m.appendReplacement(sb, Matcher.quoteReplacement(m.group(1) + "=\"" + refMatch + "\""));

        }
    }
    m.appendTail(sb);

    return sb.toString();
}

From source file:au.org.ala.biocache.dao.SearchDAOImpl.java

/**
 * Format the search input query for a full-text search.
 *
 * This includes constructing a user friendly version of the query to
 * be used for display purposes./*from  w w w.ja  va 2s .  co  m*/
 * 
 * TODO Fix this to use a state.  REVISE!!
 *
 * @param searchParams
 */
protected void formatSearchQuery(SpatialSearchRequestParams searchParams, boolean forceQueryFormat) {
    //Only format the query if it doesn't already supply a formattedQuery.
    if (forceQueryFormat || StringUtils.isEmpty(searchParams.getFormattedQuery())) {
        // set the query
        String query = searchParams.getQ();

        //cached query parameters are already formatted
        if (query.contains("qid:")) {
            Matcher matcher = qidPattern.matcher(query);
            long qid = 0;
            while (matcher.find()) {
                String value = matcher.group();
                try {
                    String qidValue = SearchUtils.stripEscapedQuotes(value.substring(4));
                    qid = Long.parseLong(qidValue);
                    ParamsCacheObject pco = ParamsCache.get(qid);
                    if (pco != null) {
                        searchParams.setQId(qid);
                        searchParams.setQ(pco.getQ());
                        //add the fqs from the params cache
                        if (pco.getFqs() != null) {
                            String[] currentFqs = searchParams.getFq();
                            if (currentFqs == null || (currentFqs.length == 1 && currentFqs[0].length() == 0)) {
                                searchParams.setFq(pco.getFqs());
                            } else {
                                //we need to add the current Fqs together
                                searchParams.setFq((String[]) ArrayUtils.addAll(currentFqs, pco.getFqs()));
                            }
                        }
                        String displayString = pco.getDisplayString();

                        if (StringUtils.isNotEmpty(pco.getWkt())) {
                            displayString = displayString + " within user defined polygon";
                        }
                        searchParams.setDisplayString(displayString);

                        if (searchParams instanceof SpatialSearchRequestParams) {
                            ((SpatialSearchRequestParams) searchParams).setWkt(pco.getWkt());
                        } else if (StringUtils.isNotEmpty(pco.getWkt())) {
                            String originalQ = searchParams.getQ();
                            searchParams.setQ(spatialField + ":\"Intersects(" + pco.getWkt() + ")");
                            if (StringUtils.isNotEmpty(originalQ))
                                searchParams.setQ(searchParams.getQ() + " AND " + originalQ);
                        }
                        searchParams.setFormattedQuery(searchParams.getQ());
                        return;
                    }
                } catch (NumberFormatException e) {
                } catch (ParamsCacheMissingException e) {
                }
            }
        }
        StringBuffer queryString = new StringBuffer();
        StringBuffer displaySb = new StringBuffer();
        String displayString = query;

        // look for field:term sub queries and catch fields: matched_name & matched_name_children
        if (query.contains(":")) {
            // will match foo:bar, foo:"bar bash" & foo:bar\ bash
            Matcher matcher = termPattern.matcher(query);
            queryString.setLength(0);

            while (matcher.find()) {
                String value = matcher.group();
                logger.debug("term query: " + value);
                logger.debug("groups: " + matcher.group(1) + "|" + matcher.group(2));

                if ("matched_name".equals(matcher.group(1))) {
                    // name -> accepted taxon name (taxon_name:)
                    String field = matcher.group(1);
                    String queryText = matcher.group(2);

                    if (queryText != null && !queryText.isEmpty()) {
                        String guid = speciesLookupService.getGuidForName(queryText.replaceAll("\"", "")); // strip any quotes
                        logger.info("GUID for " + queryText + " = " + guid);

                        if (guid != null && !guid.isEmpty()) {
                            String acceptedName = speciesLookupService.getAcceptedNameForGuid(guid); // strip any quotes
                            logger.info("acceptedName for " + queryText + " = " + acceptedName);

                            if (acceptedName != null && !acceptedName.isEmpty()) {
                                field = "taxon_name";
                                queryText = acceptedName;
                            }
                        } else {
                            field = "taxon_name";
                        }

                        // also change the display query
                        displayString = displayString.replaceAll("matched_name", "taxon_name");
                    }

                    if (StringUtils.containsAny(queryText, CHARS) && !queryText.startsWith("[")) {
                        // quote any text that has spaces or colons but not range queries
                        queryText = QUOTE + queryText + QUOTE;
                    }

                    logger.debug("queryText: " + queryText);

                    matcher.appendReplacement(queryString, matcher.quoteReplacement(field + ":" + queryText));

                } else if ("matched_name_children".equals(matcher.group(1))) {
                    String field = matcher.group(1);
                    String queryText = matcher.group(2);

                    if (queryText != null && !queryText.isEmpty()) {
                        String guid = speciesLookupService.getGuidForName(queryText.replaceAll("\"", "")); // strip any quotes
                        logger.info("GUID for " + queryText + " = " + guid);

                        if (guid != null && !guid.isEmpty()) {
                            field = "lsid";
                            queryText = guid;
                        } else {
                            field = "taxon_name";
                        }
                    }

                    if (StringUtils.containsAny(queryText, CHARS) && !queryText.startsWith("[")) {
                        // quote any text that has spaces or colons but not range queries
                        queryText = QUOTE + queryText + QUOTE;
                    }

                    matcher.appendReplacement(queryString, matcher.quoteReplacement(field + ":" + queryText));
                } else {
                    matcher.appendReplacement(queryString, matcher.quoteReplacement(value));
                }
            }
            matcher.appendTail(queryString);
            query = queryString.toString();
        }

        //if the query string contains lsid: we will need to replace it with the corresponding lft range
        int last = 0;
        if (query.contains("lsid:")) {
            Matcher matcher = lsidPattern.matcher(query);
            queryString.setLength(0);
            while (matcher.find()) {
                //only want to process the "lsid" if it does not represent taxon_concept_lsid etc...
                if ((matcher.start() > 0 && query.charAt(matcher.start() - 1) != '_') || matcher.start() == 0) {
                    String value = matcher.group();
                    logger.debug("preprocessing " + value);
                    String lsid = matcher.group(2);
                    if (lsid.contains("\"")) {
                        //remove surrounding quotes, if present
                        lsid = lsid.replaceAll("\"", "");
                    }
                    if (lsid.contains("\\")) {
                        //remove internal \ chars, if present
                        //noinspection MalformedRegex
                        lsid = lsid.replaceAll("\\\\", "");
                    }
                    logger.debug("lsid = " + lsid);
                    String[] values = searchUtils.getTaxonSearch(lsid);
                    String lsidHeader = matcher.group(1).length() > 0 ? matcher.group(1) : "";
                    matcher.appendReplacement(queryString, lsidHeader + values[0]);
                    displaySb.append(query.substring(last, matcher.start()));
                    if (!values[1].startsWith("taxon_concept_lsid:"))
                        displaySb.append(lsidHeader).append("<span class='lsid' id='").append(lsid).append("'>")
                                .append(values[1]).append("</span>");
                    else
                        displaySb.append(lsidHeader).append(values[1]);
                    last = matcher.end();
                    //matcher.appendReplacement(displayString, values[1]);
                }
            }
            matcher.appendTail(queryString);
            displaySb.append(query.substring(last, query.length()));

            query = queryString.toString();
            displayString = displaySb.toString();
        }

        if (query.contains("urn")) {
            //escape the URN strings before escaping the rest this avoids the issue with attempting to search on a urn field
            Matcher matcher = urnPattern.matcher(query);
            queryString.setLength(0);
            while (matcher.find()) {
                String value = matcher.group();

                logger.debug("escaping lsid urns  " + value);
                matcher.appendReplacement(queryString, prepareSolrStringForReplacement(value));
            }
            matcher.appendTail(queryString);
            query = queryString.toString();
        }

        if (query.contains("Intersects")) {
            Matcher matcher = spatialPattern.matcher(query);
            if (matcher.find()) {
                String spatial = matcher.group();
                SpatialSearchRequestParams subQuery = new SpatialSearchRequestParams();
                logger.debug("region Start : " + matcher.regionStart() + " start :  " + matcher.start()
                        + " spatial length " + spatial.length() + " query length " + query.length());
                //format the search query of the remaining text only
                subQuery.setQ(query.substring(matcher.start() + spatial.length(), query.length()));
                //format the remaining query
                formatSearchQuery(subQuery);

                //now append Q's together
                queryString.setLength(0);
                //need to include the prefix
                queryString.append(query.substring(0, matcher.start()));
                queryString.append(spatial);
                queryString.append(subQuery.getFormattedQuery());
                searchParams.setFormattedQuery(queryString.toString());
                //add the spatial information to the display string
                if (spatial.contains("circles")) {
                    String[] values = spatial.substring(spatial.indexOf("=") + 1, spatial.indexOf("}"))
                            .split(",");
                    if (values.length == 3) {
                        displaySb.setLength(0);
                        displaySb.append(subQuery.getDisplayString());
                        displaySb.append(" - within ").append(values[2]).append(" km of point(")
                                .append(values[0]).append(",").append(values[1]).append(")");
                        searchParams.setDisplayString(displaySb.toString());
                    }

                } else {
                    searchParams.setDisplayString(subQuery.getDisplayString() + " - within supplied region");
                }
            }
        } else {
            //escape reserved characters unless the colon represnts a field name colon
            queryString.setLength(0);

            Matcher matcher = spacesPattern.matcher(query);
            while (matcher.find()) {
                String value = matcher.group();

                //special cases to ignore from character escaping
                //if the value is a single - or * it means that we don't want to escape it as it is likely to have occurred in the following situation -(occurrence_date:[* TO *]) or *:*
                if (!value.equals("-")
                        && /*!value.equals("*")  && !value.equals("*:*") && */ !value.endsWith("*")) {

                    //split on the colon
                    String[] bits = StringUtils.split(value, ":", 2);
                    if (bits.length == 2) {
                        if (!bits[0].contains("urn") && !bits[1].contains("urn\\"))
                            matcher.appendReplacement(queryString,
                                    bits[0] + ":" + prepareSolrStringForReplacement(bits[1]));

                    } else if (!value.endsWith(":")) {
                        //need to ignore field names where the : is at the end because the pattern matching will return field_name: as a match when it has a double quoted value
                        //default behaviour is to escape all 
                        matcher.appendReplacement(queryString, prepareSolrStringForReplacement(value));
                    }
                }
            }
            matcher.appendTail(queryString);

            //substitute better display strings for collection/inst etc searches
            if (displayString.contains("_uid")) {
                displaySb.setLength(0);
                String normalised = displayString.replaceAll("\"", "");
                matcher = uidPattern.matcher(normalised);
                while (matcher.find()) {
                    String newVal = "<span>"
                            + searchUtils.getUidDisplayString(matcher.group(1), matcher.group(2)) + "</span>";
                    if (newVal != null)
                        matcher.appendReplacement(displaySb, newVal);
                }
                matcher.appendTail(displaySb);
                displayString = displaySb.toString();
            }
            if (searchParams.getQ().equals("*:*")) {
                displayString = "[all records]";
            }
            if (searchParams.getLat() != null && searchParams.getLon() != null
                    && searchParams.getRadius() != null) {
                displaySb.setLength(0);
                displaySb.append(displayString);
                displaySb.append(" - within ").append(searchParams.getRadius()).append(" km of point(")
                        .append(searchParams.getLat()).append(",").append(searchParams.getLon()).append(")");
                displayString = displaySb.toString();

            }

            // substitute i18n version of field name, if found in messages.properties
            displayString = formatDisplayStringWithI18n(displayString);

            searchParams.setFormattedQuery(queryString.toString());
            logger.debug("formattedQuery = " + queryString);
            logger.debug("displayString = " + displayString);
            searchParams.setDisplayString(displayString);
        }

        //format the fq's for facets that need ranges substituted
        for (int i = 0; i < searchParams.getFq().length; i++) {
            String fq = searchParams.getFq()[i];
            String[] parts = fq.split(":", 2);
            //check to see if the first part is a range based query and update if necessary
            Map<String, String> titleMap = RangeBasedFacets.getTitleMap(parts[0]);
            if (titleMap != null) {
                searchParams.getFq()[i] = titleMap.get(parts[1]);
            }
        }
    }
    searchParams.setDisplayString(formatDisplayStringWithI18n(searchParams.getDisplayString()));
}

From source file:com.ibm.cics.ca1y.Emit.java

/**
 * Using the key and pattern to identify a token, replace all tokens for the key value in the property.
 * /*  w  w  w .j a  va 2s.  com*/
 * @param key
 *            - The string to search for tokens
 * @param pattern
 *            - The pattern to use to find tokens
 * @param props
 *            - The properties to use as the lookup table
 * @param cicsChannel
 *            - The channel to get containers from
 * @param channelDFHEP
 *            - True if the name of the current CICS channel is identified as event processing
 * @param allowReevaluateForTokens
 *            - reevaluated the key for tokens if at least one token was replaced with a value that could contain
 *            another token
 * @return true if at least one token was replaced
 */
private static boolean resolveTokensInKey(String key, Pattern pattern, EmitProperties props,
        Channel cicsChannel, boolean channelDFHEP, boolean allowReevaluateForTokens) {

    if ((key == null) || (pattern == null) || (props == null) || (props.getProperty(key) == null)) {
        return false;
    }

    if (props.getPropertyResolved(key)) {
        // Property is already resolved
        if (logger.isLoggable(Level.FINE)) {
            logger.fine(messages.getString("PropertyResolved") + " "
                    + EmitProperties.getPropertySummary(props, key));
        }

        return false;
    }

    if ((logger.isLoggable(Level.FINE))) {
        logger.fine(
                messages.getString("PropertyResolving") + " " + EmitProperties.getPropertySummary(props, key));
    }

    // Do not change the order of the command options, as later processing relies on this order
    final String[] nomoretokensCommand = { "nomoretokens" };
    final String[] noinnertokensCommand = { "noinnertokens" };
    final String[] datetimeCommand = { "datetime=" };
    final String[] mimeCommand = { "mime=", ":to=", ":xslt=" };
    final String[] nameCommand = { "name=" };
    final String[] responsecontainerCommand = { "responsecontainer=" };
    final String[] fileCommand = { "file=", ":encoding=", ":binary", ":options=" };
    final String[] zipCommand = { "zip=", ":include=" };
    final String[] doctemplateCommand = { "doctemplate=", ":addPropertiesAsSymbols", ":binary" };
    final String[] transformCommand = { "transform=", ":xmltransform=", ":jsontransform=", ":jvmserver=" };
    final String[] ftpCommand = { "ftp=", ":server=", ":username=", ":userpassword=", ":transfer=", ":mode=",
            ":epsv=", ":protocol=", ":trustmgr=", ":datatimeout=", ":proxyserver=", ":proxyusername=",
            ":proxypassword=" };
    final String[] hexCommand = { "hex=" };
    final String[] systemsymbolCommand = { "systemsymbol=" };
    final String[] commonBaseEventRESTCommand = { "commonbaseeventrest" };
    final String[] commonBaseEventCommandCommand = { "commonbaseevent" };
    final String[] cicsFlattenedEventCommand = { "cicsflattenedevent" };
    final String[] jsonCommand = { "json", ":properties=" };
    final String[] emitCommand = { "emit" };
    final String[] linkCommand = { "link=" };
    final String[] putcontainerCommand = { "putcontainer=" };
    final String[] htmltableCommand = { "htmltable", ":properties=", ":containers=", ":summary" };
    final String[] texttableCommand = { "texttable", ":properties=", ":containers=", ":summary" };
    final String[] trimCommand = { "trim" };

    final int maxTimesToResolveTokens = 10;

    boolean tokenReplaced = false;
    boolean allowPropertyToBeReevaluatedForTokens = true;
    String putContainer = null;

    // Indicate the property has been resolved at this point to prevent recursion resolving tokens within the
    // property
    props.setPropertyResolved(key, true);

    // As a token's replacement may itself contain tokens we need to iterate token searching, up to a maximum number
    // of times
    for (int i = 0; (i < maxTimesToResolveTokens) && (allowPropertyToBeReevaluatedForTokens); i++) {
        boolean reevaluateForTokens = false;
        Matcher matcher = pattern.matcher(props.getProperty(key));
        StringBuffer buffer = new StringBuffer();

        while (matcher.find()) {
            String token = matcher.group(1).trim();

            if (logger.isLoggable(Level.FINE)) {
                logger.fine(Emit.messages.getString("ResolvingToken") + " " + token);
            }

            if (token.startsWith(nomoretokensCommand[0])) {
                // Do not process any more tokens in the key
                tokenReplaced = true;
                matcher.appendReplacement(buffer, "");
                reevaluateForTokens = false;
                break;

            } else if (token.startsWith(noinnertokensCommand[0])) {
                // Processing all tokens in this property but prevent re-evaluation tokens in resolve tokens
                // eg. token is "noinntertokens"
                tokenReplaced = true;
                matcher.appendReplacement(buffer, "");
                allowPropertyToBeReevaluatedForTokens = false;

            } else if (token.startsWith(transformCommand[0])) {
                tokenReplaced = true;
                matcher.appendReplacement(buffer, "");
                String[] options = Util.getOptions(token, transformCommand);

                if (isCICS == false) {
                    // if CICS API not available, ignore this token

                } else if (options[1] != null) {
                    // eg. {transform=property:xmltranform=resource}
                    try {
                        Container inputContainer = cicsChannel.createContainer("CA1Y-TRANSFORM-I");
                        Container outputContainer = cicsChannel.createContainer("CA1Y-TRANSFORM-O");

                        if (props.getPropertyAttachment(options[0]) == null) {
                            // property is text
                            inputContainer.putString(props.getProperty(options[0]));

                        } else {
                            // property is binary
                            inputContainer.put(props.getPropertyAttachment(options[0]));
                        }

                        XmlTransform xmltransform = new XmlTransform(options[1]);
                        TransformInput transforminput = new TransformInput();
                        transforminput.setChannel(cicsChannel);
                        transforminput.setDataContainer(inputContainer);
                        transforminput.setXmlContainer(outputContainer);
                        transforminput.setXmltransform(xmltransform);

                        Transform.dataToXML(transforminput);

                        buffer.append(new String(outputContainer.get(Emit.defaultCharset)));

                        inputContainer.delete();
                        outputContainer.delete();

                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                } else if (options[2] != null) {
                    // eg. {transform=propertyName:jsontransform=jsontransfrmResource:jvmserver=jvmResource}
                    try {
                        Container inputContainer = cicsChannel.createContainer("DFHJSON-DATA");
                        inputContainer.put(props.getPropertyAttachment(options[0]));

                        inputContainer = cicsChannel.createContainer("DFHJSON-TRANSFRM");
                        inputContainer.putString(options[2]);

                        if (options[3] != null) {
                            inputContainer = cicsChannel.createContainer("DFHJSON-JVMSERVR");
                            inputContainer.putString(options[3]);
                        }

                        Program p = new Program();
                        p.setName("DFHJSON");
                        p.link(cicsChannel);

                        Container outputContainer = (cicsChannel).createContainer("DFHSON-JSON");
                        buffer.append(outputContainer.get("UTF-8"));

                        inputContainer.delete();
                        outputContainer.delete();

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

            } else if (token.startsWith(mimeCommand[0])) {
                // Store the MIME type in the property
                // eg. token is "mime=application/octet-stream"
                // eg. token is "mime=application/xml"
                // eg. token is "mime=text/xsl:to=application/pdf"
                // eg. token is "mime=text/xml:to=application/pdf:xslt=xsltProperty"
                tokenReplaced = true;
                matcher.appendReplacement(buffer, "");
                String[] options = Util.getOptions(token, mimeCommand);

                props.setPropertyMime(key, options[0]);

                if (options[1] != null) {
                    props.setPropertyMimeTo(key, options[1]);
                }

                if (options[2] != null) {
                    props.setPropertyXSLT(key, options[2]);
                }

            } else if (token.startsWith(nameCommand[0])) {
                // Store the name in the property
                // eg. token is "name=My Picture"
                tokenReplaced = true;
                matcher.appendReplacement(buffer, "");
                String[] options = Util.getOptions(token, nameCommand);
                props.setPropertyAlternateName(key, options[0]);

            } else if (token.startsWith(datetimeCommand[0])) {
                // For tokens that start with DATETIME_PREFIX, replace the token with the resolved date and time
                // format
                // eg. token is "datetime=yyyy.MM.dd G 'at' HH:mm:ss z"
                tokenReplaced = true;
                matcher.appendReplacement(buffer, "");
                String[] options = Util.getOptions(token, datetimeCommand);

                Date date = null;
                if (props.containsKey("EPCX_ABSTIME")) {
                    date = Util.getDateFromAbstime(props.getProperty("EPCX_ABSTIME"));

                } else {
                    date = new Date();
                }

                if (options[0].isEmpty()) {
                    buffer.append(date.toString());

                } else {
                    buffer.append((new SimpleDateFormat(options[0])).format(date));
                }

            } else if (token.startsWith(zipCommand[0])) {
                // Store the zip in the property
                // eg. token is "zip="
                // eg. token is "zip=MyZip.zip"
                // eg. token is "zip=MyZip.zip:include=property1"
                // eg. token is "zip=MyZip.zip:include=property1,property2"
                tokenReplaced = true;
                matcher.appendReplacement(buffer, "");
                String[] options = Util.getOptions(token, zipCommand);

                if (options[0].isEmpty()) {
                    props.setPropertyZip(key, key + ".zip");

                } else {
                    props.setPropertyZip(key, options[0]);
                }

                if (options[1] != null) {
                    props.setPropertyZipInclude(key, options[1]);

                } else {
                    props.setPropertyZipInclude(key, key);
                }

            } else if (token.startsWith(fileCommand[0])) {
                // Replace the token with the content of the file
                // eg. token is "file=//dataset"
                // eg. token is "file=//dataset:encoding=Cp1047"
                // eg. token is "file=//dataset:binary"
                tokenReplaced = true;
                matcher.appendReplacement(buffer, "");
                String[] options = Util.getOptions(token, fileCommand);

                if (options[0].startsWith("//")) {
                    ZFile zf = null;

                    if (options[2] != null) {

                        try {
                            // Read dataset in binary using IBM JZOS library
                            zf = new ZFile(options[0], (options[3] == null) ? "rb" : options[3]);

                            if (props.getPropertyAlternateName(key) == null) {
                                props.setPropertyAlternateName(key, zf.getActualFilename());
                            }

                            if (props.getPropertyMime(key) == null) {
                                props.setPropertyMimeDefault(key, zf.getActualFilename());
                            }

                            props.setPropertyAttachment(key, IOUtils.toByteArray(zf.getInputStream()));

                        } catch (Exception e) {
                            e.printStackTrace();

                        } finally {
                            if (zf != null) {
                                try {
                                    zf.close();
                                } catch (Exception e2) {
                                }
                            }
                        }

                    } else {
                        try {
                            // Read dataset in text using IBM JZOS library
                            zf = new ZFile(options[0], (options[3] == null) ? "r" : options[3]);

                            if (props.getPropertyAlternateName(key) == null) {
                                props.setPropertyAlternateName(key, zf.getActualFilename());
                            }

                            buffer.append(IOUtils.toString(zf.getInputStream(),
                                    (options[1] == null) ? ZUtil.getDefaultPlatformEncoding() : options[1]));
                            reevaluateForTokens = true;

                        } catch (Exception e) {
                            e.printStackTrace();

                        } finally {
                            if (zf != null) {
                                try {
                                    zf.close();
                                } catch (Exception e2) {
                                }
                            }
                        }
                    }

                } else {
                    File f = null;

                    if (options[2] != null) {
                        try {
                            f = new File(options[0]);
                            // Read zFS file as binary

                            if (props.getPropertyAlternateName(key) == null) {
                                props.setPropertyAlternateName(key, f.getName());
                            }

                            if (props.getPropertyMime(key) == null) {
                                props.setPropertyMimeDefault(key, f.getName());
                            }

                            props.setPropertyAttachment(key, FileUtils.readFileToByteArray(f));

                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    } else {
                        try {
                            // Read zFS file as text
                            f = new File(options[0]);

                            if (props.getPropertyAlternateName(key) == null) {
                                props.setPropertyAlternateName(key, f.getName());
                            }

                            buffer.append(FileUtils.readFileToString(f, options[1]));
                            reevaluateForTokens = true;

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }

            } else if (token.startsWith(doctemplateCommand[0])) {
                // Replace the token with the contents of the CICS document template resource
                // eg. token is "doctemplate=MyTemplate"
                // eg. token is "doctemplate=MyTemplate:addPropertiesAsSymbols"
                // eg. token is "doctemplate=MyTemplate:binary"
                tokenReplaced = true;
                matcher.appendReplacement(buffer, "");
                String[] options = Util.getOptions(token, doctemplateCommand);

                if (options[2] == null) {
                    try {
                        // Read doctemplate as text
                        if (props.getPropertyAlternateName(key) == null) {
                            props.setPropertyAlternateName(key, options[0]);
                        }

                        Document document = new Document();

                        if (options[1] != null) {
                            // Add all properties except the current property as symbols to document
                            Enumeration<?> e = props.propertyNamesOrdered();

                            while (e.hasMoreElements()) {
                                String key2 = (String) e.nextElement();

                                // Only add property if it is not the current key and is not private
                                if ((key.equals(key2) == false) && (props.getPropertyPrivacy(key2) == false)) {
                                    try {
                                        if ((props.getPropertyResolved(key2) == false)
                                                && (allowPropertyToBeReevaluatedForTokens)) {
                                            // resolve the token before attempting to use it
                                            resolveTokensInKey(key2, pattern, props, cicsChannel, channelDFHEP,
                                                    allowReevaluateForTokens);
                                        }

                                        document.addSymbol(key2, props.getProperty(key2));

                                    } catch (Exception e2) {
                                        // Continue even if there are errors adding a symbol
                                    }
                                }
                            }
                        }

                        document.createTemplate(options[0]);
                        buffer.append(new String(document.retrieve("UTF-8", true), "UTF-8"));
                        reevaluateForTokens = true;

                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                } else {
                    try {
                        // Read doctemplate as binary
                        if (props.getPropertyAlternateName(key) == null) {
                            props.setPropertyAlternateName(key, options[0]);
                        }

                        if (props.getPropertyMime(key) == null) {
                            props.setPropertyMimeDefault(key, options[0]);
                        }

                        Document document = new Document();
                        document.createTemplate(options[0]);
                        props.setPropertyAttachment(key, document.retrieve());

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

            } else if (token.startsWith(commonBaseEventRESTCommand[0])) {
                tokenReplaced = true;
                matcher.appendReplacement(buffer, "");
                buffer.append(getCommonBaseEventREST(props));
                reevaluateForTokens = true;

                if (props.getPropertyMime(key) == null) {
                    props.setPropertyMime(key, MIME_XML);
                }

            } else if (token.startsWith(commonBaseEventCommandCommand[0])) {
                tokenReplaced = true;
                matcher.appendReplacement(buffer, "");
                buffer.append(getCommonBaseEvent(props));
                reevaluateForTokens = true;

                if (props.getPropertyMime(key) == null) {
                    props.setPropertyMime(key, MIME_XML);
                }

            } else if (token.startsWith(cicsFlattenedEventCommand[0])) {
                tokenReplaced = true;
                matcher.appendReplacement(buffer, "");
                buffer.append(getCicsFlattenedEvent(props));
                reevaluateForTokens = true;

            } else if (token.startsWith(jsonCommand[0])) {
                tokenReplaced = true;
                matcher.appendReplacement(buffer, "");
                String options[] = Util.getOptions(token, jsonCommand);
                buffer.append(getJson(props, options[1]));
                reevaluateForTokens = true;

                if (props.getPropertyMime(key) == null) {
                    props.setPropertyMime(key, MIME_JSON);
                }

            } else if (token.startsWith(htmltableCommand[0])) {
                tokenReplaced = true;
                matcher.appendReplacement(buffer, "");
                String options[] = Util.getOptions(token, htmltableCommand);
                buffer.append(getHtmlTable(props, options[1], options[2], options[3], isCICS));

            } else if (token.startsWith(texttableCommand[0])) {
                tokenReplaced = true;
                matcher.appendReplacement(buffer, "");
                String options[] = Util.getOptions(token, texttableCommand);
                buffer.append(getTextTable(props, options[1], options[2], options[3]));

            } else if (token.startsWith(ftpCommand[0])) {
                tokenReplaced = true;
                matcher.appendReplacement(buffer, "");
                String options[] = Util.getOptions(token, ftpCommand);
                String defaultanonymouspassword = null;

                if (isCICS) {
                    defaultanonymouspassword = channelDFHEP ? props.getProperty("EPCX_USERID")
                            : props.getProperty("TASK_USERID");

                    if (defaultanonymouspassword != null) {
                        try {
                            defaultanonymouspassword = (new StringBuilder(
                                    String.valueOf(defaultanonymouspassword))).append("@")
                                            .append(InetAddress.getLocalHost().getHostName()).toString();
                        } catch (Exception _ex) {
                        }
                    }
                }

                byte b[] = getFileUsingFTP(options[0], options[1], options[2], options[3], options[4],
                        options[5], options[6], options[7], options[8], options[9], options[10], options[11],
                        options[12], defaultanonymouspassword);

                if (b != null) {
                    if ("binary".equalsIgnoreCase(options[4])) {
                        String filename = FilenameUtils.getName(options[0]);

                        if (props.getPropertyAlternateName(key) == null) {
                            props.setPropertyAlternateName(key, filename);
                        }

                        if (props.getPropertyMime(key) == null) {
                            props.setPropertyMimeDefault(key, filename);
                        }

                        props.setPropertyAttachment(key, b);

                    } else {
                        try {
                            buffer.append(new String(b, "US-ASCII"));
                            reevaluateForTokens = true;

                        } catch (Exception _ex) {
                            // Ignore
                        }
                    }
                }

            } else if (token.startsWith(responsecontainerCommand[0])) {
                // Store the name in the property
                // eg. token is "responsecontainer="
                // eg. token is "responsecontainer=MyContainer"
                tokenReplaced = true;
                matcher.appendReplacement(buffer, "");
                String[] options = Util.getOptions(token, responsecontainerCommand);
                props.setPropertyReturnContainer(key, (options[0].isEmpty()) ? key : options[0]);

            } else if (token.startsWith(hexCommand[0])) {
                tokenReplaced = true;
                matcher.appendReplacement(buffer, "");
                String options[] = Util.getOptions(token, hexCommand);

                if (props.getProperty(options[0]) != null) {
                    buffer.append(Util.getHex(props.getProperty(options[0]).getBytes()));

                } else {
                    if (props.getPropertyAttachment(options[0]) != null)
                        buffer.append(Util.getHex(props.getPropertyAttachment(options[0])));
                }

            } else if (token.startsWith(emitCommand[0])) {
                // Set this property to be emitted
                tokenReplaced = true;
                matcher.appendReplacement(buffer, "");
                props.putBusinessInformationItem(key, 0);

            } else if (token.startsWith(systemsymbolCommand[0])) {
                // Get the z/OS system symbol
                tokenReplaced = true;
                matcher.appendReplacement(buffer, "");
                String options[] = Util.getOptions(token, systemsymbolCommand);

                try {
                    buffer.append(Util.getSystemSymbol(options[0]));
                } catch (Exception e) {
                }

            } else if (token.startsWith(linkCommand[0])) {
                // Link to CICS program
                tokenReplaced = true;
                matcher.appendReplacement(buffer, "");
                String options[] = Util.getOptions(token, linkCommand);

                if (isCICS) {
                    // Only execute if CICS API is available

                    if (logger.isLoggable(Level.FINE)) {
                        logger.fine(messages.getString("LinkTo") + " " + options[0]);
                    }

                    Program p = new Program();
                    p.setName(options[0]);

                    try {
                        p.link(cicsChannel);

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

            } else if (token.startsWith(putcontainerCommand[0])) {
                // Put the value of this property into a CICS container once the tokens have been resolve
                tokenReplaced = true;
                matcher.appendReplacement(buffer, "");
                String options[] = Util.getOptions(token, putcontainerCommand);

                putContainer = options[0];

            } else if (token.startsWith(trimCommand[0])) {
                tokenReplaced = true;
                matcher.appendReplacement(buffer, "");

                // Remove leading and training spaces from the buffer up to the point of the token
                buffer = new StringBuffer(buffer.toString().trim());

            } else if (props.containsKey(token)) {
                // The token refers to a property
                tokenReplaced = true;
                matcher.appendReplacement(buffer, "");

                // Do not copy property if it is the current key or it is marked as private
                if ((key.equals(token) == false) && (props.getPropertyPrivacy(key) == false)) {

                    if ((props.getPropertyResolved(token) == false)
                            && (allowPropertyToBeReevaluatedForTokens)) {
                        // Resolve the tokens in the property before attempting to use it
                        resolveTokensInKey(token, pattern, props, cicsChannel, channelDFHEP,
                                allowReevaluateForTokens);
                    }

                    if (props.getPropertyAttachment(token) == null) {
                        // Copy the tokens' property
                        buffer.append(props.getProperty(token));

                    } else {
                        // Copy the tokens' property attachment

                        if (props.getPropertyAttachment(key) == null) {
                            // current property does not have an attachment
                            props.setPropertyAttachment(key, Arrays.copyOf(props.getPropertyAttachment(token),
                                    props.getPropertyAttachment(token).length));

                        } else {
                            // Current property has an attachment, so append the tokens' attachment
                            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

                            try {
                                outputStream.write(props.getPropertyAttachment(key));
                                outputStream.write(props.getPropertyAttachment(token));

                            } catch (Exception e) {
                            }

                            props.setPropertyAttachment(key, outputStream.toByteArray());
                        }
                    }

                    props.setPropertyAlternateName(key, props.getPropertyAlternateName(token));
                }

            } else if (System.getProperty(token) != null) {
                // The token refers to a system property
                tokenReplaced = true;
                matcher.appendReplacement(buffer, "");
                buffer.append(System.getProperty(token));

            } else if (isCICS && (token.getBytes().length >= CONTAINER_NAME_LENGTH_MIN)
                    && (token.getBytes().length <= CONTAINER_NAME_LENGTH_MAX)) {
                // If this is not an EP event and the token is a max of 16 characters, attempt to replace token with
                // contents of the CICS container named by the token in current channel
                tokenReplaced = true;
                matcher.appendReplacement(buffer, "");

                if (logger.isLoggable(Level.FINE)) {
                    logger.fine(messages.getString("GetContainer") + " " + token);
                }

                try {
                    Container container = (cicsChannel).createContainer(token);

                    try {
                        // Assume container is of type CHAR and get CICS to convert it
                        buffer.append(container.getString());
                        reevaluateForTokens = true;

                    } catch (CodePageErrorException e) {
                        // As container could not be converted, assume it is of type BIT and copy the contents into
                        // an attachment

                        if (props.getPropertyAttachment(key) == null) {
                            props.setPropertyAttachment(key, container.get());

                        } else {
                            // Append the property named by token to the existing attachment
                            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

                            try {
                                outputStream.write(props.getPropertyAttachment(key));
                                outputStream.write(container.get());

                            } catch (Exception e2) {
                            }

                            props.setPropertyAttachment(key, outputStream.toByteArray());
                        }
                    }

                    if (props.getPropertyAlternateName(key) == null) {
                        props.setPropertyAlternateName(key, token);
                    }

                } catch (Exception e) {
                    if (logger.isLoggable(Level.FINE)) {
                        logger.fine(messages.getString("GetContainerError") + " " + token);
                    }
                }

            } else {
                // Token could not be resolved, so just remove it
                matcher.appendReplacement(buffer, "");

                if (logger.isLoggable(Level.FINE)) {
                    logger.fine(messages.getString("UnResolvedToken") + " " + token);
                }
            }
        }

        matcher.appendTail(buffer);
        props.setProperty(key, buffer.toString());

        if ((reevaluateForTokens == false) || (allowReevaluateForTokens == false)) {
            break;
        }
    }

    if ((isCICS) && (putContainer != null) && (putContainer.length() >= CONTAINER_NAME_LENGTH_MIN)
            && (putContainer.length() <= CONTAINER_NAME_LENGTH_MAX)) {
        try {
            Container container = cicsChannel.createContainer(putContainer);

            if (props.getPropertyAttachment(key) == null) {
                // Create a container of type CHAR.
                // Should use container.putString but this was only added in
                // CICS TS V5.1
                if (logger.isLoggable(Level.FINE)) {
                    logger.fine(messages.getString("PutContainerChar") + " " + putContainer);
                }

                container.putString(props.getProperty(key));

            } else {
                // Creates a container of type BIT
                if (logger.isLoggable(Level.FINE)) {
                    logger.fine(messages.getString("PutContainerBit") + " " + putContainer);
                }

                container.put(props.getPropertyAttachment(key));
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    if (logger.isLoggable(Level.FINE) && (tokenReplaced)) {
        logger.fine(
                messages.getString("PropertyReplaced") + " " + EmitProperties.getPropertySummary(props, key));
    }

    return tokenReplaced;
}

From source file:org.sleeksnap.impl.Language.java

/**
 * Get a language string/* w  ww.  j  av a  2  s.c om*/
 * 
 * @param token
 *            The language token name
 * @param args
 *            The language arguments
 * @return The parsed language string, or "" if null
 */
public static String getString(final String token, final Object... args) {
    String str = tokens.get(token);

    if (str == null) {
        return "";
    }

    if (str.indexOf('{') != -1) {
        final Matcher m = VAR_PATTERN.matcher(str);

        final StringBuffer out = new StringBuffer();
        while (m.find()) {
            final int paramIndex = Integer.parseInt(m.group(1));
            if (args.length > paramIndex - 1) {
                m.appendReplacement(out, args[paramIndex - 1].toString());
            }
        }
        m.appendTail(out);

        str = out.toString();
    }
    return str;
}