| 1 | |
package net.sf.statsvn.input; |
| 2 | |
|
| 3 | |
import java.text.ParseException; |
| 4 | |
import java.util.ArrayList; |
| 5 | |
import java.util.Date; |
| 6 | |
import java.util.HashMap; |
| 7 | |
|
| 8 | |
import net.sf.statsvn.output.SvnConfigurationOptions; |
| 9 | |
import net.sf.statsvn.util.XMLUtil; |
| 10 | |
|
| 11 | |
import org.xml.sax.Attributes; |
| 12 | |
import org.xml.sax.SAXException; |
| 13 | |
import org.xml.sax.SAXParseException; |
| 14 | |
import org.xml.sax.helpers.DefaultHandler; |
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
public class SvnXmlLogFileHandler extends DefaultHandler { |
| 26 | |
|
| 27 | |
private static final String INVALID_SVN_LOG_FILE = "Invalid SVN log file."; |
| 28 | |
|
| 29 | |
private static final String AUTHOR = "author"; |
| 30 | |
|
| 31 | |
private static final String DATE = "date"; |
| 32 | |
|
| 33 | |
private static final String FATAL_ERROR_MESSAGE = INVALID_SVN_LOG_FILE; |
| 34 | |
|
| 35 | |
private static final String LOG = "log"; |
| 36 | |
|
| 37 | |
private static final String LOGENTRY = "logentry"; |
| 38 | |
|
| 39 | |
private static final String MSG = "msg"; |
| 40 | |
|
| 41 | |
private static final String PATH = "path"; |
| 42 | |
|
| 43 | |
private static final String PATHS = "paths"; |
| 44 | |
|
| 45 | |
private final SvnLogBuilder builder; |
| 46 | |
|
| 47 | |
private ArrayList currentFilenames; |
| 48 | |
|
| 49 | |
private RevisionData currentRevisionData; |
| 50 | |
|
| 51 | |
private ArrayList currentRevisions; |
| 52 | |
|
| 53 | 4 | private String lastElement = ""; |
| 54 | |
|
| 55 | 4 | private String pathAction = ""; |
| 56 | |
|
| 57 | 4 | private String stringData = ""; |
| 58 | |
|
| 59 | 4 | private String copyfromRev = ""; |
| 60 | |
|
| 61 | 4 | private String copyfromPath = ""; |
| 62 | |
|
| 63 | |
private final RepositoryFileManager repositoryFileManager; |
| 64 | |
|
| 65 | 4 | private final HashMap tagsMap = new HashMap(); |
| 66 | |
|
| 67 | 4 | private final HashMap tagsDateMap = new HashMap(); |
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | 4 | public SvnXmlLogFileHandler(final SvnLogBuilder builder, final RepositoryFileManager repositoryFileManager) { |
| 78 | 4 | this.builder = builder; |
| 79 | 4 | this.repositoryFileManager = repositoryFileManager; |
| 80 | 4 | } |
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
public void characters(final char[] ch, final int start, final int length) throws SAXException { |
| 87 | 71800 | super.characters(ch, start, length); |
| 88 | 71800 | stringData += new String(ch, start, length); |
| 89 | 71800 | } |
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
private void checkLastElement(final String last) throws SAXException { |
| 100 | 67512 | if (!lastElement.equals(last)) { |
| 101 | 0 | fatalError(FATAL_ERROR_MESSAGE); |
| 102 | |
} |
| 103 | 67512 | } |
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
private void endAuthor() throws SAXException { |
| 112 | 2416 | checkLastElement(LOGENTRY); |
| 113 | 2416 | currentRevisionData.setLoginName(stringData); |
| 114 | 2416 | } |
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
private void endDate() throws SAXException { |
| 126 | 2416 | checkLastElement(LOGENTRY); |
| 127 | |
Date dt; |
| 128 | |
try { |
| 129 | 2416 | dt = XMLUtil.parseXsdDateTime(stringData); |
| 130 | 2416 | currentRevisionData.setDate(dt); |
| 131 | 0 | } catch (final ParseException e) { |
| 132 | 0 | warning("Invalid date specified."); |
| 133 | 2416 | } |
| 134 | 2416 | } |
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
public void endElement(final String uri, final String localName, final String qName) throws SAXException { |
| 144 | 33756 | super.endElement(uri, localName, qName); |
| 145 | 33756 | String eName = localName; |
| 146 | 33756 | if ("".equals(eName)) { |
| 147 | 33756 | eName = qName; |
| 148 | |
} |
| 149 | 33756 | if (eName.equals(LOG)) { |
| 150 | 4 | endLog(); |
| 151 | 33752 | } else if (eName.equals(LOGENTRY)) { |
| 152 | 2416 | endLogEntry(); |
| 153 | 31336 | } else if (eName.equals(AUTHOR)) { |
| 154 | 2416 | endAuthor(); |
| 155 | 28920 | } else if (eName.equals(DATE)) { |
| 156 | 2416 | endDate(); |
| 157 | 26504 | } else if (eName.equals(MSG)) { |
| 158 | 2416 | endMsg(); |
| 159 | 24088 | } else if (eName.equals(PATHS)) { |
| 160 | 2416 | endPaths(); |
| 161 | 21672 | } else if (eName.equals(PATH)) { |
| 162 | 21672 | endPath(); |
| 163 | |
} else { |
| 164 | 0 | fatalError(INVALID_SVN_LOG_FILE); |
| 165 | |
} |
| 166 | 33756 | } |
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
private void endLog() throws SAXException { |
| 175 | 4 | checkLastElement(LOG); |
| 176 | 4 | lastElement = ""; |
| 177 | 4 | } |
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
private void endLogEntry() throws SAXException { |
| 187 | 2416 | checkLastElement(LOGENTRY); |
| 188 | 2416 | lastElement = LOG; |
| 189 | |
|
| 190 | 24088 | for (int i = 0; i < currentFilenames.size(); i++) { |
| 191 | 21672 | if (currentFilenames.get(i) == null) { |
| 192 | 0 | continue; |
| 193 | |
} |
| 194 | 21672 | final RevisionData revisionData = (RevisionData) currentRevisions.get(i); |
| 195 | 21672 | revisionData.setComment(currentRevisionData.getComment()); |
| 196 | 21672 | revisionData.setDate(currentRevisionData.getDate()); |
| 197 | 21672 | revisionData.setLoginName(currentRevisionData.getLoginName()); |
| 198 | 21672 | final String currentFilename = currentFilenames.get(i).toString(); |
| 199 | |
|
| 200 | 21672 | final boolean isBinary = repositoryFileManager.isBinary(currentFilename); |
| 201 | 21672 | builder.buildFile(currentFilename, isBinary, revisionData.isDeletion(), tagsMap, tagsDateMap); |
| 202 | 21672 | builder.buildRevision(revisionData); |
| 203 | |
} |
| 204 | 2416 | } |
| 205 | |
|
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
|
| 212 | |
private void endMsg() throws SAXException { |
| 213 | 2416 | checkLastElement(LOGENTRY); |
| 214 | 2416 | currentRevisionData.setComment(stringData); |
| 215 | 2416 | } |
| 216 | |
|
| 217 | |
|
| 218 | |
|
| 219 | |
|
| 220 | |
|
| 221 | |
|
| 222 | |
|
| 223 | |
|
| 224 | |
private void endPath() throws SAXException { |
| 225 | 21672 | checkLastElement(PATHS); |
| 226 | |
|
| 227 | |
|
| 228 | |
|
| 229 | 21672 | final String filename = repositoryFileManager.absoluteToRelativePath(stringData); |
| 230 | 21672 | final RevisionData data = currentRevisionData.createCopy(); |
| 231 | 21672 | if (!pathAction.equals("D")) { |
| 232 | 20332 | data.setStateExp(true); |
| 233 | 20332 | if (pathAction.equals("A") || pathAction.equals("R")) { |
| 234 | 5080 | data.setStateAdded(true); |
| 235 | |
} |
| 236 | |
} else { |
| 237 | 1340 | data.setStateDead(true); |
| 238 | |
} |
| 239 | |
|
| 240 | 21672 | final String tagsStr = SvnConfigurationOptions.getTagsDirectory(); |
| 241 | 21672 | if (copyfromRev != null && filename == null && stringData != null && stringData.indexOf(tagsStr) >= 0) { |
| 242 | 0 | String tag = stringData.substring(stringData.indexOf(tagsStr) + tagsStr.length()); |
| 243 | 0 | if (tag.indexOf("/") >= 0) { |
| 244 | 0 | tag = tag.substring(0, tag.indexOf("/")); |
| 245 | |
} |
| 246 | |
|
| 247 | 0 | if (!tagsMap.containsKey(tag) && builder.matchesTagPatterns(tag)) { |
| 248 | 0 | SvnConfigurationOptions.getTaskLogger().info("= TAG " + tag + " rev:" + copyfromRev + " stringData [" + stringData + "]"); |
| 249 | 0 | tagsMap.put(tag, copyfromRev); |
| 250 | 0 | tagsDateMap.put(tag, currentRevisionData.getDate()); |
| 251 | |
} |
| 252 | |
} |
| 253 | |
|
| 254 | 21672 | data.setCopyfromPath(copyfromPath); |
| 255 | 21672 | data.setCopyfromRevision(copyfromRev); |
| 256 | |
|
| 257 | 21672 | currentRevisions.add(data); |
| 258 | 21672 | currentFilenames.add(filename); |
| 259 | 21672 | } |
| 260 | |
|
| 261 | |
|
| 262 | |
|
| 263 | |
|
| 264 | |
|
| 265 | |
|
| 266 | |
|
| 267 | |
private void endPaths() throws SAXException { |
| 268 | 2416 | checkLastElement(PATHS); |
| 269 | 2416 | lastElement = LOGENTRY; |
| 270 | 2416 | } |
| 271 | |
|
| 272 | |
|
| 273 | |
|
| 274 | |
|
| 275 | |
|
| 276 | |
|
| 277 | |
|
| 278 | |
|
| 279 | |
|
| 280 | |
private void fatalError(final String message) throws SAXException { |
| 281 | 0 | fatalError(new SAXParseException(message, null)); |
| 282 | 0 | } |
| 283 | |
|
| 284 | |
|
| 285 | |
|
| 286 | |
|
| 287 | |
|
| 288 | |
|
| 289 | |
|
| 290 | |
private void startAuthorDateMsg() throws SAXException { |
| 291 | 7248 | checkLastElement(LOGENTRY); |
| 292 | 7248 | } |
| 293 | |
|
| 294 | |
|
| 295 | |
|
| 296 | |
|
| 297 | |
|
| 298 | |
|
| 299 | |
|
| 300 | |
|
| 301 | |
public void startElement(final String uri, final String localName, final String qName, final Attributes attributes) throws SAXException { |
| 302 | 33756 | super.startElement(uri, localName, qName, attributes); |
| 303 | 33756 | stringData = ""; |
| 304 | 33756 | String eName = localName; |
| 305 | 33756 | if ("".equals(eName)) { |
| 306 | 33756 | eName = qName; |
| 307 | |
} |
| 308 | 33756 | if (eName.equals(LOG)) { |
| 309 | 4 | startLog(); |
| 310 | 33752 | } else if (eName.equals(LOGENTRY)) { |
| 311 | 2416 | startLogEntry(attributes); |
| 312 | 31336 | } else if (eName.equals(AUTHOR) || eName.equals(DATE) || eName.equals(MSG)) { |
| 313 | 7248 | startAuthorDateMsg(); |
| 314 | 24088 | } else if (eName.equals(PATHS)) { |
| 315 | 2416 | startPaths(); |
| 316 | 21672 | } else if (eName.equals(PATH)) { |
| 317 | 21672 | startPath(attributes); |
| 318 | |
} else { |
| 319 | 0 | fatalError(INVALID_SVN_LOG_FILE); |
| 320 | |
} |
| 321 | 33756 | } |
| 322 | |
|
| 323 | |
|
| 324 | |
|
| 325 | |
|
| 326 | |
|
| 327 | |
|
| 328 | |
|
| 329 | |
private void startLog() throws SAXException { |
| 330 | 4 | checkLastElement(""); |
| 331 | 4 | lastElement = LOG; |
| 332 | |
|
| 333 | |
try { |
| 334 | 4 | repositoryFileManager.loadInfo(); |
| 335 | 4 | builder.buildModule(repositoryFileManager.getModuleName()); |
| 336 | 0 | } catch (final Exception e) { |
| 337 | 0 | throw new SAXException(e); |
| 338 | 4 | } |
| 339 | |
|
| 340 | 4 | } |
| 341 | |
|
| 342 | |
|
| 343 | |
|
| 344 | |
|
| 345 | |
|
| 346 | |
|
| 347 | |
|
| 348 | |
|
| 349 | |
private void startLogEntry(final Attributes attributes) throws SAXException { |
| 350 | 2416 | checkLastElement(LOG); |
| 351 | 2416 | lastElement = LOGENTRY; |
| 352 | 2416 | currentRevisionData = new RevisionData(); |
| 353 | 2416 | currentRevisions = new ArrayList(); |
| 354 | 2416 | currentFilenames = new ArrayList(); |
| 355 | 2416 | if (attributes != null && attributes.getValue("revision") != null) { |
| 356 | 2416 | currentRevisionData.setRevisionNumber(attributes.getValue("revision")); |
| 357 | |
} else { |
| 358 | 0 | fatalError(INVALID_SVN_LOG_FILE); |
| 359 | |
} |
| 360 | 2416 | } |
| 361 | |
|
| 362 | |
|
| 363 | |
|
| 364 | |
|
| 365 | |
|
| 366 | |
|
| 367 | |
|
| 368 | |
private void startPath(final Attributes attributes) throws SAXException { |
| 369 | 21672 | checkLastElement(PATHS); |
| 370 | 21672 | if (attributes != null && attributes.getValue("action") != null) { |
| 371 | 21672 | pathAction = attributes.getValue("action"); |
| 372 | |
} else { |
| 373 | 0 | fatalError(INVALID_SVN_LOG_FILE); |
| 374 | |
} |
| 375 | |
|
| 376 | 21672 | copyfromPath = attributes.getValue("copyfrom-path"); |
| 377 | 21672 | copyfromRev = attributes.getValue("copyfrom-rev"); |
| 378 | |
|
| 379 | 21672 | } |
| 380 | |
|
| 381 | |
|
| 382 | |
|
| 383 | |
|
| 384 | |
|
| 385 | |
|
| 386 | |
|
| 387 | |
private void startPaths() throws SAXException { |
| 388 | 2416 | checkLastElement(LOGENTRY); |
| 389 | 2416 | lastElement = PATHS; |
| 390 | 2416 | } |
| 391 | |
|
| 392 | |
|
| 393 | |
|
| 394 | |
|
| 395 | |
|
| 396 | |
|
| 397 | |
|
| 398 | |
|
| 399 | |
|
| 400 | |
private void warning(final String message) throws SAXException { |
| 401 | 0 | SvnConfigurationOptions.getTaskLogger().info(message); |
| 402 | 0 | } |
| 403 | |
} |