aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/helper/PatternBuilder.java
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2015-10-24 22:57:38 +1300
committerAnton Tananaev <anton.tananaev@gmail.com>2015-10-24 22:57:38 +1300
commit5538a83ac7eda6ad5775e582688dd0199e6c538c (patch)
tree7b261a5e9767fd807124c868d2c4a5d72315e7df /src/org/traccar/helper/PatternBuilder.java
parentbe1baf2b062587099171b485ac1e3f591034816f (diff)
downloadtrackermap-server-5538a83ac7eda6ad5775e582688dd0199e6c538c.tar.gz
trackermap-server-5538a83ac7eda6ad5775e582688dd0199e6c538c.tar.bz2
trackermap-server-5538a83ac7eda6ad5775e582688dd0199e6c538c.zip
Refactor pattern builder class
Diffstat (limited to 'src/org/traccar/helper/PatternBuilder.java')
-rw-r--r--src/org/traccar/helper/PatternBuilder.java85
1 files changed, 31 insertions, 54 deletions
diff --git a/src/org/traccar/helper/PatternBuilder.java b/src/org/traccar/helper/PatternBuilder.java
index 5092200be..5d9d3afa8 100644
--- a/src/org/traccar/helper/PatternBuilder.java
+++ b/src/org/traccar/helper/PatternBuilder.java
@@ -15,104 +15,81 @@
*/
package org.traccar.helper;
+import java.util.ArrayList;
import java.util.regex.Pattern;
public class PatternBuilder {
- private final StringBuilder pattern = new StringBuilder();
+ private final ArrayList<String> fragments = new ArrayList<>();
- public interface Builder {
- void build(PatternBuilder builder);
+ public PatternBuilder optional() {
+ return optional(1);
}
- // eXPRession
- public PatternBuilder xpr(String s) {
- pattern.append(s);
+ public PatternBuilder optional(int count) {
+ fragments.add(fragments.size() - count, "(?:");
+ fragments.add(")?");
return this;
}
- // GRouP
- public PatternBuilder grp(String s) {
- return xpr("(?:").xpr(s).xpr(")");
- }
-
- // OPtional eXpression
- public PatternBuilder opx(String s) {
- return xpr("(?:").xpr(s).xpr(")?");
- }
-
- // TeXT
- public PatternBuilder txt(String s) {
- pattern.append(s.replaceAll("([\\\\\\.\\[\\{\\(\\)\\*\\+\\?\\^\\$\\|])", "\\\\$1"));
+ public PatternBuilder expression(String s) {
+ fragments.add(s);
return this;
}
- // OPtional Text
- public PatternBuilder opt(String s) {
- return xpr("(?:").txt(s).xpr(")?");
+ public PatternBuilder text(String s) {
+ fragments.add(s.replaceAll("([\\\\\\.\\[\\{\\(\\)\\*\\+\\?\\^\\$\\|])", "\\\\$1"));
+ return this;
}
- // NUMber
- public PatternBuilder num(String s) {
+ public PatternBuilder number(String s) {
s = s.replace("dddd", "d{4}").replace("ddd", "d{3}").replace("dd", "d{2}");
s = s.replace("xxxx", "x{4}").replace("xxx", "x{3}").replace("xx", "x{2}");
s = s.replace("d", "\\d").replace("x", "\\p{XDigit}").replaceAll("([\\.])", "\\\\$1");
s = s.replaceAll("\\|$", "\\\\|"); // special case for delimiter
- pattern.append(s);
-
+ fragments.add(s);
return this;
}
- // OPtional Number
- public PatternBuilder opn(String s) {
- return xpr("(?:").num(s).xpr(")?");
- }
-
public PatternBuilder any() {
- pattern.append(".*");
+ fragments.add(".*");
return this;
}
- public PatternBuilder not(String s) {
- return xpr("[^").txt(s).xpr("]*");
- }
-
- // NeXT
- public PatternBuilder nxt(String s) {
- return not(s).txt(s);
- }
-
- // BINary
- public PatternBuilder bin(String s) {
- pattern.append(s.replaceAll("(\\p{XDigit}{2})", "\\\\$1"));
+ public PatternBuilder binary(String s) {
+ fragments.add(s.replaceAll("(\\p{XDigit}{2})", "\\\\$1"));
return this;
}
public PatternBuilder groupBegin() {
- return xpr("(?:");
+ return expression("(?:");
}
- public PatternBuilder groupEnd(boolean optional) {
- if (optional) {
- return xpr(")?");
- } else {
- return xpr(")");
- }
+ public PatternBuilder groupEnd() {
+ return expression(")");
+ }
+
+ public PatternBuilder groupEnd(String s) {
+ return expression(")" + s);
}
public PatternBuilder or() {
- return xpr("|");
+ return expression("|");
}
public Pattern compile() {
- return Pattern.compile(pattern.toString(), Pattern.DOTALL);
+ return Pattern.compile(toString(), Pattern.DOTALL);
}
@Override
public String toString() {
- return pattern.toString();
+ StringBuilder builder = new StringBuilder();
+ for (String fragment : fragments) {
+ builder.append(fragment);
+ }
+ return builder.toString();
}
}