KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > convert > ConverterImpl


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.wcf.convert;
14
15 import java.lang.reflect.InvocationTargetException JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import org.apache.log4j.Logger;
22 import org.w3c.dom.Document JavaDoc;
23 import org.w3c.dom.Element JavaDoc;
24 import org.w3c.dom.Node JavaDoc;
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 /**
35  * implements the converter
36  *
37  * @author av
38  */

39 public class ConverterImpl implements Converter {
40
41   HashMap JavaDoc handlers = new HashMap JavaDoc();
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   /* ---------------------- convert from request to DOM Tree and bean ----------------------- */
60
61   public void validate(Map JavaDoc params, Map JavaDoc fileParams, Node JavaDoc node, Object JavaDoc bean) throws ConvertException, FormatException {
62     try {
63       formatException = null;
64       Document JavaDoc root = XmlUtils.getDocument(node);
65       traverse(node, params, fileParams, bean);
66       if (formatException != null)
67         throw formatException;
68     } catch (IllegalAccessException JavaDoc e) {
69       logger.error("?", e);
70       throw new SoftException(e);
71     } catch (NoSuchMethodException JavaDoc e) {
72       logger.error("?", e);
73       throw new SoftException(e);
74     }
75   }
76
77   void traverse(Node JavaDoc node, Map JavaDoc params, Map JavaDoc fileParams, Object JavaDoc bean)
78     throws ConvertException, IllegalAccessException JavaDoc, NoSuchMethodException JavaDoc {
79     if (node.getNodeType() == Node.ELEMENT_NODE) {
80       String JavaDoc name = node.getNodeName();
81       NodeConverter handler = (NodeConverter) handlers.get(name);
82       Element JavaDoc elem = (Element JavaDoc)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 JavaDoc e) {
93           Throwable JavaDoc 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 JavaDoc) {
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 JavaDoc children = DomUtils.getChildElements(node);
109     for (Iterator JavaDoc it = children.iterator(); it.hasNext();) {
110       Element JavaDoc child = (Element JavaDoc) it.next();
111       traverse(child, params, fileParams, bean);
112     }
113   }
114
115   private boolean ignoreAll(Element JavaDoc 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   /* ---------------------- convert from bean to DOM Tree ----------------------- */
124
125   public void revert(Object JavaDoc bean, Node JavaDoc node) throws ConvertException {
126     try {
127       formatException = null;
128       traverse(node, bean);
129       if (formatException != null)
130         throw formatException;
131     } catch (IllegalAccessException JavaDoc e) {
132       throw new SoftException(e);
133     } catch (NoSuchMethodException JavaDoc e) {
134       throw new SoftException(e);
135     } catch (InvocationTargetException JavaDoc e) {
136       throw new SoftException(e);
137     }
138   }
139
140   void traverse(Node JavaDoc node, Object JavaDoc bean)
141     throws ConvertException, IllegalAccessException JavaDoc, NoSuchMethodException JavaDoc, InvocationTargetException JavaDoc {
142     if (node.getNodeType() == Node.ELEMENT_NODE) {
143       String JavaDoc name = node.getNodeName();
144       Element JavaDoc elem = (Element JavaDoc)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 JavaDoc children = DomUtils.getChildElements(node);
160     for (Iterator JavaDoc it = children.iterator(); it.hasNext();) {
161       Element JavaDoc child = (Element JavaDoc) it.next();
162       traverse(child, bean);
163     }
164
165   }
166
167 }
168
Popular Tags