From bbcce416663093fa2513675963f46a95cdf2d013 Mon Sep 17 00:00:00 2001 From: gotty Date: Sun, 12 Jan 2020 03:24:30 +0300 Subject: [PATCH] [LIB-13] Add regular expressions classes --- .../chess/scanner/regex/RegexBuilder.java | 33 +++++ .../chess/scanner/regex/RegexMatcher.java | 32 +++++ .../chess/scanner/regex/RegexMatcherResult.java | 146 +++++++++++++++++++++ .../hedgecode/chess/scanner/regex/RegexParams.java | 71 ++++++++++ .../hedgecode/chess/scanner/regex/RegexResult.java | 35 +++++ .../hedgecode/chess/scanner/regex/RegexType.java | 75 +++++++++++ 6 files changed, 392 insertions(+) create mode 100644 src/main/java/org/hedgecode/chess/scanner/regex/RegexBuilder.java create mode 100644 src/main/java/org/hedgecode/chess/scanner/regex/RegexMatcher.java create mode 100644 src/main/java/org/hedgecode/chess/scanner/regex/RegexMatcherResult.java create mode 100644 src/main/java/org/hedgecode/chess/scanner/regex/RegexParams.java create mode 100644 src/main/java/org/hedgecode/chess/scanner/regex/RegexResult.java create mode 100644 src/main/java/org/hedgecode/chess/scanner/regex/RegexType.java diff --git a/src/main/java/org/hedgecode/chess/scanner/regex/RegexBuilder.java b/src/main/java/org/hedgecode/chess/scanner/regex/RegexBuilder.java new file mode 100644 index 0000000..789882f --- /dev/null +++ b/src/main/java/org/hedgecode/chess/scanner/regex/RegexBuilder.java @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2019-2020. Developed by Hedgecode. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.hedgecode.chess.scanner.regex; + +/** + * RegexBuilder + * + * @author Dmitry Samoshin aka gotty + */ +public class RegexBuilder { + + public static String build(RegexType type, String target, RegexParams params) { + return type.format( + target, + params + ); + } + +} diff --git a/src/main/java/org/hedgecode/chess/scanner/regex/RegexMatcher.java b/src/main/java/org/hedgecode/chess/scanner/regex/RegexMatcher.java new file mode 100644 index 0000000..8d7a8e9 --- /dev/null +++ b/src/main/java/org/hedgecode/chess/scanner/regex/RegexMatcher.java @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2019-2020. Developed by Hedgecode. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.hedgecode.chess.scanner.regex; + +/** + * RegexMatcher + * + * @author Dmitry Samoshin aka gotty + */ +public interface RegexMatcher { + + void match(String input); + + boolean isBreak(); + + RegexResult result(); + +} diff --git a/src/main/java/org/hedgecode/chess/scanner/regex/RegexMatcherResult.java b/src/main/java/org/hedgecode/chess/scanner/regex/RegexMatcherResult.java new file mode 100644 index 0000000..80bd66d --- /dev/null +++ b/src/main/java/org/hedgecode/chess/scanner/regex/RegexMatcherResult.java @@ -0,0 +1,146 @@ +/* + * Copyright (c) 2019-2020. Developed by Hedgecode. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.hedgecode.chess.scanner.regex; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * RegexMatcherResult + * + * @author Dmitry Samoshin aka gotty + */ +public class RegexMatcherResult implements RegexMatcher, RegexResult { + + private Pattern pattern; + private Pattern startPattern, endPattern; + private final boolean isSingle, isFirst, isMap; + + private boolean isMatch = false; + private boolean isBreak = false; + + private int matchNumber = 0; + + private List resultList = new ArrayList<>(); + private Map resultMap = new HashMap<>(); + + public RegexMatcherResult(String match, boolean isFirst) { + this(match, isFirst, false); + } + + public RegexMatcherResult(String match, boolean isFirst, boolean isMap) { + this.pattern = Pattern.compile(match); + this.isSingle = true; + this.isFirst = isFirst; + this.isMap = isMap; + } + + public RegexMatcherResult(String startMatch, String endMatch, boolean isFirst) { + this(startMatch, endMatch, isFirst, false); + } + + public RegexMatcherResult(String startMatch, String endMatch, boolean isFirst, boolean isMap) { + this.startPattern = Pattern.compile(startMatch); + this.endPattern = Pattern.compile(endMatch); + this.isSingle = false; + this.isFirst = isFirst; + this.isMap = isMap; + } + + @Override + public void match(String input) { + Matcher matcher; + if (isSingle) { + matcher = pattern.matcher(input); + if (matcher.find()) { + addMatch(matcher); + if (isFirst) { + isBreak = true; + } + } + } else { + matcher = isMatch ? endPattern.matcher(input) : startPattern.matcher(input); + if (matcher.find()) { + add(input); + if (isMatch && isFirst) { + isBreak = true; + } + isMatch = !isMatch; + } else { + if (isMatch) { + add(input); + } + } + } + } + + private void add(String input) { + if (isMap) { + resultMap.put( + Integer.toString(matchNumber++), + input + ); + } else { + resultList.add( + input + ); + } + } + + private void addMatch(Matcher matcher) { + if (isMap) { + resultMap.put( + matcher.group(1), + matcher.group(2) + ); + } else { + resultList.add( + matcher.group(1) + ); + } + } + + @Override + public boolean isBreak() { + return isBreak; + } + + @Override + public RegexResult result() { + return this; + } + + @Override + public boolean isEmpty() { + return isMap ? resultMap.isEmpty() : resultList.isEmpty(); + } + + @Override + public List resultList() { + return resultList; + } + + @Override + public Map resultMap() { + return resultMap; + } + +} diff --git a/src/main/java/org/hedgecode/chess/scanner/regex/RegexParams.java b/src/main/java/org/hedgecode/chess/scanner/regex/RegexParams.java new file mode 100644 index 0000000..f54243d --- /dev/null +++ b/src/main/java/org/hedgecode/chess/scanner/regex/RegexParams.java @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2019-2020. Developed by Hedgecode. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.hedgecode.chess.scanner.regex; + +/** + * RegexParams + * + * @author Dmitry Samoshin aka gotty + */ +public final class RegexParams { + + public static final String TOURNAMENT_ID = "[tournamentId]"; + public static final String PAGE_ID = "[pageId]"; + public static final String GAME_ID = "[gameId]"; + public static final String QUERY = "[query]"; + + private String tournamentId; + private String pageId; + private String gameId; + private String query; + private boolean isUrlEncode; + + public RegexParams(String tournamentId, String pageId) { + this.tournamentId = tournamentId; + this.pageId = pageId; + } + + public RegexParams(String gameId) { + this.gameId = gameId; + } + + public RegexParams(String query, boolean isUrlEncode) { + this.query = query; + this.isUrlEncode = isUrlEncode; + } + + public String getTournamentId() { + return tournamentId; + } + + public String getPageId() { + return pageId; + } + + public String getGameId() { + return gameId; + } + + public String getQuery() { + return query; + } + + public boolean isUrlEncode() { + return isUrlEncode; + } + +} diff --git a/src/main/java/org/hedgecode/chess/scanner/regex/RegexResult.java b/src/main/java/org/hedgecode/chess/scanner/regex/RegexResult.java new file mode 100644 index 0000000..db00f89 --- /dev/null +++ b/src/main/java/org/hedgecode/chess/scanner/regex/RegexResult.java @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2019-2020. Developed by Hedgecode. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.hedgecode.chess.scanner.regex; + +import java.util.List; +import java.util.Map; + +/** + * RegexResult + * + * @author Dmitry Samoshin aka gotty + */ +public interface RegexResult { + + boolean isEmpty(); + + List resultList(); + + Map resultMap(); + +} diff --git a/src/main/java/org/hedgecode/chess/scanner/regex/RegexType.java b/src/main/java/org/hedgecode/chess/scanner/regex/RegexType.java new file mode 100644 index 0000000..c63af8c --- /dev/null +++ b/src/main/java/org/hedgecode/chess/scanner/regex/RegexType.java @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2019-2020. Developed by Hedgecode. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.hedgecode.chess.scanner.regex; + +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; + +import org.hedgecode.chess.scanner.ChessHogScannerConstants; + +/** + * RegexType + * + * @author Dmitry Samoshin aka gotty + */ +public enum RegexType { + + TOURNAMENT { + @Override + public String format(String target, RegexParams params) { + if (params.getPageId() != null) { + target = target.replace( + RegexParams.PAGE_ID, + params.getPageId() + ); + } + return target.replace( + RegexParams.TOURNAMENT_ID, + params.getTournamentId() + ); + } + }, + + GAME { + @Override + public String format(String target, RegexParams params) { + return target.replace( + RegexParams.GAME_ID, + params.getGameId() + ); + } + }, + + QUERY { + @Override + public String format(String target, RegexParams params) { + try { + return target.replace( + RegexParams.QUERY, + params.isUrlEncode() + ? URLEncoder.encode(params.getQuery(), ChessHogScannerConstants.CHARSET.name()) + : params.getQuery() + ); + } catch (UnsupportedEncodingException e) { + return null; + } + } + }; + + public abstract String format(String target, RegexParams params); + +} -- 2.10.0