summaryrefslogtreecommitdiffstats
path: root/sdo-java/trunk/impl/src/main/java/org/apache/tuscany/sdo/util/resource/SDOXMLResourceImpl.java
blob: 172afb73dbf84b8b1a407ebfd85e822d8f5dd5ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
/**
 *
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 */
package org.apache.tuscany.sdo.util.resource;

import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;
import java.net.URL;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.xml.XMLConstants;
import javax.xml.namespace.NamespaceContext;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;

import org.apache.tuscany.sdo.SDOExtendedMetaData;
import org.apache.tuscany.sdo.helper.HelperContextImpl;
import org.apache.tuscany.sdo.helper.SDOExtendedMetaDataImpl;
import org.apache.tuscany.sdo.helper.XMLStreamHelper;
import org.apache.tuscany.sdo.helper.XSDHelperImpl;
import org.apache.tuscany.sdo.api.SDOHelper;
import org.apache.tuscany.sdo.api.SDOUtil;
import org.apache.tuscany.sdo.util.StAX2SAXAdapter;
import org.apache.tuscany.sdo.model.internal.InternalFactory;
import org.apache.tuscany.sdo.model.internal.impl.InternalFactoryImpl;
import org.eclipse.emf.common.util.EMap;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EDataType;
import org.eclipse.emf.ecore.EFactory;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.util.ExtendedMetaData;
import org.eclipse.emf.ecore.util.FeatureMap;
import org.eclipse.emf.ecore.xmi.XMIException;
import org.eclipse.emf.ecore.xmi.XMLHelper;
import org.eclipse.emf.ecore.xmi.XMLLoad;
import org.eclipse.emf.ecore.xmi.XMLOptions;
import org.eclipse.emf.ecore.xmi.XMLResource;
import org.eclipse.emf.ecore.xmi.XMLSave;
import org.eclipse.emf.ecore.xmi.impl.SAXXMLHandler;
import org.eclipse.emf.ecore.xmi.impl.XMLHelperImpl;
import org.eclipse.emf.ecore.xmi.impl.XMLLoadImpl;
import org.eclipse.emf.ecore.xmi.impl.XMLOptionsImpl;
import org.eclipse.emf.ecore.xmi.impl.XMLResourceImpl;
import org.eclipse.emf.ecore.xmi.impl.XMLSaveImpl;
import org.eclipse.emf.ecore.xmi.impl.XMLString;
import org.eclipse.emf.ecore.xmi.util.DefaultEcoreBuilder;
import org.eclipse.emf.ecore.xml.type.XMLTypePackage;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import commonj.sdo.ChangeSummary;
import commonj.sdo.DataObject;
import commonj.sdo.helper.XSDHelper;

public class SDOXMLResourceImpl extends XMLResourceImpl {
    private XMLStreamReader reader;

    /**
     * [rfeng] Override the XMLHelperImpl to replace the NamespaceSupport so that it's aware of the NamespaceContext from the XMLStreamReader
     */
    public static class SDOXMLHelperImpl extends XMLHelperImpl {

        /**
         * EMF XMLResource (SAX) may be used to load from only a *portion* of a StAX stream
         * which may reference (global) namespaces bound outside the (local) portion.
         * This class extends EMF's NamespaceSupport to make {@link #getPrefix} and {@link #getURI}
         * query these global binding(s) after first checking the local context(s).
         */
        private static class StreamNamespaceSupport extends XMLHelperImpl.NamespaceSupport {
            protected NamespaceContext nameSpaceContext;

            public String getPrefix(String uri) {
                String prefix = super.getPrefix(uri);
                if (prefix == null)
                    try {
                        prefix = nameSpaceContext.getPrefix(uri);
                    } catch (Exception e) {
                        // HACK:
                        // java.lang.UnsupportedOperationException
                        // at org.apache.axiom.om.impl.llom.OMStAXWrapper.getNamespaceContext(OMStAXWrapper.java:984)
                    }
                return prefix;
            }

            public String getURI(String prefix) {
                String uri = super.getURI(prefix);
                if (uri == null)
                    try {
                        uri = nameSpaceContext.getNamespaceURI(prefix);
                    } catch (Exception e) {
                        // HACK:
                        // java.lang.UnsupportedOperationException
                        // at org.apache.axiom.om.impl.llom.OMStAXWrapper.getNamespaceContext(OMStAXWrapper.java:984)
                    }
                return uri;
            }

