|
2 | 2 |
|
3 | 3 | import org.fugerit.java.core.lang.helpers.ClassHelper; |
4 | 4 | import org.fugerit.java.core.lang.helpers.reflect.MethodHelper; |
5 | | -import org.slf4j.LoggerFactory; |
6 | 5 | import org.slf4j.Logger; |
| 6 | +import org.slf4j.LoggerFactory; |
7 | 7 | import org.w3c.dom.Attr; |
8 | 8 | import org.w3c.dom.Element; |
9 | 9 | import org.w3c.dom.NamedNodeMap; |
| 10 | +import org.w3c.dom.Node; |
| 11 | +import org.w3c.dom.NodeList; |
10 | 12 |
|
11 | 13 | public class XmlBeanHelper { |
12 | 14 |
|
13 | 15 | private static final Logger logger = LoggerFactory.getLogger( XmlBeanHelper.class ); |
14 | 16 |
|
| 17 | + public static final String MODE_XML_ATTRIBUTES = "bean-xml-attributes"; |
| 18 | + |
| 19 | + public static final String MODE_XML_ELEMENTS = "bean-xml-elements"; |
| 20 | + |
| 21 | + public static final String MODE_XML_FULL = "bean-xml-full"; |
| 22 | + |
| 23 | + public static final String MODE_XML_DEFAULT = MODE_XML_ATTRIBUTES; |
| 24 | + |
15 | 25 | public static <T> T setFromElement( String type, Element config ) throws Exception { |
16 | 26 | @SuppressWarnings("unchecked") |
17 | 27 | T bean = (T) ClassHelper.newInstance( type ); |
18 | 28 | setFromElement(bean, config); |
19 | 29 | return bean; |
20 | 30 | } |
21 | 31 |
|
| 32 | + public static <T> T setFromElement( String type, Element config, String mode ) throws Exception { |
| 33 | + @SuppressWarnings("unchecked") |
| 34 | + T bean = (T) ClassHelper.newInstance( type ); |
| 35 | + setFromElement(bean, config, mode); |
| 36 | + return bean; |
| 37 | + } |
| 38 | + |
22 | 39 | public static <T> void setFromElement( T bean, Element config ) throws Exception { |
23 | | - NamedNodeMap atts = config.getAttributes(); |
24 | | - for ( int ak=0; ak<atts.getLength(); ak++ ) { |
25 | | - Attr att = (Attr)atts.item( ak ); |
26 | | - String key = att.getName(); |
27 | | - String value = att.getValue(); |
28 | | - MethodHelper.invokeSetter( bean , key, String.class, value ); |
| 40 | + setFromElement( bean, config, MODE_XML_DEFAULT ); |
| 41 | + } |
| 42 | + |
| 43 | + public static <T> void setFromElement( T bean, Element config, String mode ) throws Exception { |
| 44 | + if ( MODE_XML_ATTRIBUTES.equalsIgnoreCase( mode ) || MODE_XML_FULL.equalsIgnoreCase( mode ) ) { |
| 45 | + setFromElementAtts(bean, config); |
| 46 | + } |
| 47 | + if ( MODE_XML_ELEMENTS.equalsIgnoreCase( mode ) || MODE_XML_FULL.equalsIgnoreCase( mode ) ) { |
| 48 | + setFromElementKids(bean, config); |
29 | 49 | } |
30 | 50 | if ( bean instanceof TextValueType ) { |
31 | 51 | ((TextValueType) bean ).setTextValue( config.getTextContent() ); |
32 | 52 | } |
33 | 53 | } |
34 | 54 |
|
35 | | - public static <T> void setFromElementSafe( T bean, Element config ) { |
| 55 | + public static <T> void setFromElementSafe( T bean, Element config, String mode ) { |
36 | 56 | try { |
37 | | - setFromElement( bean , config); |
| 57 | + setFromElement( bean , config, mode ); |
38 | 58 | } catch (Exception e) { |
39 | 59 | logger.warn( "Cannot set all parameters from bean, usually safe to ignore : "+e ); |
40 | 60 | } |
41 | 61 | } |
42 | 62 |
|
| 63 | + public static <T> void setFromElementSafe( T bean, Element config ) { |
| 64 | + setFromElementSafe(bean, config, MODE_XML_DEFAULT ); |
| 65 | + } |
| 66 | + |
| 67 | + public static <T> void setFromElementAtts( T bean, Element config ) throws Exception { |
| 68 | + NamedNodeMap atts = config.getAttributes(); |
| 69 | + for ( int ak=0; ak<atts.getLength(); ak++ ) { |
| 70 | + Attr att = (Attr)atts.item( ak ); |
| 71 | + String key = att.getName(); |
| 72 | + String value = att.getValue(); |
| 73 | + MethodHelper.invokeSetter( bean , key, String.class, value ); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + public static <T> void setFromElementKids( T bean, Element config ) throws Exception { |
| 78 | + NodeList kids = config.getChildNodes(); |
| 79 | + for ( int ak=0; ak<kids.getLength(); ak++ ) { |
| 80 | + Node current = kids.item( ak ); |
| 81 | + if ( current.getNodeType() == Node.ELEMENT_NODE ) { |
| 82 | + Element tag = (Element)current; |
| 83 | + String key = tag.getTagName(); |
| 84 | + MethodHelper.invokeSetter( bean , key, String.class, tag.getTextContent() ); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
43 | 89 | } |
0 commit comments