Coverage Report - net.sf.statsvn.input.RepositoriesBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
RepositoriesBuilder
79%
27/34
50%
5/10
2.333
 
 1  
 package net.sf.statsvn.input;
 2  
 
 3  
 import javax.xml.parsers.DocumentBuilder;
 4  
 import javax.xml.parsers.DocumentBuilderFactory;
 5  
 import javax.xml.parsers.ParserConfigurationException;
 6  
 
 7  
 import net.sf.statcvs.output.ConfigurationOptions;
 8  
 
 9  
 import org.w3c.dom.Document;
 10  
 import org.w3c.dom.Element;
 11  
 import org.w3c.dom.NodeList;
 12  
 
 13  
 /**
 14  
  * <p>
 15  
  * This class receives information from the (@link net.sf.statsvn.input.SvnXmlRepositoriesFileHandler)
 16  
  * to build a DOM-based XML structure containing the names of all repositories and associated line counts xml files.
 17  
  * It then allows to retrieve the line counts XML file name for a given repository.
 18  
  * </p>
 19  
  * 
 20  
  * @author Gunter Mussbacher <gunterm@site.uottawa.ca>
 21  
  * 
 22  
  * @version $Id: RepositoriesBuilder.java 351 2008-03-28 18:46:26Z benoitx $
 23  
  * 
 24  
  */
 25  
 public class RepositoriesBuilder {
 26  
         private static final String FILE_EXTENSION = ".xml";
 27  
 
 28  
         private static final String FILE_PREFIX = "cache_";
 29  
 
 30  
         private static final String REPOSITORIES = "repositories";
 31  
 
 32  
         private static final String UUID = "uuid";
 33  
 
 34  
         private static final String FILE = "file";
 35  
 
 36  
         private static final String PROJECT = "project";
 37  
 
 38  
         private static final String REPOSITORY = "repository";
 39  
 
 40  30
         private Document document = null;
 41  
 
 42  30
         private Element repositories = null;
 43  
 
 44  
         /**
 45  
          * Constructs the RepositoriesBuilder
 46  
          * 
 47  
          */
 48  30
         public RepositoriesBuilder() {
 49  30
         }
 50  
 
 51  
         /**
 52  
          * Finds a repository in the DOM.
 53  
          * 
 54  
          * @param uuid
 55  
          *            the uuid of the repository
 56  
          * @return the repository or null if the repository does not exist
 57  
          */
 58  
         private Element findRepository(final String uuid) {
 59  30
                 final NodeList paths = repositories.getChildNodes();
 60  30
                 for (int i = 0; i < paths.getLength(); i++) {
 61  30
                         final Element path = (Element) paths.item(i);
 62  30
                         if (uuid.equals(path.getAttribute(UUID))) {
 63  30
                                 return path;
 64  
                         }
 65  
                 }
 66  0
                 return null;
 67  
         }
 68  
 
 69  
         /**
 70  
          * Adds a repository to the DOM structure.
 71  
          * 
 72  
          * @param uuid
 73  
          *            the uuid of the repository
 74  
          * @param file
 75  
          *            the filename for the XML line counts file
 76  
          */
 77  
         public Element buildRepository(final String uuid, final String file) {
 78  30
                 final Element repository = document.createElement(REPOSITORY);
 79  30
                 repository.setAttribute(UUID, uuid);
 80  30
                 repository.setAttribute(FILE, file);
 81  30
                 repository.setAttribute(PROJECT, ConfigurationOptions.getProjectName());
 82  30
                 repositories.appendChild(repository);
 83  30
                 return repository;
 84  
         }
 85  
 
 86  
         /**
 87  
          * Builds the DOM root.
 88  
          * 
 89  
          * @throws ParserConfigurationException
 90  
          */
 91  
         public void buildRoot() throws ParserConfigurationException {
 92  30
                 final DocumentBuilderFactory factoryDOM = DocumentBuilderFactory.newInstance();
 93  
                 DocumentBuilder builderDOM;
 94  30
                 builderDOM = factoryDOM.newDocumentBuilder();
 95  30
                 document = builderDOM.newDocument();
 96  30
                 repositories = document.createElement(REPOSITORIES);
 97  30
                 document.appendChild(repositories);
 98  30
         }
 99  
 
 100  
         /**
 101  
          * Retrieves the file name of the line counts xml file for a given repository.
 102  
          * Creates a new file name if the line counts xml file does not exist.
 103  
          * 
 104  
          * If the repositories xml file does not exist (i.e. the document is null), 
 105  
          * a new document is created.
 106  
          * 
 107  
          * @param uuid
 108  
          *            the uuid of the repository
 109  
          *            
 110  
          * @return the file name or "" if an unexpected error occurs          
 111  
          */
 112  
         public String getFileName(final String uuid) {
 113  30
                 if (document == null) {
 114  
                         try {
 115  0
                                 buildRoot();
 116  0
                         } catch (final ParserConfigurationException e) {
 117  0
                                 document = null;
 118  0
                         }
 119  
                 }
 120  30
                 if (document != null) {
 121  30
                         Element repository = findRepository(uuid);
 122  30
                         if (repository == null) {
 123  0
                                 repository = buildRepository(uuid, FILE_PREFIX + uuid + FILE_EXTENSION);
 124  
                         }
 125  30
                         return repository.getAttribute(FILE);
 126  
                 }
 127  0
                 return "";
 128  
         }
 129  
 
 130  
         /**
 131  
          * Returns the DOM object when building is complete.
 132  
          * 
 133  
          * @return the DOM document.
 134  
          */
 135  
         public Document getDocument() {
 136  30
                 return document;
 137  
         }
 138  
 
 139  
 }