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 org.tmatesoft.svn.util.ISVNDebugLog;
015: import org.tmatesoft.svn.util.SVNDebugLog;
016: import org.xml.sax.ContentHandler;
017: import org.xml.sax.Locator;
018: import org.xml.sax.SAXException;
019: import org.xml.sax.helpers.AttributesImpl;
020:
021: /**
022: * <b>AbstractXMLLogger</b> is a basic XML formatter for all
023: * XML handler classes which are provided in this package. All
024: * XML output is written to a specified <b>ContentHandler</b>.
025: *
026: * @version 1.1.1
027: * @author TMate Software Ltd.
028: */
029: public abstract class AbstractXMLHandler implements Locator {
030:
031: private AttributesImpl mySharedAttributes;
032: private ContentHandler myHandler;
033: private ISVNDebugLog myLog;
034:
035: protected AbstractXMLHandler(ContentHandler contentHandler,
036: ISVNDebugLog log) {
037: myHandler = contentHandler;
038: myLog = log == null ? SVNDebugLog.getDefaultLog() : log;
039: }
040:
041: protected ISVNDebugLog getDebugLog() {
042: return myLog;
043: }
044:
045: /**
046: * Starts logging.
047: *
048: */
049: public void startDocument() {
050: try {
051: getHandler().setDocumentLocator(this );
052: getHandler().startDocument();
053: openTag(getHeaderName());
054: } catch (SAXException e) {
055: }
056: }
057:
058: /**
059: * Stops logging.
060: *
061: */
062: public void endDocument() {
063: try {
064: closeTag(getHeaderName());
065: getHandler().endDocument();
066: } catch (SAXException e) {
067: }
068: }
069:
070: private ContentHandler getHandler() {
071: return myHandler;
072: }
073:
074: protected abstract String getHeaderName();
075:
076: protected void openTag(String name) throws SAXException {
077: if (mySharedAttributes == null) {
078: mySharedAttributes = new AttributesImpl();
079: }
080: getHandler().startElement("", "", name, mySharedAttributes);
081: mySharedAttributes.clear();
082: }
083:
084: protected void closeTag(String name) throws SAXException {
085: getHandler().endElement("", "", name);
086: }
087:
088: protected void addTag(String tagName, String value)
089: throws SAXException {
090: if (mySharedAttributes == null) {
091: mySharedAttributes = new AttributesImpl();
092: }
093: getHandler().startElement("", "", tagName, mySharedAttributes);
094: mySharedAttributes.clear();
095: value = value == null ? "" : value;
096: getHandler().characters(value.toCharArray(), 0, value.length());
097: getHandler().endElement("", "", tagName);
098: }
099:
100: protected void addAttribute(String name, String value) {
101: if (mySharedAttributes == null) {
102: mySharedAttributes = new AttributesImpl();
103: }
104: mySharedAttributes.addAttribute("", "", name, "CDATA", value);
105: }
106:
107: public int getColumnNumber() {
108: return 0;
109: }
110:
111: public int getLineNumber() {
112: return 0;
113: }
114:
115: public String getPublicId() {
116: return null;
117: }
118:
119: public String getSystemId() {
120: return null;
121: }
122: }
|