]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/symbollibrary/ui/DefaultFilterStrategy.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / symbollibrary / ui / DefaultFilterStrategy.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.diagram.symbollibrary.ui;
13
14 import java.nio.CharBuffer;
15
16 /**
17  * Default implementation of IFilterStrategy.
18  * 
19  * <p>
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.
26  * 
27  * <p>
28  * This strategy forces the filter string to lowercase.
29  * 
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.
31  * 
32  * @author Tuukka Lehtonen
33  */
34 class DefaultFilterStrategy {
35
36     private static final boolean DEBUG = false;
37
38     private static StringBuilder addSearchWord(StringBuilder sb, String pattern) {
39         if (DEBUG)
40             System.out.println("addSearchWord(" + pattern + ") to '" + sb.toString() + "'");
41
42         if (pattern == null || pattern.isEmpty())
43             return sb;
44         if (sb.length() > 0)
45             sb.append('|');
46         sb.append('(');
47         sb.append(pattern);
48         sb.append(')');
49         return sb;
50     }
51
52     private static String toString(CharBuffer cb) {
53         cb.limit(cb.position());
54         cb.reset();
55         if (DEBUG)
56             System.out.println("toString(" + cb + ")");
57         String result = cb.toString();
58         cb.limit(cb.capacity());
59         return result;
60     }
61
62     public static String toSinglePatternString(String filter, boolean implicitPreAsterisk) {
63         if (!filter.isEmpty()) {
64             // Force searching in lowercase.
65             filter = filter.toLowerCase();
66
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("&&", "\\&&")   // && -> \&&
85             ;
86
87             if (implicitPreAsterisk)
88                 if (!regExFilter.startsWith(".*"))
89                     regExFilter = ".*" + regExFilter ;
90             if (!regExFilter.endsWith(".*"))
91                 regExFilter += ".*" ;
92
93             return regExFilter;
94         }
95         return null;
96     }
97
98     public static String defaultToPatternString(String filter, boolean implicitPreAsterisk) {
99         if (filter.isEmpty())
100             return null;
101
102         CharBuffer buf = CharBuffer.allocate(filter.length()*2);
103         buf.mark();
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);
109             if (DEBUG)
110                 System.out.println("char[" + i + "]: '" + ch + "'");
111
112             if (ch == '"') {
113                 if (!inQuote) {
114                     if (DEBUG)
115                         System.out.println("begin quoted text");
116                     inQuote = true;
117                 } else {
118                     if (DEBUG)
119                         System.out.println("end quoted text");
120                     inQuote = false;
121                     addSearchWord(sb, toSinglePatternString( toString(buf), implicitPreAsterisk ));
122                 }
123                 ++i;
124                 continue;
125             } else if (ch == '\\') {
126                 // Next character is escaped, i.e. taken as is.
127                 ++i;
128                 if (i >= len)
129                     // Unexpected end-of-string
130                     break;
131
132                 ch = filter.charAt(i);
133                 if (DEBUG)
134                     System.out.println("append escaped character '" + ch + "'");
135
136                 buf.append(ch);
137                 ++i;
138                 break;
139             } else if (ch == ' ') {
140                 if (inQuote) {
141                     if (DEBUG)
142                         System.out.println("append char '" + ch + "'");
143                     buf.append(ch);
144                     ++i;
145                 } else {
146                     if (buf.position() > 0) {
147                         addSearchWord(sb, toSinglePatternString( toString(buf), implicitPreAsterisk ));
148                     }
149                     ++i;
150                 }
151             } else {
152                 if (DEBUG)
153                     System.out.println("append char '" + ch + "'");
154                 buf.append(ch);
155                 ++i;
156             }
157         }
158         if (buf.position() > 0) {
159             addSearchWord(sb, toSinglePatternString( toString(buf), implicitPreAsterisk ));
160         }
161
162         //sb.append(".*");
163
164         return sb.toString();
165     }
166
167 }