1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.diagram.symbollibrary.ui;
14 import java.nio.CharBuffer;
17 * Default implementation of IFilterStrategy.
20 * It implements simple search semantics with only the special wildcard
21 * characters '*' ( 0 to n any characters) and '?' (any one character)
22 * recognized. In order to allow the filter to pass arbitrary prefixes, the
23 * client has to give a '*' prefix in the filter string. On the contrary, the
24 * client does not have to specify a '*' in order to pass arbitrary suffixes -
25 * arbitrary suffixes are allowed by default by this strategy.
28 * This strategy forces the filter string to lowercase.
30 * TODO: this code is duplicated from org.simantics.browsing.ui.common.views since there was no good way of getting this same. Remove code duplication.
32 * @author Tuukka Lehtonen
34 class DefaultFilterStrategy {
36 private static final boolean DEBUG = false;
38 private static StringBuilder addSearchWord(StringBuilder sb, String pattern) {
40 System.out.println("addSearchWord(" + pattern + ") to '" + sb.toString() + "'");
42 if (pattern == null || pattern.isEmpty())
52 private static String toString(CharBuffer cb) {
53 cb.limit(cb.position());
56 System.out.println("toString(" + cb + ")");
57 String result = cb.toString();
58 cb.limit(cb.capacity());
62 public static String toSinglePatternString(String filter, boolean implicitPreAsterisk) {
63 if (!filter.isEmpty()) {
64 // Force searching in lowercase.
65 filter = filter.toLowerCase();
67 // Construct a regular expression from the specified text.
68 String regExFilter = filter
69 .replace("\\", "\\\\") // \ -> \\
70 .replace(".", "\\.") // . -> \.
71 .replace("*", ".*") // * -> Any 0..n characters
72 .replace("?", ".") // ? -> Any single character
73 .replace("+", "\\+") // + -> \+
74 .replace("(", "\\(") // ( -> \(
75 .replace(")", "\\)") // ) -> \)
76 .replace("[", "\\[") // [ -> \[
77 .replace("]", "\\]") // ] -> \]
78 .replace("{", "\\{") // { -> \{
79 .replace("}", "\\}") // } -> \}
80 .replace("^", "\\^") // ^ -> \^
81 .replace("$", "\\$") // $ -> \$
82 .replace("|", ".*|") // $ -> \$
83 //.replace("|", "\\|") // | -> \|
84 .replace("&&", "\\&&") // && -> \&&
87 if (implicitPreAsterisk)
88 if (!regExFilter.startsWith(".*"))
89 regExFilter = ".*" + regExFilter ;
90 if (!regExFilter.endsWith(".*"))
98 public static String defaultToPatternString(String filter, boolean implicitPreAsterisk) {
102 CharBuffer buf = CharBuffer.allocate(filter.length()*2);
104 StringBuilder sb = new StringBuilder(filter.length()*2);
105 boolean inQuote = false;
106 int len = filter.length();
107 for (int i = 0; i < len;) {
108 char ch = filter.charAt(i);
110 System.out.println("char[" + i + "]: '" + ch + "'");
115 System.out.println("begin quoted text");
119 System.out.println("end quoted text");
121 addSearchWord(sb, toSinglePatternString( toString(buf), implicitPreAsterisk ));
125 } else if (ch == '\\') {
126 // Next character is escaped, i.e. taken as is.
129 // Unexpected end-of-string
132 ch = filter.charAt(i);
134 System.out.println("append escaped character '" + ch + "'");
139 } else if (ch == ' ') {
142 System.out.println("append char '" + ch + "'");
146 if (buf.position() > 0) {
147 addSearchWord(sb, toSinglePatternString( toString(buf), implicitPreAsterisk ));
153 System.out.println("append char '" + ch + "'");
158 if (buf.position() > 0) {
159 addSearchWord(sb, toSinglePatternString( toString(buf), implicitPreAsterisk ));
164 return sb.toString();