View Javadoc
1   package net.avcompris.commons3.api.tests;
2   
3   import static com.google.common.base.Preconditions.checkNotNull;
4   import static net.avcompris.commons3.api.tests.AbstractApiTest.extractGetter;
5   import static net.avcompris.commons3.api.tests.AbstractApiTest.invoke;
6   import static org.apache.commons.lang3.StringUtils.substringAfter;
7   import static org.apache.commons.lang3.StringUtils.substringBefore;
8   import static org.apache.commons.lang3.StringUtils.substringBetween;
9   
10  import java.lang.reflect.Array;
11  import java.lang.reflect.Method;
12  
13  import javax.annotation.Nullable;
14  
15  import org.apache.commons.lang3.NotImplementedException;
16  import org.json.simple.JSONArray;
17  import org.json.simple.JSONObject;
18  
19  abstract class DataUtils {
20  
21  	@Nullable
22  	public static String getPropertyAsString(final Object dataBean, final String propertyName) {
23  
24  		checkNotNull(dataBean, "dataBean");
25  		checkNotNull(propertyName, "propertyName");
26  
27  		final Object value;
28  
29  		if (dataBean instanceof JSONObject) {
30  
31  			final JSONObject jsonObject = (JSONObject) dataBean;
32  
33  			if (propertyName.endsWith(".length")) {
34  
35  				final JSONArray array = (JSONArray) jsonObject.get(substringBefore(propertyName, ".length"));
36  
37  				value = array.size();
38  
39  			} else if (propertyName.endsWith("]")) {
40  
41  				final String indexAsString = substringBetween(propertyName, "[", "]");
42  
43  				final int index;
44  
45  				try {
46  
47  					index = Integer.parseInt(indexAsString);
48  
49  				} catch (final NumberFormatException e) {
50  
51  					throw new NotImplementedException("propertyName: " + propertyName);
52  				}
53  
54  				final JSONArray array = (JSONArray) jsonObject.get(substringBefore(propertyName, "["));
55  
56  				value = array.get(index);
57  
58  			} else if (propertyName.contains("].")) {
59  
60  				final String indexAsString = substringBetween(propertyName, "[", "]");
61  
62  				final int index;
63  
64  				try {
65  
66  					index = Integer.parseInt(indexAsString);
67  
68  				} catch (final NumberFormatException e) {
69  
70  					throw new NotImplementedException("propertyName: " + propertyName);
71  				}
72  
73  				final JSONArray array = (JSONArray) jsonObject.get(substringBefore(propertyName, "["));
74  
75  				final Object sub = array.get(index);
76  
77  				return getPropertyAsString(sub, substringAfter(propertyName, "]."));
78  
79  			} else if (propertyName.contains(".")) {
80  
81  				throw new NotImplementedException("propertyName: " + propertyName);
82  
83  			} else {
84  
85  				value = jsonObject.get(propertyName);
86  			}
87  
88  		} else if (propertyName.endsWith(".length")) {
89  
90  			final Method getter = extractGetter(dataBean.getClass(), substringBefore(propertyName, ".length"));
91  
92  			value = Array.getLength(invoke(getter, dataBean));
93  
94  		} else if (propertyName.endsWith("]")) {
95  
96  			final String indexAsString = substringBetween(propertyName, "[", "]");
97  
98  			final int index;
99  
100 			try {
101 
102 				index = Integer.parseInt(indexAsString);
103 
104 			} catch (final NumberFormatException e) {
105 
106 				throw new NotImplementedException("propertyName: " + propertyName);
107 			}
108 
109 			final Method getter = extractGetter(dataBean.getClass(), substringBefore(propertyName, "["));
110 
111 			value = Array.get(invoke(getter, dataBean), index);
112 
113 		} else if (propertyName.contains("].")) {
114 
115 			final String indexAsString = substringBetween(propertyName, "[", "]");
116 
117 			final int index;
118 
119 			try {
120 
121 				index = Integer.parseInt(indexAsString);
122 
123 			} catch (final NumberFormatException e) {
124 
125 				throw new NotImplementedException("propertyName: " + propertyName);
126 			}
127 
128 			final Method getter = extractGetter(dataBean.getClass(), substringBefore(propertyName, "["));
129 
130 			final Object sub = Array.get(invoke(getter, dataBean), index);
131 
132 			return getPropertyAsString(sub, substringAfter(propertyName, "]."));
133 
134 		} else if (propertyName.contains(".")) {
135 
136 			throw new NotImplementedException("propertyName: " + propertyName);
137 
138 		} else {
139 
140 			final Method getter = extractGetter(dataBean.getClass(), propertyName);
141 
142 			value = invoke(getter, dataBean);
143 		}
144 
145 		if (value == null) {
146 
147 			return null;
148 
149 		} else {
150 
151 			return value.toString();
152 		}
153 	}
154 }