use of org.apache.xerces.xs.XSMultiValueFacet in project iaf by ibissource.
the class XmlTypeToJsonSchemaConverter method applyFacet.
private void applyFacet(XSSimpleTypeDefinition simpleTypeDefinition, JsonObjectBuilder builder, String key, short facet) {
if (simpleTypeDefinition.getFacet(facet) != null) {
String lexicalFacetValue = simpleTypeDefinition.getLexicalFacetValue(facet);
if (lexicalFacetValue != null) {
switch(facet) {
case XSSimpleTypeDefinition.FACET_MAXINCLUSIVE:
case XSSimpleTypeDefinition.FACET_MININCLUSIVE:
case XSSimpleTypeDefinition.FACET_MAXEXCLUSIVE:
case XSSimpleTypeDefinition.FACET_MINEXCLUSIVE:
case XSSimpleTypeDefinition.FACET_MAXLENGTH:
case XSSimpleTypeDefinition.FACET_MINLENGTH:
/*
Not sure about this..
simpleTypeDefinition.getLexicalFacetValue(facet) returns a numeric value as string
if value > MAX_INT, Integer.parseInt(value) will throw NumberFormatException
currently this exception is catched and retried as Long.ParseLong(value)
but what if this throws NumberFormatException?
how to deal with this properly?
-----
UPDATE:
Tried parsing as long and logging the value when couldn't parse, appears to be a 20 digit numeric value
which would require to use BigInteger
What is the best method to do this? Try and catch int, long & then bigint or directly to big int?
*/
try {
builder.add(key, Integer.parseInt(lexicalFacetValue));
} catch (NumberFormatException nfe) {
log.warn("Couldn't parse value [" + lexicalFacetValue + "] as Integer... retrying as Long");
try {
builder.add(key, Long.parseLong(lexicalFacetValue));
} catch (NumberFormatException nfex) {
log.warn("Couldn't parse value [" + lexicalFacetValue + "] as Long... retrying as BigInteger");
try {
builder.add(key, new BigInteger(lexicalFacetValue));
} catch (NumberFormatException nfexx) {
log.warn("Couldn't parse value [" + lexicalFacetValue + "] as BigInteger");
}
}
}
break;
default:
// hmm never reaches this block?
log.debug("Setting value [" + lexicalFacetValue + "] as String for facet [" + simpleTypeDefinition.getFacet(facet) + "]");
builder.add(key, lexicalFacetValue);
break;
}
} else if (facet == XSSimpleTypeDefinition.FACET_PATTERN || facet == XSSimpleTypeDefinition.FACET_ENUMERATION) {
XSObjectList multiValuedFacets = simpleTypeDefinition.getMultiValueFacets();
for (int i = 0; i < multiValuedFacets.getLength(); i++) {
XSMultiValueFacet multiValuedFacet = (XSMultiValueFacet) multiValuedFacets.item(i);
if (log.isTraceEnabled())
log.trace("Inspecting single multi valued facet [" + multiValuedFacet + "] which is named [" + multiValuedFacet.getName() + "] which is of type [" + multiValuedFacet.getType() + "]");
if (log.isTraceEnabled())
log.trace("Inspecting multiValuedFacet.getLexicalFacetValues() for [" + multiValuedFacet.getName() + "] which has value of [" + multiValuedFacet.getLexicalFacetValues() + "]");
if (log.isTraceEnabled())
log.trace("Inspecting multiValuedFacet.getEnumerationValues() for [" + multiValuedFacet.getName() + "] which has value of [" + multiValuedFacet.getEnumerationValues() + "]");
if (log.isTraceEnabled())
log.trace("Inspecting multiValuedFacet.getFacetKind() == enum for [" + multiValuedFacet.getName() + "] which has value of [" + (multiValuedFacet.getFacetKind() == XSSimpleTypeDefinition.FACET_ENUMERATION) + "]");
if (log.isTraceEnabled())
log.trace("Inspecting multiValuedFacet.getFacetKind() == pattern for [" + multiValuedFacet.getName() + "] which has value of [" + (multiValuedFacet.getFacetKind() == XSSimpleTypeDefinition.FACET_PATTERN) + "]");
if (facet == multiValuedFacet.getFacetKind()) {
StringList lexicalFacetValues = multiValuedFacet.getLexicalFacetValues();
if (facet == XSSimpleTypeDefinition.FACET_ENUMERATION) {
JsonArrayBuilder enumBuilder = Json.createArrayBuilder();
for (int x = 0; x < lexicalFacetValues.getLength(); x++) {
lexicalFacetValue = lexicalFacetValues.item(x);
enumBuilder.add(lexicalFacetValue);
}
builder.add(key, enumBuilder.build());
} else if (facet == XSSimpleTypeDefinition.FACET_PATTERN) {
builder.add(key, lexicalFacetValues.item(0));
}
}
}
}
}
}
Aggregations