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 | |
public class SvnXmlCacheFileHandler extends DefaultHandler { |
18 | |
private static final String FATAL_ERROR_MESSAGE = "Invalid StatSVN cache file."; |
19 | |
|
20 | 30 | private String lastElement = ""; |
21 | |
|
22 | |
private final CacheBuilder cacheBuilder; |
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | 30 | public SvnXmlCacheFileHandler(final CacheBuilder cacheBuilder) { |
31 | 30 | this.cacheBuilder = cacheBuilder; |
32 | 30 | } |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
private void checkLastElement(final String last) throws SAXException { |
43 | 287760 | if (!lastElement.equals(last)) { |
44 | 0 | fatalError(FATAL_ERROR_MESSAGE); |
45 | |
} |
46 | 287760 | } |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
public void endElement(final String uri, final String localName, final String qName) throws SAXException { |
55 | 143880 | super.endElement(uri, localName, qName); |
56 | 143880 | String eName = localName; |
57 | 143880 | if ("".equals(eName)) { |
58 | 143880 | eName = qName; |
59 | |
} |
60 | |
|
61 | 143880 | if (eName.equals(CacheConfiguration.CACHE)) { |
62 | 30 | endCache(); |
63 | 143850 | } else if (eName.equals(CacheConfiguration.PATH)) { |
64 | 31530 | endPath(); |
65 | 112320 | } else if (eName.equals(CacheConfiguration.REVISION)) { |
66 | 112320 | endRevision(); |
67 | |
} else { |
68 | 0 | fatalError(FATAL_ERROR_MESSAGE); |
69 | |
} |
70 | 143880 | } |
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
private void endCache() throws SAXException { |
79 | 30 | checkLastElement(CacheConfiguration.CACHE); |
80 | 30 | lastElement = ""; |
81 | 30 | } |
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
private void endPath() throws SAXException { |
90 | 31530 | checkLastElement(CacheConfiguration.PATH); |
91 | 31530 | lastElement = CacheConfiguration.CACHE; |
92 | 31530 | } |
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
private void endRevision() throws SAXException { |
101 | 112320 | checkLastElement(CacheConfiguration.PATH); |
102 | 112320 | } |
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
private void fatalError(final String message) throws SAXException { |
113 | 0 | fatalError(new SAXParseException(message, null)); |
114 | 0 | } |
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
public void startElement(final String uri, final String localName, final String qName, final Attributes attributes) throws SAXException { |
123 | 143880 | super.startElement(uri, localName, qName, attributes); |
124 | |
|
125 | 143880 | String eName = localName; |
126 | 143880 | if ("".equals(eName)) { |
127 | 143880 | eName = qName; |
128 | |
} |
129 | |
|
130 | 143880 | if (eName.equals(CacheConfiguration.CACHE)) { |
131 | 30 | startCache(); |
132 | 143850 | } else if (eName.equals(CacheConfiguration.PATH)) { |
133 | 31530 | startPath(attributes); |
134 | 112320 | } else if (eName.equals(CacheConfiguration.REVISION)) { |
135 | 112320 | startRevision(attributes); |
136 | |
} else { |
137 | 0 | fatalError(FATAL_ERROR_MESSAGE); |
138 | |
} |
139 | 143880 | } |
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
private void startCache() throws SAXException { |
148 | 30 | checkLastElement(""); |
149 | 30 | lastElement = CacheConfiguration.CACHE; |
150 | |
try { |
151 | 30 | cacheBuilder.buildRoot(); |
152 | 0 | } catch (final ParserConfigurationException e) { |
153 | 0 | fatalError(FATAL_ERROR_MESSAGE); |
154 | 30 | } |
155 | 30 | } |
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
private void startPath(final Attributes attributes) throws SAXException { |
166 | 31530 | checkLastElement(CacheConfiguration.CACHE); |
167 | 31530 | lastElement = CacheConfiguration.PATH; |
168 | 31530 | if (attributes != null && attributes.getValue(CacheConfiguration.NAME) != null) { |
169 | 31530 | final String name = attributes.getValue(CacheConfiguration.NAME); |
170 | 31530 | String revision = "0"; |
171 | 31530 | if (attributes.getValue(CacheConfiguration.LATEST_REVISION) != null) { |
172 | 31530 | revision = attributes.getValue(CacheConfiguration.LATEST_REVISION); |
173 | |
} |
174 | 31530 | String binaryStatus = CacheConfiguration.UNKNOWN; |
175 | 31530 | if (attributes.getValue(CacheConfiguration.BINARY_STATUS) != null) { |
176 | 31530 | binaryStatus = attributes.getValue(CacheConfiguration.BINARY_STATUS); |
177 | |
} |
178 | 31530 | cacheBuilder.buildPath(name, revision, binaryStatus); |
179 | 28377 | } else { |
180 | 0 | fatalError(FATAL_ERROR_MESSAGE); |
181 | |
} |
182 | 31530 | } |
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
private void startRevision(final Attributes attributes) throws SAXException { |
193 | 112320 | checkLastElement(CacheConfiguration.PATH); |
194 | 112320 | if (attributes != null && attributes.getValue(CacheConfiguration.NUMBER) != null && attributes.getValue(CacheConfiguration.ADDED) != null |
195 | |
&& attributes.getValue(CacheConfiguration.REMOVED) != null) { |
196 | 112320 | final String number = attributes.getValue(CacheConfiguration.NUMBER); |
197 | 112320 | final String added = attributes.getValue(CacheConfiguration.ADDED); |
198 | 112320 | final String removed = attributes.getValue(CacheConfiguration.REMOVED); |
199 | 112320 | String binaryStatus = CacheConfiguration.UNKNOWN; |
200 | 112320 | if (attributes.getValue(CacheConfiguration.BINARY_STATUS) != null) { |
201 | 112320 | binaryStatus = attributes.getValue(CacheConfiguration.BINARY_STATUS); |
202 | |
} |
203 | 112320 | cacheBuilder.buildRevision(number, added, removed, binaryStatus); |
204 | 101088 | } else { |
205 | 0 | fatalError(FATAL_ERROR_MESSAGE); |
206 | |
} |
207 | 112320 | } |
208 | |
} |