1 18 package org.apache.tools.ant.taskdefs.cvslib; 19 20 import java.text.ParseException ; 21 import java.text.SimpleDateFormat ; 22 import java.util.Date ; 23 import java.util.Enumeration ; 24 import java.util.Hashtable ; 25 import java.util.Locale ; 26 import java.util.TimeZone ; 27 28 32 class ChangeLogParser { 33 private static final int GET_FILE = 1; 35 private static final int GET_DATE = 2; 36 private static final int GET_COMMENT = 3; 37 private static final int GET_REVISION = 4; 38 private static final int GET_PREVIOUS_REV = 5; 39 40 42 43 private static final SimpleDateFormat INPUT_DATE 44 = new SimpleDateFormat ("yyyy/MM/dd HH:mm:ss", Locale.US); 45 48 private static final SimpleDateFormat CVS1129_INPUT_DATE = 49 new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss Z", Locale.US); 50 51 static { 52 TimeZone utc = TimeZone.getTimeZone("UTC"); 53 INPUT_DATE.setTimeZone(utc); 54 CVS1129_INPUT_DATE.setTimeZone(utc); 55 } 56 57 private String file; 59 private String date; 60 private String author; 61 private String comment; 62 private String revision; 63 private String previousRevision; 64 65 private int status = GET_FILE; 66 67 68 private final Hashtable entries = new Hashtable (); 69 70 75 public CVSEntry[] getEntrySetAsArray() { 76 final CVSEntry[] array = new CVSEntry[ entries.size() ]; 77 int i = 0; 78 for (Enumeration e = entries.elements(); e.hasMoreElements();) { 79 array[i++] = (CVSEntry) e.nextElement(); 80 } 81 return array; 82 } 83 84 89 public void stdout(final String line) { 90 switch(status) { 91 case GET_FILE: 92 reset(); 95 processFile(line); 96 break; 97 case GET_REVISION: 98 processRevision(line); 99 break; 100 101 case GET_DATE: 102 processDate(line); 103 break; 104 105 case GET_COMMENT: 106 processComment(line); 107 break; 108 109 case GET_PREVIOUS_REV: 110 processGetPreviousRevision(line); 111 break; 112 113 default: 114 break; 116 } 117 } 118 119 124 private void processComment(final String line) { 125 final String lineSeparator = System.getProperty("line.separator"); 126 if (line.equals( 127 "=============================================================================")) { 128 final int end 131 = comment.length() - lineSeparator.length(); comment = comment.substring(0, end); 133 saveEntry(); 134 status = GET_FILE; 135 } else if (line.equals("----------------------------")) { 136 final int end 137 = comment.length() - lineSeparator.length(); comment = comment.substring(0, end); 139 status = GET_PREVIOUS_REV; 140 } else { 141 comment += line + lineSeparator; 142 } 143 } 144 145 150 private void processFile(final String line) { 151 if (line.startsWith("Working file:")) { 152 file = line.substring(14, line.length()); 153 status = GET_REVISION; 154 } 155 } 156 157 162 private void processRevision(final String line) { 163 if (line.startsWith("revision")) { 164 revision = line.substring(9); 165 status = GET_DATE; 166 } else if (line.startsWith("======")) { 167 status = GET_FILE; 170 } 171 } 172 173 178 private void processDate(final String line) { 179 if (line.startsWith("date:")) { 180 int endOfDateIndex = line.indexOf(';'); 184 date = line.substring("date: ".length(), endOfDateIndex); 185 186 int startOfAuthorIndex = line.indexOf("author: ", endOfDateIndex + 1); 187 int endOfAuthorIndex = line.indexOf(';', startOfAuthorIndex + 1); 188 author = line.substring("author: ".length() + startOfAuthorIndex, endOfAuthorIndex); 189 190 status = GET_COMMENT; 191 192 comment = ""; 195 } 196 } 197 198 203 private void processGetPreviousRevision(final String line) { 204 if (!line.startsWith("revision ")) { 205 throw new IllegalStateException ("Unexpected line from CVS: " 206 + line); 207 } 208 previousRevision = line.substring("revision ".length()); 209 210 saveEntry(); 211 212 revision = previousRevision; 213 status = GET_DATE; 214 } 215 216 219 private void saveEntry() { 220 final String entryKey = date + author + comment; 221 CVSEntry entry; 222 if (!entries.containsKey(entryKey)) { 223 Date dateObject = parseDate(date); 224 entry = new CVSEntry(dateObject, author, comment); 225 entries.put(entryKey, entry); 226 } else { 227 entry = (CVSEntry) entries.get(entryKey); 228 } 229 230 entry.addFile(file, revision, previousRevision); 231 } 232 233 239 private Date parseDate(final String date) { 240 try { 241 return INPUT_DATE.parse(date); 242 } catch (ParseException e) { 243 try { 244 return CVS1129_INPUT_DATE.parse(date); 245 } catch (ParseException e2) { 246 throw new IllegalStateException ("Invalid date format: " + date); 247 } 248 } 249 } 250 251 254 public void reset() { 255 this.file = null; 256 this.date = null; 257 this.author = null; 258 this.comment = null; 259 this.revision = null; 260 this.previousRevision = null; 261 } 262 } 263 | Popular Tags |