Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 411373 | Differences between
and this patch

Collapse All | Expand All

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

Return to bug 411373