Addressed comment

This commit is contained in:
Rahul Kumar 2020-09-03 11:17:10 +05:30
parent 900a8cc945
commit 310f18fcdc
3 changed files with 38 additions and 12 deletions

View File

@ -360,9 +360,9 @@ public class XML {
&& NULL_ATTR.equals(string) && NULL_ATTR.equals(string)
&& Boolean.parseBoolean((String) token)) { && Boolean.parseBoolean((String) token)) {
nilAttributeFound = true; nilAttributeFound = true;
} else if(config.xsiTypeMap != null } else if(config.getXsiTypeMap() != null && !config.getXsiTypeMap().isEmpty()
&& TYPE_ATTR.equals(string)) { && TYPE_ATTR.equals(string)) {
xmlXsiTypeConverter = config.xsiTypeMap.get(token); xmlXsiTypeConverter = config.getXsiTypeMap().get(token);
} else if (!nilAttributeFound) { } else if (!nilAttributeFound) {
jsonObject.accumulate(string, jsonObject.accumulate(string,
config.isKeepStrings() config.isKeepStrings()

View File

@ -23,6 +23,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. SOFTWARE.
*/ */
import java.util.Collections;
import java.util.Map; import java.util.Map;
@ -62,7 +63,7 @@ public class XMLParserConfiguration {
/** /**
* This will allow type conversion for values in XML if xsi:type attribute is defined * This will allow type conversion for values in XML if xsi:type attribute is defined
*/ */
public Map<String, XMLXsiTypeConverter<?>> xsiTypeMap; private Map<String, XMLXsiTypeConverter<?>> xsiTypeMap;
/** /**
* Default parser configuration. Does not keep strings (tries to implicitly convert * Default parser configuration. Does not keep strings (tries to implicitly convert
@ -72,6 +73,7 @@ public class XMLParserConfiguration {
this.keepStrings = false; this.keepStrings = false;
this.cDataTagName = "content"; this.cDataTagName = "content";
this.convertNilAttributeToNull = false; this.convertNilAttributeToNull = false;
this.xsiTypeMap = Collections.emptyMap();
} }
/** /**
@ -148,14 +150,13 @@ public class XMLParserConfiguration {
* <code>false</code> to parse values with attribute xsi:nil="true" as {"xsi:nil":true}. * <code>false</code> to parse values with attribute xsi:nil="true" as {"xsi:nil":true}.
* @param xsiTypeMap <code>new HashMap<String, XMLXsiTypeConverter<?>>()</code> to parse values with attribute * @param xsiTypeMap <code>new HashMap<String, XMLXsiTypeConverter<?>>()</code> to parse values with attribute
* xsi:type="integer" as integer, xsi:type="string" as string * xsi:type="integer" as integer, xsi:type="string" as string
* <code>null</code> to use default behaviour.
*/ */
public XMLParserConfiguration (final boolean keepStrings, final String cDataTagName, private XMLParserConfiguration (final boolean keepStrings, final String cDataTagName,
final boolean convertNilAttributeToNull, final Map<String, XMLXsiTypeConverter<?>> xsiTypeMap ) { final boolean convertNilAttributeToNull, final Map<String, XMLXsiTypeConverter<?>> xsiTypeMap ) {
this.keepStrings = keepStrings; this.keepStrings = keepStrings;
this.cDataTagName = cDataTagName; this.cDataTagName = cDataTagName;
this.convertNilAttributeToNull = convertNilAttributeToNull; this.convertNilAttributeToNull = convertNilAttributeToNull;
this.xsiTypeMap = xsiTypeMap; this.xsiTypeMap = Collections.unmodifiableMap(xsiTypeMap);
} }
/** /**
@ -171,7 +172,8 @@ public class XMLParserConfiguration {
return new XMLParserConfiguration( return new XMLParserConfiguration(
this.keepStrings, this.keepStrings,
this.cDataTagName, this.cDataTagName,
this.convertNilAttributeToNull this.convertNilAttributeToNull,
this.xsiTypeMap
); );
} }
@ -253,4 +255,30 @@ public class XMLParserConfiguration {
newConfig.convertNilAttributeToNull = newVal; newConfig.convertNilAttributeToNull = newVal;
return newConfig; return newConfig;
} }
/**
* When parsing the XML into JSON, specifies that the values with attribute xsi:type
* will be converted to target type defined to client in this configuration
* <code>Map<String, XMLXsiTypeConverter<?>></code> to parse values with attribute
* xsi:type="integer" as integer, xsi:type="string" as string
* @return {@link #xsiTypeMap} unmodifiable configuration map.
*/
public Map<String, XMLXsiTypeConverter<?>> getXsiTypeMap() {
return this.xsiTypeMap;
}
/**
* When parsing the XML into JSON, specifies that the values with attribute xsi:type
* will be converted to target type defined to client in this configuration
* <code>Map<String, XMLXsiTypeConverter<?>></code> to parse values with attribute
* xsi:type="integer" as integer, xsi:type="string" as string
* @param xsiTypeMap <code>new HashMap<String, XMLXsiTypeConverter<?>>()</code> to parse values with attribute
* xsi:type="integer" as integer, xsi:type="string" as string
* @return The existing configuration will not be modified. A new configuration is returned.
*/
public XMLParserConfiguration withXsiTypeMap(final Map<String, XMLXsiTypeConverter<?>> xsiTypeMap) {
XMLParserConfiguration newConfig = this.clone();
newConfig.xsiTypeMap = Collections.unmodifiableMap(xsiTypeMap);
return newConfig;
}
} }

View File

@ -1008,8 +1008,7 @@ public class XMLTest {
return Integer.valueOf(value); return Integer.valueOf(value);
} }
}); });
JSONObject actualJson = XML.toJSONObject(originalXml, new XMLParserConfiguration(false, JSONObject actualJson = XML.toJSONObject(originalXml, new XMLParserConfiguration().withXsiTypeMap(xsiTypeMap));
"content", false, xsiTypeMap));
Util.compareActualVsExpectedJsonObjects(actualJson,expectedJson); Util.compareActualVsExpectedJsonObjects(actualJson,expectedJson);
} }
@ -1030,8 +1029,7 @@ public class XMLTest {
return Integer.valueOf(value); return Integer.valueOf(value);
} }
}); });
JSONObject actualJson = XML.toJSONObject(originalXml, new XMLParserConfiguration(false, JSONObject actualJson = XML.toJSONObject(originalXml, new XMLParserConfiguration().withXsiTypeMap(xsiTypeMap));
"content", false, xsiTypeMap));
Util.compareActualVsExpectedJsonObjects(actualJson,expectedJson); Util.compareActualVsExpectedJsonObjects(actualJson,expectedJson);
} }