Coverage Report - net.sf.statsvn.input.SvnXmlRepositoriesFileHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
SvnXmlRepositoriesFileHandler
84%
43/51
60%
12/20
2
 
 1  
 package net.sf.statsvn.input;
 2  
 
 3  
 import javax.xml.parsers.ParserConfigurationException;
 4  
 
 5  
 import org.xml.sax.Attributes;
 6  
 import org.xml.sax.SAXException;
 7  
 import org.xml.sax.SAXParseException;
 8  
 import org.xml.sax.helpers.DefaultHandler;
 9  
 
 10  
 /**
 11  
  * This is the SAX parser for the repositories xml file. This xml file 
 12  
  * identifies the line counts xml files for all repositories for which 
 13  
  * SVN stats have been compiled.
 14  
  * 
 15  
  * @author Gunter Mussbacher <gunterm@site.uottawa.ca>
 16  
  * 
 17  
  * @version $Id: SvnXmlRepositoriesFileHandler.java 351 2008-03-28 18:46:26Z benoitx $
 18  
  */
 19  
 public class SvnXmlRepositoriesFileHandler extends DefaultHandler {
 20  
 
 21  
         private static final String FATAL_ERROR_MESSAGE = "Invalid StatSvn repositories file.";
 22  
 
 23  
         private static final String REPOSITORIES = "repositories";
 24  
 
 25  
         private static final String REPOSITORY = "repository";
 26  
 
 27  
         private static final String UUID = "uuid";
 28  
 
 29  
         private static final String FILE = "file";
 30  
 
 31  30
         private String lastElement = "";
 32  
 
 33  
         private final RepositoriesBuilder repositoriesBuilder;
 34  
 
 35  
         /**
 36  
          * Default constructor
 37  
          * 
 38  
          * @param repositoriesBuilder
 39  
          *            the RepositoriesBuilder to which to send back the repository information.
 40  
          */
 41  30
         public SvnXmlRepositoriesFileHandler(final RepositoriesBuilder repositoriesBuilder) {
 42  30
                 this.repositoriesBuilder = repositoriesBuilder;
 43  30
         }
 44  
 
 45  
         /**
 46  
          * Makes sure the last element received is appropriate.
 47  
          * 
 48  
          * @param last
 49  
          *            the expected last element.
 50  
          * @throws SAXException
 51  
          *             unexpected event.
 52  
          */
 53  
         private void checkLastElement(final String last) throws SAXException {
 54  120
                 if (!lastElement.equals(last)) {
 55  0
                         fatalError(FATAL_ERROR_MESSAGE);
 56  
                 }
 57  120
         }
 58  
 
 59  
         /**
 60  
          * Handles the end of an xml element and redirects to the appropriate end* method.
 61  
          * 
 62  
          * @throws SAXException
 63  
          *             unexpected event.
 64  
          */
 65  
         public void endElement(final String uri, final String localName, final String qName) throws SAXException {
 66  60
                 super.endElement(uri, localName, qName);
 67  60
                 String eName = localName; // element name
 68  60
                 if ("".equals(eName)) {
 69  60
                         eName = qName; // namespaceAware = false
 70  
                 }
 71  
 
 72  60
                 if (eName.equals(REPOSITORIES)) {
 73  30
                         endRepositories();
 74  30
                 } else if (eName.equals(REPOSITORY)) {
 75  30
                         endRepository();
 76  
                 } else {
 77  0
                         fatalError(FATAL_ERROR_MESSAGE);
 78  
                 }
 79  60
         }
 80  
 
 81  
         /**
 82  
          * End of repositories element.
 83  
          * 
 84  
          * @throws SAXException
 85  
          *             unexpected event.
 86  
          */
 87  
         private void endRepositories() throws SAXException {
 88  30
                 checkLastElement(REPOSITORIES);
 89  30
                 lastElement = "";
 90  30
         }
 91  
 
 92  
         /**
 93  
          * End of repository element.
 94  
          * 
 95  
          * @throws SAXException
 96  
          *             unexpected event.
 97  
          */
 98  
         private void endRepository() throws SAXException {
 99  30
                 checkLastElement(REPOSITORY);
 100  30
                 lastElement = REPOSITORIES;
 101  30
         }
 102  
 
 103  
         /**
 104  
          * Throws a fatal error with the specified message.
 105  
          * 
 106  
          * @param message
 107  
          *            the reason for the error
 108  
          * @throws SAXException
 109  
          *             the error
 110  
          */
 111  
         private void fatalError(final String message) throws SAXException {
 112  0
                 fatalError(new SAXParseException(message, null));
 113  0
         }
 114  
 
 115  
         /**
 116  
          * Handles the start of an xml element and redirects to the appropriate start* method.
 117  
          * 
 118  
          * @throws SAXException
 119  
          *             unexpected event.
 120  
          */
 121  
         public void startElement(final String uri, final String localName, final String qName, final Attributes attributes) throws SAXException {
 122  60
                 super.startElement(uri, localName, qName, attributes);
 123  
 
 124  60
                 String eName = localName; // element name
 125  60
                 if ("".equals(eName)) {
 126  60
                         eName = qName; // namespaceAware = false
 127  
                 }
 128  
 
 129  60
                 if (eName.equals(REPOSITORIES)) {
 130  30
                         startRepositories();
 131  30
                 } else if (eName.equals(REPOSITORY)) {
 132  30
                         startRepository(attributes);
 133  
                 } else {
 134  0
                         fatalError(FATAL_ERROR_MESSAGE);
 135  
                 }
 136  60
         }
 137  
 
 138  
         /**
 139  
          * Handles the start of the document. Initializes the repository builder.
 140  
          * 
 141  
          * @throws SAXException
 142  
          *             unable to build the root.
 143  
          */
 144  
         private void startRepositories() throws SAXException {
 145  30
                 checkLastElement("");
 146  30
                 lastElement = REPOSITORIES;
 147  
                 try {
 148  30
                         repositoriesBuilder.buildRoot();
 149  0
                 } catch (final ParserConfigurationException e) {
 150  0
                         fatalError(FATAL_ERROR_MESSAGE);
 151  30
                 }
 152  30
         }
 153  
 
 154  
         /**
 155  
          * Handles start of a repository. Initializes repositories builder for use with repository.
 156  
          * 
 157  
          * @param attributes
 158  
          *            element's xml attributes.
 159  
          * @throws SAXException
 160  
          *             missing some data.
 161  
          */
 162  
         private void startRepository(final Attributes attributes) throws SAXException {
 163  30
                 checkLastElement(REPOSITORIES);
 164  30
                 lastElement = REPOSITORY;
 165  30
                 if (attributes != null && attributes.getValue(UUID) != null && attributes.getValue(FILE) != null) {
 166  30
                         final String uuid = attributes.getValue(UUID);
 167  30
                         final String file = attributes.getValue(FILE);
 168  30
                         repositoriesBuilder.buildRepository(uuid, file);
 169  27
                 } else {
 170  0
                         fatalError(FATAL_ERROR_MESSAGE);
 171  
                 }
 172  30
         }
 173  
 
 174  
 }