1 package org.testng.xml.dom;
2 
3 import org.testng.collections.Lists;
4 import org.w3c.dom.Element;
5 import org.w3c.dom.NodeList;
6 
7 import java.lang.annotation.Annotation;
8 import java.util.List;
9 
10 public class Wrapper {
11 
12   private OnElement m_onElement;
13   private OnElementList m_onElementList;
14   private Tag m_tag;
15   private TagContent m_tagContent;
16   private Object m_bean;
17 
Wrapper(Annotation a, Object bean)18   public Wrapper(Annotation a, Object bean) {
19     m_bean = bean;
20     if (a instanceof OnElement) m_onElement = (OnElement) a;
21     else if (a instanceof OnElementList) m_onElementList = (OnElementList) a;
22     else if (a instanceof Tag) m_tag = (Tag) a;
23     else if (a instanceof TagContent) m_tagContent = (TagContent) a;
24     else throw new RuntimeException("Illegal annotation " + a);
25   }
26 
getTagName()27   public String getTagName() {
28     if (m_onElement != null) return m_onElement.tag();
29     else if (m_onElementList != null) return m_onElementList.tag();
30     else return m_tag.name();
31   }
32 
getParameters(Element element)33   public List<Object[]> getParameters(Element element) {
34     List<Object[]> allParameters = Lists.newArrayList();
35     if (m_onElement != null) {
36       List<Object> result = Lists.newArrayList();
37       for (String attributeName : m_onElement.attributes()) {
38         result.add(element.getAttribute(attributeName));
39       }
40       allParameters.add(result.toArray());
41     } else if (m_tag != null) {
42       List<Object> result = Lists.newArrayList();
43       result.add(m_bean);
44       allParameters.add(result.toArray());
45     } else {
46       NodeList childNodes = element.getChildNodes();
47       for (int i = 0; i < childNodes.getLength(); i++) {
48         if (childNodes.item(i).hasAttributes()) {
49           Element item = (Element) childNodes.item(i);
50           List<Object> result = Lists.newArrayList();
51           for (String attributeName : m_onElementList.attributes()) {
52             result.add(item.getAttribute(attributeName));
53           }
54           allParameters.add(result.toArray());
55         }
56       }
57     }
58 
59     return allParameters;
60   }
61 }
62