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 private String lastElement = "";
32
33 private final RepositoriesBuilder repositoriesBuilder;
34
35
36
37
38
39
40
41 public SvnXmlRepositoriesFileHandler(final RepositoriesBuilder repositoriesBuilder) {
42 this.repositoriesBuilder = repositoriesBuilder;
43 }
44
45
46
47
48
49
50
51
52
53 private void checkLastElement(final String last) throws SAXException {
54 if (!lastElement.equals(last)) {
55 fatalError(FATAL_ERROR_MESSAGE);
56 }
57 }
58
59
60
61
62
63
64
65 public void endElement(final String uri, final String localName, final String qName) throws SAXException {
66 super.endElement(uri, localName, qName);
67 String eName = localName;
68 if ("".equals(eName)) {
69 eName = qName;
70 }
71
72 if (eName.equals(REPOSITORIES)) {
73 endRepositories();
74 } else if (eName.equals(REPOSITORY)) {
75 endRepository();
76 } else {
77 fatalError(FATAL_ERROR_MESSAGE);
78 }
79 }
80
81
82
83
84
85
86
87 private void endRepositories() throws SAXException {
88 checkLastElement(REPOSITORIES);
89 lastElement = "";
90 }
91
92
93
94
95
96
97
98 private void endRepository() throws SAXException {
99 checkLastElement(REPOSITORY);
100 lastElement = REPOSITORIES;
101 }
102
103
104
105
106
107
108
109
110
111 private void fatalError(final String message) throws SAXException {
112 fatalError(new SAXParseException(message, null));
113 }
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 super.startElement(uri, localName, qName, attributes);
123
124 String eName = localName;
125 if ("".equals(eName)) {
126 eName = qName;
127 }
128
129 if (eName.equals(REPOSITORIES)) {
130 startRepositories();
131 } else if (eName.equals(REPOSITORY)) {
132 startRepository(attributes);
133 } else {
134 fatalError(FATAL_ERROR_MESSAGE);
135 }
136 }
137
138
139
140
141
142
143
144 private void startRepositories() throws SAXException {
145 checkLastElement("");
146 lastElement = REPOSITORIES;
147 try {
148 repositoriesBuilder.buildRoot();
149 } catch (final ParserConfigurationException e) {
150 fatalError(FATAL_ERROR_MESSAGE);
151 }
152 }
153
154
155
156
157
158
159
160
161
162 private void startRepository(final Attributes attributes) throws SAXException {
163 checkLastElement(REPOSITORIES);
164 lastElement = REPOSITORY;
165 if (attributes != null && attributes.getValue(UUID) != null && attributes.getValue(FILE) != null) {
166 final String uuid = attributes.getValue(UUID);
167 final String file = attributes.getValue(FILE);
168 repositoriesBuilder.buildRepository(uuid, file);
169 } else {
170 fatalError(FATAL_ERROR_MESSAGE);
171 }
172 }
173
174 }