KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > tigris > scarab > util > IssueIdParser


1 package org.tigris.scarab.util;
2
3 /* ================================================================
4  * Copyright (c) 2000-2002 CollabNet. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * 3. The end-user documentation included with the redistribution, if
18  * any, must include the following acknowlegement: "This product includes
19  * software developed by Collab.Net <http://www.Collab.Net/>."
20  * Alternately, this acknowlegement may appear in the software itself, if
21  * and wherever such third-party acknowlegements normally appear.
22  *
23  * 4. The hosted project names must not be used to endorse or promote
24  * products derived from this software without prior written
25  * permission. For written permission, please contact info@collab.net.
26  *
27  * 5. Products derived from this software may not use the "Tigris" or
28  * "Scarab" names nor may "Tigris" or "Scarab" appear in their names without
29  * prior written permission of Collab.Net.
30  *
31  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
32  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
33  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34  * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
35  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
37  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
39  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
40  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42  *
43  * ====================================================================
44  *
45  * This software consists of voluntary contributions made by many
46  * individuals on behalf of Collab.Net.
47  */

48
49 // JDK classes
50
import java.util.List JavaDoc;
51 import java.util.Iterator JavaDoc;
52 import java.util.ArrayList JavaDoc;
53 import java.util.Map JavaDoc;
54 import java.util.HashMap JavaDoc;
55
56 import org.apache.regexp.RECompiler;
57 import org.apache.regexp.REProgram;
58 import org.apache.regexp.RE;
59
60 // Turbine classes
61
import org.apache.torque.TorqueException;
62
63 // Scarab classes
64
import org.tigris.scarab.om.Module;
65 import org.tigris.scarab.om.Issue;
66 import org.tigris.scarab.om.IssueManager;
67
68 /**
69  * This class contains logic for finding issue ids in generic text.
70  *
71  * @author <a HREF="mailto:jmcnally@collab.net">John McNally</a>
72  * @version $Id: IssueIdParser.java 7604 2003-04-09 22:55:31Z jmcnally $
73  */

74 public class IssueIdParser
75 {
76     private static REProgram idREProgram = null;
77         
78     static
79     {
80         try
81         {
82             RECompiler rec = new RECompiler();
83             idREProgram = rec.compile("[:alpha:]*\\d+");
84         }
85         catch (Exception JavaDoc e)
86         {
87             Log.get().error("An npe is going to occur because I could not " +
88                             "compile regex: [:alpha:]*\\d+", e);
89         }
90     }
91
92     /**
93      * Parses text for any valid issue ids and returns matches.
94      * The regular expression to determine ids is given by the module
95      */

96     public static List JavaDoc getIssueIdTokens(Module module, String JavaDoc text)
97         throws TorqueException
98     {
99         List JavaDoc result = new ArrayList JavaDoc();
100         RE re = new RE(module.getIssueRegex());
101         re.setMatchFlags(RE.MATCH_CASEINDEPENDENT);
102         int pos = 0;
103         while (re.match(text, pos))
104         {
105             Log.get().debug(re.getParen(0) + " found at " + re.getParenStart(0));
106             result.add(re.getParen(0));
107             pos = re.getParenEnd(0);
108         }
109
110         return result;
111     }
112
113     /**
114      * Parses text for any valid issue ids. The text is broken up
115      * into tokens at potential id boundaries. if a token corresponds
116      * to a valid issue id, a List is returned with [0] as the
117      * token and [1] is the id. if a token does not contain an id
118      * the text is added as a String.
119      */

120     public static List JavaDoc tokenizeText(Module module, String JavaDoc text)
121         throws TorqueException
122     {
123         List JavaDoc result = new ArrayList JavaDoc();
124         RE re = new RE(module.getIssueRegex());
125         re.setMatchFlags(RE.MATCH_CASEINDEPENDENT);
126         int pos = 0;
127         while (re.match(text, pos))
128         {
129             Log.get().debug(re.getParen(0) + " found at " + re.getParenStart(0));
130             // Add any text that did not contain an id
131
if (re.getParenStart(0) > pos)
132             {
133                 result.add(text.substring(pos, re.getParenStart(0)));
134             }
135                 
136             String JavaDoc token = re.getParen(0);
137             String JavaDoc id = getIssueIdFromToken(module, token);
138             if (id == null)
139             {
140                 result.add(token);
141             }
142             else
143             {
144                 List JavaDoc tokenId = new ArrayList JavaDoc(2);
145                 tokenId.add(token);
146                 tokenId.add(id);
147                 result.add(tokenId);
148             }
149             
150             pos = re.getParenEnd(0);
151         }
152
153         if (pos < text.length())
154         {
155             result.add(text.substring(pos));
156         }
157             
158         return result;
159     }
160
161     /**
162      * Assumption is the token will contain at most one issue id.
163      * A number is a potential valid id within the given module.
164      * A syntactically correct id will be checked against the db
165      * to make sure a valid issue exists. if the token does not
166      * result in a valid issue, null is returned. Otherwise the
167      * id will be returned including the module code.
168      *
169      * @param module a <code>Module</code> value
170      * @param token a <code>String</code> value
171      * @return a <code>String</code> value
172      */

173     public static String JavaDoc getIssueIdFromToken(Module module, String JavaDoc token)
174     {
175         RE re = new RE(idREProgram);
176         re.setMatchFlags(RE.MATCH_CASEINDEPENDENT);
177         String JavaDoc id = null;
178         if (re.match(token))
179         {
180             id = re.getParen(0);
181             Issue issue = null;
182             try
183             {
184                 // is the issue required to be in the module? the current
185
// code will return an issue with Code that is the same
186
// as the given module.
187
issue = IssueManager.getIssueById(id, module.getCode());
188             }
189             catch(Exception JavaDoc e)
190             {
191                 // Ignored on purpose
192
}
193             if (issue == null || issue.getDeleted())
194             {
195                 id = null;
196             }
197         }
198         return id;
199     }
200
201     /**
202      * A Map of ids where the keys are the tokens such as "issue#35" and the
203      * value is the unique id, "PACS35".
204      */

205     public static Map JavaDoc getIssueIds(Module module, String JavaDoc text)
206         throws TorqueException
207     {
208         List JavaDoc tokens = getIssueIdTokens(module, text);
209         Map JavaDoc idMap = new HashMap JavaDoc(presizeMap(tokens.size()));
210         Iterator JavaDoc i = tokens.iterator();
211         while (i.hasNext())
212         {
213             String JavaDoc token = (String JavaDoc)i.next();
214             String JavaDoc id = getIssueIdFromToken(module, token);
215             if (id != null)
216             {
217                 idMap.put(token, id);
218             }
219         }
220         return idMap;
221     }
222
223     private static int presizeMap(int size)
224     {
225         return (int) (1.25*size + 1.0);
226     }
227 }
228
Popular Tags