            public StreamNamespaceSupport(XMLStreamReader reader) {
                super();
                nameSpaceContext = reader.getNamespaceContext();
            }

        }

        public SDOXMLHelperImpl(XMLResource resource, XMLStreamReader reader) {
            this(reader);
            setResource(resource);
        }

        public SDOXMLHelperImpl(XMLStreamReader reader) {
            super();
            if (reader instanceof XMLDocumentStreamReader) // Only use StreamNamespaceSupport when loading from a *portion* of a StAX stream
                namespaceSupport = new StreamNamespaceSupport(reader);
        }
        
        private class NameSpaceContext implements NamespaceContext { // TODO Helper# pushContext() & popContext
            public String getNamespaceURI(String prefix) {
                return SDOXMLHelperImpl.this.getNamespaceURI(prefix);
            }

            public String getPrefix(String namespaceURI) {
                return SDOXMLHelperImpl.this.getPrefix(namespaceURI);
            }

            public Iterator getPrefixes(String namespaceURI) {
                return ((Collection) urisToPrefixes.get(namespaceURI)).iterator();
            }
        }

        NameSpaceContext nameSpaceContext/* = null */;

        protected final NameSpaceContext nameSpaceContext() {
            if (nameSpaceContext == null)
                nameSpaceContext = new NameSpaceContext();
            return nameSpaceContext;
        }
        
        private String xsdQName2SDOURI(String xsdQName) {
            org.eclipse.emf.ecore.xml.type.internal.QName qname = new org.eclipse.emf.ecore.xml.type.internal.QName(xsdQName);
            try {
                updateQNameURI(qname);
            }
            catch (IllegalArgumentException e) {
                return xsdQName;
            }
            String uri = qname.getNamespaceURI();
            if (uri != "")
              return uri + "#" + qname.getLocalPart();
            else
              return qname.getLocalPart();
        }
        
        private String getPrefixFromNamespaceURI(String nsURI) {
            String nsPrefix = null;

            List prefixes = (List)urisToPrefixes.get(nsURI);
            if (prefixes != null)
            {
              for (Iterator i = prefixes.iterator(); i.hasNext(); )
              {
                nsPrefix = (String)i.next();
                if (nsPrefix.length() >= 0) {
                    // When the length is 0, it's the default namespace
                    return nsPrefix;
                }
              }
            }
            
            nsPrefix = namespaceSupport.getPrefix(nsURI);
            if (nsPrefix != null)
            {
              return nsPrefix;
            }
            
            // Demand create a new package
            EPackage ePackage = extendedMetaData.demandPackage(nsURI);

            if (ExtendedMetaData.XSI_URI.equals(nsURI)) {
                ePackage.setNsPrefix(ExtendedMetaData.XSI_PREFIX);
            }
            
            // getPrefix() will make sure all mapping tables are configured correctly
            nsPrefix = getPrefix(ePackage, true);
            
            return nsPrefix;
        }
        
        private String SDOURI2XsdQName(String sdoURI) {
            String namespace = null;
            String localPart = sdoURI;
            
            int index = sdoURI.indexOf('#');
            if (index == -1) {
                return localPart;
            }
            else {
                namespace = sdoURI.substring(0, index);
                localPart = sdoURI.substring(index+1);
                
                String prefix = getPrefixFromNamespaceURI(namespace);

                if (prefix.length() == 0)
                    return localPart;
                
                return prefix + ":" + localPart;
            }
        }
        
        protected Object createFromString(EFactory eFactory, EDataType eDataType, String value) {
            Object obj = super.createFromString(eFactory, eDataType, value);
            if (eDataType == ((InternalFactoryImpl)InternalFactory.INSTANCE).getQName()) {
                if (extendedMetaData != null) {
                    if (obj instanceof List) {
                        List list = (List)obj;
                        for (int i=0; i<list.size(); i++) {
                            String xsdQName = (String)list.get(i);
                            list.set(i, xsdQName2SDOURI(xsdQName));
                        }
                    }
                    else {
                        obj = xsdQName2SDOURI((String)obj);
                    }
                }
            }
            return obj;
        }
        
