001: /*
002: * ====================================================================
003: * Copyright (c) 2004-2008 TMate Software Ltd. All rights reserved.
004: *
005: * This software is licensed as described in the file COPYING, which
006: * you should have received as part of this distribution. The terms
007: * are also available at http://svnkit.com/license.html
008: * If newer versions of this license are posted there, you may use a
009: * newer version instead, at your option.
010: * ====================================================================
011: */
012: package org.tmatesoft.svn.core.wc.xml;
013:
014: import java.util.Date;
015:
016: import org.tmatesoft.svn.core.SVNErrorCode;
017: import org.tmatesoft.svn.core.SVNErrorMessage;
018: import org.tmatesoft.svn.core.SVNException;
019: import org.tmatesoft.svn.core.internal.util.SVNTimeUtil;
020: import org.tmatesoft.svn.core.internal.wc.SVNErrorManager;
021: import org.tmatesoft.svn.core.wc.ISVNAnnotateHandler;
022: import org.tmatesoft.svn.util.ISVNDebugLog;
023: import org.xml.sax.ContentHandler;
024: import org.xml.sax.SAXException;
025:
026: /**
027: * This is an implementation of the <b>ISVNAnnotateHandler</b> interface
028: * that writes XML formatted annotation information to a specified
029: * <b>ContentHandler</b>.
030: *
031: * @version 1.1.1
032: * @author TMate Software Ltd.
033: */
034: public class SVNXMLAnnotateHandler extends AbstractXMLHandler implements
035: ISVNAnnotateHandler {
036:
037: public static final String PATH_ATTR = "path";
038: public static final String REVISION_ATTR = "revision";
039: public static final String DATE_TAG = "date";
040: public static final String AUTHOR_TAG = "author";
041: public static final String COMMIT_TAG = "commit";
042: public static final String ENTRY_TAG = "entry";
043: public static final String LINE_NUMBER_TAG = "line-number";
044: public static final String TARGET_TAG = "target";
045: public static final String BLAME_TAG = "blame";
046:
047: private long myLineNumber;
048:
049: /**
050: * Creates a new annotation handler.
051: *
052: * @param contentHandler a <b>ContentHandler</b> to form
053: * an XML tree
054: */
055: public SVNXMLAnnotateHandler(ContentHandler contentHandler) {
056: this (contentHandler, null);
057: }
058:
059: /**
060: * Creates a new annotation handler.
061: *
062: * @param contentHandler a <b>ContentHandler</b> to form
063: * an XML tree
064: * @param log a debug logger
065: */
066: public SVNXMLAnnotateHandler(ContentHandler contentHandler,
067: ISVNDebugLog log) {
068: super (contentHandler, log);
069: }
070:
071: protected String getHeaderName() {
072: return BLAME_TAG;
073: }
074:
075: /**
076: * Begins an XML tree with the target path/URL for which
077: * annotating is run.
078: *
079: * @param pathOrURL a target file WC path or URL
080: */
081: public void startTarget(String pathOrURL) {
082: myLineNumber = 1;
083: try {
084: addAttribute(PATH_ATTR, pathOrURL);
085: openTag(TARGET_TAG);
086: } catch (SAXException e) {
087: getDebugLog().error(e);
088: }
089: }
090:
091: /**
092: * Closes the formatted XML output.
093: *
094: */
095: public void endTarget() {
096: myLineNumber = 1;
097: try {
098: closeTag(TARGET_TAG);
099: } catch (SAXException e) {
100: getDebugLog().error(e);
101: }
102: }
103:
104: public void handleLine(Date date, long revision, String author,
105: String line) throws SVNException {
106: try {
107: addAttribute(LINE_NUMBER_TAG, myLineNumber + "");
108: openTag(ENTRY_TAG);
109: if (revision >= 0) {
110: addAttribute(REVISION_ATTR, revision + "");
111: openTag(COMMIT_TAG);
112: addTag(AUTHOR_TAG, author);
113: addTag(DATE_TAG, SVNTimeUtil.formatDate(date));
114: closeTag(COMMIT_TAG);
115: }
116: closeTag(ENTRY_TAG);
117: } catch (SAXException e) {
118: getDebugLog().error(e);
119: SVNErrorMessage err = SVNErrorMessage
120: .create(SVNErrorCode.XML_MALFORMED, e
121: .getLocalizedMessage());
122: SVNErrorManager.error(err, e);
123: } finally {
124: myLineNumber++;
125: }
126: }
127: }
|