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