summaryrefslogtreecommitdiffstats
path: root/sca-java-1.x/branches/sca-java-1.6.2/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/BaseStAXArtifactProcessor.java
blob: 023c6786173570464a4c1d916ccd3d55890438bf (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
/*
 * 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.sca.contribution.processor;

import static javax.xml.stream.XMLStreamConstants.END_ELEMENT;
import static javax.xml.stream.XMLStreamConstants.START_ELEMENT;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.StringTokenizer;

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

import org.apache.tuscany.sca.assembly.Extensible;
import org.apache.tuscany.sca.assembly.Extension;
import org.apache.tuscany.sca.assembly.ExtensionFactory;
import org.apache.tuscany.sca.contribution.service.ContributionReadException;
import org.apache.tuscany.sca.contribution.service.ContributionWriteException;


/**
 * A base class with utility methods for the other artifact processors in this module.
 *
 * @version $Rev$ $Date$
 */
public abstract class BaseStAXArtifactProcessor {

    /**
     * Returns a QName from a string.
     * @param reader
     * @param value
     * @return
     */
    protected QName getQNameValue(XMLStreamReader reader, String value) {
        if (value != null) {
            int index = value.indexOf(':');
            String prefix = index == -1 ? "" : value.substring(0, index);
            String localName = index == -1 ? value : value.substring(index + 1);
            String ns = reader.getNamespaceContext().getNamespaceURI(prefix);
            if (ns == null) {
                ns = "";
            }
            return new QName(ns, localName, prefix);
        } else {
            return null;
        }
    }

    /**
     * Returns the boolean value of an attribute.
     * @param reader
     * @param name
     * @return
     */
    protected boolean getBoolean(XMLStreamReader reader, String name) {
        String value = reader.getAttributeValue(null, name);
        if (value == null) {
            return false;
        }
        return Boolean.valueOf(value);
    }

    /**
     * Returns the QName value of an attribute.
     * @param reader
     * @param name
     * @return
     */
    protected QName getQName(XMLStreamReader reader, String name) {
        String qname = reader.getAttributeValue(null, name);
        return getQNameValue(reader, qname);
    }

    /**
     * Returns the value of an attribute as a list of QNames.
     * @param reader
     * @param name
     * @return
     */
    protected List<QName> getQNames(XMLStreamReader reader, String name) {
        String value = reader.getAttributeValue(null, name);
        if (value != null) {
            List<QName> qnames = new ArrayList<QName>();
            for (StringTokenizer tokens = new StringTokenizer(value); tokens.hasMoreTokens();) {
                qnames.add(getQName(reader, tokens.nextToken()));
            }
            return qnames;
        } else {
            return Collections.emptyList();
        }
    }

    /**
     * Returns the string value of an attribute.
     * @param reader
     * @param name
     * @return
     */
    protected String getString(XMLStreamReader reader, String name) {
        return reader.getAttributeValue(null, name);
    }
    
    /**
     * TUSCANY-242
     * 
     * Returns the URI value of an attribute as a string and first applies the 
     * URI whitespace processing as defined in section 4.3.6 of XML Schema Part2: Datatypes
     * [http://www.w3.org/TR/xmlschema-2/#rf-facets]. anyURI is defined with the following 
     * XSD:
     *   <xs:simpleType name="anyURI" id="anyURI">
     *        <xs:restriction base="xs:anySimpleType">
     *          <xs:whiteSpace value="collapse" fixed="true" id="anyURI.whiteSpace"/>
     *        </xs:restriction>
     *   </xs:simpleType>
     *   
     * The <xs:whiteSpace value="collapse"/> constraining facet is defined as follows
     * 
     *   replace
     *      All occurrences of #x9 (tab), #xA (line feed) and #xD (carriage return) are replaced with #x20 (space) 
     *   collapse
     *      After the processing implied by replace, contiguous sequences of #x20's are collapsed to a single #x20, 
     *      and leading and trailing #x20's are removed
     *      
     * It seems that the StAX parser does apply this rule so we do it here. 
     * 
     * @param reader
     * @param name
     * @return
     */
    public static String getURIString(XMLStreamReader reader, String name) {
        // get the basic string value
        String uri = reader.getAttributeValue(null, name);
        
        // apply the "collapse" rule
        if (uri != null){
            // turn tabs, line feeds and carriage returns into spaces
            uri = uri.replace('\t', ' ');
            uri = uri.replace('\n', ' ');
            uri = uri.replace('\r', ' ');
            
            // remote leading and trailing spaces. Other whitespace
            // has already been converted to spaces above
            uri = uri.trim();
                        
            // collapse any contiguous spaces into a single space
            StringBuilder sb= new StringBuilder(uri.length());
            boolean spaceFound= false;
            for(int i=0; i< uri.length(); ++i){
                char c= uri.charAt(i);
                if(c == ' '){
                    if(!spaceFound){
                        sb.append(c);
                        spaceFound = true;
                    } else {
                        // collapse the space by ignoring it
                    }
                }else{
                    sb.append(c);
                    spaceFound= false;
                }
            }     
            uri = sb.toString();            
        }
        
        return uri;
    }    

    /**
     * Test if an attribute is explicitly set
     * @param reader
     * @param name
     * @return
     */
    protected boolean isSet(XMLStreamReader reader, String name) {
        return reader.getAttributeValue(null, name) != null;
    }

    /**
     * Returns the value of xsi:type attribute
     * @param reader The XML stream reader
     * @return The QName of the type, if the attribute is not present, null is
     *         returned.
     */
    protected QName getXSIType(XMLStreamReader reader) {
        String qname = reader.getAttributeValue(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "type");
        return getQNameValue(reader, qname);
    }

    /**
     * Parse the next child element.
     * @param reader
     * @return
     * @throws XMLStreamException
     */
    protected boolean nextChildElement(XMLStreamReader reader) throws XMLStreamException {
        while (reader.hasNext()) {
            int event = reader.next();
            if (event == END_ELEMENT) {
                return false;
            }
            if (event == START_ELEMENT) {
                return true;
            }
        }
        return false;
    }

    /**
     * Advance the stream to the next END_ELEMENT event skipping any nested
     * content.
     * @param reader the reader to advance
     * @throws XMLStreamException if there was a problem reading the stream
     */
    protected void skipToEndElement(XMLStreamReader reader) throws XMLStreamException {
        int depth = 0;
        while (reader.hasNext()) {
            int event = reader.next();
            if (event == XMLStreamConstants.START_ELEMENT) {
                depth++;
            } else if (event == XMLStreamConstants.END_ELEMENT) {
                if (depth == 0) {
                    return;
                }
                depth--;
            }
        }
    }

    /**
     *
     * @param writer
     * @param uri
     * @throws XMLStreamException
     */
    private String writeElementPrefix(XMLStreamWriter writer, String uri) throws XMLStreamException {
        if (uri == null) {
            return null;
        }
        String prefix = writer.getPrefix(uri);
        if (prefix != null) {
            return null;
        } else {

            // Find an available prefix and bind it to the given URI
            NamespaceContext nsc = writer.getNamespaceContext();
            for (int i=1; ; i++) {
                prefix = "ns" + i;
                if (nsc.getNamespaceURI(prefix) == null) {
                    break;
                }
            }
            // writer.setPrefix(prefix, uri);
            return prefix;
        }

    }

    /**
     * Start an element.
     * @param uri
     * @param name
     * @param attrs
     * @throws XMLStreamException
     */
    protected void writeStart(XMLStreamWriter writer, String uri, String name, XAttr... attrs) throws XMLStreamException {
        String prefix = writeElementPrefix(writer, uri);
        writer.writeStartElement(uri, name);
        if (prefix != null){
            writer.writeNamespace(prefix,uri);
        }
        writeAttributePrefixes(writer, attrs);
        writeAttributes(writer, attrs);
    }

    /**
     * Start an element.
     * @param qname
     * @param attrs
     * @throws XMLStreamException
     */
    protected void writeStart(XMLStreamWriter writer, QName qname, XAttr... attrs) throws XMLStreamException {
        writeStart(writer, qname.getNamespaceURI(), qname.getLocalPart(), attrs);
    }

    /**
     * End an element.
     * @param writer
     * @throws XMLStreamException
     */
    protected void writeEnd(XMLStreamWriter writer) throws XMLStreamException {
        writer.writeEndElement();
    }

    /**
     * Start a document.
     * @param writer
     * @throws XMLStreamException
     */
    protected void writeStartDocument(XMLStreamWriter writer, String uri, String name, XAttr... attrs) throws XMLStreamException {
        writer.writeStartDocument();
        writer.setDefaultNamespace(uri);
        writeStart(writer, uri, name, attrs);
        writer.writeDefaultNamespace(uri);
    }

    /**
     * Start a document.
     * @param writer
     * @param qname
     * @param attrs
     * @throws XMLStreamException
     */
    protected void writeStartDocument(XMLStreamWriter writer, QName qname, XAttr... attrs) throws XMLStreamException {
        writeStartDocument(writer, qname.getNamespaceURI(), qname.getLocalPart(), attrs);
    }

    /**
     * End a document.
     * @param writer
     * @throws XMLStreamException
     */
    protected void writeEndDocument(XMLStreamWriter writer) throws XMLStreamException {
        writer.writeEndDocument();
    }

    /**
     * Write attributes to the current element.
     * @param writer
     * @param attrs
     * @throws XMLStreamException
     */
    protected void writeAttributes(XMLStreamWriter writer, XAttr... attrs) throws XMLStreamException {
        for (XAttr attr : attrs) {
            if (attr != null)
                attr.write(writer);
        }
    }

    /**
     * Write attribute prefixes to the current element.
     * @param writer
     * @param attrs
     * @throws XMLStreamException
     */
    protected void writeAttributePrefixes(XMLStreamWriter writer, XAttr... attrs) throws XMLStreamException {
        for (XAttr attr : attrs) {
            if (attr != null)
                attr.writePrefix(writer);
        }
    }

    /**
     *
     * @param reader
     * @param elementName
     * @param extensible
     * @param extensionAttributeProcessor
     * @param extensionAttributeProcessor
     * @param extensionFactory
     * @throws ContributionReadException
     * @throws XMLStreamException
     */
    protected void readExtendedAttributes(XMLStreamReader reader,
                                          Extensible extensible,
                                          StAXAttributeProcessor extensionAttributeProcessor,
                                          ExtensionFactory extensionFactory) throws ContributionReadException,
        XMLStreamException {
        QName elementName = reader.getName();
        for (int a = 0; a < reader.getAttributeCount(); a++) {
            QName attributeName = reader.getAttributeName(a);
            if (attributeName.getNamespaceURI() != null && attributeName.getNamespaceURI().length() > 0) {
                if (!elementName.getNamespaceURI().equals(attributeName.getNamespaceURI())) {
                    Object attributeValue = extensionAttributeProcessor.read(attributeName, reader);
                    Extension attributeExtension;
                    if (attributeValue instanceof Extension) {
                        attributeExtension = (Extension)attributeValue;
                    } else {
                        attributeExtension = extensionFactory.createExtension(attributeName, attributeValue, true);
                    }
                    extensible.getAttributeExtensions().add(attributeExtension);
                }
            }
        }
    }

    /**
     *
     * @param attributeModel
     * @param writer
     * @param extensibleElement
     * @param extensionAttributeProcessor
     * @throws ContributionWriteException
     * @throws XMLStreamException
     */
    protected void writeExtendedAttributes(XMLStreamWriter writer,
                                           Extensible extensibleElement,
                                           StAXAttributeProcessor extensionAttributeProcessor)
        throws ContributionWriteException, XMLStreamException {
        for (Extension extension : extensibleElement.getAttributeExtensions()) {
            if (extension.isAttribute()) {
                extensionAttributeProcessor.write(extension, writer);
            }
        }
    }

    protected void readExtendedElement(XMLStreamReader reader,
                                       Extensible extensible,
                                       StAXArtifactProcessor extensionProcessor) throws ContributionReadException,
        XMLStreamException {
        Object ext = extensionProcessor.read(reader);
        if (extensible != null) {
            extensible.getExtensions().add(ext);
        }
    }

    protected void writeExtendedElements(XMLStreamWriter writer,
                                         Extensible extensible,
                                         StAXArtifactProcessor extensionProcessor) throws ContributionWriteException,
        XMLStreamException {
        for (Object ext : extensible.getExtensions()) {
            extensionProcessor.write(ext, writer);
        }
    }

    /**
     * Represents an XML attribute that needs to be written to a document.
     */
    public static class XAttr {

        private static final String SCA10_NS = "http://www.osoa.org/xmlns/sca/1.0";

        private String uri = SCA10_NS;
        private String name;
        private Object value;

        public XAttr(String uri, String name, String value) {
            this.uri = uri;
            this.name = name;
            this.value = value;
        }

        public XAttr(String name, String value) {
            this(null, name, value);
        }

        public XAttr(String uri, String name, List<?> values) {
            this.uri = uri;
            this.name = name;
            this.value = values;
        }

        public XAttr(String name, List<?> values) {
            this(null, name, values);
        }

        public XAttr(String uri, String name, Boolean value) {
            this.uri = uri;
            this.name = name;
            this.value = value;
        }

        public XAttr(String name, Boolean value) {
            this(null, name, value);
        }

        public XAttr(String uri, String name, Integer value) {
            this.uri = uri;
            this.name = name;
            this.value = value;
        }

        public XAttr(String name, Integer value) {
            this(null, name, value);
        }

        public XAttr(String uri, String name, Double value) {
            this.uri = uri;
            this.name = name;
            this.value = value;
        }

        public XAttr(String name, Double value) {
            this(null, name, value);
        }

        public XAttr(String uri, String name, QName value) {
            this.uri = uri;
            this.name = name;
            this.value = value;
        }

        public XAttr(String name, QName value) {
            this(null, name, value);
        }

        /**
         * Writes a string from a QName and registers a prefix for its namespace.
         * @param reader
         * @param value
         * @return
         */
        private String writeQNameValue(XMLStreamWriter writer, QName qname) throws XMLStreamException {
            if (qname != null) {
                String prefix = qname.getPrefix();
                String uri = qname.getNamespaceURI();
                prefix = writer.getPrefix(uri);
                if (prefix != null) {

                    // Use the prefix already bound to the given URI
                    if (prefix.length() > 0) {
                        return prefix + ":" + qname.getLocalPart();
                    } else {

                        // Empty prefix, just return the local part of the given qname
                        return qname.getLocalPart();
                    }

                } else {

                    // Find an available prefix and bind it to the given URI
                    NamespaceContext nsc = writer.getNamespaceContext();
                    for (int i=1; ; i++) {
                        prefix = "ns" + i;
                        if (nsc.getNamespaceURI(prefix) == null) {
                            break;
                        }
                    }
                    // writer.setPrefix(prefix, uri);
                    writer.writeNamespace(prefix, uri);
                    return prefix + ":" + qname.getLocalPart();
                }
            } else {
                return null;
            }
        }

        /**
         * Registers a prefix for the namespace of a QName.
         * @param reader
         * @param value
         * @return
         */
        private void writeQNamePrefix(XMLStreamWriter writer, QName qname) throws XMLStreamException {
            if (qname != null) {
                String prefix = qname.getPrefix();
                String uri = qname.getNamespaceURI();
                prefix = writer.getPrefix(uri);
                if (prefix != null) {
                    return;
                } else {

                    // Find an available prefix and bind it to the given URI
                    NamespaceContext nsc = writer.getNamespaceContext();
                    for (int i=1; ; i++) {
                        prefix = "ns" + i;
                        if (nsc.getNamespaceURI(prefix) == null) {
                            break;
                        }
                    }
                    // writer.setPrefix(prefix, uri);
                    writer.writeNamespace(prefix, uri);
                }
            }
        }

        /**
         * Write to document
         * @param writer
         * @throws XMLStreamException
         */
        public void write(XMLStreamWriter writer) throws XMLStreamException {
            String str;
            if (value instanceof QName) {

                // Write a QName
                str = writeQNameValue(writer, (QName)value);

            } else if (value instanceof List) {

                // Write a list of values
                List<?> values = (List<?>)value;
                if (values.isEmpty()) {
                    return;
                }
                StringBuffer buffer = new StringBuffer();
                for (Object v: values) {
                    if (v == null) {
                        // Skip null values
                        continue;
                    }

                    if (v instanceof XAttr) {
                        // Write an XAttr value
                        ((XAttr)v).write(writer);
                        continue;
                    }

                    if (buffer.length() != 0) {
                        buffer.append(' ');
                    }
                    if (v instanceof QName) {
                        // Write a QName value
                        buffer.append(writeQNameValue(writer, (QName)v));
                    } else {
                        // Write value as a string
                        buffer.append(String.valueOf(v));
                    }
                }
                str = buffer.toString();

            } else {

                // Write a string
                if (value == null) {
                    return;
                }
                str = String.valueOf(value);
            }
            if (str.length() == 0) {
                return;
            }

            // Write the attribute
            if (uri != null && !uri.equals(SCA10_NS)) {
                writer.writeAttribute(uri, name, str);
            } else {
                writer.writeAttribute(name,str);
            }
        }

        /**
         * Registers a prefix for the namespace of a QName or list of QNames
         * @param writer
         * @throws XMLStreamException
         */
        public void writePrefix(XMLStreamWriter writer) throws XMLStreamException {
            if (value instanceof QName) {

                // Write prefix for a single QName value
                writeQNamePrefix(writer, (QName)value);

            } else if (value instanceof List) {

                // Write prefixes for a list of values
                for (Object v: (List<?>)value) {
                    if (v instanceof QName) {
                        // Write prefix for a QName value
                        writeQNamePrefix(writer, (QName)v);

                    } else if (v instanceof XAttr) {
                        // Write prefix for an XAttr value
                        ((XAttr)v).writePrefix(writer);
                    }
                }
            }
        }
    }

}