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