view mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/MercurialXmlLogParser.java @ 704:5ee94ee69b29

helper method
author eugene.petrenko@jetbrains.com
date Wed, 08 Jan 2014 17:42:41 +0100
parents 3daa8e36f8f1
children 31a1aca3305c
line wrap: on
line source
package jetbrains.buildServer.buildTriggers.vcs.mercurial.command;

import org.jetbrains.annotations.NotNull;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
* @author dmitry.neverov
*/
public class MercurialXmlLogParser extends DefaultHandler {

  private static final String DATE_FORMAT = "EEE MMM d HH:mm:ss yyyy Z";
  private final SimpleDateFormat mySimpleDateFormat = new SimpleDateFormat(DATE_FORMAT, Locale.ENGLISH);
  private List<ChangeSet> myChangeSets = new ArrayList<ChangeSet>();
  private ChangeSet myCset = null;
  private StringBuilder myText = new StringBuilder();
  private List<FileStatus> myFiles;
  private String myFileAction;

  @NotNull
  public List<ChangeSet> getChangeSets() {
    return myChangeSets;
  }

  @Override
  public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException {
    if ("logentry".equals(qName)) {
      myCset = new ChangeSet(getRevision(attrs), getId(attrs));
      myCset.setBranch(attrs.getValue("branch"));
    } else if ("parent".equals(qName)) {
      ChangeSetRevision parentCset = new ChangeSetRevision(getRevision(attrs), getId(attrs));
      myCset.addParent(parentCset);
    } else if ("author".equals(qName)) {
      myCset.setUser(attrs.getValue("original"));
    } else if ("paths".equals(qName)) {
      myFiles = new ArrayList<FileStatus>();
    } else if ("path".equals(qName)) {
      myFileAction = attrs.getValue("action");
      resetText();
    } else if ("msg".equals(qName)) {
      resetText();
    } else if ("date".equals(qName)) {
      resetText();
    }
  }

  @Override
  public void endElement(String uri, String localName, String qName) throws SAXException {
    if ("logentry".equals(qName)) {
      myChangeSets.add(myCset);
    } else if ("paths".equals(qName)) {
      myCset.setModifiedFiles(myFiles);
    } else if ("path".equals(qName)) {
      myFiles.add(new FileStatus(Status.makeStatus(myFileAction), getText()));
    } else if ("msg".equals(qName)) {
      myCset.setDescription(getText());
    } else if ("date".equals(qName)) {
      myCset.setTimestamp(parseDate());
    }
  }

  @Override
  public void characters(char[] ch, int start, int length) throws SAXException {
    myText.append(ch, start, length);
  }

  private String getText() {
    return myText.toString();
  }

  private void resetText() {
    myText = new StringBuilder();
  }

  private Date parseDate() throws SAXException {
    try {
      return mySimpleDateFormat.parse(getText());
    } catch (ParseException e) {
      throw new SAXException(e);
    }
  }

  private int getRevision(@NotNull Attributes attrs) {
    return Integer.parseInt(attrs.getValue("revision"));
  }

  private String getId(@NotNull Attributes attrs) {
    return attrs.getValue("shortnode");
  }
}