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