View Javadoc

1   package net.sf.statscm;
2   
3   import java.util.HashSet;
4   import java.util.Set;
5   import java.util.regex.Matcher;
6   import java.util.regex.Pattern;
7   
8   import net.sf.statcvs.Messages;
9   import net.sf.statcvs.model.Directory;
10  import net.sf.statcvs.model.Revision;
11  import net.sf.statcvs.model.VersionedFile;
12  import net.sf.statcvs.output.WebRepositoryIntegration;
13  import net.sf.statcvs.pages.HTML;
14  import net.sf.statcvs.pages.ReportSuiteMaker;
15  import net.sf.statcvs.weblinks.bugs.BugTracker;
16  
17  public class Trac extends BugTracker implements WebRepositoryIntegration
18  {
19      protected static Pattern bugRegex = Pattern.compile( "(?:#\\s*)?(\\d+)" );
20      private Set atticFileNames = new HashSet();
21  
22      public Trac(final String baseURL)
23      {
24          super( baseURL );
25      }
26  
27      public String getName()
28      {
29          return "Trac";
30      }
31  
32      public String bugURL( String bugNumber )
33      {
34          return baseURL() + "ticket/" + bugNumber;
35      }
36  
37      /**
38       * Filters a String, e.g. a commit message, replacing bug references with links to the tracker.
39       * 
40       * @param plainTextInput String to examine for bug references
41       * @return A copy of <code>input</code>, with bug references replaced with HTML links
42       */
43      public String toHTMLWithLinks( String plainTextInput )
44      {
45          if ( baseURL() == null || baseURL().length() == 0 )
46          {
47              return HTML.escape( plainTextInput );
48          }
49          StringBuffer result = new StringBuffer();
50          final Matcher m = bugRegex.matcher( plainTextInput );
51          int offset = 0;
52          while ( m.find() )
53          {
54              String linkLabel = m.group();
55              String bugNumber = m.group( 1 );
56              String bugURL = bugURL( bugNumber );
57              result.append( HTML.escape( plainTextInput.substring( offset, m.start() ) ) );
58              if ( bugURL == null )
59              {
60                  result.append( HTML.escape( linkLabel ) );
61              }
62              else
63              {
64                  result.append( HTML.getLink( bugURL, linkLabel, HTML.getIcon( ReportSuiteMaker.BUG_ICON, Messages
65                          .getString( "BUG_ICON" ) ), "" ) );
66              }
67              offset = m.end();
68          }
69          result.append( HTML.escape( plainTextInput.substring( offset, plainTextInput.length() ) ) );
70          return result.toString();
71      }
72  
73      protected String getFileUrl( VersionedFile file )
74      {
75          String filename;
76          if ( isInAttic( file ) )
77          {
78              String path = file.getDirectory().getPath();
79              filename = "/" + path + "Attic/" + file.getFilename();
80  
81          }
82          else
83          {
84              filename = "/" + file.getFilenameWithPath();
85          }
86  
87          return filename;
88      }
89  
90      public String getDiffUrl( Revision oldRevision, Revision newRevision )
91      {
92          return baseURL() + //
93                  "/changeset/" + oldRevision.getRevisionNumber() + "/" + //
94                  oldRevision.getFile() + //
95                  "?old=" + newRevision.getRevisionNumber() + //
96                  "&oldPath=" + newRevision.getFile();
97      }
98  
99      public String getDirectoryUrl( Directory directory )
100     {
101         return baseURL() + "/browser" + directory.getPath();
102     }
103 
104     public String getFileHistoryUrl( VersionedFile file )
105     {
106         return baseURL() + "log/" + getFileUrl( file );
107     }
108 
109     public String getFileViewUrl( VersionedFile file )
110     {
111         return baseURL() + getFileUrl( file );
112     }
113 
114     public String getFileViewUrl( Revision revision )
115     {
116         return baseURL() + getFileUrl( revision.getFile() ) + "?rev=" + revision.getRevisionNumber();
117     }
118 
119     public void setAtticFileNames( Set atticFileNames )
120     {
121         this.atticFileNames = atticFileNames;
122     }
123 
124     protected boolean isInAttic( VersionedFile file )
125     {
126         return atticFileNames.contains( file.getFilenameWithPath() );
127     }
128 
129     public String getBaseUrl() {
130         return super.baseURL();
131     }
132 
133 }