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:
013: package org.tmatesoft.svn.core.internal.io.dav.handlers;
014:
015: import java.util.HashMap;
016: import java.util.Map;
017: import java.util.Stack;
018:
019: import org.tmatesoft.svn.core.SVNErrorCode;
020: import org.tmatesoft.svn.core.SVNErrorMessage;
021: import org.tmatesoft.svn.core.SVNException;
022: import org.tmatesoft.svn.core.internal.io.dav.DAVElement;
023: import org.tmatesoft.svn.core.internal.wc.SVNErrorManager;
024: import org.tmatesoft.svn.util.SVNDebugLog;
025: import org.xml.sax.Attributes;
026: import org.xml.sax.SAXException;
027: import org.xml.sax.helpers.DefaultHandler;
028:
029: /**
030: * @version 1.1.1
031: * @author TMate Software Ltd.
032: */
033: public abstract class BasicDAVHandler extends DefaultHandler {
034:
035: private static final Object ROOT = new Object();
036:
037: private Map myPrefixesMap;
038: private StringBuffer myCDATA;
039: private Stack myParent;
040: private byte[] myDeltaBuffer;
041:
042: protected BasicDAVHandler() {
043: myPrefixesMap = new HashMap();
044: myParent = new Stack();
045: }
046:
047: protected void init() {
048: myPrefixesMap.clear();
049: myParent.clear();
050: myParent.push(ROOT);
051: }
052:
053: public void startElement(String uri, String localName,
054: String qName, Attributes attributes) throws SAXException {
055: DAVElement element = getDAVElement(qName);
056: try {
057: startElement(getParent(), element, attributes);
058: } catch (SVNException e) {
059: SVNDebugLog.getDefaultLog().info(e);
060: throw new SAXException(e);
061: }
062: myParent.push(element);
063: myCDATA = new StringBuffer();
064: }
065:
066: public void endElement(String uri, String localName, String qName)
067: throws SAXException {
068: myParent.pop();
069: DAVElement element = getDAVElement(qName);
070: try {
071: endElement(getParent(), element, myCDATA);
072: } catch (SVNException e) {
073: SVNDebugLog.getDefaultLog().info(e);
074: throw new SAXException(e);
075: }
076: myCDATA = null;
077: }
078:
079: public void characters(char[] ch, int start, int length)
080: throws SAXException {
081: if (myCDATA != null) {
082: myCDATA.append(ch, start, length);
083: }
084: }
085:
086: public void startPrefixMapping(String prefix, String uri)
087: throws SAXException {
088: Stack mappings = (Stack) myPrefixesMap.get(prefix);
089: if (mappings == null) {
090: mappings = new Stack();
091: myPrefixesMap.put(prefix, mappings);
092: }
093: mappings.push(uri);
094: }
095:
096: public void endPrefixMapping(String prefix) throws SAXException {
097: Stack mappings = (Stack) myPrefixesMap.get(prefix);
098: if (mappings != null) {
099: mappings.pop();
100: }
101: }
102:
103: protected abstract void startElement(DAVElement parent,
104: DAVElement element, Attributes attrs) throws SVNException;
105:
106: protected abstract void endElement(DAVElement parent,
107: DAVElement element, StringBuffer cdata) throws SVNException;
108:
109: protected void invalidXML() throws SVNException {
110: SVNErrorManager.error(SVNErrorMessage.create(
111: SVNErrorCode.XML_MALFORMED, "Malformed XML"));
112:
113: }
114:
115: private DAVElement getParent() {
116: Object parent = myParent.peek();
117: if (parent == ROOT) {
118: return null;
119: }
120: return (DAVElement) parent;
121: }
122:
123: private DAVElement getDAVElement(String qName) {
124: String prefix = null;
125: int index = qName.indexOf(':');
126: if (index >= 0) {
127: prefix = qName.substring(0, index);
128: Stack prefixes = (Stack) myPrefixesMap.get(prefix);
129: if (prefixes != null && !prefixes.isEmpty()) {
130: prefix = (String) prefixes.peek();
131: }
132: qName = qName.substring(index + 1);
133: }
134: return DAVElement.getElement(prefix, qName);
135: }
136:
137: protected byte[] allocateBuffer(int length) {
138: if (myDeltaBuffer == null || myDeltaBuffer.length < length) {
139: myDeltaBuffer = new byte[length * 3 / 2];
140: }
141: return myDeltaBuffer;
142: }
143:
144: }
|