001: // DAVBody.java
002: // $Id: DAVBody.java,v 1.7 2000/10/13 13:45:06 bmahe Exp $
003: // (c) COPYRIGHT MIT, INRIA and Keio, 2000.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005: package org.w3c.www.webdav.xml;
006:
007: import java.io.InputStream;
008: import java.io.IOException;
009:
010: import org.w3c.dom.DOMException;
011: import org.w3c.dom.DOMImplementation;
012: import org.w3c.dom.Document;
013: import org.w3c.dom.Element;
014: import org.w3c.dom.Node;
015:
016: import org.xml.sax.ErrorHandler;
017: import org.xml.sax.InputSource;
018: import org.xml.sax.SAXException;
019: import org.xml.sax.SAXParseException;
020: import org.xml.sax.SAXNotRecognizedException;
021: import org.xml.sax.SAXNotSupportedException;
022:
023: import org.apache.xerces.parsers.DOMParser;
024: import org.apache.xerces.dom.DocumentImpl;
025:
026: import org.w3c.www.webdav.WEBDAV;
027:
028: /**
029: * @version $Revision: 1.7 $
030: * @author Benoît Mahé (bmahe@w3.org)
031: */
032: public class DAVBody implements ErrorHandler {
033:
034: protected static DOMParser parser = null;
035:
036: protected static DOMImplementation domimpl = null;
037:
038: private static boolean setValidation = false; //defaults
039: private static boolean setNameSpaces = true;
040: private static boolean setSchemaSupport = true;
041: private static boolean setDeferredDOM = true;
042:
043: static {
044: parser = new DOMParser();
045: domimpl = (new DocumentImpl()).getImplementation();
046: try {
047: parser
048: .setFeature(
049: "http://apache.org/xml/features/dom/defer-node-expansion",
050: setDeferredDOM);
051: parser.setFeature("http://xml.org/sax/features/validation",
052: setValidation);
053: parser.setFeature("http://xml.org/sax/features/namespaces",
054: setNameSpaces);
055: parser.setFeature(
056: "http://apache.org/xml/features/validation/schema",
057: setSchemaSupport);
058: } catch (Exception ex) {
059: ex.printStackTrace(System.err);
060: }
061: }
062:
063: protected Document document = null;
064:
065: public static synchronized Document getDocument(InputStream in,
066: ErrorHandler handler) throws IOException, SAXException {
067: parser.setErrorHandler(handler);
068: InputSource source = new InputSource(in);
069: parser.parse(source);
070: return parser.getDocument();
071: }
072:
073: //
074: // XML part
075: //
076:
077: public DAVMultiStatus getMultiStatus() {
078: Node n = DAVNode.getDAVNode(document, DAVNode.MULTISTATUS_NODE);
079: if (n != null) {
080: return new DAVMultiStatus((Element) n);
081: }
082: return null;
083: }
084:
085: public DAVPropertyBehavior getPropertyBehavior() {
086: Node n = DAVNode.getDAVNode(document,
087: DAVNode.PROPERTYBEHAVIOR_NODE);
088: if (n != null) {
089: return new DAVPropertyBehavior((Element) n);
090: }
091: return null;
092: }
093:
094: public DAVPropertyUpdate getPropertyUpdate() {
095: Node n = DAVNode.getDAVNode(document,
096: DAVNode.PROPERTYUPDATE_NODE);
097: if (n != null) {
098: return new DAVPropertyUpdate((Element) n);
099: }
100: return null;
101: }
102:
103: public DAVPropFind getPropFind() {
104: Node n = DAVNode.getDAVNode(document, DAVNode.PROPFIND_NODE);
105: if (n != null) {
106: return new DAVPropFind((Element) n);
107: }
108: return null;
109: }
110:
111: public DAVActiveLock getActiveLock() {
112: Node n = DAVNode.getDAVNode(document, DAVNode.ACTIVELOCK_NODE);
113: if (n != null) {
114: return new DAVActiveLock((Element) n);
115: }
116: return null;
117: }
118:
119: public DAVLockInfo getLockInfo() {
120: Node n = DAVNode.getDAVNode(document, DAVNode.LOCKINFO_NODE);
121: if (n != null) {
122: return new DAVLockInfo((Element) n);
123: }
124: return null;
125: }
126:
127: //
128: // creation
129: //
130:
131: public static Document createDocument(String root) {
132: Document newdoc = domimpl.createDocument(WEBDAV.NAMESPACE_URI,
133: WEBDAV.NAMESPACE_PREFIX + ":" + root, null);
134: Element rootel = newdoc.getDocumentElement();
135: try {
136: rootel.setAttribute("xmlns:" + WEBDAV.NAMESPACE_PREFIX,
137: WEBDAV.NAMESPACE_URI);
138: } catch (DOMException ex) {
139: ex.printStackTrace();
140: }
141: return newdoc;
142: }
143:
144: public static Document createDocumentNS(String root, String ns,
145: String prefix) {
146: Document newdoc = createDocument(root);
147: Element rootel = newdoc.getDocumentElement();
148: try {
149: rootel.setAttribute("xmlns:" + prefix, ns);
150: } catch (DOMException ex) {
151: ex.printStackTrace();
152: }
153: return newdoc;
154: }
155:
156: // Error Handler
157:
158: /**
159: * Warning
160: */
161: public void warning(SAXParseException ex) {
162: System.err.println("[Warning] " + getLocationString(ex) + ": "
163: + ex.getMessage());
164: }
165:
166: /**
167: * Error.
168: */
169: public void error(SAXParseException ex) {
170: System.err.println("[Error] " + getLocationString(ex) + ": "
171: + ex.getMessage());
172: }
173:
174: /**
175: * Fatal error.
176: */
177: public void fatalError(SAXParseException ex) throws SAXException {
178: System.err.println("[Fatal Error] " + getLocationString(ex)
179: + ": " + ex.getMessage());
180: throw ex;
181: }
182:
183: //
184: // Private methods
185: //
186:
187: /**
188: * Returns a string of the location.
189: */
190: private String getLocationString(SAXParseException ex) {
191: StringBuffer str = new StringBuffer();
192:
193: String systemId = ex.getSystemId();
194: if (systemId != null) {
195: int index = systemId.lastIndexOf('/');
196: if (index != -1)
197: systemId = systemId.substring(index + 1);
198: str.append(systemId);
199: }
200: str.append(':');
201: str.append(ex.getLineNumber());
202: str.append(':');
203: str.append(ex.getColumnNumber());
204:
205: return str.toString();
206:
207: }
208:
209: /**
210: * Constructor
211: */
212: public DAVBody(InputStream in) throws IOException, SAXException {
213: this.document = getDocument(in, this);
214: }
215:
216: }
|