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
15
16
17
18
19
20
21
22
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
46
47
48 public RepositoriesBuilder() {
49 }
50
51
52
53
54
55
56
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
71
72
73
74
75
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
88
89
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
102
103
104
105
106
107
108
109
110
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
132
133
134
135 public Document getDocument() {
136 return document;
137 }
138
139 }