        public String convertToString(EFactory factory, EDataType eDataType, Object value) {
            if (eDataType == ((InternalFactoryImpl)InternalFactory.INSTANCE).getQName()) {
                if (extendedMetaData != null) {
                    if (value instanceof List) {
                        List list = (List)value;
                        for (int i=0; i<list.size(); i++) {
                            String sdoURI = (String)list.get(i);
                            list.set(i, SDOURI2XsdQName(sdoURI));
                        }
                    }
                    else {
                        value = SDOURI2XsdQName((String)value);
                    }
                }
            }
            
            return super.convertToString(factory, eDataType, value);
        }
    }

    public EObject root;

    /**
     * An EMF XMLLoad that loads a model from a StAX stream
     */
    public class SDOXMLLoadImpl extends XMLLoadImpl {
        public SDOXMLLoadImpl(XMLHelper helper) {
            super(helper);
        }

        final class XmlHandler extends SAXXMLHandler {
            XmlHandler() {
                super(resource, SDOXMLLoadImpl.this.helper, options);
            }

            protected void handleTopLocations(String prefix, String name) {
                processSchemaLocations(prefix, name);
                if (!processAnyXML)
                    return;
                String nameSpace = helper.getURI(prefix);
                if (extendedMetaData.getPackage(nameSpace) == null)
                    if (options.get(XMLStreamHelper.OPTION_DEFAULT_ROOT_TYPE) == null)
                        extendedMetaData.demandFeature(nameSpace, name, true);
                    else
                        extendedMetaData.demandPackage(nameSpace);
            }

            EClassifier defaultRootType(String prefix, String name, boolean isElement, EObject peekObject, String value) {
                Object type = options.get(XMLStreamHelper.OPTION_DEFAULT_ROOT_TYPE);
                if (type != null)
                    return (EClassifier) type;
                super.handleUnknownFeature(prefix, name, isElement, peekObject, value);
                return null;
            }

            protected void handleUnknownFeature(String prefix, String name, boolean isElement, EObject peekObject, String value) {
                if (objects.size() == 1) {
                    EFactory eFactory;
                    EClassifier type;
                    String typeQName = getXSIType();
                    if (typeQName == null) {
                        type = defaultRootType(prefix, name, isElement, peekObject, value);
                        if (type == null)
                            return;
                        eFactory = type.getEPackage().getEFactoryInstance();
                    } else {// createObjectFromTypeName
                        String typeName = null;
                        String xsiPrefix = XMLConstants.DEFAULT_NS_PREFIX;
                        int index = typeQName.indexOf(":");
                        if (index > 0) {
                            xsiPrefix = typeQName.substring(0, index);
                            typeName = typeQName.substring(index + 1);
                        } else
                            typeName = typeQName;
                        eFactory = getFactoryForPrefix(xsiPrefix);
                        if (eFactory != null)
                            type = helper.getType(eFactory, typeName);
                        else if (XMLConstants.DEFAULT_NS_PREFIX.equals(xsiPrefix) && helper.getURI(xsiPrefix) == null) {
                            EPackage ePackage = handleMissingPackage(null);
                            if (ePackage == null) {
                                type = defaultRootType(prefix, name, isElement, peekObject, value);
                                if (type == null)
                                    return;
                                eFactory = type.getEPackage().getEFactoryInstance();
                            } else
                                type = helper.getType(eFactory = ePackage.getEFactoryInstance(), typeName);
                        } else {
                            type = defaultRootType(prefix, name, isElement, peekObject, value);
                            if (type == null)
                                return;
                            eFactory = type.getEPackage().getEFactoryInstance();
                        }
                    }
                    root = helper.createObject(eFactory, type);
                    if (root != null) {
                        if (disableNotify)
                            root.eSetDeliver(false);
                        handleObjectAttribs(root);
                        processObject(root);
                        return;
                    }
                }
                super.handleUnknownFeature(prefix, name, isElement, peekObject, value);
            }

            protected RecordedEventXMLStreamReader.Tag tag/* =null */;

            protected List nameSpaces/* = null */;

            public void startPrefixMapping(String prefix, String uri) {
                if (nameSpaces == null)
                    nameSpaces = new ArrayList();
                RecordedEventXMLStreamReader.Tag.bind(prefix, uri, nameSpaces);
                if (tag == null)
                    super.startPrefixMapping(prefix, uri);
            }

