Enable formatters to only handle files with certain prefixes

A formatter can now be configured to only handle files that start with
a defined prefix. This is useful if files with the same mime type or
file extension in different folders should be handled by different
formatters. E.g.

  [formatter "ASCIIDOCTOR"]
    prefix = Documentation/
    mimeType = text/plain
  [formatter "MARKDOWN"]
    prefix = ReleaseNotes/
    mimeType = text/plain

Change-Id: If9af7606547dc13976773fa0ca36ba28b1ad21bd
Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
diff --git a/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocGlobalConfig.java b/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocGlobalConfig.java
index c3bb2d9..cbb6201 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocGlobalConfig.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocGlobalConfig.java
@@ -26,6 +26,7 @@
   public static final String KEY_ENABLED = "enabled";
   public static final String KEY_EXT = "ext";
   public static final String KEY_MIME_TYPE = "mimeType";
+  public static final String KEY_PREFIX = "prefix";
 
   private final Config cfg;
 
diff --git a/src/main/java/com/googlesource/gerrit/plugins/xdocs/formatter/Formatters.java b/src/main/java/com/googlesource/gerrit/plugins/xdocs/formatter/Formatters.java
index eacfd57..efbde10 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/xdocs/formatter/Formatters.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/xdocs/formatter/Formatters.java
@@ -17,6 +17,7 @@
 import static com.googlesource.gerrit.plugins.xdocs.XDocGlobalConfig.KEY_ENABLED;
 import static com.googlesource.gerrit.plugins.xdocs.XDocGlobalConfig.KEY_EXT;
 import static com.googlesource.gerrit.plugins.xdocs.XDocGlobalConfig.KEY_MIME_TYPE;
+import static com.googlesource.gerrit.plugins.xdocs.XDocGlobalConfig.KEY_PREFIX;
 import static com.googlesource.gerrit.plugins.xdocs.XDocGlobalConfig.SECTION_FORMATTER;
 
 import org.apache.commons.io.FilenameUtils;
@@ -83,6 +84,19 @@
           continue;
         }
         ConfigSection formatterCfg = getFormatterConfig(e.getKey(), project);
+        String[] prefixes = formatterCfg.getStringList(KEY_PREFIX);
+        if (prefixes.length > 0) {
+          boolean match = false;
+          for (String prefix : prefixes) {
+            if (fileName.startsWith(prefix)) {
+              match = true;
+              break;
+            }
+          }
+          if (!match) {
+            continue;
+          }
+        }
         for (String configuredMimeType :
           formatterCfg.getStringList(KEY_MIME_TYPE)) {
           if (mimeType.equals(new MimeType(configuredMimeType))) {
diff --git a/src/main/resources/Documentation/config.md b/src/main/resources/Documentation/config.md
index d4c4d3f..f8e4c57 100644
--- a/src/main/resources/Documentation/config.md
+++ b/src/main/resources/Documentation/config.md
@@ -45,6 +45,15 @@
 	Overrides the [globally configured mime types](#formatterMimeType)
 	for this formatter.
 
+<a id="prefix">
+formatter.<formatter>.prefix
+:	The prefix that a file must match to be handled by this formatter.
+
+	Multiple prefixes may be specified for a formatter.
+
+	Overrides the [globally configured prefixes](#formatterPrefix)
+	for this formatter.
+
 <a id="globalConfig">
 Global Configuration
 --------------------
@@ -86,6 +95,14 @@
 
 	Can be overridden on [project-level](#mimeType).
 
+<a id="formatterPrefix">
+formatter.<formatter>.prefix
+:	The prefix that a file must match to be handled by this formatter.
+
+	Multiple prefixes may be specified for a formatter.
+
+	Can be overridden on [project-level](#prefix).
+
 <a id="formatterAllowHtml">
 formatter.<formatter>.allowHtml
 :	Whether inline HTML blocks and inline HTML tags are allowed for