|
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.internal.oxm.record.json; |
| 14 |
|
| 15 |
import java.io.FileInputStream; |
| 16 |
import java.io.FileNotFoundException; |
| 17 |
import java.io.IOException; |
| 18 |
import java.io.InputStream; |
| 19 |
import java.io.InputStreamReader; |
| 20 |
import java.net.MalformedURLException; |
| 21 |
import java.net.URL; |
| 22 |
import java.util.ArrayList; |
| 23 |
import java.util.Iterator; |
| 24 |
import java.util.List; |
| 25 |
import java.util.Map; |
| 26 |
import java.util.Map.Entry; |
| 27 |
import java.util.Set; |
| 28 |
|
| 29 |
import javax.json.Json; |
| 30 |
import javax.json.JsonArray; |
| 31 |
import javax.json.JsonNumber; |
| 32 |
import javax.json.JsonException; |
| 33 |
import javax.json.JsonObject; |
| 34 |
import javax.json.JsonReader; |
| 35 |
import javax.json.JsonString; |
| 36 |
import javax.json.JsonStructure; |
| 37 |
import javax.json.JsonValue; |
| 38 |
import javax.json.JsonValue.ValueType; |
| 39 |
import javax.xml.namespace.QName; |
| 40 |
|
| 41 |
import org.eclipse.persistence.exceptions.XMLMarshalException; |
| 42 |
import org.eclipse.persistence.internal.oxm.CollectionGroupingElementNodeValue; |
| 43 |
import org.eclipse.persistence.internal.oxm.ConversionManager; |
| 44 |
import org.eclipse.persistence.internal.oxm.Constants; |
| 45 |
import org.eclipse.persistence.internal.oxm.ContainerValue; |
| 46 |
import org.eclipse.persistence.internal.oxm.MappingNodeValue; |
| 47 |
import org.eclipse.persistence.internal.oxm.MediaType; |
| 48 |
import org.eclipse.persistence.internal.oxm.NamespaceResolver; |
| 49 |
import org.eclipse.persistence.internal.oxm.NodeValue; |
| 50 |
import org.eclipse.persistence.internal.oxm.Root; |
| 51 |
import org.eclipse.persistence.internal.oxm.Unmarshaller; |
| 52 |
import org.eclipse.persistence.internal.oxm.XPathFragment; |
| 53 |
import org.eclipse.persistence.internal.oxm.mappings.Field; |
| 54 |
import org.eclipse.persistence.internal.oxm.record.AbstractUnmarshalRecord; |
| 55 |
import org.eclipse.persistence.internal.oxm.record.SAXUnmarshallerHandler; |
| 56 |
import org.eclipse.persistence.internal.oxm.record.UnmarshalRecord; |
| 57 |
import org.eclipse.persistence.internal.oxm.XPathNode; |
| 58 |
import org.eclipse.persistence.internal.oxm.record.XMLReaderAdapter; |
| 59 |
import org.eclipse.persistence.internal.oxm.record.deferred.DeferredContentHandler; |
| 60 |
import org.eclipse.persistence.oxm.mappings.nullpolicy.AbstractNullPolicy; |
| 61 |
import org.eclipse.persistence.oxm.record.XMLRootRecord; |
| 62 |
import org.xml.sax.ErrorHandler; |
| 63 |
import org.xml.sax.InputSource; |
| 64 |
import org.xml.sax.SAXException; |
| 65 |
import org.xml.sax.helpers.AttributesImpl; |
| 66 |
|
| 67 |
public class JsonStructureReader extends XMLReaderAdapter { |
| 68 |
|
| 69 |
private static final String TRUE = "true"; |
| 70 |
private static final String FALSE = "false"; |
| 71 |
private String attributePrefix = null; |
| 72 |
private NamespaceResolver namespaces = null; |
| 73 |
private boolean includeRoot; |
| 74 |
private String textWrapper; |
| 75 |
private Class unmarshalClass; |
| 76 |
private boolean isInCollection; |
| 77 |
private JsonStructure jsonStructure; |
| 78 |
private JsonAttributes attributes = new JsonAttributes(); |
| 79 |
|
| 80 |
|
| 81 |
public JsonStructureReader(Unmarshaller u) { |
| 82 |
this(u, null); |
| 83 |
} |
| 84 |
|
| 85 |
public JsonStructureReader(Unmarshaller u, Class clazz) { |
| 86 |
this(u.getAttributePrefix(), u.getNamespaceResolver(), u.getNamespaceResolver() != null, u.isIncludeRoot(), u.getNamespaceSeparator(), u.getErrorHandler(), u.getValueWrapper(), clazz); |
| 87 |
} |
| 88 |
|
| 89 |
private JsonStructureReader(String attrPrefix, NamespaceResolver nr,boolean namespaceAware, boolean includeRoot,Character namespaceSeparator, ErrorHandler errorHandler, String textWrapper, Class unmarshalClass) { |
| 90 |
this.attributePrefix = attrPrefix; |
| 91 |
if (attributePrefix == Constants.EMPTY_STRING) { |
| 92 |
attributePrefix = null; |
| 93 |
} |
| 94 |
namespaces = nr; |
| 95 |
this.namespaceAware = namespaceAware; |
| 96 |
if (namespaceSeparator == null) { |
| 97 |
this.namespaceSeparator = Constants.DOT; |
| 98 |
} else { |
| 99 |
this.namespaceSeparator = namespaceSeparator; |
| 100 |
} |
| 101 |
this.includeRoot = includeRoot; |
| 102 |
this.setErrorHandler(errorHandler); |
| 103 |
this.textWrapper = textWrapper; |
| 104 |
this.unmarshalClass = unmarshalClass; |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
public void setJsonStructure(JsonStructure jsonStructure) { |
| 109 |
this.jsonStructure = jsonStructure; |
| 110 |
} |
| 111 |
|
| 112 |
@Override |
| 113 |
public void parse(InputSource input) throws IOException, SAXException, JsonException { |
| 114 |
if (input == null) { |
| 115 |
if (jsonStructure != null) { |
| 116 |
parseRoot(jsonStructure); |
| 117 |
} |
| 118 |
return; |
| 119 |
} |
| 120 |
|
| 121 |
try { |
| 122 |
InputStream inputStream = null; |
| 123 |
JsonReader jsonReader = null; |
| 124 |
if (null != input.getByteStream()) { |
| 125 |
inputStream = input.getByteStream(); |
| 126 |
jsonReader = Json.createReader(new InputStreamReader(inputStream)); |
| 127 |
} else if (null != input.getCharacterStream()) { |
| 128 |
jsonReader = Json.createReader(input.getCharacterStream()); |
| 129 |
} else { |
| 130 |
try { |
| 131 |
URL url = new URL(input.getSystemId()); |
| 132 |
inputStream = url.openStream(); |
| 133 |
} catch (MalformedURLException malformedURLException) { |
| 134 |
try { |
| 135 |
inputStream = new FileInputStream(input.getSystemId()); |
| 136 |
} catch (FileNotFoundException fileNotFoundException) { |
| 137 |
throw malformedURLException; |
| 138 |
} |
| 139 |
} |
| 140 |
jsonReader = Json.createReader(new InputStreamReader(inputStream)); |
| 141 |
} |
| 142 |
if (jsonReader != null) { |
| 143 |
JsonStructure structure = jsonReader.read(); |
| 144 |
parseRoot(structure); |
| 145 |
} |
| 146 |
|
| 147 |
if (null != inputStream) { |
| 148 |
inputStream.close(); |
| 149 |
} |
| 150 |
} catch (JsonException je) { |
| 151 |
throw XMLMarshalException.unmarshalException(je); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
@Override |
| 156 |
public void parse(String systemId) { |
| 157 |
try { |
| 158 |
parse(new InputSource(systemId)); |
| 159 |
} catch (IOException e) { |
| 160 |
throw XMLMarshalException.unmarshalException(e); |
| 161 |
} catch (SAXException e) { |
| 162 |
throw XMLMarshalException.unmarshalException(e); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
public void parseRoot(JsonValue jsonValue) throws SAXException { |
| 167 |
if (namespaces != null) { |
| 168 |
Map<String, String> namespacePairs = namespaces.getPrefixesToNamespaces(); |
| 169 |
Iterator<String> keys = namespacePairs.keySet().iterator(); |
| 170 |
while (keys.hasNext()) { |
| 171 |
String nextKey = keys.next(); |
| 172 |
contentHandler.startPrefixMapping(nextKey, namespacePairs.get(nextKey)); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
if (jsonValue.getValueType() == ValueType.OBJECT) { |
| 177 |
contentHandler.startDocument(); |
| 178 |
JsonObject jsonObject = (JsonObject) jsonValue; |
| 179 |
|
| 180 |
Set<Entry<String, JsonValue>> children = jsonObject.entrySet(); |
| 181 |
if (children == null || children.size() == 0 && unmarshalClass == null) { |
| 182 |
return; |
| 183 |
} |
| 184 |
|
| 185 |
Iterator<Entry<String, JsonValue>> iter = children.iterator(); |
| 186 |
|
| 187 |
if (includeRoot) { |
| 188 |
if (children.size() > 0) { |
| 189 |
Entry<String, JsonValue> nextEntry = iter.next(); |
| 190 |
parsePair(nextEntry.getKey(), nextEntry.getValue()); |
| 191 |
} |
| 192 |
|
| 193 |
} else { |
| 194 |
|
| 195 |
contentHandler.startElement(Constants.EMPTY_STRING, Constants.EMPTY_STRING, null, attributes.setValue(jsonValue, attributePrefix, namespaces, namespaceSeparator, namespaceAware)); |
| 196 |
|
| 197 |
while (iter.hasNext()) { |
| 198 |
Entry<String, JsonValue> nextEntry = iter.next(); |
| 199 |
parsePair(nextEntry.getKey(), nextEntry.getValue()); |
| 200 |
} |
| 201 |
contentHandler.endElement(Constants.EMPTY_STRING, Constants.EMPTY_STRING, null); |
| 202 |
} |
| 203 |
contentHandler.endDocument(); |
| 204 |
} else if (jsonValue.getValueType() == ValueType.ARRAY) { |
| 205 |
|
| 206 |
SAXUnmarshallerHandler rootContentHandler = null; |
| 207 |
if (getContentHandler() instanceof SAXUnmarshallerHandler) { |
| 208 |
rootContentHandler = (SAXUnmarshallerHandler) getContentHandler(); |
| 209 |
} |
| 210 |
JsonArray jsonArray = (JsonArray) jsonValue; |
| 211 |
int size = jsonArray.size(); |
| 212 |
|
| 213 |
List list = new ArrayList(size); |
| 214 |
for (int x = 0; x < size; x++) { |
| 215 |
parseRoot(jsonArray.get(x)); |
| 216 |
if (getContentHandler() instanceof SAXUnmarshallerHandler) { |
| 217 |
SAXUnmarshallerHandler saxUnmarshallerHandler = (SAXUnmarshallerHandler) contentHandler; |
| 218 |
list.add(saxUnmarshallerHandler.getObject()); |
| 219 |
saxUnmarshallerHandler.setObject(null); |
| 220 |
} else if (getContentHandler() instanceof UnmarshalRecord) { |
| 221 |
UnmarshalRecord unmarshalRecord = (UnmarshalRecord) contentHandler; |
| 222 |
Object unmarshalledObject = unmarshalRecord.getCurrentObject(); |
| 223 |
if (includeRoot && unmarshalClass != null) { |
| 224 |
if (!(unmarshalledObject instanceof Root)) { |
| 225 |
Root xmlRoot = unmarshalRecord.createRoot(); |
| 226 |
xmlRoot.setNamespaceURI(unmarshalRecord.getRootElementNamespaceUri()); |
| 227 |
xmlRoot.setLocalName(unmarshalRecord.getLocalName()); |
| 228 |
xmlRoot.setObject(unmarshalledObject); |
| 229 |
unmarshalledObject = xmlRoot; |
| 230 |
} |
| 231 |
} |
| 232 |
list.add(unmarshalledObject); |
| 233 |
unmarshalRecord.setCurrentObject(null); |
| 234 |
unmarshalRecord.setRootElementName(null); |
| 235 |
unmarshalRecord.setLocalName(null); |
| 236 |
} |
| 237 |
} |
| 238 |
if (getContentHandler() instanceof SAXUnmarshallerHandler) { |
| 239 |
((SAXUnmarshallerHandler) getContentHandler()).setObject(list); |
| 240 |
} else if (getContentHandler() instanceof UnmarshalRecord) { |
| 241 |
((UnmarshalRecord) getContentHandler()).setCurrentObject(list); |
| 242 |
((UnmarshalRecord) getContentHandler()).setRootElementName(Constants.EMPTY_STRING); |
| 243 |
((UnmarshalRecord) getContentHandler()).setLocalName(Constants.EMPTY_STRING); |
| 244 |
if (rootContentHandler != null) { |
| 245 |
rootContentHandler.setObject(list); |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
private void parseValue(JsonValue jsonValue) throws SAXException { |
| 253 |
ValueType valueType = jsonValue.getValueType(); |
| 254 |
if (valueType == ValueType.STRING) { |
| 255 |
String string = ((JsonString) jsonValue).getString(); |
| 256 |
contentHandler.characters(string); |
| 257 |
} else if (valueType == ValueType.FALSE) { |
| 258 |
contentHandler.characters(FALSE); |
| 259 |
} else if (valueType == ValueType.TRUE) { |
| 260 |
contentHandler.characters(TRUE); |
| 261 |
} else if (valueType == ValueType.NULL) { |
| 262 |
// do nothing |
| 263 |
} else if (valueType == ValueType.NUMBER) { |
| 264 |
JsonNumber number = ((JsonNumber)jsonValue); |
| 265 |
contentHandler.characters(number.toString()); |
| 266 |
} else if (valueType == ValueType.OBJECT) { |
| 267 |
JsonObject childObject = (JsonObject) jsonValue; |
| 268 |
Iterator<Entry<String, JsonValue>> iter = childObject.entrySet().iterator(); |
| 269 |
while (iter.hasNext()) { |
| 270 |
Entry<String, JsonValue> nextEntry = iter.next(); |
| 271 |
parsePair(nextEntry.getKey(), nextEntry.getValue()); |
| 272 |
} |
| 273 |
} else if(valueType == ValueType.ARRAY) { |
| 274 |
JsonArray childArray = (JsonArray)jsonValue; |
| 275 |
int size = childArray.size(); |
| 276 |
|
| 277 |
List list = new ArrayList(size); |
| 278 |
for(int x=0; x<size; x++) { |
| 279 |
JsonValue nextArrayValue = childArray.get(x); |
| 280 |
parseValue(nextArrayValue); |
| 281 |
} |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
private void parsePair(String name, JsonValue jsonValue) throws SAXException { |
| 286 |
if (jsonValue == null) { |
| 287 |
return; |
| 288 |
} |
| 289 |
ValueType valueType = jsonValue.getValueType(); |
| 290 |
|
| 291 |
if (valueType == ValueType.ARRAY) { |
| 292 |
JsonArray jsonArray = (JsonArray) jsonValue; |
| 293 |
String parentLocalName = name; |
| 294 |
|
| 295 |
if (attributePrefix != null && parentLocalName.startsWith(attributePrefix)) { |
| 296 |
// do nothing; |
| 297 |
return; |
| 298 |
} |
| 299 |
String uri = Constants.EMPTY_STRING; |
| 300 |
if (namespaceAware && namespaces != null) { |
| 301 |
if (parentLocalName.length() > 2) { |
| 302 |
int nsIndex = parentLocalName.indexOf(namespaceSeparator, 1); |
| 303 |
if (nsIndex > -1) { |
| 304 |
String prefix = parentLocalName.substring(0, nsIndex); |
| 305 |
uri = namespaces.resolveNamespacePrefix(prefix); |
| 306 |
} |
| 307 |
if (uri == null) { |
| 308 |
uri = namespaces.getDefaultNamespaceURI(); |
| 309 |
} else { |
| 310 |
parentLocalName = parentLocalName.substring(nsIndex + 1); |
| 311 |
} |
| 312 |
} else { |
| 313 |
uri = namespaces.getDefaultNamespaceURI(); |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
boolean isTextValue = isTextValue(parentLocalName); |
| 318 |
int arraySize = jsonArray.size(); |
| 319 |
if (arraySize == 0) { |
| 320 |
if (contentHandler instanceof UnmarshalRecord) { |
| 321 |
UnmarshalRecord ur = (UnmarshalRecord) contentHandler; |
| 322 |
XPathNode node = ur.getNonAttributeXPathNode(uri, parentLocalName, parentLocalName, null); |
| 323 |
if (node != null) { |
| 324 |
NodeValue nv = node.getNodeValue(); |
| 325 |
if (nv == null && node.getTextNode() != null) { |
| 326 |
nv = node.getTextNode().getUnmarshalNodeValue(); |
| 327 |
} |
| 328 |
if (nv != null && nv.isContainerValue()) { |
| 329 |
ur.getContainerInstance(((ContainerValue) nv)); |
| 330 |
} |
| 331 |
} |
| 332 |
} |
| 333 |
} |
| 334 |
startCollection(); |
| 335 |
|
| 336 |
XPathFragment groupingXPathFragment = null; |
| 337 |
XPathFragment itemXPathFragment = null; |
| 338 |
if (contentHandler instanceof UnmarshalRecord) { |
| 339 |
UnmarshalRecord unmarshalRecord = (UnmarshalRecord) contentHandler; |
| 340 |
if (unmarshalRecord.getUnmarshaller().isWrapperAsCollectionName()) { |
| 341 |
XPathNode unmarshalRecordXPathNode = unmarshalRecord.getXPathNode(); |
| 342 |
if (null != unmarshalRecordXPathNode) { |
| 343 |
XPathFragment currentFragment = new XPathFragment(); |
| 344 |
currentFragment.setLocalName(parentLocalName); |
| 345 |
currentFragment.setNamespaceURI(uri); |
| 346 |
currentFragment.setNamespaceAware(namespaceAware); |
| 347 |
XPathNode groupingXPathNode = unmarshalRecordXPathNode.getNonAttributeChildrenMap().get(currentFragment); |
| 348 |
if (groupingXPathNode != null) { |
| 349 |
if (groupingXPathNode.getUnmarshalNodeValue() instanceof CollectionGroupingElementNodeValue) { |
| 350 |
groupingXPathFragment = groupingXPathNode.getXPathFragment(); |
| 351 |
contentHandler.startElement(uri, parentLocalName, parentLocalName, new AttributesImpl()); |
| 352 |
XPathNode itemXPathNode = groupingXPathNode.getNonAttributeChildren().get(0); |
| 353 |
itemXPathFragment = itemXPathNode.getXPathFragment(); |
| 354 |
} else if (groupingXPathNode.getUnmarshalNodeValue() == null) { |
| 355 |
XPathNode itemXPathNode = groupingXPathNode.getNonAttributeChildren().get(0); |
| 356 |
if (itemXPathNode != null) { |
| 357 |
if (((MappingNodeValue) itemXPathNode.getUnmarshalNodeValue()).isContainerValue()) { |
| 358 |
groupingXPathFragment = groupingXPathNode.getXPathFragment(); |
| 359 |
contentHandler.startElement(uri, parentLocalName, parentLocalName, new AttributesImpl()); |
| 360 |
itemXPathFragment = itemXPathNode.getXPathFragment(); |
| 361 |
} |
| 362 |
} |
| 363 |
} |
| 364 |
} |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
for (int i = 0; i < arraySize; i++) { |
| 369 |
JsonValue nextArrayValue = jsonArray.get(i); |
| 370 |
if (nextArrayValue.getValueType() == ValueType.NULL) { |
| 371 |
((UnmarshalRecord) contentHandler).setNil(true); |
| 372 |
} |
| 373 |
|
| 374 |
if (!isTextValue) { |
| 375 |
if (null != itemXPathFragment) { |
| 376 |
contentHandler.startElement(itemXPathFragment.getNamespaceURI(), itemXPathFragment.getLocalName(), itemXPathFragment.getLocalName(), attributes.setValue(nextArrayValue, attributePrefix,namespaces, namespaceSeparator,namespaceAware)); |
| 377 |
} else { |
| 378 |
contentHandler.startElement(uri, parentLocalName,parentLocalName, attributes.setValue(nextArrayValue, attributePrefix,namespaces, namespaceSeparator,namespaceAware)); |
| 379 |
} |
| 380 |
|
| 381 |
} |
| 382 |
parseValue(nextArrayValue); |
| 383 |
if (!isTextValue) { |
| 384 |
if (null != itemXPathFragment) { |
| 385 |
contentHandler.endElement(itemXPathFragment.getNamespaceURI(),itemXPathFragment.getLocalName(),itemXPathFragment.getLocalName()); |
| 386 |
} else { |
| 387 |
contentHandler.endElement(uri, parentLocalName,parentLocalName); |
| 388 |
} |
| 389 |
} |
| 390 |
} |
| 391 |
} |
| 392 |
if (null != groupingXPathFragment) { |
| 393 |
contentHandler.endElement(uri,groupingXPathFragment.getLocalName(),groupingXPathFragment.getLocalName()); |
| 394 |
} |
| 395 |
endCollection(); |
| 396 |
} else { |
| 397 |
String qualifiedName = name; |
| 398 |
if (attributePrefix != null && qualifiedName.startsWith(attributePrefix)) { |
| 399 |
return; |
| 400 |
} |
| 401 |
String localName = qualifiedName; |
| 402 |
String uri = Constants.EMPTY_STRING; |
| 403 |
if (namespaceAware && namespaces != null) { |
| 404 |
if (localName.length() > 2) { |
| 405 |
int nsIndex = localName.indexOf(namespaceSeparator, 1); |
| 406 |
String prefix = Constants.EMPTY_STRING; |
| 407 |
if (nsIndex > -1) { |
| 408 |
prefix = localName.substring(0, nsIndex); |
| 409 |
} |
| 410 |
uri = namespaces.resolveNamespacePrefix(prefix); |
| 411 |
if (uri == null) { |
| 412 |
uri = namespaces.getDefaultNamespaceURI(); |
| 413 |
} else { |
| 414 |
localName = localName.substring(nsIndex + 1); |
| 415 |
} |
| 416 |
|
| 417 |
if (localName.equals(Constants.SCHEMA_TYPE_ATTRIBUTE) && uri.equals(javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI)) { |
| 418 |
return; |
| 419 |
} |
| 420 |
} else { |
| 421 |
uri = namespaces.getDefaultNamespaceURI(); |
| 422 |
} |
| 423 |
} |
| 424 |
if (contentHandler instanceof XMLRootRecord || contentHandler instanceof DeferredContentHandler) { |
| 425 |
// if its not namespaceAware don't report the "type" child as it |
| 426 |
// is will be read by the xsi:type lookup |
| 427 |
if (!namespaceAware && localName.equals(Constants.SCHEMA_TYPE_ATTRIBUTE)) { |
| 428 |
return; |
| 429 |
} |
| 430 |
if (textWrapper != null && textWrapper.equals(localName)) { |
| 431 |
parseValue(jsonValue); |
| 432 |
return; |
| 433 |
} |
| 434 |
} else if (contentHandler instanceof UnmarshalRecord && ((UnmarshalRecord) contentHandler).getXPathNode() != null) { |
| 435 |
if (!namespaceAware && localName.equals(Constants.SCHEMA_TYPE_ATTRIBUTE) && !((UnmarshalRecord) contentHandler).getXPathNode().hasTypeChild()) { |
| 436 |
return; |
| 437 |
} |
| 438 |
boolean isTextValue = isTextValue(localName); |
| 439 |
if (isTextValue) { |
| 440 |
parseValue(jsonValue); |
| 441 |
return; |
| 442 |
} |
| 443 |
} |
| 444 |
if (jsonValue != null && jsonValue.getValueType() == valueType.NULL) { |
| 445 |
contentHandler.setNil(true); |
| 446 |
} |
| 447 |
|
| 448 |
contentHandler.startElement(uri, localName, localName, attributes.setValue(jsonValue, attributePrefix, namespaces,namespaceSeparator, namespaceAware)); |
| 449 |
parseValue(jsonValue); |
| 450 |
contentHandler.endElement(uri, localName, localName); |
| 451 |
|
| 452 |
} |
| 453 |
|
| 454 |
} |
| 455 |
|
| 456 |
public boolean isNullRepresentedByXsiNil(AbstractNullPolicy nullPolicy) { |
| 457 |
return true; |
| 458 |
} |
| 459 |
|
| 460 |
private void startCollection() { |
| 461 |
isInCollection = true; |
| 462 |
} |
| 463 |
|
| 464 |
private void endCollection() { |
| 465 |
isInCollection = false; |
| 466 |
} |
| 467 |
|
| 468 |
public boolean isInCollection() { |
| 469 |
return isInCollection; |
| 470 |
} |
| 471 |
|
| 472 |
private boolean isTextValue(String localName) { |
| 473 |
XPathNode currentNode = ((UnmarshalRecord) contentHandler).getXPathNode(); |
| 474 |
if (currentNode == null) { |
| 475 |
return textWrapper != null && textWrapper.equals(localName); |
| 476 |
} |
| 477 |
|
| 478 |
return ((currentNode.getNonAttributeChildrenMap() == null || currentNode.getNonAttributeChildrenMap().size() == 0 || (currentNode.getNonAttributeChildrenMap().size() == 1 && currentNode.getTextNode() != null))&& textWrapper != null && textWrapper.equals(localName)); |
| 479 |
} |
| 480 |
|
| 481 |
@Override |
| 482 |
public Object convertValueBasedOnSchemaType(Field xmlField, Object value, ConversionManager conversionManager, AbstractUnmarshalRecord record) { |
| 483 |
if (xmlField.getSchemaType() != null) { |
| 484 |
if (Constants.QNAME_QNAME.equals(xmlField.getSchemaType())) { |
| 485 |
String stringValue = (String) value; |
| 486 |
int indexOpen = stringValue.indexOf('{'); |
| 487 |
int indexClose = stringValue.indexOf('}'); |
| 488 |
String uri = null; |
| 489 |
String localName = null; |
| 490 |
if (indexOpen > -1 && indexClose > -1) { |
| 491 |
uri = stringValue.substring(indexOpen + 1, indexClose); |
| 492 |
localName = stringValue.substring(indexClose + 1); |
| 493 |
} else { |
| 494 |
QName obj = (QName) xmlField.convertValueBasedOnSchemaType(stringValue, conversionManager, record); |
| 495 |
localName = obj.getLocalPart(); |
| 496 |
uri = obj.getNamespaceURI(); |
| 497 |
} |
| 498 |
if (uri != null) { |
| 499 |
return new QName(uri, localName); |
| 500 |
} else { |
| 501 |
return new QName(localName); |
| 502 |
} |
| 503 |
} else { |
| 504 |
Class fieldType = xmlField.getType(); |
| 505 |
if (fieldType == null) { |
| 506 |
fieldType = xmlField.getJavaClass(xmlField.getSchemaType()); |
| 507 |
} |
| 508 |
return conversionManager.convertObject(value, fieldType, xmlField.getSchemaType()); |
| 509 |
} |
| 510 |
} |
| 511 |
return value; |
| 512 |
} |
| 513 |
|
| 514 |
/** |
| 515 |
* INTERNAL: The MediaType associated with this reader |
| 516 |
* |
| 517 |
* @return |
| 518 |
*/ |
| 519 |
@Override |
| 520 |
public MediaType getMediaType() { |
| 521 |
return Constants.APPLICATION_JSON; |
| 522 |
} |
| 523 |
|
| 524 |
private static class JsonAttributes extends IndexedAttributeList { |
| 525 |
|
| 526 |
private JsonValue value; |
| 527 |
private String attributePrefix; |
| 528 |
private char namespaceSeparator; |
| 529 |
private NamespaceResolver namespaces; |
| 530 |
private boolean namespaceAware; |
| 531 |
|
| 532 |
public JsonAttributes setValue(JsonValue value, String attributePrefix, NamespaceResolver nr, char namespaceSeparator, boolean namespaceAware) { |
| 533 |
reset(); |
| 534 |
this.value = value; |
| 535 |
this.attributePrefix = attributePrefix; |
| 536 |
this.namespaces = nr; |
| 537 |
this.namespaceSeparator = namespaceSeparator; |
| 538 |
this.namespaceAware = namespaceAware; |
| 539 |
return this; |
| 540 |
} |
| 541 |
|
| 542 |
private void addSimpleAttribute(List attributes, String uri, String attributeLocalName, JsonValue childValue) { |
| 543 |
if (childValue.getValueType() == ValueType.STRING) { |
| 544 |
String stringValue = ((JsonString) childValue).getString(); |
| 545 |
attributes.add(new Attribute(uri, attributeLocalName, attributeLocalName, stringValue)); |
| 546 |
} else if (childValue.getValueType() == ValueType.NUMBER) { |
| 547 |
attributes.add(new Attribute(uri, attributeLocalName, attributeLocalName, ((JsonNumber) childValue).toString())); |
| 548 |
} else if (childValue.getValueType() == ValueType.NULL) { |
| 549 |
attributes.add(new Attribute(uri, attributeLocalName, attributeLocalName, Constants.EMPTY_STRING)); |
| 550 |
} else if (childValue.getValueType() == ValueType.FALSE) { |
| 551 |
attributes.add(new Attribute(uri, attributeLocalName, attributeLocalName, FALSE)); |
| 552 |
} else if (childValue.getValueType() == ValueType.TRUE) { |
| 553 |
attributes.add(new Attribute(uri, attributeLocalName, attributeLocalName, TRUE)); |
| 554 |
} |
| 555 |
} |
| 556 |
|
| 557 |
public int getIndex(String uri, String localName) { |
| 558 |
if (null == localName) { |
| 559 |
return -1; |
| 560 |
} |
| 561 |
int index = 0; |
| 562 |
for (Attribute attribute : attributes()) { |
| 563 |
if (namespaceAware) { |
| 564 |
QName testQName = new QName(uri, localName); |
| 565 |
if (localName.equals(attribute.getLocalName()) && uri.equals(attribute.getUri())) { |
| 566 |
return index; |
| 567 |
} |
| 568 |
} else { |
| 569 |
if (attribute.getName().equals(localName)) { |
| 570 |
return index; |
| 571 |
} |
| 572 |
} |
| 573 |
index++; |
| 574 |
} |
| 575 |
return -1; |
| 576 |
} |
| 577 |
|
| 578 |
@Override |
| 579 |
protected Attribute[] attributes() { |
| 580 |
if (null == attributes) { |
| 581 |
|
| 582 |
if (value.getValueType() == ValueType.NULL) { |
| 583 |
return NO_ATTRIBUTES; |
| 584 |
} |
| 585 |
|
| 586 |
if (value.getValueType() == ValueType.OBJECT) { |
| 587 |
JsonObject jsonObject = (JsonObject) value; |
| 588 |
ArrayList<Attribute> attributesList = new ArrayList<Attribute>(jsonObject.values().size()); |
| 589 |
|
| 590 |
Iterator<Entry<String, JsonValue>> iter = jsonObject.entrySet().iterator(); |
| 591 |
while (iter.hasNext()) { |
| 592 |
Entry<String, JsonValue> nextEntry = iter.next(); |
| 593 |
JsonValue nextValue = nextEntry.getValue(); |
| 594 |
String attributeLocalName = nextEntry.getKey(); |
| 595 |
|
| 596 |
if (attributePrefix != null) { |
| 597 |
if (attributeLocalName.startsWith(attributePrefix)) { |
| 598 |
attributeLocalName = attributeLocalName.substring(attributePrefix.length()); |
| 599 |
} else { |
| 600 |
break; |
| 601 |
} |
| 602 |
} |
| 603 |
|
| 604 |
String uri = Constants.EMPTY_STRING; |
| 605 |
|
| 606 |
if (namespaceAware && namespaces != null) { |
| 607 |
if (attributeLocalName.length() > 2) { |
| 608 |
String prefix = Constants.EMPTY_STRING; |
| 609 |
int nsIndex = attributeLocalName.indexOf(namespaceSeparator, 1); |
| 610 |
if (nsIndex > -1) { |
| 611 |
prefix = attributeLocalName.substring(0, nsIndex); |
| 612 |
} |
| 613 |
uri = namespaces.resolveNamespacePrefix(prefix); |
| 614 |
if (uri == null) { |
| 615 |
uri = namespaces.getDefaultNamespaceURI(); |
| 616 |
} else { |
| 617 |
attributeLocalName = attributeLocalName.substring(nsIndex + 1); |
| 618 |
} |
| 619 |
} else { |
| 620 |
uri = namespaces.getDefaultNamespaceURI(); |
| 621 |
} |
| 622 |
} |
| 623 |
|
| 624 |
if (nextValue.getValueType() == ValueType.ARRAY) { |
| 625 |
JsonArray jsonArray = (JsonArray) nextValue; |
| 626 |
if (jsonArray.size() == 0) { |
| 627 |
attributesList.add(new Attribute(uri, attributeLocalName, attributeLocalName, "")); |
| 628 |
} |
| 629 |
for (int y = 0; y < jsonArray.size(); y++) { |
| 630 |
JsonValue nextChildValue = jsonArray.get(y); |
| 631 |
addSimpleAttribute(attributesList, uri, attributeLocalName, nextChildValue); |
| 632 |
} |
| 633 |
} else { |
| 634 |
addSimpleAttribute(attributesList, uri, attributeLocalName, nextValue); |
| 635 |
} |
| 636 |
} |
| 637 |
|
| 638 |
attributes = attributesList.toArray(new Attribute[attributesList.size()]); |
| 639 |
} else { |
| 640 |
attributes = NO_ATTRIBUTES; |
| 641 |
} |
| 642 |
} |
| 643 |
return attributes; |
| 644 |
} |
| 645 |
|
| 646 |
} |
| 647 |
|
| 648 |
} |