001: package net.sf.statsvn.input;
002:
003: import javax.xml.parsers.ParserConfigurationException;
004:
005: import org.xml.sax.Attributes;
006: import org.xml.sax.SAXException;
007: import org.xml.sax.SAXParseException;
008: import org.xml.sax.helpers.DefaultHandler;
009:
010: /**
011: * This is the SAX parser for the repositories xml file. This xml file
012: * identifies the line counts xml files for all repositories for which
013: * SVN stats have been compiled.
014: *
015: * @author Gunter Mussbacher <gunterm@site.uottawa.ca>
016: *
017: * @version $Id: SvnXmlRepositoriesFileHandler.java 144 2006-11-19 19:35:09Z benoitx $
018: */
019: public class SvnXmlRepositoriesFileHandler extends DefaultHandler {
020:
021: private static final String FATAL_ERROR_MESSAGE = "Invalid StatSvn repositories file.";
022: private static final String REPOSITORIES = "repositories";
023: private static final String REPOSITORY = "repository";
024: private static final String UUID = "uuid";
025: private static final String FILE = "file";
026: private String lastElement = "";
027: private RepositoriesBuilder repositoriesBuilder;
028:
029: /**
030: * Default constructor
031: *
032: * @param repositoriesBuilder
033: * the RepositoriesBuilder to which to send back the repository information.
034: */
035: public SvnXmlRepositoriesFileHandler(
036: final RepositoriesBuilder repositoriesBuilder) {
037: this .repositoriesBuilder = repositoriesBuilder;
038: }
039:
040: /**
041: * Makes sure the last element received is appropriate.
042: *
043: * @param last
044: * the expected last element.
045: * @throws SAXException
046: * unexpected event.
047: */
048: private void checkLastElement(final String last)
049: throws SAXException {
050: if (!lastElement.equals(last)) {
051: fatalError(FATAL_ERROR_MESSAGE);
052: }
053: }
054:
055: /**
056: * Handles the end of an xml element and redirects to the appropriate end* method.
057: *
058: * @throws SAXException
059: * unexpected event.
060: */
061: public void endElement(final String uri, final String localName,
062: final String qName) throws SAXException {
063: super .endElement(uri, localName, qName);
064: String eName = localName; // element name
065: if ("".equals(eName)) {
066: eName = qName; // namespaceAware = false
067: }
068:
069: if (eName.equals(REPOSITORIES)) {
070: endRepositories();
071: } else if (eName.equals(REPOSITORY)) {
072: endRepository();
073: } else {
074: fatalError(FATAL_ERROR_MESSAGE);
075: }
076: }
077:
078: /**
079: * End of repositories element.
080: *
081: * @throws SAXException
082: * unexpected event.
083: */
084: private void endRepositories() throws SAXException {
085: checkLastElement(REPOSITORIES);
086: lastElement = "";
087: }
088:
089: /**
090: * End of repository element.
091: *
092: * @throws SAXException
093: * unexpected event.
094: */
095: private void endRepository() throws SAXException {
096: checkLastElement(REPOSITORY);
097: lastElement = REPOSITORIES;
098: }
099:
100: /**
101: * Throws a fatal error with the specified message.
102: *
103: * @param message
104: * the reason for the error
105: * @throws SAXException
106: * the error
107: */
108: private void fatalError(final String message) throws SAXException {
109: fatalError(new SAXParseException(message, null));
110: }
111:
112: /**
113: * Handles the start of an xml element and redirects to the appropriate start* method.
114: *
115: * @throws SAXException
116: * unexpected event.
117: */
118: public void startElement(final String uri, final String localName,
119: final String qName, final Attributes attributes)
120: throws SAXException {
121: super .startElement(uri, localName, qName, attributes);
122:
123: String eName = localName; // element name
124: if ("".equals(eName)) {
125: eName = qName; // namespaceAware = false
126: }
127:
128: if (eName.equals(REPOSITORIES)) {
129: startRepositories();
130: } else if (eName.equals(REPOSITORY)) {
131: startRepository(attributes);
132: } else {
133: fatalError(FATAL_ERROR_MESSAGE);
134: }
135: }
136:
137: /**
138: * Handles the start of the document. Initializes the repository builder.
139: *
140: * @throws SAXException
141: * unable to build the root.
142: */
143: private void startRepositories() throws SAXException {
144: checkLastElement("");
145: lastElement = REPOSITORIES;
146: try {
147: repositoriesBuilder.buildRoot();
148: } catch (final ParserConfigurationException e) {
149: fatalError(FATAL_ERROR_MESSAGE);
150: }
151: }
152:
153: /**
154: * Handles start of a repository. Initializes repositories builder for use with repository.
155: *
156: * @param attributes
157: * element's xml attributes.
158: * @throws SAXException
159: * missing some data.
160: */
161: private void startRepository(final Attributes attributes)
162: throws SAXException {
163: checkLastElement(REPOSITORIES);
164: lastElement = REPOSITORY;
165: if (attributes != null && attributes.getValue(UUID) != null
166: && attributes.getValue(FILE) != null) {
167: final String uuid = attributes.getValue(UUID);
168: final String file = attributes.getValue(FILE);
169: repositoriesBuilder.buildRepository(uuid, file);
170: } else {
171: fatalError(FATAL_ERROR_MESSAGE);
172: }
173: }
174:
175: }
|