View Javadoc

1   package org.apache.maven.changes;
2   
3   /* ====================================================================
4    *   Copyright 2001-2004 The Apache Software Foundation.
5    *
6    *   Licensed under the Apache License, Version 2.0 (the "License");
7    *   you may not use this file except in compliance with the License.
8    *   You may obtain a copy of the License at
9    *
10   *       http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing, software
13   *   distributed under the License is distributed on an "AS IS" BASIS,
14   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *   See the License for the specific language governing permissions and
16   *   limitations under the License.
17   * ====================================================================
18   */
19  
20  /**
21   * @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
22   *
23   * @version $Id: IssueFinder.java 434563 2006-08-24 23:32:54Z ltheussl $
24   */
25  public class IssueFinder
26  {
27      /**
28       * @param trackerURL the issue tracker URL (this is supposed to be the
29       *        value of &lt;issueTrackingUrl&gt; found in the POM.
30       * @param issue the issue reference
31       * @param template the template string. Allowed template patterns are
32       *        <code>%URL%</code> and <code>%ISSUE%</code>. They will be
33       *        replaced by the base tracker URL extracted from trackerURL and
34       *        by the issue reference.
35       * @return The URL of the issue.
36       */
37      public static String getIssueURL( String trackerURL, String issue,
38          String template )
39      {
40          // Find base URL of issue tracker
41          String baseURL;
42          int pos = trackerURL.lastIndexOf( "/" );
43          if ( pos > 0 )
44          {
45              baseURL = trackerURL.substring( 0, pos );
46          }
47          else
48          {
49              throw new RuntimeException( "Failed to extract tracker URL from ["
50                  + trackerURL + "]" );
51          }
52  
53          // Use template to construct issue URL
54          String issueURL = replace( template, "%URL%", baseURL );
55          issueURL = replace( issueURL, "%ISSUE%", issue );
56  
57          return issueURL;
58      }
59  
60      /**
61       * @param original the original template
62       * @param oldPattern the pattern to replace
63       * @param newPattern the new pattern
64       * @return the modified template string with patterns applied
65       */
66      public static final String replace( String original, String oldPattern,
67          String newPattern )
68      {
69          int index, oldIndex;
70          StringBuffer buffer = new StringBuffer();
71  
72          if ( ( index = original.indexOf( oldPattern ) ) != -1 )
73          {
74              oldIndex = 0;
75              while ( ( index = original.indexOf( oldPattern, oldIndex ) ) != -1 )
76              {
77                  buffer.append( original.substring( oldIndex, index ) );
78                  buffer.append( newPattern);
79                  oldIndex = index + oldPattern.length();
80              }
81              buffer.append( original.substring( oldIndex ) );
82              original = buffer.toString();
83          }
84          return original;
85      }
86  
87  }