View Javadoc

1   /*
2    * LICENSE
3    *
4    * "THE BEER-WARE LICENSE" (Revision 42):
5    * "Sven Strittmatter" <ich@weltraumschaf.de> wrote this file.
6    * As long as you retain this notice you can do whatever you want with
7    * this stuff. If we meet some day, and you think this stuff is worth it,
8    * you can buy me a beer in return.
9    */
10  package org.jenkinsci.plugins.darcs;
11  
12  import hudson.model.AbstractBuild;
13  import hudson.scm.ChangeLogParser;
14  import java.io.ByteArrayOutputStream;
15  import java.io.File;
16  import java.io.IOException;
17  import java.io.StringReader;
18  import java.util.logging.Logger;
19  import org.xml.sax.InputSource;
20  import org.xml.sax.SAXException;
21  import org.xml.sax.XMLReader;
22  import org.xml.sax.helpers.XMLReaderFactory;
23  
24  /**
25   * Parses the output of Darcs log.
26   *
27   * @author Sven Strittmatter <ich@weltraumschaf.de>
28   * @author Ralph Lange <Ralph.Lange@gmx.de>
29   */
30  class DarcsChangeLogParser extends ChangeLogParser {
31  
32      /**
33       * Logger facility.
34       */
35      private static final Logger LOGGER = Logger.getLogger(DarcsChangeLogParser.class.getName());
36      /**
37       * Custom SAX Parser.
38       */
39      private final DarcsSaxHandler handler;
40      /**
41       * Sanitizes XML character encoding.
42       */
43      private final DarcsXmlSanitizer sanitizer;
44  
45      /**
46       * Convenience constructor which initializes all dependencies.
47       */
48      public DarcsChangeLogParser() {
49          this(new DarcsSaxHandler(), new DarcsXmlSanitizer());
50      }
51  
52      /**
53       * Dedicated constructor.
54       *
55       * @param handler implementation of a SAX parser
56       * @param sani sanitize to clean comments
57       */
58      public DarcsChangeLogParser(final DarcsSaxHandler handler, final DarcsXmlSanitizer sani) {
59          super();
60          this.handler = handler;
61          this.sanitizer = sani;
62      }
63  
64      /**
65       * Parses the Darcs change log file.
66       *
67       * The Darcs change log file is in XML format (as given by the command
68       * {@literal darcs changes --xml-output --summary}).
69       *
70       * @param build the current build
71       * @param changelogFile the change log file
72       * @return change set list
73       * @throws IOException on read errors
74       * @throws SAXException on parse errors
75       */
76      @Override
77      public DarcsChangeSetList parse(final AbstractBuild build, final File changelogFile)
78          throws IOException, SAXException {
79          LOGGER.info(String.format("Parsing changelog file %s...", changelogFile.toString()));
80          final StringReader input = new StringReader(sanitizer.cleanse(changelogFile));
81          return parse(build, new InputSource(input));
82      }
83  
84      /**
85       * @see #parse(hudson.model.AbstractBuild, java.io.File)
86       *
87       * @param changeLog stream to read XML from
88       * @return change set list w/ current build null
89       * @throws IOException on read errors
90       * @throws SAXException on parse errors
91       */
92      public DarcsChangeSetList parse(final ByteArrayOutputStream changeLog) throws IOException, SAXException {
93          final StringReader input = new StringReader(sanitizer.cleanse(changeLog.toByteArray()));
94          return parse(null, new InputSource(input));
95      }
96  
97      private DarcsChangeSetList parse(final AbstractBuild build, final InputSource changeLog) throws IOException, SAXException {
98          final XMLReader xmlReader = XMLReaderFactory.createXMLReader();
99          xmlReader.setContentHandler(handler);
100         xmlReader.setErrorHandler(handler);
101         xmlReader.parse(changeLog);
102         return new DarcsChangeSetList(build, handler.getChangeSets());
103     }
104 }