1 13 package com.tonbeller.wcf.convert; 14 15 import java.lang.reflect.InvocationTargetException ; 16 import java.util.HashMap ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 import java.util.Map ; 20 21 import org.apache.log4j.Logger; 22 import org.w3c.dom.Document ; 23 import org.w3c.dom.Element ; 24 import org.w3c.dom.Node ; 25 26 import com.tonbeller.wcf.format.FormatException; 27 import com.tonbeller.wcf.format.Formatter; 28 import com.tonbeller.wcf.ui.XoplonCtrl; 29 import com.tonbeller.wcf.utils.DomUtils; 30 import com.tonbeller.wcf.utils.SoftException; 31 import com.tonbeller.wcf.utils.XmlUtils; 32 import com.tonbeller.wcf.utils.XoplonNS; 33 34 39 public class ConverterImpl implements Converter { 40 41 HashMap handlers = new HashMap (); 42 Formatter formatter; 43 FormatException formatException = null; 44 private static Logger logger = Logger.getLogger(ConverterImpl.class); 45 46 47 public Formatter getFormatter() { 48 return formatter; 49 } 50 51 public void setFormatter(Formatter formatter) { 52 this.formatter = formatter; 53 } 54 55 public void addHandler(NodeConverter nc) { 56 handlers.put(nc.getElementName(), nc); 57 } 58 59 60 61 public void validate(Map params, Map fileParams, Node node, Object bean) throws ConvertException, FormatException { 62 try { 63 formatException = null; 64 Document root = XmlUtils.getDocument(node); 65 traverse(node, params, fileParams, bean); 66 if (formatException != null) 67 throw formatException; 68 } catch (IllegalAccessException e) { 69 logger.error("?", e); 70 throw new SoftException(e); 71 } catch (NoSuchMethodException e) { 72 logger.error("?", e); 73 throw new SoftException(e); 74 } 75 } 76 77 void traverse(Node node, Map params, Map fileParams, Object bean) 78 throws ConvertException, IllegalAccessException , NoSuchMethodException { 79 if (node.getNodeType() == Node.ELEMENT_NODE) { 80 String name = node.getNodeName(); 81 NodeConverter handler = (NodeConverter) handlers.get(name); 82 Element elem = (Element )node; 83 if (ignoreAll(elem)) 84 return; 85 86 if (handler != null) { 87 try { 88 handler.convert(formatter, params, fileParams, elem, bean); 89 } catch (FormatException e) { 90 formatException = e; 91 XoplonNS.setAttribute(elem, "error", formatException.getMessage()); 92 } catch (InvocationTargetException e) { 93 Throwable te = e.getTargetException(); 94 if (te instanceof FormatException) { 95 formatException = (FormatException) te; 96 XoplonNS.setAttribute(elem, "error", formatException.getMessage()); 97 } else if (te instanceof IllegalArgumentException ) { 98 formatException = new FormatException(te.getMessage()); 99 XoplonNS.setAttribute(elem, "error", formatException.getMessage()); 100 } 101 else { 102 throw new SoftException(te); 103 } 104 } 105 } 106 } 107 108 List children = DomUtils.getChildElements(node); 109 for (Iterator it = children.iterator(); it.hasNext();) { 110 Element child = (Element ) it.next(); 111 traverse(child, params, fileParams, bean); 112 } 113 } 114 115 private boolean ignoreAll(Element elem) { 116 if (XoplonCtrl.isHidden(elem)) 117 return true; 118 if ("true".equals(elem.getAttribute("children-hidden"))) 119 return true; 120 return false; 121 } 122 123 124 125 public void revert(Object bean, Node node) throws ConvertException { 126 try { 127 formatException = null; 128 traverse(node, bean); 129 if (formatException != null) 130 throw formatException; 131 } catch (IllegalAccessException e) { 132 throw new SoftException(e); 133 } catch (NoSuchMethodException e) { 134 throw new SoftException(e); 135 } catch (InvocationTargetException e) { 136 throw new SoftException(e); 137 } 138 } 139 140 void traverse(Node node, Object bean) 141 throws ConvertException, IllegalAccessException , NoSuchMethodException , InvocationTargetException { 142 if (node.getNodeType() == Node.ELEMENT_NODE) { 143 String name = node.getNodeName(); 144 Element elem = (Element )node; 145 if (ignoreAll(elem)) 146 return; 147 148 NodeConverter handler = (NodeConverter) handlers.get(name); 149 if (handler != null) { 150 DomUtils.removeAttribute(elem, "error"); 151 try { 152 handler.convert(formatter, bean, elem); 153 } catch (FormatException e) { 154 formatException = e; 155 } 156 } 157 } 158 159 List children = DomUtils.getChildElements(node); 160 for (Iterator it = children.iterator(); it.hasNext();) { 161 Element child = (Element ) it.next(); 162 traverse(child, bean); 163 } 164 165 } 166 167 } 168 | Popular Tags |