1 package org.tigris.scarab.util; 2 3 48 49 import java.util.List ; 51 import java.util.Iterator ; 52 import java.util.ArrayList ; 53 import java.util.Map ; 54 import java.util.HashMap ; 55 56 import org.apache.regexp.RECompiler; 57 import org.apache.regexp.REProgram; 58 import org.apache.regexp.RE; 59 60 import org.apache.torque.TorqueException; 62 63 import org.tigris.scarab.om.Module; 65 import org.tigris.scarab.om.Issue; 66 import org.tigris.scarab.om.IssueManager; 67 68 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 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 96 public static List getIssueIdTokens(Module module, String text) 97 throws TorqueException 98 { 99 List result = new ArrayList (); 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 120 public static List tokenizeText(Module module, String text) 121 throws TorqueException 122 { 123 List result = new ArrayList (); 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 if (re.getParenStart(0) > pos) 132 { 133 result.add(text.substring(pos, re.getParenStart(0))); 134 } 135 136 String token = re.getParen(0); 137 String id = getIssueIdFromToken(module, token); 138 if (id == null) 139 { 140 result.add(token); 141 } 142 else 143 { 144 List tokenId = new ArrayList (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 173 public static String getIssueIdFromToken(Module module, String token) 174 { 175 RE re = new RE(idREProgram); 176 re.setMatchFlags(RE.MATCH_CASEINDEPENDENT); 177 String id = null; 178 if (re.match(token)) 179 { 180 id = re.getParen(0); 181 Issue issue = null; 182 try 183 { 184 issue = IssueManager.getIssueById(id, module.getCode()); 188 } 189 catch(Exception e) 190 { 191 } 193 if (issue == null || issue.getDeleted()) 194 { 195 id = null; 196 } 197 } 198 return id; 199 } 200 201 205 public static Map getIssueIds(Module module, String text) 206 throws TorqueException 207 { 208 List tokens = getIssueIdTokens(module, text); 209 Map idMap = new HashMap (presizeMap(tokens.size())); 210 Iterator i = tokens.iterator(); 211 while (i.hasNext()) 212 { 213 String token = (String )i.next(); 214 String 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 |