1 | |
package net.sf.statsvn.input; |
2 | |
|
3 | |
import javax.xml.parsers.ParserConfigurationException; |
4 | |
|
5 | |
import org.xml.sax.Attributes; |
6 | |
import org.xml.sax.SAXException; |
7 | |
import org.xml.sax.SAXParseException; |
8 | |
import org.xml.sax.helpers.DefaultHandler; |
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
public class SvnXmlRepositoriesFileHandler extends DefaultHandler { |
20 | |
|
21 | |
private static final String FATAL_ERROR_MESSAGE = "Invalid StatSvn repositories file."; |
22 | |
|
23 | |
private static final String REPOSITORIES = "repositories"; |
24 | |
|
25 | |
private static final String REPOSITORY = "repository"; |
26 | |
|
27 | |
private static final String UUID = "uuid"; |
28 | |
|
29 | |
private static final String FILE = "file"; |
30 | |
|
31 | 30 | private String lastElement = ""; |
32 | |
|
33 | |
private final RepositoriesBuilder repositoriesBuilder; |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | 30 | public SvnXmlRepositoriesFileHandler(final RepositoriesBuilder repositoriesBuilder) { |
42 | 30 | this.repositoriesBuilder = repositoriesBuilder; |
43 | 30 | } |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
private void checkLastElement(final String last) throws SAXException { |
54 | 120 | if (!lastElement.equals(last)) { |
55 | 0 | fatalError(FATAL_ERROR_MESSAGE); |
56 | |
} |
57 | 120 | } |
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
public void endElement(final String uri, final String localName, final String qName) throws SAXException { |
66 | 60 | super.endElement(uri, localName, qName); |
67 | 60 | String eName = localName; |
68 | 60 | if ("".equals(eName)) { |
69 | 60 | eName = qName; |
70 | |
} |
71 | |
|
72 | 60 | if (eName.equals(REPOSITORIES)) { |
73 | 30 | endRepositories(); |
74 | 30 | } else if (eName.equals(REPOSITORY)) { |
75 | 30 | endRepository(); |
76 | |
} else { |
77 | 0 | fatalError(FATAL_ERROR_MESSAGE); |
78 | |
} |
79 | 60 | } |
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
private void endRepositories() throws SAXException { |
88 | 30 | checkLastElement(REPOSITORIES); |
89 | 30 | lastElement = ""; |
90 | 30 | } |
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
private void endRepository() throws SAXException { |
99 | 30 | checkLastElement(REPOSITORY); |
100 | 30 | lastElement = REPOSITORIES; |
101 | 30 | } |
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
private void fatalError(final String message) throws SAXException { |
112 | 0 | fatalError(new SAXParseException(message, null)); |
113 | 0 | } |
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
public void startElement(final String uri, final String localName, final String qName, final Attributes attributes) throws SAXException { |
122 | 60 | super.startElement(uri, localName, qName, attributes); |
123 | |
|
124 | 60 | String eName = localName; |
125 | 60 | if ("".equals(eName)) { |
126 | 60 | eName = qName; |
127 | |
} |
128 | |
|
129 | 60 | if (eName.equals(REPOSITORIES)) { |
130 | 30 | startRepositories(); |
131 | 30 | } else if (eName.equals(REPOSITORY)) { |
132 | 30 | startRepository(attributes); |
133 | |
} else { |
134 | 0 | fatalError(FATAL_ERROR_MESSAGE); |
135 | |
} |
136 | 60 | } |
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
private void startRepositories() throws SAXException { |
145 | 30 | checkLastElement(""); |
146 | 30 | lastElement = REPOSITORIES; |
147 | |
try { |
148 | 30 | repositoriesBuilder.buildRoot(); |
149 | 0 | } catch (final ParserConfigurationException e) { |
150 | 0 | fatalError(FATAL_ERROR_MESSAGE); |
151 | 30 | } |
152 | 30 | } |
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
private void startRepository(final Attributes attributes) throws SAXException { |
163 | 30 | checkLastElement(REPOSITORIES); |
164 | 30 | lastElement = REPOSITORY; |
165 | 30 | if (attributes != null && attributes.getValue(UUID) != null && attributes.getValue(FILE) != null) { |
166 | 30 | final String uuid = attributes.getValue(UUID); |
167 | 30 | final String file = attributes.getValue(FILE); |
168 | 30 | repositoriesBuilder.buildRepository(uuid, file); |
169 | 27 | } else { |
170 | 0 | fatalError(FATAL_ERROR_MESSAGE); |
171 | |
} |
172 | 30 | } |
173 | |
|
174 | |
} |