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 private Document document = null;
41
42 private Element repositories = null;
43
44 /**
45 * Constructs the RepositoriesBuilder
46 *
47 */
48 public RepositoriesBuilder() {
49 }
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 final NodeList paths = repositories.getChildNodes();
60 for (int i = 0; i < paths.getLength(); i++) {
61 final Element path = (Element) paths.item(i);
62 if (uuid.equals(path.getAttribute(UUID))) {
63 return path;
64 }
65 }
66 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 final Element repository = document.createElement(REPOSITORY);
79 repository.setAttribute(UUID, uuid);
80 repository.setAttribute(FILE, file);
81 repository.setAttribute(PROJECT, ConfigurationOptions.getProjectName());
82 repositories.appendChild(repository);
83 return repository;
84 }
85
86 /**
87 * Builds the DOM root.
88 *
89 * @throws ParserConfigurationException
90 */
91 public void buildRoot() throws ParserConfigurationException {
92 final DocumentBuilderFactory factoryDOM = DocumentBuilderFactory.newInstance();
93 DocumentBuilder builderDOM;
94 builderDOM = factoryDOM.newDocumentBuilder();
95 document = builderDOM.newDocument();
96 repositories = document.createElement(REPOSITORIES);
97 document.appendChild(repositories);
98 }
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 if (document == null) {
114 try {
115 buildRoot();
116 } catch (final ParserConfigurationException e) {
117 document = null;
118 }
119 }
120 if (document != null) {
121 Element repository = findRepository(uuid);
122 if (repository == null) {
123 repository = buildRepository(uuid, FILE_PREFIX + uuid + FILE_EXTENSION);
124 }
125 return repository.getAttribute(FILE);
126 }
127 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 return document;
137 }
138
139 }