            public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
                if (tag != null) {
                    tag.start(uri, localName, qName, attributes, locator, nameSpaces);
                    nameSpaces = null;
                    return;
                }
                EObject peekObject = objects.peekEObject();
                if (peekObject != null) {
                    String prefix = helper.getPrefix(uri.length() == 0 ? null : uri);
                    EStructuralFeature feature = getFeature(peekObject, prefix == null ? XMLConstants.DEFAULT_NS_PREFIX : prefix, localName, true);
                    if (feature != null && feature.getEType() == ChangeSummaryStreamSerializer.ChangeSummary_TYPE) {
                        tag = new RecordedEventXMLStreamReader.Tag(uri, localName, prefix, attributes, locator, ((SDOXMLHelperImpl) helper).nameSpaceContext(),
                                nameSpaces);
                        nameSpaces = null;
                        return;
                    }
                }
                if (nameSpaces != null)
                    nameSpaces.clear();
                super.startElement(uri, localName, qName, attributes);
            }

            public void characters(char[] ch, int start, int length) {
                if (tag == null)
                    super.characters(ch, start, length);
                else
                    tag.text(XMLStreamConstants.CHARACTERS, new String(ch, start, length), locator);
            }

            protected Collection changeSummaryDeserializers/* = null */;

            public void endElement(String uri, String localName, String qName) {
                if (tag == null)
                    super.endElement(uri, localName, qName);
                else if (tag.end(uri, localName, qName, locator)) {
                    if (changeSummaryDeserializers == null)
                        changeSummaryDeserializers = new ArrayList();
                    ChangeSummaryStreamDeserializer changeSummaryDeserializer = new ChangeSummaryStreamDeserializer();
                    try {
                        changeSummaryDeserializer.begin(
                          (DataObject) objects.peekEObject(), 
                          new HelperContextImpl(extendedMetaData, false), 
                          tag.play(xmlResource));
                        changeSummaryDeserializers.add(changeSummaryDeserializer);
                    } catch (XMLStreamException e) {
                        xmlResource.getErrors().add(new XMIException(e));
                    }
                    tag = null;
                }
            }

            public void endDocument() {
                super.endDocument();
                if (changeSummaryDeserializers != null)
                    for (Iterator iterator = changeSummaryDeserializers.iterator(); iterator.hasNext();)
                        try {
                            ((ChangeSummaryStreamDeserializer) iterator.next()).end();
                            // iterator.remove();
                        } catch (XMLStreamException e) {
                            xmlResource.getErrors().add(new XMIException(e));
                        }
            }
        }

        protected DefaultHandler makeDefaultHandler() {
            return new XmlHandler();
        }

