summaryrefslogtreecommitdiffstats
path: root/sca-java-2.x/tags/2.0.1-RC1/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/BaseStAXArtifactProcessor.java
blob: 82902a11fe6a63930b4378cecaee6317bd6f0482 (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
/*
 * 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.Collection;
import java.util.List;

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

import org.apache.tuscany.sca.assembly.AssemblyFactory;
import org.apache.tuscany.sca.assembly.Extensible;
import org.apache.tuscany.sca.assembly.Extension;
import org.apache.tuscany.sca.common.xml.stax.StAXHelper;


/**
 * A base class with utility methods for the other artifact processors in this module.
 *
 * @version $Rev$ $Date$
 * @tuscany.spi.extension.inheritfrom
 */
public abstract class BaseStAXArtifactProcessor {
    private static String SCA11_NS = "http://docs.oasis-open.org/ns/opencsa/sca/200912";
    private static String SCA11_TUSCANY_NS = "http://tuscany.apache.org/xmlns/sca/1.1";

    /**
     * The StAXHelper without states
     */
    private static final StAXHelper helper = new StAXHelper(null, null, null);
    /**
     * Returns a QName from a string.
     * @param reader
     * @param value
     * @return
     */
    protected QName getQNameValue(XMLStreamReader reader, String value) {
        return StAXHelper.getValueAsQName(reader, value);
    }

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

    /**
     * Returns the QName value of an attribute.
     * @param reader
     * @param name
     * @return
     */
    protected QName getQName(XMLStreamReader reader, String name) {
        return StAXHelper.getAttributeAsQName(reader, name);
    }

    /**
     * Returns the value of an attribute as a list of QNames.
     * @param reader
     * @param name
     * @return
     */
    protected List<QName> getQNames(XMLStreamReader reader, String name) {
        return StAXHelper.getAttributeAsQNames(reader, name);
    }

    /**
     * Returns the string value of an attribute.
     * @param reader
     * @param name
     * @return
     */
    protected String getString(XMLStreamReader reader, String name) {
        return StAXHelper.getAttributeAsString(reader, name);
    }
    
    /**
     * Returns the string value of an attribute.
     * @param reader
     * @param name
     * @return
     */
    protected String getURIString(XMLStreamReader reader, String name) {
        return StAXHelper.getAttributeAsURIString(reader, name);
    }    

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

    /**
     * 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) {
        return StAXHelper.getXSIType(reader);
    }

    /**
     * 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 {
        StAXHelper.skipToEndElement(reader);
    }

    /**
     *
     * @param writer
     * @param uri
     * @throws XMLStreamException
     */
    private String setPrefix(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 {
        helper.writeStartElement(writer, "", name, uri);
        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);
        }
    }

    /**
     *
     * @param reader
     * @param extensible
     * @param extensionAttributeProcessor
     * @param extensionAttributeProcessor
     * @param extensionFactory
     * @param context TODO
     * @param elementName
     * @throws ContributionReadException
     * @throws XMLStreamException
     */
    protected void readExtendedAttributes(XMLStreamReader reader,
                                          Extensible extensible,
                                          StAXAttributeProcessor extensionAttributeProcessor,
                                          AssemblyFactory extensionFactory, ProcessorContext context) 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, context);
                    Extension attributeExtension;
                    if (attributeValue instanceof Extension) {
                        attributeExtension = (Extension)attributeValue;
                    } else {
                        attributeExtension = extensionFactory.createExtension();
                        attributeExtension.setQName(attributeName);
                        attributeExtension.setValue(attributeValue);
                        attributeExtension.setAttribute(true);
                    }
                    extensible.getAttributeExtensions().add(attributeExtension);
                }
            }
        }
    }

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

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

    protected void writeExtendedElements(XMLStreamWriter writer,
                                         Extensible extensible,
                                         StAXArtifactProcessor extensionProcessor, ProcessorContext context) 
        throws ContributionWriteException, XMLStreamException {
        
        List <Object> otherExtensions = new ArrayList<Object>();
        
        // write all generic extensions as elements 
        // to produce semanticaly equal xml output 
        for (Object ext : extensible.getExtensions()) {
            if (ext instanceof Extension) {
                extensionProcessor.write(ext, writer, context);
            } else {
                otherExtensions.add(ext);
            }
        }
        
        //wrap xml extensibility into a extension wrapper element
        if(otherExtensions.size() > 0) {
            
            writeStart(writer, SCA11_NS, "extensions");
            
            for (Object extension : otherExtensions) {
                extensionProcessor.write(extension, writer, context);
            }
            
            writeEnd(writer);
        }
        
    }

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

        private static final String SCA11_NS = "http://docs.oasis-open.org/ns/opencsa/sca/200912";

        private String uri = SCA11_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);
        }
        
        public String toString() {
            return uri == null ? name + "=\"" + value + "\"" : "{" + uri + "}" + 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 = helper.writeNamespace(writer, qname.getPrefix(), qname.getNamespaceURI());
                if ("".equals(prefix)) {
                    return qname.getLocalPart();
                } else {
                    return prefix + ":" + qname.getLocalPart();
                }
            }
            return null;
        }

        /**
         * 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 Collection) {

                // Write a list of values
                Collection<?> values = (Collection<?>)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 && (value instanceof Collection)) {
                return;
            }

            helper.writeAttribute(writer, "", name, uri, str);
        }
    }

}