Search in sources :

Example 1 with StrParser

use of org.apache.solr.search.StrParser in project lucene-solr by apache.

the class MacroExpander method _expand.

private String _expand(String val) {
    // quickest short circuit
    int idx = val.indexOf(macroStart.charAt(0));
    if (idx < 0)
        return val;
    // start of the unprocessed part of the string
    int start = 0;
    StringBuilder sb = null;
    for (; ; ) {
        idx = val.indexOf(macroStart, idx);
        int matchedStart = idx;
        // check if escaped
        if (idx > 0) {
            // check if escaped...
            // TODO: what if you *want* to actually have a backslash... perhaps that's when we allow changing
            // of the escape character?
            char ch = val.charAt(idx - 1);
            if (ch == escape) {
                idx += macroStart.length();
                continue;
            }
        } else if (idx < 0) {
            if (sb == null)
                return val;
            sb.append(val.substring(start));
            return sb.toString();
        }
        // found unescaped "${"
        idx += macroStart.length();
        int rbrace = val.indexOf('}', idx);
        if (rbrace == -1) {
            // no matching close brace...
            continue;
        }
        if (sb == null) {
            sb = new StringBuilder(val.length() * 2);
        }
        if (matchedStart > 0) {
            sb.append(val.substring(start, matchedStart));
        }
        // update "start" to be at the end of ${...}
        start = rbrace + 1;
        // String inbetween = val.substring(idx, rbrace);
        StrParser parser = new StrParser(val, idx, rbrace);
        try {
            String paramName = parser.getId();
            String defVal = null;
            boolean hasDefault = parser.opt(':');
            if (hasDefault) {
                defVal = val.substring(parser.pos, rbrace);
            }
            // in the event that expansions become context dependent... consult original?
            String[] replacementList = orig.get(paramName);
            // TODO - handle a list somehow...
            String replacement = replacementList != null ? replacementList[0] : defVal;
            if (replacement != null) {
                String expandedReplacement = expand(replacement);
                if (failOnMissingParams && expandedReplacement == null) {
                    return null;
                }
                sb.append(expandedReplacement);
            } else if (failOnMissingParams) {
                return null;
            }
        } catch (SyntaxError syntaxError) {
            // append the part we would have skipped
            sb.append(val.substring(matchedStart, start));
            continue;
        }
    }
}
Also used : SyntaxError(org.apache.solr.search.SyntaxError) StrParser(org.apache.solr.search.StrParser)

Example 2 with StrParser

use of org.apache.solr.search.StrParser in project lucene-solr by apache.

the class LegacyFacet method addStat.

private void addStat(String val) {
    StrParser sp = new StrParser(val);
    int start = 0;
    sp.eatws();
    if (sp.pos >= sp.end)
        addStat(val, val);
    // try key:func() format
    String key = null;
    String funcStr = val;
    if (key == null) {
        key = SolrReturnFields.getFieldName(sp);
        if (key != null && sp.opt(':')) {
            // OK, we got the key
            funcStr = val.substring(sp.pos);
        } else {
            // an invalid key... it must not be present.
            sp.pos = start;
            key = null;
        }
    }
    if (key == null) {
        // not really ideal
        key = funcStr;
    }
    addStat(key, funcStr);
}
Also used : StrParser(org.apache.solr.search.StrParser)

Aggregations

StrParser (org.apache.solr.search.StrParser)2 SyntaxError (org.apache.solr.search.SyntaxError)1