1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.vfs2.provider.webdav4;
18
19 import java.lang.reflect.Constructor;
20
21 import org.apache.commons.vfs2.FileSystemException;
22 import org.apache.jackrabbit.webdav.DavConstants;
23 import org.apache.jackrabbit.webdav.DavException;
24 import org.apache.jackrabbit.webdav.xml.DomUtil;
25 import org.w3c.dom.Element;
26
27
28
29
30
31
32 public final class ExceptionConverter {
33
34
35
36
37
38
39
40
41 public static FileSystemException generate(final DavException cause) throws FileSystemException {
42 String msg = cause.getMessage();
43 if (cause.hasErrorCondition()) {
44 try {
45 final Element error = cause.toXml(DomUtil.createDocument());
46 if (DomUtil.matches(error, DavException.XML_ERROR, DavConstants.NAMESPACE) && DomUtil.hasChildElement(error, "exception", null)) {
47 final Element exc = DomUtil.getChildElement(error, "exception", null);
48 if (DomUtil.hasChildElement(exc, "message", null)) {
49 msg = DomUtil.getChildText(exc, "message", null);
50 }
51 if (DomUtil.hasChildElement(exc, "class", null)) {
52 final Class<?> cl = Class.forName(DomUtil.getChildText(exc, "class", null));
53 final Constructor<?> excConstr = cl.getConstructor(String.class);
54 final Object o = excConstr.newInstance(msg);
55 if (o instanceof FileSystemException) {
56 return (FileSystemException) o;
57 }
58 if (o instanceof Exception) {
59 return new FileSystemException(msg, (Exception) o);
60 }
61 }
62 }
63 } catch (final Exception e) {
64 throw new FileSystemException(e);
65 }
66 }
67
68 return new FileSystemException(msg);
69 }
70
71
72 private ExceptionConverter() {
73 }
74 }