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 | 30 | private Document document = null; |
41 | |
|
42 | 30 | private Element repositories = null; |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | 30 | public RepositoriesBuilder() { |
49 | 30 | } |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
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 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
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 | |
|
88 | |
|
89 | |
|
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 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
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 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
public Document getDocument() { |
136 | 30 | return document; |
137 | |
} |
138 | |
|
139 | |
} |