Lines 44-232
Link Here
|
44 |
package org.netbeans.api.search; |
44 |
package org.netbeans.api.search; |
45 |
|
45 |
|
46 |
/** |
46 |
/** |
47 |
* Pattern describes the search conditions |
47 |
* Pattern describes the replace conditions |
48 |
* |
48 |
* @since 1.8 |
49 |
* @author Martin Roskanin |
|
|
50 |
*/ |
49 |
*/ |
51 |
public final class SearchPattern { |
50 |
public final class ReplacePattern { |
52 |
|
51 |
|
53 |
/** |
52 |
private String replaceExpression; |
54 |
* SearchExpression - a text to search |
53 |
private boolean preserveCase; |
55 |
*/ |
54 |
|
56 |
private String searchExpression; |
55 |
private ReplacePattern(String replaceExpression, boolean preserveCase) { |
57 |
/** |
56 |
this.replaceExpression = replaceExpression; |
58 |
* if true, only whole words were searched |
57 |
this.preserveCase = preserveCase; |
59 |
*/ |
|
|
60 |
private boolean wholeWords; |
61 |
/** |
62 |
* if true, case sensitive search was preformed |
63 |
*/ |
64 |
private boolean matchCase; |
65 |
/** |
66 |
* if true, regular expression search was performed |
67 |
*/ |
68 |
private boolean regExp; |
69 |
|
70 |
/** |
71 |
* Creates a new instance of SearchPattern |
72 |
* |
73 |
* @param searchExpression a searched text |
74 |
* @param wholeWords if true, only whole words were searched |
75 |
* @param matchCase if true, case sensitive search was preformed |
76 |
* @param regExp if true, regular expression search was performed |
77 |
*/ |
78 |
private SearchPattern(String searchExpression, boolean wholeWords, |
79 |
boolean matchCase, boolean regExp) { |
80 |
this.searchExpression = searchExpression; |
81 |
this.wholeWords = wholeWords; |
82 |
this.matchCase = matchCase; |
83 |
this.regExp = regExp; |
84 |
} |
58 |
} |
85 |
|
59 |
|
86 |
/** |
60 |
/** |
87 |
* Creates a new SearchPattern in accordance with given parameters |
61 |
* Creates a new ReplacePattern in accordance with given parameters |
88 |
* |
62 |
* |
89 |
* @param searchExpression non-null String of a searched text |
63 |
* @param searchExpression non-null String of a searched text |
90 |
* @param wholeWords if true, only whole words were searched |
64 |
* @param wholeWords if true, only whole words were searched |
91 |
* @param matchCase if true, case sensitive search was preformed |
|
|
92 |
* @param regExp if true, regular expression search was performed |
93 |
* @return a new SearchPattern in accordance with given parameters |
94 |
*/ |
65 |
*/ |
95 |
public static SearchPattern create(String searchExpression, boolean wholeWords, |
66 |
public static ReplacePattern create(String replaceExpression, boolean preserveCase) { |
96 |
boolean matchCase, boolean regExp) { |
67 |
return new ReplacePattern(replaceExpression, preserveCase); |
97 |
return new SearchPattern(searchExpression, wholeWords, matchCase, regExp); |
68 |
} |
|
|
69 |
|
70 |
public String getReplaceExpression() { |
71 |
return replaceExpression; |
98 |
} |
72 |
} |
99 |
|
73 |
|
100 |
/** |
74 |
public boolean isPreserveCase() { |
101 |
* @return searchExpression |
75 |
return preserveCase; |
102 |
*/ |
|
|
103 |
public String getSearchExpression() { |
104 |
return searchExpression; |
105 |
} |
76 |
} |
106 |
|
77 |
|
107 |
/** |
|
|
108 |
* @return true if the wholeWords parameter was used during search |
109 |
* performing |
110 |
*/ |
111 |
public boolean isWholeWords() { |
112 |
return wholeWords; |
113 |
} |
114 |
|
115 |
/** |
116 |
* @return true if the matchCase parameter was used during search performing |
117 |
*/ |
118 |
public boolean isMatchCase() { |
119 |
return matchCase; |
120 |
} |
121 |
|
122 |
/** |
123 |
* @return true if the regExp parameter was used during search performing |
124 |
*/ |
125 |
public boolean isRegExp() { |
126 |
return regExp; |
127 |
} |
128 |
|
129 |
@Override |
78 |
@Override |
130 |
public boolean equals(Object obj) { |
79 |
public boolean equals(Object obj) { |
131 |
if (!(obj instanceof SearchPattern)) { |
80 |
if (!(obj instanceof ReplacePattern)) { |
132 |
return false; |
81 |
return false; |
133 |
} |
82 |
} |
134 |
SearchPattern sp = (SearchPattern) obj; |
83 |
ReplacePattern sp = (ReplacePattern) obj; |
135 |
return (this.searchExpression.equals(sp.getSearchExpression()) |
84 |
return (this.replaceExpression.equals(sp.getReplaceExpression()) |
136 |
&& this.wholeWords == sp.isWholeWords() |
85 |
&& this.preserveCase == sp.isPreserveCase()); |
137 |
&& this.matchCase == sp.isMatchCase() |
|
|
138 |
&& this.regExp == sp.isRegExp()); |
139 |
} |
86 |
} |
140 |
|
87 |
|
141 |
@Override |
88 |
@Override |
142 |
public int hashCode() { |
89 |
public int hashCode() { |
143 |
int result = 17; |
90 |
int result = 17; |
144 |
result = 37 * result + (this.wholeWords ? 1 : 0); |
91 |
result = 37 * result + (this.preserveCase ? 1 : 0); |
145 |
result = 37 * result + (this.matchCase ? 1 : 0); |
92 |
result = 37 * result + this.replaceExpression.hashCode(); |
146 |
result = 37 * result + (this.regExp ? 1 : 0); |
|
|
147 |
result = 37 * result + this.searchExpression.hashCode(); |
148 |
return result; |
93 |
return result; |
149 |
} |
94 |
} |
150 |
|
95 |
|
151 |
/** |
96 |
/** |
152 |
* Create new instance with "search expression" set to passed value, and |
97 |
* Create new instance with "replace expression" set to passed value, and |
153 |
* other values copied from this instance. |
98 |
* other values copied from this instance. |
154 |
* |
99 |
* |
155 |
*/ |
100 |
*/ |
156 |
public SearchPattern changeSearchExpression(String expression) { |
101 |
public ReplacePattern changeReplaceExpression(String expression) { |
157 |
if ((expression == null && this.searchExpression == null) |
102 |
if ((expression == null && this.replaceExpression == null) |
158 |
|| (expression != null |
103 |
|| (expression != null |
159 |
&& expression.equals(this.searchExpression))) { |
104 |
&& expression.equals(this.replaceExpression))) { |
160 |
return this; |
105 |
return this; |
161 |
} else { |
106 |
} else { |
162 |
return SearchPattern.create(expression, wholeWords, |
107 |
return ReplacePattern.create(expression, preserveCase); |
163 |
matchCase, regExp); |
|
|
164 |
} |
108 |
} |
165 |
} |
109 |
} |
166 |
|
110 |
|
167 |
/** |
111 |
/** |
168 |
* Create new instance with "whole words" set to passed value, and other |
112 |
* Create new instance with "preserve case" set to passed value, and other |
169 |
* values copied from this instance. |
113 |
* values copied from this instance. |
170 |
* |
114 |
* |
171 |
*/ |
115 |
*/ |
172 |
public SearchPattern changeWholeWords(boolean wholeWords) { |
116 |
public ReplacePattern changePreserveCase(boolean preserveCase) { |
173 |
if (this.wholeWords == wholeWords) { |
117 |
if (this.preserveCase == preserveCase) { |
174 |
return this; |
118 |
return this; |
175 |
} else { |
119 |
} else { |
176 |
return SearchPattern.create(searchExpression, wholeWords, |
120 |
return ReplacePattern.create(replaceExpression, preserveCase); |
177 |
matchCase, regExp); |
|
|
178 |
} |
179 |
} |
180 |
|
181 |
/** |
182 |
* Create new instance with "match case" set to passed value, and other |
183 |
* values copied from this instance. |
184 |
* |
185 |
*/ |
186 |
public SearchPattern changeMatchCase(boolean matchCase) { |
187 |
if (this.matchCase == matchCase) { |
188 |
return this; |
189 |
} else { |
190 |
return SearchPattern.create(searchExpression, wholeWords, |
191 |
matchCase, regExp); |
192 |
} |
193 |
} |
194 |
|
195 |
/** |
196 |
* Create new instance with "regular expression" set to passed value, and |
197 |
* other values copied from this instance. |
198 |
* |
199 |
*/ |
200 |
public SearchPattern changeRegExp(boolean regExp) { |
201 |
if (this.regExp == regExp) { |
202 |
return this; |
203 |
} else { |
204 |
return SearchPattern.create(searchExpression, wholeWords, |
205 |
matchCase, regExp); |
206 |
} |
121 |
} |
207 |
} |
122 |
} |
208 |
|
123 |
|
209 |
String toCanonicalString() { |
124 |
String toCanonicalString() { |
210 |
char m = isMatchCase() ? 'M' : 'm'; |
125 |
char p = isPreserveCase()? 'P' : 'p'; |
211 |
char r = isRegExp() ? 'R' : 'r'; |
126 |
return "" + p + "-" + getReplaceExpression(); //NOI18N |
212 |
char w = isWholeWords() ? 'W' : 'w'; |
|
|
213 |
return "" + m + r + w + "-" + getSearchExpression(); |
214 |
} |
127 |
} |
215 |
|
128 |
|
216 |
static SearchPattern parseSearchPattern(String canonicalString) { |
129 |
static ReplacePattern parsePattern(String canonicalString) { |
217 |
//format mrw-findwhat |
130 |
//format p-replaceWith |
218 |
if (canonicalString == null |
131 |
if (canonicalString == null |
219 |
|| Character.toUpperCase(canonicalString.charAt(0)) != 'M' |
132 |
|| Character.toUpperCase(canonicalString.charAt(0)) != 'P' |
220 |
|| Character.toUpperCase(canonicalString.charAt(1)) != 'R' |
133 |
|| canonicalString.charAt(1) != '-') { |
221 |
|| Character.toUpperCase(canonicalString.charAt(2)) != 'W' |
|
|
222 |
|| canonicalString.charAt(3) != '-') { |
223 |
return null; |
134 |
return null; |
224 |
} |
135 |
} |
225 |
boolean matchCase = Character.isUpperCase(canonicalString.charAt(0)); |
136 |
|
226 |
boolean regExp = Character.isUpperCase(canonicalString.charAt(1)); |
137 |
boolean preserveCase = Character.isUpperCase(canonicalString.charAt(0)); |
227 |
boolean wholeWords = Character.isUpperCase(canonicalString.charAt(2)); |
138 |
String replaceWith = canonicalString.substring(2); |
228 |
String findWhat = canonicalString.substring(4); |
139 |
return new ReplacePattern(replaceWith, preserveCase); |
229 |
return new SearchPattern(findWhat, wholeWords, matchCase, regExp); |
|
|
230 |
} |
140 |
} |
231 |
|
141 |
|
232 |
} |
142 |
} |