package gw.solr.mapper.query uses java.util.Set uses java.lang.Character uses java.lang.StringBuilder /** * */ @ReadOnly abstract class AbstractEncoder implements ISolrQueryEncoder { /** * Set of characters that require special escaping before being passed * to the Lucene query parser */ public static final var LUCENE_SPECIAL_CHARS : Set = { '+', '-', '&', '|', '!', '(', ')', '{', '}', '[', ']', '^', '"', '~', '*', '?', ':', '\\' } /** * Lucene query parser escape character */ public static final var ESCAPE_CHAR : char = '\\' public static final var DOUBLE_QUOTE : char = '"' public static final var HYPHEN : char = '-' public static final var PERIOD : char = '.' public static final var APOSTROPHE : char = '\'' override function encode(value : String) : String { if(value == null || value.length() == 0) { return "" } var len = value.length() var sb = new StringBuilder(len) var theEncodedChar = EncodedChar var theEscapeChar = EscapeChar var i = 0 var appendEncoded = false while( i < len ) { var data = value.charAt(i) if(isSearchable(data)) { if(appendEncoded) { sb.append(theEncodedChar) appendEncoded = false } if(needsEscape(data)) { sb.append(theEscapeChar) } sb.append(data) } else if(sb.length() > 0) { appendEncoded = true } i += 1 } return sb.toString() } /** * Determines if searching is supported for this character in this * encoder. Non-searchable characters are replaced by the EncodedChar * for this encoder. * * @param c character to check * @return true if this character is searchable, false otherwise. */ abstract function isSearchable(c : char) : boolean /** * Character that non-searchable characters are replaced by. * * @return character that non-searchable characters are replaced by */ abstract property get EncodedChar() : char /** * Determines whether this character needs to be preceded by an escape character * in this encoder. Typically this just means characters that have a special * interpretation by the Lucene query parser. The escape character is specified * by EscapeChar and defaults to a backslash. This is the Lucene query parser * escape character. * * Typically this just means characters that have a special interpretation * by the Lucene query parser. * * @param c character to check * @return true if this character needs to be escaped, false otherwise. */ function needsEscape(c : char) : boolean { return false } /** * Escape character to insert before any characters requiring escaping. * Default is backslash. * * @return character to insert before any characters requiring escaping */ property get EscapeChar() : char { return ESCAPE_CHAR } }