--- /dev/null
+/*
+ * Copyright (c) 2018. 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.wiki;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.hedgecode.chess.position.ParseException;
+import org.hedgecode.chess.position.Parser;
+import org.hedgecode.chess.position.Position;
+import org.hedgecode.chess.position.Positions;
+import org.hedgecode.chess.position.Square;
+
+/**
+ * Wikipedia chess diagram parser.
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+public final class WikiParser implements Parser {
+
+ private static final String LINE_REGEX = "\\r?\\n";
+ private static final String SQUARE_REGEX = String.format("[%s]", Wiki.SEPARATOR);
+
+ private static final Pattern PATTERN =
+ Pattern.compile(
+ String.format("^([%s][^%s]*){%d}$", Wiki.SEPARATOR, Wiki.SEPARATOR, Square.getSize())
+ );
+
+ private static Parser _instance = new WikiParser();
+
+ private WikiParser() {
+ }
+
+ @Override
+ public Position parse(String string) throws ParseException {
+ return _parse(
+ string.split(LINE_REGEX)
+ );
+ }
+
+ private Position _parse(String[] strings) throws ParseException {
+ List<String> lines = new ArrayList<>();
+ for (String line : strings) {
+ Matcher matcher = PATTERN.matcher(line);
+ if (matcher.find()) {
+ lines.add(
+ matcher.group(0)
+ );
+ }
+ }
+ if (lines.size() != Square.getSize())
+ throw new ParseException("parse.wiki.incorrect.board");
+ Position position = Positions.EMPTY.getPosition();
+ _parsePieces(
+ lines.toArray(
+ new String[lines.size()]
+ ),
+ position
+ );
+ return position;
+ }
+
+ private void _parsePieces(String[] lines, Position position) throws ParseException {
+ for (int i = 0; i < lines.length; ++i) {
+ int line = lines.length - i;
+ String[] pieces = lines[i].split(SQUARE_REGEX, -1);
+ if (pieces.length != Square.getSize() + 1)
+ throw new ParseException("parse.wiki.incorrect.board");
+ for (int j = 1; j < pieces.length; ++j) {
+ position.setPiece(
+ Wiki.getColorPiece(pieces[j]),
+ Square.getSquare(j - 1, line - 1)
+ );
+ }
+ }
+ }
+
+ public static Parser getInstance() {
+ return _instance;
+ }
+
+
+ public static void main(String[] args) throws ParseException {
+ Position position = getInstance().parse(
+ "{{Шахматная диаграмма\n" +
+ "|\n" +
+ "|\n" +
+ "|rd|nd|bd|qd|kd|bd|nd|rd\n" +
+ "|pd|pd| |pd|pd|pd|pd|pd\n" +
+ "| | | | | | | |\n" +
+ "| | | | | | | |\n" +
+ "| | | |pd|pl| | |\r\n" +
+ "| | |pl| | | | |\n" +
+ "|pl|pl| | | |pl|pl|pl\n" +
+ "|rl|nl|bl|ql|kl|bl|nl|rl\n" +
+ "| }}"
+ );
+ System.out.println(position);
+ }
+
+}