        /**
         * Start parsing an XMLReader with the default handler.
         */
        public void load(XMLResource resource, final XMLStreamReader reader, Map options) throws IOException {
            this.resource = resource;
            Map mergedOptions = new HashMap(defaultLoadOptions);
            if (options != null)
                mergedOptions.putAll(options);

            this.options = mergedOptions;

            final ContentHandler handler = makeDefaultHandler();

            if (errors != null) {
                errors.clear();
            }

            final StAX2SAXAdapter adapter = new StAX2SAXAdapter(true);
            // Parse the XMLReader and generate SAX events
            try {
                AccessController.doPrivileged(new PrivilegedExceptionAction() {
                    public Object run() throws XMLStreamException, SAXException {
                        adapter.parse(reader, handler);
                        return null;
                    }
                });
            } catch (PrivilegedActionException e) {
                throw new Resource.IOWrappedException(e.getException());
            }

            helper = null;
            if (!resource.getErrors().isEmpty()) {
                Exception error = (Exception) resource.getErrors().get(0);
                if (error instanceof XMIException) {
                    XMIException exception = (XMIException) error;
                    if (exception.getWrappedException() != null) {
                        throw new Resource.IOWrappedException(exception.getWrappedException());
                    }
                }
                throw new Resource.IOWrappedException(error);
            }
        }
    }

    public SDOXMLResourceImpl(URI uri) {
        super(uri);
    }

    protected XMLHelper createXMLHelper() {
        return new SDOXMLHelperImpl(this, reader);
    }

    /**
     * @see org.eclipse.emf.ecore.xmi.impl.XMLResourceImpl#createXMLLoad()
     */
    protected XMLLoad createXMLLoad() {
        return new SDOXMLLoadImpl(createXMLHelper());
    }

    static protected int loadLaxForm;
    static {
        int defaultLaxForm = 0x4242;
        String property = System.getProperty("XML.load.form.lax");
        if (property == null)
            loadLaxForm = defaultLaxForm;
        else
            try {
                loadLaxForm = Integer.decode(property).intValue();
            } catch (NumberFormatException eNumberFormat) {
                loadLaxForm = defaultLaxForm;
            }
    }
    
    public void doLoad(InputSource inputSource, Map options) throws IOException {
        if (options != null) {
            /*
             * Tolerates element/attribute malform unless indicated not to
             */
            Object option = options.get(SDOHelper.XMLOptions.XML_LOAD_LAX_FORM);
            int tolerance = option == null ? loadLaxForm : ((Number) option).intValue();
            option = options.get(OPTION_EXTENDED_META_DATA);
            if (tolerance == 0) {
                if (option instanceof SDOExtendedMetaData)
                    ((SDOExtendedMetaData) option).setFeatureNamespaceMatchingLax(false);
            } else if (option instanceof SDOExtendedMetaData){
                ((SDOExtendedMetaData) option).setFeatureNamespaceMatchingLax(true);
            }
            else{
                options.put(OPTION_EXTENDED_META_DATA, option = new SDOExtendedMetaDataImpl()); // TODO copy (BasicExtendedMetaData)option
            }
            /*
             * Loads schema if necessary
             */
            if (Boolean.TRUE.equals(options.get(SDOHelper.XMLOptions.XML_LOAD_SCHEMA))){
                XMLOptions xmlOptions = (XMLOptions) options.get(OPTION_XML_OPTIONS);
                if (xmlOptions == null) {
                    xmlOptions = new XMLOptionsImpl();
                    options.put(OPTION_XML_OPTIONS, xmlOptions);
                }
                xmlOptions.setProcessSchemaLocations(true);
                if (option == null){
                    option = getDefaultLoadOptions().get(OPTION_EXTENDED_META_DATA);
                }
                ExtendedMetaData extendedMetaData;
                final XSDHelper xsdHelper;
                if (option == null) {
                    extendedMetaData = ExtendedMetaData.INSTANCE;
                    xsdHelper = XSDHelper.INSTANCE;
                } else {
                    extendedMetaData = (ExtendedMetaData) option;
                    xsdHelper = (new HelperContextImpl(extendedMetaData, false)).getXSDHelper();
                }
                xmlOptions.setEcoreBuilder(new DefaultEcoreBuilder(extendedMetaData) {
                    public Collection generate(Map targetNamespaceToURI) throws IOException {
                        for (Iterator iterator = targetNamespaceToURI.values().iterator(); iterator.hasNext();) {
                            String uri = iterator.next().toString();
                            xsdHelper.define(uri.indexOf(":/") == -1 ? Thread.currentThread().getContextClassLoader().getResourceAsStream(uri)
                                    : new URL(uri).openStream(), uri);
                        }
                        return null; // XMLHandler#processSchemaLocations doesn't take the result
                    }
                });
            }
            
            if (Boolean.TRUE.equals(options.get(SDOHelper.XMLOptions.XML_LOAD_UNKNOWN_PROPERTIES))) {
                options.put(OPTION_RECORD_UNKNOWN_FEATURE , Boolean.TRUE);
            }
        }
        else if (loadLaxForm != 0) {
            /*
             * Tolerates element/attribute malform
             */
            options = new HashMap();
            options.put(OPTION_EXTENDED_META_DATA, new SDOExtendedMetaDataImpl());
        }
        super.doLoad(inputSource, options);
        // TODO there is some thinking to be done about the restoration of options
    }

    /**
     * Loads the resource from a StAX XMLStreamReader.
     */
    public void load(XMLStreamReader reader, Map options) throws IOException {
        this.reader = reader;
        SDOXMLLoadImpl xmlLoad = (SDOXMLLoadImpl) createXMLLoad();
        Map mergedOptions = new HashMap(defaultLoadOptions);
        if (options != null)
            mergedOptions.putAll(options);
        xmlLoad.load(this, reader, mergedOptions);
    }

    ChangeSummaryStreamSerializer changeSummarySerializer/* = null*/;

    static private final class LocalName extends QName {
        private LocalName(String name) {
            super(name);
        }

        public String getNamespaceURI() {
            return null;
        }
    }

    static final String INDENT = "  ", LINE_SEPARATOR = System.getProperty("line.separator");

    static final class XmlString extends XMLString {
        XmlString(int lineWidth, String temporaryFileName) {
            super(lineWidth, temporaryFileName); // setLineWidth & setTemporaryFileName
        }

        XmlString(int lineWidth, String publicId, String systemId, String temporaryFileName) {
            super(lineWidth, publicId, systemId, temporaryFileName);
        }

        void setLineBreak(String lineBreak) {
            lineSeparator = lineBreak;
        }

        void margin(String margin) {
            indents.set(0, margin);
        }

        String indent = INDENT;

        protected String getElementIndent(int extra) {
            int nesting = depth + extra - 1;
            for (int i = indents.size() - 1; i < nesting; ++i) {
                indents.add(indents.get(i) + indent);
            }
            return (String) indents.get(nesting);
        }

        protected String getAttributeIndent() {
            return getElementIndent();
        }
        
        public final boolean mixed() {
            return isMixed;
        }

        public void reset(String publicId, String systemId, int lineWidth, String temporaryFileName) {
            super.reset(publicId, systemId, lineWidth, temporaryFileName);
            setLineBreak(LINE_SEPARATOR);
            indent = INDENT;
        }
    }

    static final char MARK = '\n';

    static final String LINE_BREAK = new String(new char[] { MARK });

    final class SDOXMLSaveImpl extends XMLSaveImpl {
        SDOXMLSaveImpl(XMLHelper helper) {
            super(helper);
        }

        XmlString doc(XMLResource resource, Map options) {
            if (doc instanceof XmlString)
                return (XmlString) doc;
            Object lineWidth = options.get(OPTION_LINE_WIDTH);
            int width = lineWidth == null ? Integer.MAX_VALUE : ((Number) lineWidth).intValue();
            XmlString d = resource != null && Boolean.TRUE.equals(options.get(OPTION_SAVE_DOCTYPE)) ? new XmlString(width, resource.getPublicId(),
                    resource.getSystemId(), doc.getTemporaryFileName()) : new XmlString(width, doc.getTemporaryFileName());
            doc = d;
            return d;
        }

        Map changeSummaryOptions = new HashMap();
        String indent = INDENT, margin;

        protected void init(XMLResource resource, Map options) {
            super.init(resource, options);
            int unformat = 0;
            String lineBreak = (String) options.get(SDOHelper.XMLOptions.XML_SAVE_LINE_BREAK);
            if (lineBreak == null)
                changeSummaryOptions.put(SDOHelper.XMLOptions.XML_SAVE_LINE_BREAK, LINE_BREAK);
            else if (lineBreak.length() == 0)
                ++unformat;
            else {
                changeSummaryOptions.put(SDOHelper.XMLOptions.XML_SAVE_LINE_BREAK, LINE_BREAK);
                if (lineBreak.equals(LINE_SEPARATOR))
                    lineBreak = null;
            }
            String indent = (String) options.get(SDOHelper.XMLOptions.XML_SAVE_INDENT);
            if (indent == null)
                changeSummaryOptions.put(SDOHelper.XMLOptions.XML_SAVE_INDENT, INDENT);
            else if (indent.length() == 0)
                ++unformat;
            else {
                changeSummaryOptions.put(SDOHelper.XMLOptions.XML_SAVE_INDENT, this.indent = indent);
                if (indent.equals(INDENT))
                    indent = null;
            }
            String margin = (String) options.get(SDOHelper.XMLOptions.XML_SAVE_MARGIN);
            if (margin == null || margin.length() == 0) {
                if (unformat == 2)
                    doc.setUnformatted(true);
                else if (lineBreak != null) {
                    XmlString d = doc(resource, options);
                    d.setLineBreak(lineBreak);
                    if (indent != null)
                        d.indent = indent;
                } else if (indent != null)
                    doc(resource, options).indent = indent;
                this.margin = this.indent;
            } else {
                XmlString d = doc(resource, options);
                d.margin(margin);
                if (lineBreak != null)
                    d.setLineBreak(lineBreak);
                if (indent != null)
                    d.indent = indent;
                this.margin = margin + this.indent;
                if (!toDOM && declareXML)
                    d.add(margin);
            }
            // changeSummaryOptions.put(ChangeSummaryStreamSerializer.OPTION_RootObject_PATH, "#");
            // changeSummaryOptions.put(ChangeSummaryStreamSerializer.OPTION_OPTIMIZE_LIST, Boolean.TRUE);
            changeSummaryOptions.put(OPTION_EXTENDED_META_DATA, extendedMetaData);
        }

        QName qName(EStructuralFeature f) {
            if (extendedMetaData == null)
                return new LocalName(f.getName());
            String nameSpace = extendedMetaData.getNamespace(f), name = extendedMetaData.getName(f);
            return nameSpace == null ? new LocalName(name) : new QName(nameSpace, name);
        }

        XMLStreamWriter xmlStreamWriter/* = null*/;

        void saveChangeSummary(EObject o, EStructuralFeature f, Object changeSummary) {
            boolean notMixed;
            if (doc instanceof XmlString)
                notMixed = !((XmlString) doc).mixed();
            else if (extendedMetaData == null)
                notMixed = true;
            else
                switch (extendedMetaData.getContentKind(o.eClass())) {
                case ExtendedMetaData.MIXED_CONTENT:
                case ExtendedMetaData.SIMPLE_CONTENT:
                    notMixed = false;
                    break;
                default:
                    notMixed = true;
                }
            if (notMixed) {
                StringBuffer margin = new StringBuffer(this.margin);
                for (EObject container = o.eContainer(), grandContainer; (grandContainer = container.eContainer()) != null; container = grandContainer)
                    margin.append(indent);
                changeSummaryOptions.put(SDOHelper.XMLOptions.XML_SAVE_MARGIN, margin.toString());
            }
            try {
                if (xmlStreamWriter == null) {
                    xmlStreamWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(new Writer() {
                        public void close() {
                        }

                        public void flush() {
                        }

                        protected final void add(char[] cbuf, int index, int off) {
                            doc.addText(new String(cbuf, index, off - index));
                        }

                        public void write(char[] cbuf, int off, int len) {
                            if (len != 0)
                                for (;;) {
                                    while (cbuf[off] == MARK) {
                                        doc.addLine();
                                        if (--len == 0)
                                            return;
                                        ++off;
                                    }
                                    for (int index = off;/* true */;) {
                                        ++off;
                                        if (--len == 0)
                                            add(cbuf, index, off);
                                        else {
                                            if (cbuf[off] != MARK)
                                                continue;
                                            add(cbuf, index, off);
                                            doc.addLine();
                                            if (--len != 0)
                                                break;
                                        }
                                        return;
                                    }
                                    ++off;
                                }
                        }
                    });
                    xmlStreamWriter.setNamespaceContext(((SDOXMLHelperImpl) helper).new NameSpaceContext() {
                        public String getNamespaceURI(String prefix) {
                            return declareXSI && ExtendedMetaData.XSI_PREFIX.equals(prefix) ? ExtendedMetaData.XSI_URI : super
                                    .getNamespaceURI(prefix);
                        }

                        public String getPrefix(String namespaceURI) {
                            return declareXSI && ExtendedMetaData.XSI_URI.equals(namespaceURI) ? ExtendedMetaData.XSI_PREFIX : super
                                    .getPrefix(namespaceURI);
                        }

                        public Iterator getPrefixes(String namespaceURI) {
                            final Iterator iterator = super.getPrefixes(namespaceURI);
                            return ExtendedMetaData.XSI_URI.equals(namespaceURI) ? new Iterator() {
                                boolean first = true;

                                public boolean hasNext() {
                                    if (first)
                                        if (declareXSI) // never from true to false
                                            return true;
                                        else
                                            first = false;
                                    return iterator.hasNext();
                                }

                                public Object next() {
                                    if (first) {
                                        first = false;
                                        if (declareXSI)
                                            return ExtendedMetaData.XSI_PREFIX;
                                    }
                                    return iterator.next();
                                }

                                public void remove() {
                                    if (first)
                                        declareXSI = false;
                                    else
                                        iterator.remove();
                                }
                            } : iterator;
                        }
                    });
                    for (Iterator iterator = helper.getPrefixToNamespaceMap().iterator(); iterator.hasNext();) {
                        Map.Entry entry = (Map.Entry) iterator.next();
                        xmlStreamWriter.setPrefix((String) entry.getKey(), (String) entry.getValue());
                    }
                    if (declareXSI)
                        xmlStreamWriter.setPrefix(ExtendedMetaData.XSI_PREFIX, ExtendedMetaData.XSI_URI);
                    if (changeSummarySerializer == null)
                        changeSummarySerializer = new ChangeSummaryStreamSerializer();
                }
                changeSummarySerializer.saveChangeSummary((ChangeSummary) changeSummary, qName(f), xmlStreamWriter, changeSummaryOptions);
                if (notMixed)
                    doc.addLine();
            } catch (XMLStreamException e) {
                xmlResource.getErrors().add(new XMIException(e));
            }
        }

        protected void saveDataTypeElementSingle(EObject o, EStructuralFeature f) {
            if (f.getEType() == ChangeSummaryStreamSerializer.ChangeSummary_TYPE)
                saveChangeSummary(o, f, helper.getValue(o, f));
            else
                super.saveDataTypeElementSingle(o, f);
        }
        
        /*
         * TEMPORARILY COPIED FROM BASE CLASS - DO NOT EDIT - WILL BE REMOVED WHEN WE MOVE TO EMF 2.3
         */
        protected boolean saveElementFeatureMap(EObject o, EStructuralFeature f)
        {
          List values = (List)helper.getValue(o, f);
          int size = values.size();
          for (int i = 0; i < size; i++)
          {
            FeatureMap.Entry entry = (FeatureMap.Entry)values.get(i);
            EStructuralFeature entryFeature = entry.getEStructuralFeature();
            Object value = entry.getValue();
            if (entryFeature instanceof EReference)
            {
              if (value == null)
              {
                saveNil(o, entryFeature);
              }
              else 
              {
                EReference referenceEntryFeature = (EReference)entryFeature;
                if (referenceEntryFeature.isContainment())
                {
                  saveElement((InternalEObject)value, entryFeature);
                }
                else if (referenceEntryFeature.isResolveProxies())
                {
                  saveFeatureMapElementReference((EObject)value, referenceEntryFeature);
                }
                else
                {
                  saveElementIDRef(o, (EObject)value, entryFeature);
                }
              }
            }
            else
            {
              if (entryFeature == XMLTypePackage.eINSTANCE.getXMLTypeDocumentRoot_Text())
              {
                String svalue = value.toString();
                if (escape != null)
                {
                  svalue =  escape.convertText(svalue);
                }        
                if (!toDOM)
                {    
                  doc.addText(svalue);
                }
                else
                {
                  Node text = document.createTextNode(svalue);
                  currentNode.appendChild(text);
                  handler.recordValues(text, o, f, entry);
                }
              }
              else if (entryFeature == XMLTypePackage.eINSTANCE.getXMLTypeDocumentRoot_CDATA())
              {
                String stringValue = value.toString();
                if (escape != null)
                {
                  stringValue = escape.convertLines(stringValue);
                }
                if (!toDOM)
                {
                  doc.addCDATA(stringValue);
                }
                else
                {
                  Node cdata = document.createCDATASection(stringValue);
                  currentNode.appendChild(cdata);
                  handler.recordValues(cdata, o, f, entry);            
                }
              }
              else if (entryFeature == XMLTypePackage.eINSTANCE.getXMLTypeDocumentRoot_Comment())
              {
                String stringValue = value.toString();
                if (escape != null)
                {
                  stringValue = escape.convertLines(stringValue);
                }
                if (!toDOM)
                {
                  doc.addComment(stringValue);
                }
                else
                {
                  // TODO comments are not sent to recordValues
                  currentNode.appendChild(document.createComment(stringValue));
                }
              }
              else
              {
                saveElement(o, value, entryFeature);
              }
            }
          }
          return size > 0;
        }

        protected final void saveElement(EObject o, Object value, EStructuralFeature f)
        {
          if (f.getEType() == ChangeSummaryStreamSerializer.ChangeSummary_TYPE)
          {
            saveChangeSummary(o, f, value);
            return;
          }
          /* super.saveElement(o, value, f);
           * TEMPORARILY COPIED FROM BASE CLASS - DO NOT EDIT - WILL BE REMOVED WHEN WE MOVE TO EMF 2.3
           */
          if (value == null)
          {
            saveNil(o, f);
          }
          else
          {
            String svalue =  getDatatypeValue(value, f, false);
            if (!toDOM)
            {
              doc.saveDataValueElement(helper.getQName(f), svalue);
            }
            else
            {
              helper.populateNameInfo(nameInfo, f);
              Element elem = document.createElementNS(nameInfo.getNamespaceURI(), nameInfo.getQualifiedName());
              Node text = document.createTextNode(svalue);
              elem.appendChild(text);
              currentNode.appendChild(elem);
              handler.recordValues(elem, o, f, value);
              handler.recordValues(text, o, f, value);
            }
          }
        }
    }

    protected XMLSave createXMLSave() {
        return new SDOXMLSaveImpl(createXMLHelper());
    }
}