|
Added
Link Here
|
| 1 |
package org.eclipse.persistence.oxm.record; |
| 2 |
|
| 3 |
import java.io.IOException; |
| 4 |
import java.io.StringWriter; |
| 5 |
import java.math.BigDecimal; |
| 6 |
import java.math.BigInteger; |
| 7 |
import java.util.List; |
| 8 |
|
| 9 |
import javax.json.Json; |
| 10 |
import javax.json.JsonArrayBuilder; |
| 11 |
import javax.json.JsonObjectBuilder; |
| 12 |
import javax.xml.namespace.QName; |
| 13 |
|
| 14 |
import org.eclipse.persistence.exceptions.XMLMarshalException; |
| 15 |
import org.eclipse.persistence.internal.helper.ClassConstants; |
| 16 |
import org.eclipse.persistence.internal.oxm.Constants; |
| 17 |
import org.eclipse.persistence.internal.oxm.NamespaceResolver; |
| 18 |
import org.eclipse.persistence.internal.oxm.ObjectBuilder; |
| 19 |
import org.eclipse.persistence.internal.oxm.Root; |
| 20 |
import org.eclipse.persistence.internal.oxm.XMLBinaryDataHelper; |
| 21 |
import org.eclipse.persistence.internal.oxm.XMLConversionManager; |
| 22 |
import org.eclipse.persistence.internal.oxm.XMLMarshaller; |
| 23 |
import org.eclipse.persistence.internal.oxm.XPathFragment; |
| 24 |
import org.eclipse.persistence.internal.oxm.mappings.Descriptor; |
| 25 |
import org.eclipse.persistence.internal.oxm.record.ExtendedContentHandler; |
| 26 |
import org.eclipse.persistence.internal.oxm.record.XMLFragmentReader; |
| 27 |
import org.eclipse.persistence.oxm.CharacterEscapeHandler; |
| 28 |
import org.w3c.dom.Attr; |
| 29 |
import org.w3c.dom.Node; |
| 30 |
import org.xml.sax.Attributes; |
| 31 |
import org.xml.sax.Locator; |
| 32 |
import org.xml.sax.SAXException; |
| 33 |
import org.xml.sax.ext.LexicalHandler; |
| 34 |
|
| 35 |
|
| 36 |
//TODO replace NULL string with addNUll |
| 37 |
//TODO element method |
| 38 |
public class JsonObjectBuilderWriterRecord extends MarshalRecord <XMLMarshaller> { |
| 39 |
|
| 40 |
private Level position; |
| 41 |
private JsonObjectBuilder rootJsonObjectBuilder; |
| 42 |
private JsonArrayBuilder rootJsonArrayBuilder; |
| 43 |
private CharacterEscapeHandler characterEscapeHandler; |
| 44 |
|
| 45 |
private String attributePrefix; |
| 46 |
private boolean isRootArray; |
| 47 |
private static final String NULL="null"; |
| 48 |
private boolean isLastEventStart; |
| 49 |
|
| 50 |
public JsonObjectBuilderWriterRecord(){ |
| 51 |
super(); |
| 52 |
isLastEventStart = false; |
| 53 |
} |
| 54 |
|
| 55 |
public JsonObjectBuilderWriterRecord(JsonObjectBuilder jsonObjectBuilder){ |
| 56 |
this(); |
| 57 |
rootJsonObjectBuilder = jsonObjectBuilder; |
| 58 |
} |
| 59 |
|
| 60 |
public JsonObjectBuilderWriterRecord(JsonArrayBuilder jsonArrayBuilder){ |
| 61 |
this(); |
| 62 |
rootJsonArrayBuilder = jsonArrayBuilder; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* INTERNAL: |
| 67 |
*/ |
| 68 |
public void setMarshaller(XMLMarshaller marshaller) { |
| 69 |
super.setMarshaller(marshaller); |
| 70 |
attributePrefix = marshaller.getAttributePrefix(); |
| 71 |
if (marshaller.getValueWrapper() != null) { |
| 72 |
textWrapperFragment = new XPathFragment(); |
| 73 |
textWrapperFragment.setLocalName(marshaller.getValueWrapper()); |
| 74 |
} |
| 75 |
characterEscapeHandler = marshaller.getCharacterEscapeHandler(); |
| 76 |
} |
| 77 |
|
| 78 |
@Override |
| 79 |
public void startDocument(String encoding, String version) { |
| 80 |
if(isRootArray){ |
| 81 |
position.setEmptyCollection(false); |
| 82 |
|
| 83 |
Level newLevel = new Level(false, position); |
| 84 |
newLevel.setItemInCollection(true); |
| 85 |
position = newLevel; |
| 86 |
|
| 87 |
isLastEventStart = true; |
| 88 |
}else{ |
| 89 |
Level rootLevel = new Level(false, null); |
| 90 |
position = rootLevel; |
| 91 |
if(rootJsonObjectBuilder == null){ |
| 92 |
rootJsonObjectBuilder = Json.createObjectBuilder(); |
| 93 |
} |
| 94 |
|
| 95 |
rootLevel.setJsonObjectBuilder(rootJsonObjectBuilder); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
@Override |
| 100 |
public void endDocument() { |
| 101 |
if(position != null){ |
| 102 |
if(position.isItemInCollection() && position.parentLevel != null){ |
| 103 |
popAndSetInParentBuilder(); |
| 104 |
}else{ |
| 105 |
//this is the root level list case |
| 106 |
position = position.parentLevel; |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
private void popAndSetInParentBuilder(){ |
| 112 |
Level removedLevel = position; |
| 113 |
Level parentLevel = position.parentLevel; |
| 114 |
position = position.parentLevel; |
| 115 |
if(removedLevel.isCollection && removedLevel.isEmptyCollection() && removedLevel.keyName == null){ |
| 116 |
return; |
| 117 |
} |
| 118 |
|
| 119 |
if(parentLevel != null){ |
| 120 |
if(parentLevel.isCollection){ |
| 121 |
if(removedLevel.isCollection){ |
| 122 |
parentLevel.getJsonArrayBuilder().add(removedLevel.getJsonArrayBuilder()); |
| 123 |
}else{ |
| 124 |
parentLevel.getJsonArrayBuilder().add(removedLevel.getJsonObjectBuilder()); |
| 125 |
} |
| 126 |
}else{ |
| 127 |
if(removedLevel.isCollection){ |
| 128 |
parentLevel.getJsonObjectBuilder().add(removedLevel.getKeyName(), removedLevel.getJsonArrayBuilder()); |
| 129 |
}else{ |
| 130 |
parentLevel.getJsonObjectBuilder().add(removedLevel.getKeyName(), removedLevel.getJsonObjectBuilder()); |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
} |
| 136 |
|
| 137 |
public void startCollection() { |
| 138 |
if(position == null){ |
| 139 |
isRootArray = true; |
| 140 |
Level rootLevel = new Level(true, null); |
| 141 |
if(rootJsonArrayBuilder == null){ |
| 142 |
rootJsonArrayBuilder = Json.createArrayBuilder(); |
| 143 |
} |
| 144 |
rootLevel.setJsonArrayBuilder(rootJsonArrayBuilder); |
| 145 |
position = rootLevel; |
| 146 |
} else { |
| 147 |
if(isLastEventStart){ |
| 148 |
position.setComplex(true); |
| 149 |
} |
| 150 |
Level level = new Level(true, position); |
| 151 |
position = level; |
| 152 |
} |
| 153 |
isLastEventStart = false; |
| 154 |
} |
| 155 |
|
| 156 |
@Override |
| 157 |
public void endCollection() { |
| 158 |
popAndSetInParentBuilder(); |
| 159 |
} |
| 160 |
|
| 161 |
@Override |
| 162 |
public void openStartElement(XPathFragment xPathFragment, NamespaceResolver namespaceResolver) { |
| 163 |
super.openStartElement(xPathFragment, namespaceResolver); |
| 164 |
if(position != null){ |
| 165 |
Level newLevel = new Level(false, position); |
| 166 |
|
| 167 |
if(position.isCollection){ |
| 168 |
newLevel.itemInCollection = true; |
| 169 |
} |
| 170 |
|
| 171 |
if(isLastEventStart){ |
| 172 |
//this means 2 startevents in a row so the last this is a complex object |
| 173 |
position.setComplex(true); |
| 174 |
} |
| 175 |
|
| 176 |
String keyName = getKeyName(xPathFragment); |
| 177 |
|
| 178 |
if(position.isCollection && position.isEmptyCollection() ){ |
| 179 |
position.setKeyName(keyName); |
| 180 |
}else{ |
| 181 |
newLevel.setKeyName(keyName); |
| 182 |
} |
| 183 |
position = newLevel; |
| 184 |
isLastEventStart = true; |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
protected String getKeyName(XPathFragment xPathFragment){ |
| 189 |
String keyName = xPathFragment.getLocalName(); |
| 190 |
|
| 191 |
if(isNamespaceAware()){ |
| 192 |
if(xPathFragment.getNamespaceURI() != null){ |
| 193 |
String prefix = null; |
| 194 |
if(getNamespaceResolver() !=null){ |
| 195 |
prefix = getNamespaceResolver().resolveNamespaceURI(xPathFragment.getNamespaceURI()); |
| 196 |
} else if(namespaceResolver != null){ |
| 197 |
prefix = namespaceResolver.resolveNamespaceURI(xPathFragment.getNamespaceURI()); |
| 198 |
} |
| 199 |
if(prefix != null && !prefix.equals(Constants.EMPTY_STRING)){ |
| 200 |
keyName = prefix + getNamespaceSeparator() + keyName; |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
if(xPathFragment.isAttribute() && attributePrefix != null){ |
| 205 |
keyName = attributePrefix + keyName; |
| 206 |
} |
| 207 |
|
| 208 |
return keyName; |
| 209 |
} |
| 210 |
|
| 211 |
public void attribute(XPathFragment xPathFragment, NamespaceResolver namespaceResolver, Object value, QName schemaType){ |
| 212 |
if(xPathFragment.getNamespaceURI() != null && xPathFragment.getNamespaceURI() == javax.xml.XMLConstants.XMLNS_ATTRIBUTE_NS_URI){ |
| 213 |
return; |
| 214 |
} |
| 215 |
xPathFragment.setAttribute(true); |
| 216 |
openStartElement(xPathFragment, namespaceResolver); |
| 217 |
characters(schemaType, value, null, false, true); |
| 218 |
endElement(xPathFragment, namespaceResolver); |
| 219 |
} |
| 220 |
|
| 221 |
/** |
| 222 |
* INTERNAL: |
| 223 |
*/ |
| 224 |
@Override |
| 225 |
public void marshalWithoutRootElement(ObjectBuilder treeObjectBuilder, Object object, Descriptor descriptor, Root root, boolean isXMLRoot){ |
| 226 |
if(treeObjectBuilder != null){ |
| 227 |
addXsiTypeAndClassIndicatorIfRequired(descriptor, null, descriptor.getDefaultRootElementField(), root, object, isXMLRoot, true); |
| 228 |
treeObjectBuilder.marshalAttributes(this, object, session); |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* INTERNAL: |
| 234 |
* The character used to separate the prefix and uri portions when namespaces are present |
| 235 |
* @since 2.4 |
| 236 |
*/ |
| 237 |
public char getNamespaceSeparator(){ |
| 238 |
return marshaller.getNamespaceSeparator(); |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* INTERNAL: |
| 243 |
* The optional fragment used to wrap the text() mappings |
| 244 |
* @since 2.4 |
| 245 |
*/ |
| 246 |
public XPathFragment getTextWrapperFragment() { |
| 247 |
return textWrapperFragment; |
| 248 |
} |
| 249 |
|
| 250 |
@Override |
| 251 |
public boolean isWrapperAsCollectionName() { |
| 252 |
return marshaller.isWrapperAsCollectionName(); |
| 253 |
} |
| 254 |
|
| 255 |
@Override |
| 256 |
public void element(XPathFragment frag) { |
| 257 |
isLastEventStart = false; |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Handle marshal of an empty collection. |
| 262 |
* @param xPathFragment |
| 263 |
* @param namespaceResolver |
| 264 |
* @param openGrouping if grouping elements should be marshalled for empty collections |
| 265 |
* @return |
| 266 |
*/ |
| 267 |
public boolean emptyCollection(XPathFragment xPathFragment, NamespaceResolver namespaceResolver, boolean openGrouping) { |
| 268 |
|
| 269 |
if(marshaller.isMarshalEmptyCollections()){ |
| 270 |
super.emptyCollection(xPathFragment, namespaceResolver, true); |
| 271 |
|
| 272 |
if (null != xPathFragment) { |
| 273 |
|
| 274 |
if(xPathFragment.isSelfFragment() || xPathFragment.nameIsText()){ |
| 275 |
String keyName = position.getKeyName(); |
| 276 |
position.setComplex(false); |
| 277 |
position.parentLevel.getJsonObjectBuilder().add(keyName, Json.createArrayBuilder()); |
| 278 |
}else{ |
| 279 |
if(isLastEventStart){ |
| 280 |
position.setComplex(true); |
| 281 |
} |
| 282 |
String keyName = getKeyName(xPathFragment); |
| 283 |
if(keyName != null){ |
| 284 |
position.getJsonObjectBuilder().add(keyName, Json.createArrayBuilder()); |
| 285 |
} |
| 286 |
} |
| 287 |
isLastEventStart = false; |
| 288 |
} |
| 289 |
|
| 290 |
return true; |
| 291 |
}else{ |
| 292 |
return super.emptyCollection(xPathFragment, namespaceResolver, openGrouping); |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
@Override |
| 297 |
public void attribute(XPathFragment xPathFragment,NamespaceResolver namespaceResolver, String value) { |
| 298 |
attribute(xPathFragment, namespaceResolver, value, null); |
| 299 |
} |
| 300 |
|
| 301 |
@Override |
| 302 |
public void attribute(String namespaceURI, String localName, String qName, String value) { |
| 303 |
XPathFragment xPathFragment = new XPathFragment(); |
| 304 |
xPathFragment.setNamespaceURI(namespaceURI); |
| 305 |
xPathFragment.setAttribute(true); |
| 306 |
xPathFragment.setLocalName(localName); |
| 307 |
|
| 308 |
openStartElement(xPathFragment, namespaceResolver); |
| 309 |
characters(null, value, null, false, true); |
| 310 |
|
| 311 |
endElement(xPathFragment, namespaceResolver); |
| 312 |
|
| 313 |
} |
| 314 |
|
| 315 |
@Override |
| 316 |
public void closeStartElement() {} |
| 317 |
|
| 318 |
@Override |
| 319 |
public void endElement(XPathFragment xPathFragment,NamespaceResolver namespaceResolver) { |
| 320 |
if(position != null){ |
| 321 |
if(isLastEventStart){ |
| 322 |
position.setComplex(true); |
| 323 |
} |
| 324 |
if(position.isComplex){ |
| 325 |
popAndSetInParentBuilder(); |
| 326 |
}else{ |
| 327 |
position = position.parentLevel; |
| 328 |
} |
| 329 |
isLastEventStart = false; |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
@Override |
| 334 |
public void characters(String value) { |
| 335 |
writeValue(value, null, false); |
| 336 |
} |
| 337 |
|
| 338 |
@Override |
| 339 |
public void characters(QName schemaType, Object value, String mimeType, boolean isCDATA){ |
| 340 |
characters(schemaType, value, mimeType, isCDATA, false); |
| 341 |
} |
| 342 |
|
| 343 |
public void characters(QName schemaType, Object value, String mimeType, boolean isCDATA, boolean isAttribute){ |
| 344 |
if(mimeType != null) { |
| 345 |
if(value instanceof List){ |
| 346 |
value = XMLBinaryDataHelper.getXMLBinaryDataHelper().getBytesListForBinaryValues((List)value, marshaller, mimeType); |
| 347 |
}else{ |
| 348 |
|
| 349 |
value = XMLBinaryDataHelper.getXMLBinaryDataHelper().getBytesForBinaryValue(value, marshaller, mimeType).getData(); |
| 350 |
} |
| 351 |
} |
| 352 |
if(schemaType != null && Constants.QNAME_QNAME.equals(schemaType)){ |
| 353 |
String convertedValue = getStringForQName((QName)value); |
| 354 |
writeValue(convertedValue, null, isAttribute); |
| 355 |
} |
| 356 |
else if(value.getClass() == String.class){ |
| 357 |
//if schemaType is set and it's a numeric or boolean type don't treat as a string |
| 358 |
if(schemaType != null && isNumericOrBooleanType(schemaType)){ |
| 359 |
Class theClass = (Class) ((XMLConversionManager) session.getDatasourcePlatform().getConversionManager()).getDefaultXMLTypes().get(schemaType); |
| 360 |
Object convertedValue = ((XMLConversionManager) session.getDatasourcePlatform().getConversionManager()).convertObject(value, theClass, schemaType); |
| 361 |
writeValue(convertedValue, schemaType, isAttribute); |
| 362 |
}else if(isCDATA){ |
| 363 |
cdata((String)value); |
| 364 |
}else{ |
| 365 |
writeValue((String)value, null, isAttribute); |
| 366 |
} |
| 367 |
}else{ |
| 368 |
Class theClass = (Class) ((XMLConversionManager) session.getDatasourcePlatform().getConversionManager()).getDefaultXMLTypes().get(schemaType); |
| 369 |
|
| 370 |
if(schemaType == null || theClass == null){ |
| 371 |
if(value.getClass() == ClassConstants.BOOLEAN || ClassConstants.NUMBER.isAssignableFrom(value.getClass())){ |
| 372 |
writeValue(value, schemaType, isAttribute); |
| 373 |
}else{ |
| 374 |
String convertedValue = ((String) ((XMLConversionManager) session.getDatasourcePlatform().getConversionManager()).convertObject(value, ClassConstants.STRING, schemaType)); |
| 375 |
writeValue(convertedValue, schemaType, isAttribute); |
| 376 |
} |
| 377 |
}else if(schemaType != null && !isNumericOrBooleanType(schemaType)){ |
| 378 |
//if schemaType exists and is not boolean or number do write quotes (convert to string) |
| 379 |
String convertedValue = ((String) ((XMLConversionManager) session.getDatasourcePlatform().getConversionManager()).convertObject(value, ClassConstants.STRING, schemaType)); |
| 380 |
writeValue(convertedValue, schemaType, isAttribute); |
| 381 |
} else if(isCDATA){ |
| 382 |
String convertedValue = ((String) ((XMLConversionManager) session.getDatasourcePlatform().getConversionManager()).convertObject(value, ClassConstants.STRING, schemaType)); |
| 383 |
cdata(convertedValue); |
| 384 |
}else{ |
| 385 |
writeValue(value, schemaType, isAttribute); |
| 386 |
} |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
private boolean isNumericOrBooleanType(QName schemaType){ |
| 392 |
if(schemaType == null){ |
| 393 |
return false; |
| 394 |
}else if(schemaType.equals(Constants.BOOLEAN_QNAME) |
| 395 |
|| schemaType.equals(Constants.INTEGER_QNAME) |
| 396 |
|| schemaType.equals(Constants.INT_QNAME) |
| 397 |
|| schemaType.equals(Constants.BYTE_QNAME) |
| 398 |
|| schemaType.equals(Constants.DECIMAL_QNAME) |
| 399 |
|| schemaType.equals(Constants.FLOAT_QNAME) |
| 400 |
|| schemaType.equals(Constants.DOUBLE_QNAME) |
| 401 |
|| schemaType.equals(Constants.SHORT_QNAME) |
| 402 |
|| schemaType.equals(Constants.LONG_QNAME) |
| 403 |
|| schemaType.equals(Constants.NEGATIVE_INTEGER_QNAME) |
| 404 |
|| schemaType.equals(Constants.NON_NEGATIVE_INTEGER_QNAME) |
| 405 |
|| schemaType.equals(Constants.NON_POSITIVE_INTEGER_QNAME) |
| 406 |
|| schemaType.equals(Constants.POSITIVE_INTEGER_QNAME) |
| 407 |
|| schemaType.equals(Constants.UNSIGNED_BYTE_QNAME) |
| 408 |
|| schemaType.equals(Constants.UNSIGNED_INT_QNAME) |
| 409 |
|| schemaType.equals(Constants.UNSIGNED_LONG_QNAME) |
| 410 |
|| schemaType.equals(Constants.UNSIGNED_SHORT_QNAME) |
| 411 |
){ |
| 412 |
return true; |
| 413 |
} |
| 414 |
return false; |
| 415 |
} |
| 416 |
|
| 417 |
public void writeValue(Object value, QName schemaType, boolean isAttribute) { |
| 418 |
|
| 419 |
if (characterEscapeHandler != null && value instanceof String) { |
| 420 |
try { |
| 421 |
StringWriter stringWriter = new StringWriter(); |
| 422 |
characterEscapeHandler.escape(((String)value).toCharArray(), 0, ((String)value).length(), isAttribute, stringWriter); |
| 423 |
value = stringWriter.toString(); |
| 424 |
} catch (IOException e) { |
| 425 |
throw XMLMarshalException.marshalException(e); |
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
boolean textWrapperOpened = false; |
| 430 |
if(!isLastEventStart){ |
| 431 |
openStartElement(textWrapperFragment, namespaceResolver); |
| 432 |
textWrapperOpened = true; |
| 433 |
} |
| 434 |
|
| 435 |
Level currentLevel = position; |
| 436 |
String keyName = position.getKeyName(); |
| 437 |
if(!position.isComplex){ |
| 438 |
currentLevel = position.parentLevel; |
| 439 |
currentLevel.setComplex(true); |
| 440 |
} |
| 441 |
if(currentLevel.isCollection()){ |
| 442 |
currentLevel.setEmptyCollection(false); |
| 443 |
addValueToArrayBuilder(currentLevel.getJsonArrayBuilder(), value, schemaType); |
| 444 |
} else { |
| 445 |
JsonObjectBuilder builder = currentLevel.getJsonObjectBuilder(); |
| 446 |
addValueToObjectBuilder(builder, keyName, value, schemaType); |
| 447 |
} |
| 448 |
isLastEventStart = false; |
| 449 |
if(textWrapperOpened){ |
| 450 |
endElement(textWrapperFragment, namespaceResolver); |
| 451 |
} |
| 452 |
} |
| 453 |
|
| 454 |
private void addValueToObjectBuilder(JsonObjectBuilder jsonObjectBuilder, String keyName, Object value, QName schemaType){ |
| 455 |
if(value == NULL){ |
| 456 |
jsonObjectBuilder.addNull(keyName); |
| 457 |
}else if(value instanceof Integer){ |
| 458 |
jsonObjectBuilder.add(keyName, (Integer)value); |
| 459 |
}else if(value instanceof BigDecimal){ |
| 460 |
jsonObjectBuilder.add(keyName, (BigDecimal)value); |
| 461 |
}else if(value instanceof BigInteger){ |
| 462 |
jsonObjectBuilder.add(keyName, (BigInteger)value); |
| 463 |
}else if(value instanceof Boolean){ |
| 464 |
jsonObjectBuilder.add(keyName, (Boolean)value); |
| 465 |
}else if(value instanceof Character){ |
| 466 |
jsonObjectBuilder.add(keyName, (Character)value); |
| 467 |
}else if(value instanceof Double){ |
| 468 |
jsonObjectBuilder.add(keyName, (Double)value); |
| 469 |
}else if(value instanceof Float){ |
| 470 |
jsonObjectBuilder.add(keyName, (Float)value); |
| 471 |
}else if(value instanceof Long){ |
| 472 |
jsonObjectBuilder.add(keyName, (Long)value); |
| 473 |
}else if(value instanceof String){ |
| 474 |
jsonObjectBuilder.add(keyName, (String)value); |
| 475 |
}else{ |
| 476 |
String convertedValue = ((String) ((XMLConversionManager) session.getDatasourcePlatform().getConversionManager()).convertObject(value, ClassConstants.STRING, schemaType)); |
| 477 |
Class theClass = (Class) ((XMLConversionManager) session.getDatasourcePlatform().getConversionManager()).getDefaultXMLTypes().get(schemaType); |
| 478 |
if((schemaType == null || theClass == null) && (ClassConstants.NUMBER.isAssignableFrom(value.getClass()))){ |
| 479 |
//if it's still a number and falls through the cracks we dont want "" around the value |
| 480 |
BigDecimal convertedNumberValue = ((BigDecimal) ((XMLConversionManager) session.getDatasourcePlatform().getConversionManager()).convertObject(value, ClassConstants.BIGDECIMAL, schemaType)); |
| 481 |
jsonObjectBuilder.add(keyName, (BigDecimal)convertedNumberValue); |
| 482 |
}else{ |
| 483 |
jsonObjectBuilder.add(keyName, convertedValue); |
| 484 |
} |
| 485 |
|
| 486 |
} |
| 487 |
} |
| 488 |
|
| 489 |
private void addValueToArrayBuilder(JsonArrayBuilder jsonArrayBuilder, Object value, QName schemaType){ |
| 490 |
if(value == NULL){ |
| 491 |
jsonArrayBuilder.addNull(); |
| 492 |
}else if(value instanceof Integer){ |
| 493 |
jsonArrayBuilder.add((Integer)value); |
| 494 |
}else if(value instanceof BigDecimal){ |
| 495 |
jsonArrayBuilder.add((BigDecimal)value); |
| 496 |
}else if(value instanceof BigInteger){ |
| 497 |
jsonArrayBuilder.add((BigInteger)value); |
| 498 |
}else if(value instanceof Boolean){ |
| 499 |
jsonArrayBuilder.add((Boolean)value); |
| 500 |
}else if(value instanceof Character){ |
| 501 |
jsonArrayBuilder.add((Character)value); |
| 502 |
}else if(value instanceof Double){ |
| 503 |
jsonArrayBuilder.add((Double)value); |
| 504 |
}else if(value instanceof Float){ |
| 505 |
jsonArrayBuilder.add((Float)value); |
| 506 |
}else if(value instanceof Long){ |
| 507 |
jsonArrayBuilder.add((Long)value); |
| 508 |
}else if(value instanceof String){ |
| 509 |
jsonArrayBuilder.add((String)value); |
| 510 |
}else{ |
| 511 |
String convertedValue = ((String) ((XMLConversionManager) session.getDatasourcePlatform().getConversionManager()).convertObject(value, ClassConstants.STRING, schemaType)); |
| 512 |
Class theClass = (Class) ((XMLConversionManager) session.getDatasourcePlatform().getConversionManager()).getDefaultXMLTypes().get(schemaType); |
| 513 |
if((schemaType == null || theClass == null) && (ClassConstants.NUMBER.isAssignableFrom(value.getClass()))){ |
| 514 |
//if it's still a number and falls through the cracks we dont want "" around the value |
| 515 |
BigDecimal convertedNumberValue = ((BigDecimal) ((XMLConversionManager) session.getDatasourcePlatform().getConversionManager()).convertObject(value, ClassConstants.BIGDECIMAL, schemaType)); |
| 516 |
jsonArrayBuilder.add((BigDecimal)convertedNumberValue); |
| 517 |
}else{ |
| 518 |
jsonArrayBuilder.add(convertedValue); |
| 519 |
} |
| 520 |
} |
| 521 |
} |
| 522 |
|
| 523 |
@Override |
| 524 |
public void cdata(String value) { |
| 525 |
characters(value); |
| 526 |
} |
| 527 |
|
| 528 |
@Override |
| 529 |
public void node(Node node, NamespaceResolver resolver, String uri, String name) { |
| 530 |
|
| 531 |
if (node.getNodeType() == Node.ATTRIBUTE_NODE) { |
| 532 |
Attr attr = (Attr) node; |
| 533 |
String resolverPfx = null; |
| 534 |
if (getNamespaceResolver() != null) { |
| 535 |
resolverPfx = this.getNamespaceResolver().resolveNamespaceURI(attr.getNamespaceURI()); |
| 536 |
} |
| 537 |
String namespaceURI = attr.getNamespaceURI(); |
| 538 |
// If the namespace resolver contains a prefix for the attribute's URI, |
| 539 |
// use it instead of what is set on the attribute |
| 540 |
if (resolverPfx != null) { |
| 541 |
attribute(attr.getNamespaceURI(), Constants.EMPTY_STRING, resolverPfx+Constants.COLON+attr.getLocalName(), attr.getNodeValue()); |
| 542 |
} else { |
| 543 |
attribute(attr.getNamespaceURI(), Constants.EMPTY_STRING, attr.getName(), attr.getNodeValue()); |
| 544 |
// May need to declare the URI locally |
| 545 |
if (attr.getNamespaceURI() != null) { |
| 546 |
attribute(javax.xml.XMLConstants.XMLNS_ATTRIBUTE_NS_URI, Constants.EMPTY_STRING, javax.xml.XMLConstants.XMLNS_ATTRIBUTE + Constants.COLON + attr.getPrefix(), attr.getNamespaceURI()); |
| 547 |
this.getNamespaceResolver().put(attr.getPrefix(), attr.getNamespaceURI()); |
| 548 |
} |
| 549 |
} |
| 550 |
} else if (node.getNodeType() == Node.TEXT_NODE) { |
| 551 |
writeValue(node.getNodeValue(), null, false); |
| 552 |
} else { |
| 553 |
try { |
| 554 |
JsonObjectBuilderRecordContentHandler wrcHandler = new JsonObjectBuilderRecordContentHandler(); |
| 555 |
|
| 556 |
XMLFragmentReader xfragReader = new XMLFragmentReader(namespaceResolver); |
| 557 |
xfragReader.setContentHandler(wrcHandler); |
| 558 |
xfragReader.setProperty("http://xml.org/sax/properties/lexical-handler", wrcHandler); |
| 559 |
xfragReader.parse(node, uri, name); |
| 560 |
} catch (SAXException sex) { |
| 561 |
throw XMLMarshalException.marshalException(sex); |
| 562 |
} |
| 563 |
} |
| 564 |
|
| 565 |
} |
| 566 |
|
| 567 |
protected String getStringForQName(QName qName){ |
| 568 |
if(null == qName) { |
| 569 |
return null; |
| 570 |
} |
| 571 |
XMLConversionManager xmlConversionManager = (XMLConversionManager) getSession().getDatasourcePlatform().getConversionManager(); |
| 572 |
|
| 573 |
return (String) xmlConversionManager.convertObject(qName, String.class); |
| 574 |
} |
| 575 |
|
| 576 |
/** |
| 577 |
* INTERNAL: |
| 578 |
*/ |
| 579 |
public void namespaceDeclarations(NamespaceResolver namespaceResolver) { |
| 580 |
} |
| 581 |
|
| 582 |
public void namespaceDeclaration(String prefix, String namespaceURI){ |
| 583 |
} |
| 584 |
|
| 585 |
public void defaultNamespaceDeclaration(String defaultNamespace){ |
| 586 |
} |
| 587 |
|
| 588 |
/** |
| 589 |
* INTERNAL: |
| 590 |
*/ |
| 591 |
public void nilComplex(XPathFragment xPathFragment, NamespaceResolver namespaceResolver){ |
| 592 |
XPathFragment groupingFragment = openStartGroupingElements(namespaceResolver); |
| 593 |
closeStartGroupingElements(groupingFragment); |
| 594 |
openStartElement(xPathFragment, namespaceResolver); |
| 595 |
characters(NULL); |
| 596 |
endElement(xPathFragment, namespaceResolver); |
| 597 |
} |
| 598 |
|
| 599 |
/** |
| 600 |
* INTERNAL: |
| 601 |
*/ |
| 602 |
public void nilSimple(NamespaceResolver namespaceResolver){ |
| 603 |
XPathFragment groupingFragment = openStartGroupingElements(namespaceResolver); |
| 604 |
characters(NULL); |
| 605 |
closeStartGroupingElements(groupingFragment); |
| 606 |
} |
| 607 |
|
| 608 |
/** |
| 609 |
* Used when an empty simple value should be written |
| 610 |
* @since EclipseLink 2.4 |
| 611 |
*/ |
| 612 |
public void emptySimple(NamespaceResolver namespaceResolver){ |
| 613 |
nilSimple(namespaceResolver); |
| 614 |
} |
| 615 |
|
| 616 |
public void emptyAttribute(XPathFragment xPathFragment,NamespaceResolver namespaceResolver){ |
| 617 |
XPathFragment groupingFragment = openStartGroupingElements(namespaceResolver); |
| 618 |
openStartElement(xPathFragment, namespaceResolver); |
| 619 |
characters(NULL); |
| 620 |
endElement(xPathFragment, namespaceResolver); |
| 621 |
closeStartGroupingElements(groupingFragment); |
| 622 |
} |
| 623 |
|
| 624 |
/** |
| 625 |
* Used when an empty complex item should be written |
| 626 |
* @since EclipseLink 2.4 |
| 627 |
*/ |
| 628 |
public void emptyComplex(XPathFragment xPathFragment, NamespaceResolver namespaceResolver){ |
| 629 |
XPathFragment groupingFragment = openStartGroupingElements(namespaceResolver); |
| 630 |
closeStartGroupingElements(groupingFragment); |
| 631 |
openStartElement(xPathFragment, namespaceResolver); |
| 632 |
endElement(xPathFragment, namespaceResolver); |
| 633 |
} |
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
/** |
| 638 |
* This class will typically be used in conjunction with an XMLFragmentReader. |
| 639 |
* The XMLFragmentReader will walk a given XMLFragment node and report events |
| 640 |
* to this class - the event's data is then written to the enclosing class' |
| 641 |
* writer. |
| 642 |
* |
| 643 |
* @see org.eclipse.persistence.internal.oxm.record.XMLFragmentReader |
| 644 |
*/ |
| 645 |
protected class JsonObjectBuilderRecordContentHandler implements ExtendedContentHandler, LexicalHandler { |
| 646 |
|
| 647 |
JsonObjectBuilderRecordContentHandler() { |
| 648 |
} |
| 649 |
|
| 650 |
// --------------------- CONTENTHANDLER METHODS --------------------- // |
| 651 |
public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { |
| 652 |
XPathFragment xPathFragment = new XPathFragment(localName); |
| 653 |
xPathFragment.setNamespaceURI(namespaceURI); |
| 654 |
openStartElement(xPathFragment, namespaceResolver); |
| 655 |
handleAttributes(atts); |
| 656 |
} |
| 657 |
|
| 658 |
public void endElement(String namespaceURI, String localName, String qName) throws SAXException { |
| 659 |
XPathFragment xPathFragment = new XPathFragment(localName); |
| 660 |
xPathFragment.setNamespaceURI(namespaceURI); |
| 661 |
|
| 662 |
JsonObjectBuilderWriterRecord.this.endElement(xPathFragment, namespaceResolver); |
| 663 |
} |
| 664 |
|
| 665 |
public void startPrefixMapping(String prefix, String uri) throws SAXException { |
| 666 |
} |
| 667 |
|
| 668 |
public void characters(char[] ch, int start, int length) throws SAXException { |
| 669 |
String characters = new String (ch, start, length); |
| 670 |
characters(characters); |
| 671 |
} |
| 672 |
|
| 673 |
public void characters(CharSequence characters) throws SAXException { |
| 674 |
JsonObjectBuilderWriterRecord.this.characters(characters.toString()); |
| 675 |
} |
| 676 |
|
| 677 |
// --------------------- LEXICALHANDLER METHODS --------------------- // |
| 678 |
public void comment(char[] ch, int start, int length) throws SAXException { |
| 679 |
} |
| 680 |
|
| 681 |
public void startCDATA() throws SAXException { |
| 682 |
} |
| 683 |
|
| 684 |
public void endCDATA() throws SAXException { |
| 685 |
} |
| 686 |
|
| 687 |
// --------------------- CONVENIENCE METHODS --------------------- // |
| 688 |
protected void handleAttributes(Attributes atts) { |
| 689 |
for (int i=0, attsLength = atts.getLength(); i<attsLength; i++) { |
| 690 |
String qName = atts.getQName(i); |
| 691 |
if((qName != null && (qName.startsWith(javax.xml.XMLConstants.XMLNS_ATTRIBUTE + Constants.COLON) || qName.equals(javax.xml.XMLConstants.XMLNS_ATTRIBUTE)))) { |
| 692 |
continue; |
| 693 |
} |
| 694 |
attribute(atts.getURI(i), atts.getLocalName(i), qName, atts.getValue(i)); |
| 695 |
} |
| 696 |
} |
| 697 |
|
| 698 |
protected void writeComment(char[] chars, int start, int length) { |
| 699 |
} |
| 700 |
|
| 701 |
protected void writeCharacters(char[] chars, int start, int length) { |
| 702 |
try { |
| 703 |
characters(chars, start, length); |
| 704 |
} catch (SAXException e) { |
| 705 |
throw XMLMarshalException.marshalException(e); |
| 706 |
} |
| 707 |
} |
| 708 |
// --------------- SATISFY CONTENTHANDLER INTERFACE --------------- // |
| 709 |
public void endPrefixMapping(String prefix) throws SAXException {} |
| 710 |
public void processingInstruction(String target, String data) throws SAXException {} |
| 711 |
public void setDocumentLocator(Locator locator) {} |
| 712 |
public void startDocument() throws SAXException {} |
| 713 |
public void endDocument() throws SAXException {} |
| 714 |
public void skippedEntity(String name) throws SAXException {} |
| 715 |
public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {} |
| 716 |
|
| 717 |
// --------------- SATISFY LEXICALHANDLER INTERFACE --------------- // |
| 718 |
public void startEntity(String name) throws SAXException {} |
| 719 |
public void endEntity(String name) throws SAXException {} |
| 720 |
public void startDTD(String name, String publicId, String systemId) throws SAXException {} |
| 721 |
public void endDTD() throws SAXException {} |
| 722 |
@Override |
| 723 |
public void setNil(boolean isNil) {} |
| 724 |
|
| 725 |
} |
| 726 |
|
| 727 |
|
| 728 |
/** |
| 729 |
* Instances of this class are used to maintain state about the current |
| 730 |
* level of the JSON message being marshalled. |
| 731 |
*/ |
| 732 |
protected static class Level { |
| 733 |
|
| 734 |
private boolean isCollection; |
| 735 |
private boolean emptyCollection; |
| 736 |
private String keyName; |
| 737 |
private JsonObjectBuilder jsonObjectBuilder; |
| 738 |
private JsonArrayBuilder jsonArrayBuilder; |
| 739 |
private boolean isComplex; |
| 740 |
private boolean itemInCollection; |
| 741 |
private Level parentLevel; |
| 742 |
|
| 743 |
public Level(boolean isCollection, Level parentLevel) { |
| 744 |
setCollection(isCollection); |
| 745 |
emptyCollection = true; |
| 746 |
this.parentLevel = parentLevel; |
| 747 |
} |
| 748 |
|
| 749 |
public boolean isCollection() { |
| 750 |
return isCollection; |
| 751 |
} |
| 752 |
|
| 753 |
public void setCollection(boolean isCollection) { |
| 754 |
this.isCollection = isCollection; |
| 755 |
if(isCollection && jsonArrayBuilder == null){ |
| 756 |
jsonArrayBuilder = Json.createArrayBuilder(); |
| 757 |
} |
| 758 |
} |
| 759 |
|
| 760 |
public String getKeyName() { |
| 761 |
return keyName; |
| 762 |
} |
| 763 |
|
| 764 |
public void setKeyName(String keyName) { |
| 765 |
this.keyName = keyName; |
| 766 |
} |
| 767 |
|
| 768 |
public JsonObjectBuilder getJsonObjectBuilder() { |
| 769 |
return jsonObjectBuilder; |
| 770 |
} |
| 771 |
|
| 772 |
public void setJsonObjectBuilder(JsonObjectBuilder jsonObjectBuilder) { |
| 773 |
this.jsonObjectBuilder = jsonObjectBuilder; |
| 774 |
} |
| 775 |
|
| 776 |
public JsonArrayBuilder getJsonArrayBuilder() { |
| 777 |
return jsonArrayBuilder; |
| 778 |
} |
| 779 |
|
| 780 |
public void setJsonArrayBuilder(JsonArrayBuilder jsonArrayBuilder) { |
| 781 |
this.jsonArrayBuilder = jsonArrayBuilder; |
| 782 |
} |
| 783 |
|
| 784 |
public boolean isEmptyCollection() { |
| 785 |
return emptyCollection; |
| 786 |
} |
| 787 |
|
| 788 |
public void setEmptyCollection(boolean emptyCollection) { |
| 789 |
this.emptyCollection = emptyCollection; |
| 790 |
} |
| 791 |
public boolean isComplex() { |
| 792 |
return isComplex; |
| 793 |
} |
| 794 |
|
| 795 |
public void setComplex(boolean isComplex) { |
| 796 |
this.isComplex = isComplex; |
| 797 |
if(isComplex && jsonObjectBuilder == null){ |
| 798 |
jsonObjectBuilder = Json.createObjectBuilder(); |
| 799 |
} |
| 800 |
} |
| 801 |
|
| 802 |
public boolean isItemInCollection() { |
| 803 |
return itemInCollection; |
| 804 |
} |
| 805 |
|
| 806 |
public void setItemInCollection(boolean itemInCollection) { |
| 807 |
this.itemInCollection = itemInCollection; |
| 808 |
} |
| 809 |
} |
| 810 |
|
| 811 |
|
| 812 |
} |