summaryrefslogtreecommitdiffstats
path: root/src/main/bugtraq
diff options
context:
space:
mode:
authorJames Moger <james.moger@gitblit.com>2013-11-25 13:02:20 -0500
committerJames Moger <james.moger@gitblit.com>2013-12-03 10:36:19 -0500
commitc6f3d01c29bb67156b8154bfe5780537b0ef43ac (patch)
tree9af2cdf8bc2137d937d48ef78ecd0971e9f7e7a6 /src/main/bugtraq
parentb6976fc0a4b1fd07e1e72fdd06597a0ffb9d48ef (diff)
downloadgitblit-c6f3d01c29bb67156b8154bfe5780537b0ef43ac.tar.gz
gitblit-c6f3d01c29bb67156b8154bfe5780537b0ef43ac.zip
Add support for per-repository bugtraq configuration
Imported the reference implementation contributed by syntevo which is used in their SmartGit product. You may create a bugtraq config section inf your .git/config file OR you may add a .gitbugtraq file to the root of your repository. Example: [bugtraq "issues"] url = http://code.google.com/p/gitblit/issues/detail?id=%BUGID% logRegex = "[Ii]ssue[-#:\\s]{1}\\d+" logRegex1 = "\\d+" [bugtraq "[pullrequests"] url = "https://github.com/gitblit/gitblit/pull/%BUGID%" logRegex = "(?:pull request|pull|pr)\\s*[-#]?([0-9]+)" Change-Id: Iaba305bf4280d08cc4d1abf533c2f1365470a43f
Diffstat (limited to 'src/main/bugtraq')
-rw-r--r--src/main/bugtraq/LICENSE43
-rw-r--r--src/main/bugtraq/com/syntevo/bugtraq/BugtraqConfig.java240
-rw-r--r--src/main/bugtraq/com/syntevo/bugtraq/BugtraqEntry.java61
-rw-r--r--src/main/bugtraq/com/syntevo/bugtraq/BugtraqException.java49
-rw-r--r--src/main/bugtraq/com/syntevo/bugtraq/BugtraqFormatter.java118
-rw-r--r--src/main/bugtraq/com/syntevo/bugtraq/BugtraqParser.java151
-rw-r--r--src/main/bugtraq/com/syntevo/bugtraq/BugtraqParserIssueId.java61
7 files changed, 723 insertions, 0 deletions
diff --git a/src/main/bugtraq/LICENSE b/src/main/bugtraq/LICENSE
new file mode 100644
index 00000000..550aacc5
--- /dev/null
+++ b/src/main/bugtraq/LICENSE
@@ -0,0 +1,43 @@
+Copyright (c) 2013 by syntevo GmbH. All Rights Reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ o Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+
+ o Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ o Neither the name of syntevo GmbH nor the names of
+ its contributors may be used to endorse or promote products derived
+ from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
+EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+Third Parties
+-------------
+
+The following third parties have rights on parts of the SOFTWARE:
+
+- IDEA annotations.jar, copyright by JetBrains, Inc.
+
+- JGit, copyright by various authors (http://repo.or.cz/w/jgit.git and
+ http://jgit.org).
+ The corresponding license agreement can be found at
+ http://repo.or.cz/w/jgit.git/blob/HEAD:/LICENSE.
+
+- JUnit, copyright by www.hamcrest.org.
+ The corresponding license agreement can be found at
+ in subdirectory lib/JUNIT-LICENSE.
diff --git a/src/main/bugtraq/com/syntevo/bugtraq/BugtraqConfig.java b/src/main/bugtraq/com/syntevo/bugtraq/BugtraqConfig.java
new file mode 100644
index 00000000..1512a9b0
--- /dev/null
+++ b/src/main/bugtraq/com/syntevo/bugtraq/BugtraqConfig.java
@@ -0,0 +1,240 @@
+/*
+ * Copyright (c) 2013 by syntevo GmbH. All Rights Reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * o Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * o Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * o Neither the name of syntevo GmbH nor the names of
+ * its contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
+ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package com.syntevo.bugtraq;
+
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import org.eclipse.jgit.errors.ConfigInvalidException;
+import org.eclipse.jgit.lib.Config;
+import org.eclipse.jgit.lib.Constants;
+import org.eclipse.jgit.lib.FileMode;
+import org.eclipse.jgit.lib.ObjectId;
+import org.eclipse.jgit.lib.ObjectLoader;
+import org.eclipse.jgit.lib.Repository;
+import org.eclipse.jgit.lib.StoredConfig;
+import org.eclipse.jgit.revwalk.RevCommit;
+import org.eclipse.jgit.revwalk.RevObject;
+import org.eclipse.jgit.revwalk.RevTree;
+import org.eclipse.jgit.revwalk.RevWalk;
+import org.eclipse.jgit.storage.file.FileBasedConfig;
+import org.eclipse.jgit.treewalk.TreeWalk;
+import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+public final class BugtraqConfig {
+
+ // Constants ==============================================================
+
+ private static final String DOT_GIT_BUGTRAQ = ".gitbugtraq";
+
+ private static final String BUGTRAQ = "bugtraq";
+
+ private static final String URL = "url";
+ private static final String ENABLED = "enabled";
+ private static final String LOG_REGEX = "logRegex";
+
+ // Static =================================================================
+
+ @Nullable
+ public static BugtraqConfig read(@NotNull Repository repository) throws IOException, ConfigInvalidException {
+ final Config baseConfig = getBaseConfig(repository);
+ final Set<String> allNames = new HashSet<String>();
+ final Config config = repository.getConfig();
+ allNames.addAll(config.getSubsections(BUGTRAQ));
+ if (baseConfig != null) {
+ allNames.addAll(baseConfig.getSubsections(BUGTRAQ));
+ }
+
+ final List<BugtraqEntry> entries = new ArrayList<BugtraqEntry>();
+ for (String name : allNames) {
+ final String url = getString(name, URL, config, baseConfig);
+ final String enabled = getString(name, ENABLED, config, baseConfig);
+ if (enabled != null && !"true".equals(enabled)) {
+ continue;
+ }
+
+ final String logIdRegex = getString(name, LOG_REGEX, config, baseConfig);
+ if (url == null || logIdRegex == null) {
+ return null;
+ }
+
+ final List<String> logIdRegexs = new ArrayList<String>();
+ logIdRegexs.add(logIdRegex);
+
+ for (int index = 1; index < Integer.MAX_VALUE; index++) {
+ final String logIdRegexN = getString(name, LOG_REGEX + index, config, baseConfig);
+ if (logIdRegexN == null) {
+ break;
+ }
+
+ logIdRegexs.add(logIdRegexN);
+ }
+
+ entries.add(new BugtraqEntry(url, logIdRegexs));
+ }
+
+ if (entries.isEmpty()) {
+ return null;
+ }
+
+ return new BugtraqConfig(entries);
+ }
+
+ // Fields =================================================================
+
+ @NotNull
+ private final List<BugtraqEntry> entries;
+
+ // Setup ==================================================================
+
+ BugtraqConfig(@NotNull List<BugtraqEntry> entries) {
+ this.entries = entries;
+ }
+
+ // Accessing ==============================================================
+
+ @NotNull
+ public List<BugtraqEntry> getEntries() {
+ return Collections.unmodifiableList(entries);
+ }
+
+ // Utils ==================================================================
+
+ @Nullable
+ private static Config getBaseConfig(Repository repository) throws IOException, ConfigInvalidException {
+ final Config baseConfig;
+ if (repository.isBare()) {
+ // read bugtraq config directly from the repository
+ String content = null;
+ String head = repository.getFullBranch();
+ RevWalk rw = new RevWalk(repository);
+ TreeWalk tw = new TreeWalk(repository);
+ tw.setFilter(PathFilterGroup.createFromStrings(DOT_GIT_BUGTRAQ));
+ try {
+ ObjectId headId = repository.resolve(head);
+ RevCommit commit = rw.parseCommit(headId);
+ RevTree tree = commit.getTree();
+ tw.reset(tree);
+ while (tw.next()) {
+ if (tw.isSubtree()) {
+ tw.enterSubtree();
+ continue;
+ }
+ ObjectId entid = tw.getObjectId(0);
+ FileMode entmode = tw.getFileMode(0);
+ if (entmode == FileMode.REGULAR_FILE) {
+ RevObject ro = rw.lookupAny(entid, entmode.getObjectType());
+ rw.parseBody(ro);
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
+ ObjectLoader ldr = repository.open(ro.getId(), Constants.OBJ_BLOB);
+ byte[] tmp = new byte[4096];
+ InputStream in = ldr.openStream();
+ int n;
+ while ((n = in.read(tmp)) > 0) {
+ os.write(tmp, 0, n);
+ }
+ in.close();
+ content = new String(os.toByteArray(), commit.getEncoding());
+ }
+ }
+ } finally {
+ rw.dispose();
+ tw.release();
+ }
+
+ if (content == null) {
+ // config not found
+ baseConfig = null;
+ } else {
+ // parse the config
+ Config cfg = new Config();
+ cfg.fromText(content);
+ baseConfig = new StoredConfig(cfg) {
+ @Override
+ public void save() throws IOException {
+ }
+
+ @Override
+ public void load() throws IOException, ConfigInvalidException {
+ }
+ };
+ }
+ } else {
+ // read bugtraq config from work tree
+ final File baseFile = new File(repository.getWorkTree(), DOT_GIT_BUGTRAQ);
+ if (baseFile.isFile()) {
+ FileBasedConfig fileConfig = new FileBasedConfig(baseFile, repository.getFS());
+ fileConfig.load();
+ baseConfig = fileConfig;
+ }
+ else {
+ baseConfig = null;
+ }
+ }
+ return baseConfig;
+ }
+
+ @Nullable
+ private static String getString(@NotNull String subsection, @NotNull String key, @NotNull Config config, @Nullable Config baseConfig) {
+ final String value = config.getString(BUGTRAQ, subsection, key);
+ if (value != null) {
+ return trimMaybeNull(value);
+ }
+
+ if (baseConfig != null) {
+ return trimMaybeNull(baseConfig.getString(BUGTRAQ, subsection, key));
+ }
+
+ return value;
+ }
+
+ @Nullable
+ private static String trimMaybeNull(@Nullable String string) {
+ if (string == null) {
+ return null;
+ }
+
+ string = string.trim();
+ if (string.length() == 0) {
+ return null;
+ }
+
+ return string;
+ }
+} \ No newline at end of file
diff --git a/src/main/bugtraq/com/syntevo/bugtraq/BugtraqEntry.java b/src/main/bugtraq/com/syntevo/bugtraq/BugtraqEntry.java
new file mode 100644
index 00000000..7ecd8bf8
--- /dev/null
+++ b/src/main/bugtraq/com/syntevo/bugtraq/BugtraqEntry.java
@@ -0,0 +1,61 @@
+/*
+ * Copyright (c) 2013 by syntevo GmbH. All Rights Reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * o Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * o Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * o Neither the name of syntevo GmbH nor the names of
+ * its contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
+ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package com.syntevo.bugtraq;
+
+import java.util.*;
+
+import org.jetbrains.annotations.*;
+
+final class BugtraqEntry {
+
+ // Fields =================================================================
+
+ private final String url;
+ private final BugtraqParser parser;
+
+ // Setup ==================================================================
+
+ public BugtraqEntry(@NotNull String url, @NotNull List<String> logIdRegexs) throws BugtraqException {
+ this.url = url;
+ this.parser = BugtraqParser.createInstance(logIdRegexs);
+ }
+
+ // Accessing ==============================================================
+
+ @NotNull
+ public String getUrl() {
+ return url;
+ }
+
+ @NotNull
+ public BugtraqParser getParser() {
+ return parser;
+ }
+} \ No newline at end of file
diff --git a/src/main/bugtraq/com/syntevo/bugtraq/BugtraqException.java b/src/main/bugtraq/com/syntevo/bugtraq/BugtraqException.java
new file mode 100644
index 00000000..fdc8dae6
--- /dev/null
+++ b/src/main/bugtraq/com/syntevo/bugtraq/BugtraqException.java
@@ -0,0 +1,49 @@
+/*
+ * Copyright (c) 2013 by syntevo GmbH. All Rights Reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * o Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * o Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * o Neither the name of syntevo GmbH nor the names of
+ * its contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
+ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package com.syntevo.bugtraq;
+
+import java.io.*;
+
+public class BugtraqException extends IOException {
+
+ // Setup ==================================================================
+
+ public BugtraqException(String message) {
+ super(message);
+ }
+
+ public BugtraqException(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+ public BugtraqException(Throwable cause) {
+ super(cause);
+ }
+} \ No newline at end of file
diff --git a/src/main/bugtraq/com/syntevo/bugtraq/BugtraqFormatter.java b/src/main/bugtraq/com/syntevo/bugtraq/BugtraqFormatter.java
new file mode 100644
index 00000000..978cd8c9
--- /dev/null
+++ b/src/main/bugtraq/com/syntevo/bugtraq/BugtraqFormatter.java
@@ -0,0 +1,118 @@
+/*
+ * Copyright (c) 2013 by syntevo GmbH. All Rights Reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * o Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * o Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * o Neither the name of syntevo GmbH nor the names of
+ * its contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
+ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package com.syntevo.bugtraq;
+
+import java.util.*;
+
+import org.jetbrains.annotations.*;
+
+public final class BugtraqFormatter {
+
+ // Fields =================================================================
+
+ private final BugtraqConfig config;
+
+ // Setup ==================================================================
+
+ public BugtraqFormatter(@NotNull BugtraqConfig config) {
+ this.config = config;
+ }
+
+ // Accessing ==============================================================
+
+ public void formatLogMessage(@NotNull String message, @NotNull OutputHandler outputHandler) {
+ final SortedSet<IssueId> allIds = new TreeSet<IssueId>(new Comparator<IssueId>() {
+ @Override
+ public int compare(IssueId o1, IssueId o2) {
+ final int from1 = o1.id.getFrom();
+ final int from2 = o2.id.getFrom();
+ return from1 > from2 ? +1 : from1 < from2 ? -1 : 0;
+ }
+ });
+
+ for (BugtraqEntry entry : config.getEntries()) {
+ final List<BugtraqParserIssueId> ids = entry.getParser().parse(message);
+ if (ids == null) {
+ continue;
+ }
+
+ for (BugtraqParserIssueId id : ids) {
+ allIds.add(new IssueId(entry, id));
+ }
+ }
+
+ int lastIdEnd = -1;
+ for (IssueId issueId : allIds) {
+ final BugtraqParserIssueId id = issueId.id;
+ if (id.getFrom() <= lastIdEnd) {
+ continue;
+ }
+
+ appendText(message.substring(lastIdEnd + 1, id.getFrom()), outputHandler);
+
+ final String linkText = message.substring(id.getFrom(), id.getTo() + 1);
+ final String target = issueId.entry.getUrl().replace("%BUGID%", id.getId());
+ outputHandler.appendLink(linkText, target);
+ lastIdEnd = id.getTo();
+ }
+
+ if (lastIdEnd - 1 < message.length()) {
+ appendText(message.substring(lastIdEnd + 1, message.length()), outputHandler);
+ }
+ }
+
+ // Utils ==================================================================
+
+ private static void appendText(@NotNull String message, @NotNull OutputHandler outputHandler) {
+ if (message.length() == 0) {
+ return;
+ }
+
+ outputHandler.appendText(message);
+ }
+
+ // Inner Classes ==========================================================
+
+ public interface OutputHandler {
+ void appendText(@NotNull String text);
+
+ void appendLink(@NotNull String name, @NotNull String target);
+ }
+
+ private static class IssueId {
+ private final BugtraqEntry entry;
+ private final BugtraqParserIssueId id;
+
+ private IssueId(BugtraqEntry entry, BugtraqParserIssueId id) {
+ this.entry = entry;
+ this.id = id;
+ }
+ }
+} \ No newline at end of file
diff --git a/src/main/bugtraq/com/syntevo/bugtraq/BugtraqParser.java b/src/main/bugtraq/com/syntevo/bugtraq/BugtraqParser.java
new file mode 100644
index 00000000..8619c4c5
--- /dev/null
+++ b/src/main/bugtraq/com/syntevo/bugtraq/BugtraqParser.java
@@ -0,0 +1,151 @@
+/*
+ * Copyright (c) 2013 by syntevo GmbH. All Rights Reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * o Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * o Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * o Neither the name of syntevo GmbH nor the names of
+ * its contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
+ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package com.syntevo.bugtraq;
+
+import java.util.*;
+import java.util.regex.*;
+
+import org.jetbrains.annotations.*;
+
+final class BugtraqParser {
+
+ // Static =================================================================
+
+ @NotNull
+ public static BugtraqParser createInstance(@NotNull List<String> regexs) throws BugtraqException {
+ try {
+ return new BugtraqParser(regexs);
+ }
+ catch (PatternSyntaxException ex) {
+ throw new BugtraqException(ex);
+ }
+ }
+
+ // Fields =================================================================
+
+ private final List<Pattern> patterns;
+
+ // Setup ==================================================================
+
+ private BugtraqParser(List<String> regexs) {
+ this.patterns = new ArrayList<Pattern>();
+
+ for (String regex : regexs) {
+ patterns.add(compilePatternSafe(regex));
+ }
+ }
+
+ // Accessing ==============================================================
+
+ @Nullable
+ public List<BugtraqParserIssueId> parse(@NotNull String message) {
+ List<Part> parts = new ArrayList<Part>();
+ parts.add(new Part(message, 0, message.length() - 1));
+
+ boolean firstMatch = false;
+
+ for (Pattern pattern : patterns) {
+ final List<Part> newParts = new ArrayList<Part>();
+ for (Part part : parts) {
+ final Matcher matcher = pattern.matcher(part.text);
+ while (matcher.find()) {
+ firstMatch = true;
+ if (matcher.groupCount() == 0) {
+ addNewPart(part, matcher, 0, newParts);
+ }
+ else {
+ addNewPart(part, matcher, 1, newParts);
+ }
+ }
+ }
+
+ parts = newParts;
+ if (parts.isEmpty()) {
+ parts = null;
+ break;
+ }
+ }
+
+ if (!firstMatch) {
+ return null;
+ }
+
+ if (parts == null) {
+ parts = new ArrayList<Part>();
+ }
+
+ final List<BugtraqParserIssueId> ids = new ArrayList<BugtraqParserIssueId>();
+ for (Part part : parts) {
+ final BugtraqParserIssueId id = new BugtraqParserIssueId(part.from, part.to, part.text);
+ if (ids.size() > 0) {
+ final BugtraqParserIssueId lastId = ids.get(ids.size() - 1);
+ if (id.getFrom() <= lastId.getTo()) {
+ continue;
+ }
+ }
+
+ ids.add(id);
+ }
+
+ return ids;
+ }
+
+ // Utils ==================================================================
+
+ private static void addNewPart(Part part, Matcher matcher, int group, List<Part> newParts) {
+ final int textStart = matcher.start(group) + part.from;
+ final int textEnd = matcher.end(group) - 1 + part.from;
+ if (textEnd < 0) {
+ return;
+ }
+
+ final Part newPart = new Part(matcher.group(group), textStart, textEnd);
+ newParts.add(newPart);
+ }
+
+ private static Pattern compilePatternSafe(String pattern) throws PatternSyntaxException {
+ return Pattern.compile(pattern);
+ }
+
+ // Inner Classes ==========================================================
+
+ private static class Part {
+
+ private final int from;
+ private final int to;
+ private final String text;
+
+ public Part(String text, int from, int to) {
+ this.text = text;
+ this.from = from;
+ this.to = to;
+ }
+ }
+} \ No newline at end of file
diff --git a/src/main/bugtraq/com/syntevo/bugtraq/BugtraqParserIssueId.java b/src/main/bugtraq/com/syntevo/bugtraq/BugtraqParserIssueId.java
new file mode 100644
index 00000000..fddb4dfc
--- /dev/null
+++ b/src/main/bugtraq/com/syntevo/bugtraq/BugtraqParserIssueId.java
@@ -0,0 +1,61 @@
+/*
+ * Copyright (c) 2013 by syntevo GmbH. All Rights Reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * o Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * o Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * o Neither the name of syntevo GmbH nor the names of
+ * its contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
+ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package com.syntevo.bugtraq;
+
+final class BugtraqParserIssueId {
+
+ // Fields =================================================================
+
+ private final int from;
+ private final int to;
+ private final String id;
+
+ // Setup ==================================================================
+
+ public BugtraqParserIssueId(int from, int to, String id) {
+ this.id = id;
+ this.from = from;
+ this.to = to;
+ }
+
+ // Accessing ==============================================================
+
+ public int getFrom() {
+ return from;
+ }
+
+ public int getTo() {
+ return to;
+ }
+
+ public String getId() {
+ return id;
+ }
+} \ No newline at end of file