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.Iterator;
015:
016: import org.tmatesoft.svn.core.ISVNLogEntryHandler;
017: import org.tmatesoft.svn.core.SVNErrorCode;
018: import org.tmatesoft.svn.core.SVNErrorMessage;
019: import org.tmatesoft.svn.core.SVNException;
020: import org.tmatesoft.svn.core.SVNLogEntry;
021: import org.tmatesoft.svn.core.SVNLogEntryPath;
022: import org.tmatesoft.svn.core.internal.util.SVNTimeUtil;
023: import org.tmatesoft.svn.core.internal.wc.SVNErrorManager;
024: import org.tmatesoft.svn.util.ISVNDebugLog;
025: import org.xml.sax.ContentHandler;
026: import org.xml.sax.SAXException;
027:
028: /**
029: * This log handler implementation writes xml formatted information
030: * about the log entries it's passed to a specified <b>ContentHandler</b>.
031: *
032: * @version 1.1.1
033: * @author TMate Software Ltd.
034: */
035: public class SVNXMLLogHandler extends AbstractXMLHandler implements
036: ISVNLogEntryHandler {
037:
038: public static final String COPYFROM_REV_ATTR = "copyfrom-rev";
039: public static final String COPYFROM_PATH_ATTR = "copyfrom-path";
040: public static final String ACTION_ATTR = "action";
041: public static final String REVISION_ATTR = "revision";
042: public static final String MSG_TAG = "msg";
043: public static final String PATH_TAG = "path";
044: public static final String PATHS_TAG = "paths";
045: public static final String DATE_TAG = "date";
046: public static final String AUTHOR_TAG = "author";
047: public static final String LOGENTRY_TAG = "logentry";
048: public static final String LOG_TAG = "log";
049:
050: /**
051: * Creates a new log handler.
052: *
053: * @param contentHandler a <b>ContentHandler</b> to form
054: * an XML tree
055: */
056: public SVNXMLLogHandler(ContentHandler contentHandler) {
057: this (contentHandler, null);
058: }
059:
060: /**
061: * Creates a new log handler.
062: *
063: * @param contentHandler a <b>ContentHandler</b> to form
064: * an XML tree
065: * @param log a debug logger
066: */
067: public SVNXMLLogHandler(ContentHandler contentHandler,
068: ISVNDebugLog log) {
069: super (contentHandler, log);
070: }
071:
072: /**
073: * Returns the header name specific for a log handler.
074: *
075: * @return {@link #LOG_TAG} string
076: */
077: public String getHeaderName() {
078: return LOG_TAG;
079: }
080:
081: public void handleLogEntry(SVNLogEntry logEntry)
082: throws SVNException {
083: try {
084: sendToHandler(logEntry);
085: } catch (SAXException e) {
086: SVNErrorMessage err = SVNErrorMessage
087: .create(SVNErrorCode.XML_MALFORMED, e
088: .getLocalizedMessage());
089: SVNErrorManager.error(err, e);
090: }
091: }
092:
093: private void sendToHandler(SVNLogEntry logEntry)
094: throws SAXException {
095: if (logEntry.getRevision() == 0
096: && logEntry.getMessage() == null) {
097: return;
098: }
099: addAttribute(REVISION_ATTR, logEntry.getRevision() + "");
100: openTag(LOGENTRY_TAG);
101: if (logEntry.getAuthor() != null) {
102: addTag(AUTHOR_TAG, logEntry.getAuthor());
103: }
104: if (logEntry.getDate() != null
105: && logEntry.getDate().getTime() != 0) {
106: addTag(DATE_TAG, SVNTimeUtil.formatDate(logEntry.getDate()));
107: }
108: if (logEntry.getChangedPaths() != null
109: && !logEntry.getChangedPaths().isEmpty()) {
110: openTag(PATHS_TAG);
111: for (Iterator paths = logEntry.getChangedPaths().keySet()
112: .iterator(); paths.hasNext();) {
113: String key = (String) paths.next();
114: SVNLogEntryPath path = (SVNLogEntryPath) logEntry
115: .getChangedPaths().get(key);
116: addAttribute(ACTION_ATTR, path.getType() + "");
117: if (path.getCopyPath() != null) {
118: addAttribute(COPYFROM_PATH_ATTR, path.getCopyPath());
119: addAttribute(COPYFROM_REV_ATTR, path
120: .getCopyRevision()
121: + "");
122: }
123: addTag(PATH_TAG, path.getPath());
124: }
125: closeTag(PATHS_TAG);
126: }
127: String message = logEntry.getMessage();
128: message = message == null ? "" : message;
129: addTag(MSG_TAG, message);
130: closeTag(LOGENTRY_TAG);
131: }
132:
133: }
|