| 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 | 4 | private Document document = null; |
| 41 | |
|
| 42 | 4 | private Element repositories = null; |
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | 4 | public RepositoriesBuilder() { |
| 49 | 4 | } |
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
private Element findRepository(final String uuid) { |
| 59 | 4 | final NodeList paths = repositories.getChildNodes(); |
| 60 | 4 | for (int i = 0; i < paths.getLength(); i++) { |
| 61 | 4 | final Element path = (Element) paths.item(i); |
| 62 | 4 | if (uuid.equals(path.getAttribute(UUID))) { |
| 63 | 4 | 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 | 4 | final Element repository = document.createElement(REPOSITORY); |
| 79 | 4 | repository.setAttribute(UUID, uuid); |
| 80 | 4 | repository.setAttribute(FILE, file); |
| 81 | 4 | repository.setAttribute(PROJECT, ConfigurationOptions.getProjectName()); |
| 82 | 4 | repositories.appendChild(repository); |
| 83 | 4 | return repository; |
| 84 | |
} |
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
public void buildRoot() throws ParserConfigurationException { |
| 92 | 4 | final DocumentBuilderFactory factoryDOM = DocumentBuilderFactory.newInstance(); |
| 93 | |
DocumentBuilder builderDOM; |
| 94 | 4 | builderDOM = factoryDOM.newDocumentBuilder(); |
| 95 | 4 | document = builderDOM.newDocument(); |
| 96 | 4 | repositories = document.createElement(REPOSITORIES); |
| 97 | 4 | document.appendChild(repositories); |
| 98 | 4 | } |
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
public String getFileName(final String uuid) { |
| 113 | 4 | if (document == null) { |
| 114 | |
try { |
| 115 | 0 | buildRoot(); |
| 116 | 0 | } catch (final ParserConfigurationException e) { |
| 117 | 0 | document = null; |
| 118 | 0 | } |
| 119 | |
} |
| 120 | 4 | if (document != null) { |
| 121 | 4 | Element repository = findRepository(uuid); |
| 122 | 4 | if (repository == null) { |
| 123 | 0 | repository = buildRepository(uuid, FILE_PREFIX + uuid + FILE_EXTENSION); |
| 124 | |
} |
| 125 | 4 | return repository.getAttribute(FILE); |
| 126 | |
} |
| 127 | 0 | return ""; |
| 128 | |
} |
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
public Document getDocument() { |
| 136 | 4 | return document; |
| 137 | |
} |
| 138 | |
|
| 139 | |
} |