Coverage Report - net.sf.statcvs.weblinks.bugs.BugTracker
 
Classes in this File Line Coverage Branch Coverage Complexity
BugTracker
22%
5/23
10%
1/10
1.5
BugTracker$1
40%
2/5
N/A
1.5
 
 1  
 package net.sf.statcvs.weblinks.bugs;
 2  
 
 3  
 import java.util.regex.Matcher;
 4  
 import java.util.regex.Pattern;
 5  
 
 6  
 import net.sf.statcvs.Messages;
 7  
 import net.sf.statcvs.pages.HTML;
 8  
 import net.sf.statcvs.pages.ReportSuiteMaker;
 9  
 
 10  
 /**
 11  
  * A BugTracker generates links to numbered bugs. We use this to turn
 12  
  * bug references in commit log messages (e.g. "Bug #123") into clickable
 13  
  * links.
 14  
  *
 15  
  * @author Richard Cyganiak (richard@cyganiak.de)
 16  
  * @version $Id: BugTracker.java,v 1.9 2009/03/09 21:45:42 benoitx Exp $
 17  
  */
 18  
 public abstract class BugTracker {
 19  32
     protected static final Pattern bugRegex = Pattern.compile("bug\\s*(?:number\\s*)?(?:#\\s*)?(\\d+)", Pattern.CASE_INSENSITIVE);
 20  
 
 21  
     /**
 22  
      * A null object that can be used in place of a real bug tracker.
 23  
      */
 24  12
     public static final BugTracker NO_BUG_TRACKER = new BugTracker("") {
 25  
         public String bugURL(final String bugNumber) {
 26  0
             return null;
 27  
         }
 28  
 
 29  
         public String getName() {
 30  0
             return "No bug tracker";
 31  
         }
 32  
 
 33  20
         public String toHTMLWithLinks(final String plainText) {
 34  0
             return HTML.webifyLinksFromPlainText(plainText);
 35  
         }
 36  
     };
 37  
 
 38  
     private final String baseURL;
 39  
 
 40  
     /**
 41  
      * Creates a new BugTracker instance.
 42  
      * @param baseURL The bug tracker's base URL; a slash is appended
 43  
      *                 if it doesn't end in a slash
 44  
      */
 45  32
     public BugTracker(final String baseURL) {
 46  32
         this.baseURL = baseURL + (baseURL.endsWith("/") ? "" : "/");
 47  32
     }
 48  
 
 49  
     /**
 50  
      * Returns the name of the bug tracker
 51  
      * @return the name of the bug tracker
 52  
      */
 53  
     public abstract String getName();
 54  
 
 55  
     /**
 56  
      * Returns the bug tracker's base URL.
 57  
      * @return The bug tracker's base URL
 58  
      */
 59  
     public String baseURL() {
 60  0
         return this.baseURL;
 61  
     }
 62  
 
 63  
     /**
 64  
      * Returns the URL of the bug tracker page about a certain bug.
 65  
      * @param bugNumber The bug number; one or more digits.
 66  
      * @return The URL of the bug page
 67  
      */
 68  
     public abstract String bugURL(String bugNumber);
 69  
 
 70  
     /**
 71  
      * Filters a String, e.g. a commit message, replacing bug references with
 72  
      * links to the tracker.
 73  
      * @param plainTextInput String to examine for bug references
 74  
      * @return A copy of <code>input</code>, with bug references replaced with HTML links
 75  
      */
 76  
     public String toHTMLWithLinks(final String plainTextInput) {
 77  0
         if (baseURL() == null || baseURL().length() == 0) {
 78  0
             return HTML.webifyLinksFromPlainText(plainTextInput);
 79  
         }
 80  0
         final StringBuffer result = new StringBuffer();
 81  0
         final Matcher m = bugRegex.matcher(plainTextInput);
 82  0
         int offset = 0;
 83  0
         while (m.find()) {
 84  0
             final String linkLabel = m.group();
 85  0
             final String bugNumber = m.group(1);
 86  0
             final String bugURL = bugURL(bugNumber);
 87  0
             result.append(HTML.webifyLinksFromPlainText(plainTextInput.substring(offset, m.start())));
 88  0
             if (bugURL == null) {
 89  0
                 result.append(HTML.webifyLinksFromPlainText(linkLabel));
 90  
             } else {
 91  0
                 result.append(HTML.getLink(bugURL, linkLabel, HTML.getIcon(ReportSuiteMaker.BUG_ICON, Messages.getString("BUG_ICON")), ""));
 92  
             }
 93  0
             offset = m.end();
 94  0
         }
 95  0
         result.append(HTML.webifyLinksFromPlainText(plainTextInput.substring(offset, plainTextInput.length())));
 96  0
         return result.toString();
 97  
     }
 98  
 }