view mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/MercurialXmlLogParser.java @ 732:31a1aca3305c

Update copyright
author Dmitry Neverov <dmitry.neverov@jetbrains.com>
date Tue, 14 Jan 2014 12:45:10 +0100
parents 3daa8e36f8f1
children 7bf4d943d5bb
line wrap: on
line source
/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * 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 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");
  }
}