Skip to content

Commit 92de45c

Browse files
GenericListCatalogConfig can now read xml kids as bean properties
1 parent 7589c4b commit 92de45c

File tree

2 files changed

+68
-10
lines changed

2 files changed

+68
-10
lines changed

fj-core/src/main/java/org/fugerit/java/core/cfg/xml/GenericListCatalogConfig.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,16 @@ public GenericListCatalogConfig( String attTagDataList, String attTagData ) {
153153
* Type to use for the for the elements (data)
154154
*/
155155
public static final String ATT_TYPE = "type";
156+
157+
/**
158+
* Bean population mode
159+
*/
160+
public static final String ATT_BEAN_MODE = "bean-mode";
161+
162+
/**
163+
* Default, bean population mode (by default read only attributes)
164+
*/
165+
public static final String ATT_BEAN_MODE_DEFAULT = XmlBeanHelper.MODE_XML_ATTRIBUTES;
156166

157167
/**
158168
* Type to use for the config containers (data-list)
@@ -253,6 +263,8 @@ public void configure(Element tag) throws ConfigException {
253263

254264
String listType = this.getGeneralProps().getProperty( ATT_LIST_TYPE );
255265

266+
String beanMode = this.getGeneralProps().getProperty( ATT_BEAN_MODE, ATT_BEAN_MODE_DEFAULT );
267+
256268
String type = this.getGeneralProps().getProperty( ATT_TYPE );
257269
if ( StringUtils.isEmpty( type ) ) {
258270
throw new ConfigException( "No type defined" );
@@ -319,7 +331,7 @@ public void configure(Element tag) throws ConfigException {
319331
}
320332
} else {
321333
try {
322-
T t = XmlBeanHelper.setFromElement(type, currentSchemaTag );
334+
T t = XmlBeanHelper.setFromElement( type, currentSchemaTag, beanMode );
323335
t = this.customEntryHandling( t );
324336
listCurrent.add( t );
325337
} catch (Exception e) {

fj-core/src/main/java/org/fugerit/java/core/cfg/xml/XmlBeanHelper.java

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,88 @@
22

33
import org.fugerit.java.core.lang.helpers.ClassHelper;
44
import org.fugerit.java.core.lang.helpers.reflect.MethodHelper;
5-
import org.slf4j.LoggerFactory;
65
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
77
import org.w3c.dom.Attr;
88
import org.w3c.dom.Element;
99
import org.w3c.dom.NamedNodeMap;
10+
import org.w3c.dom.Node;
11+
import org.w3c.dom.NodeList;
1012

1113
public class XmlBeanHelper {
1214

1315
private static final Logger logger = LoggerFactory.getLogger( XmlBeanHelper.class );
1416

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+
1525
public static <T> T setFromElement( String type, Element config ) throws Exception {
1626
@SuppressWarnings("unchecked")
1727
T bean = (T) ClassHelper.newInstance( type );
1828
setFromElement(bean, config);
1929
return bean;
2030
}
2131

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+
2239
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);
2949
}
3050
if ( bean instanceof TextValueType ) {
3151
((TextValueType) bean ).setTextValue( config.getTextContent() );
3252
}
3353
}
3454

35-
public static <T> void setFromElementSafe( T bean, Element config ) {
55+
public static <T> void setFromElementSafe( T bean, Element config, String mode ) {
3656
try {
37-
setFromElement( bean , config);
57+
setFromElement( bean , config, mode );
3858
} catch (Exception e) {
3959
logger.warn( "Cannot set all parameters from bean, usually safe to ignore : "+e );
4060
}
4161
}
4262

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+
4389
}

0 commit comments

Comments
 (0)