/* * Copyright 2013 Brian Rosenberger (Brutex Network) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.brutex.svn; import java.util.ArrayList; import java.util.Date; import java.util.Iterator; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.log4j.Logger; /** * The Class SVNCommitInfo represents changes commited to * a svn repository. The information is based on svnlook. * * 'A ' Object dem Projektarchiv hinzugefügt * 'D ' Objekt aus dem Projektarchiv gelöscht * 'U ' Dateiinhalt geändert * '_U' Eigenschaften eines Objektes geändert * 'UU' Dateiinhalt und Eigenschaften geändert * * @author Brian Rosenberger, bru(at)brutex.de * @since 0.1 * */ public class SVNCommitInfo { /** The logger. */ private final Logger logger = Logger.getLogger(SVNCommitInfo.class); /** The author. */ private final String author; /** The commit message */ private String logmessage; /** The date. */ private final Date date; /** The Alist. */ private final List Alist = new ArrayList(0); /** The Dlist. */ private final List Dlist = new ArrayList(0); /** The Ulist. */ private final List Ulist = new ArrayList(5); /** The _ ulist. */ private final List _Ulist = new ArrayList(0); /** The U ulist. */ private final List UUlist = new ArrayList(0); /** The issues. */ private final List issues = new ArrayList(1); /** The txn. */ private String txn=""; /** The rev. */ private String rev=""; /** * Instantiates a new SVN commit info. * * @param author the commiter * @param date the commit date * @param logmessage the commit message */ public SVNCommitInfo(String author, Date date, String logmessage) { this.author = author; this.date = date; this.logmessage = logmessage; } /** * Adds the file info. * * @param t the t * @param file the file */ public void addFileInfo(ChangeType t, String file) { switch (t) { case ADDED: Alist.add(file); break; case DELETED: Dlist.add(file); break; case UPDATED: Ulist.add(file); break; case METAUPDATE: _Ulist.add(file); break; case BOTHUPDATE: UUlist.add(file); break; default: break; } } /** * Gets the author. * * @return the author */ public String getAuthor() { return author; } /** * Gets the commit message. * * @return the commit message */ public String getLogmessage() { return logmessage; } /** * Gets the commit date. * * @return the commit date */ public Date getDate() { return date; } /** * Gets the svn transaction id. * * @return the txn */ public String getTxn() { return txn; } /** * Sets the txn. * * @param txn the new txn */ public void setTxn(String txn) { this.txn = txn; } /** * Gets the id. This is either the txn or revision. * * @return the id */ public String getId() { if(txn!=null) return txn; return rev; } /** * Sets the rev. * * @param rev the new rev */ public void setRev(String rev) { this.rev = rev; } /** * Gets the rev. * * @return the rev */ public String getRev() { return rev; } /* * http://openbook.galileocomputing.de/javainsel9/javainsel_04_007.htm#mjd5b5d84cb3f1b5bcb7638ea9221a491f */ /** * Parses the issues. * * @param patterns the patterns * @param isRemoveIssues */ public void parseIssues(String[] patterns, boolean isRemoveIssues) { issues.clear(); //reset int count = 0; for(String p : patterns) { Pattern regex = Pattern.compile(p); Matcher matcher = regex.matcher(logmessage); logger.debug(String.format("Matching regex pattern '%s' against logmessage '%s'.", matcher.pattern().pattern(), logmessage)); while( matcher.find()) { issues.add( matcher.group() ); logger.debug("Found issue '" + matcher.group() + "' in the logmessage."); count++; } if(isRemoveIssues) { logmessage = matcher.replaceAll(""); logger.debug("Removing all matched issues from commit message"); } } logger.debug("Found '" + count + "' issues in the logmessage."); } /** * Gets the change file list as string. * * @return the change file list as string */ public String getChangeFileListAsString() { StringBuilder sb = new StringBuilder(); for (Iterator iterator = Alist.iterator(); iterator.hasNext();) { sb.append("A \t"); sb.append(iterator.next()); sb.append("\n"); } for (Iterator iterator = Dlist.iterator(); iterator.hasNext();) { sb.append("D \t"); sb.append(iterator.next()); sb.append("\n"); } for (Iterator iterator = Ulist.iterator(); iterator.hasNext();) { sb.append("U \t"); sb.append(iterator.next()); sb.append("\n"); } for (Iterator iterator = _Ulist.iterator(); iterator.hasNext();) { sb.append("_U\t"); sb.append(iterator.next()); sb.append("\n"); } for (Iterator iterator = UUlist.iterator(); iterator.hasNext();) { sb.append("UU\t"); sb.append(iterator.next()); sb.append("\n"); } sb.append("Summary: " + (Ulist.size()+UUlist.size()+_Ulist.size()) + " files updated, "); sb.append(Alist.size() + " files added, " + Dlist.size() + " files deleted."); return sb.toString(); } /** * Gets the added files. * * @return the added files */ public List getAddedFiles() { return Alist; } /** * Gets the deleted files. * * @return the deleted files */ public List getDeletedFiles() { return Dlist; } /** * Gets the changed files. * * @return the changed files */ public List getChangedFiles() { List changed = new ArrayList(); changed.addAll(Ulist); changed.addAll(UUlist); changed.addAll(_Ulist); return changed; } /** * Gets the issues. * * @return the issues */ public List getIssues() { return issues; } /** * The Enum ChangeType. * * @author Brian Rosenberger, bru(at)brutex.de */ public enum ChangeType { /** The added. */ ADDED("A "), /** The deleted. */ DELETED("D "), /** The updated. */ UPDATED("U "), /** The metaupdate. */ METAUPDATE("_U"), /** The bothupdate. */ BOTHUPDATE("UU"); /** The indicator. */ private final String indicator; /** * Instantiates a new change type. * * @param svn the svn */ private ChangeType(String svn) { this.indicator = svn; } /** * Gets the enum. * * @param s the s * @return the enum */ public static ChangeType getEnum(String s) { for(ChangeType e : ChangeType.values()) { if(s.equals(e.getIndicator())) return e; } throw new IllegalArgumentException("ChangeType enum for value '"+s+"' does not exist."); } /** * Gets the indicator. * * @return the indicator */ public String getIndicator() { return this.indicator; } } }