summaryrefslogtreecommitdiffstats
path: root/sca-java-2.x/tags/2.0.1-RC1/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ValidatingXMLStreamReader.java
blob: 6de25d4eebe73c01e5a4d87baf7fcb29b8a40904 (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
/*
 * 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 java.util.logging.Logger;

import javax.xml.namespace.NamespaceContext;
import javax.xml.namespace.QName;
import javax.xml.stream.Location;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.util.StreamReaderDelegate;
import javax.xml.validation.Schema;
import javax.xml.validation.ValidatorHandler;

import org.apache.tuscany.sca.monitor.Monitor;
import org.apache.tuscany.sca.monitor.Problem;
import org.apache.tuscany.sca.monitor.Problem.Severity;
import org.xml.sax.Attributes;
import org.xml.sax.ErrorHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.AttributesImpl;

/**
 * 
 * A validating XMLStreamReader that reports XMLSchema validation errors.
 *
 * @version $Rev$ $Date$
 */
class ValidatingXMLStreamReader extends StreamReaderDelegate implements XMLStreamReader {

    private static final Logger logger = Logger.getLogger(ValidatingXMLStreamReader.class.getName());
    
    private ValidatorHandler handler;
    private Schema schema;
    private Monitor monitor;
    
    /**
     * Constructs a new ValidatingXMLStreamReader.
     * 
     * @param reader
     * @param schema
     * @throws XMLStreamException
     */
    ValidatingXMLStreamReader(XMLStreamReader reader, Schema schema, Monitor monitor) throws XMLStreamException {
        super(reader);
        this.monitor = monitor;
        this.schema = schema;
    }
    
    void setMonitor(Monitor monitor) {
        this.monitor = monitor;
    }

    private synchronized ValidatorHandler getHandler() throws XMLStreamException {
        if (schema == null || handler!=null) {
            return handler;
        }
        handler = schema.newValidatorHandler();
        handler.setDocumentLocator(new LocatorAdapter());
        try {
            handler.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
        } catch (SAXException e) {
        	XMLStreamException xse = new XMLStreamException(e);
        	error("XMLStreamException", handler, xse);
            throw xse;
        }
        
        // These validation errors are just warnings for us as we want to support
        // running from an XML document with XSD validation errors, as long as we can
        // get the metadata we need from the document
        handler.setErrorHandler(new ErrorHandler() {        	
            private String getMessage(SAXParseException e) {
                return "XMLSchema validation problem in: " + getArtifactName( e.getSystemId() ) + ", line: " + e.getLineNumber() + ", column: " + e.getColumnNumber() + "\n" + e.getMessage();
            }
            
            public void error(SAXParseException exception) throws SAXException {            	
            	if (ValidatingXMLStreamReader.this.monitor == null)
            		logger.warning(getMessage(exception));
            	else            		
            		ValidatingXMLStreamReader.this.error("SchemaError", ValidatingXMLStreamReader.this.getClass(), getArtifactName( exception.getSystemId() ), 
                   		exception.getLineNumber(), exception.getColumnNumber(), exception.getMessage());                              
            }
            
            public void fatalError(SAXParseException exception) throws SAXException {            	
            	if (ValidatingXMLStreamReader.this.monitor == null)
            		logger.warning(getMessage(exception));
            	else
            		ValidatingXMLStreamReader.this.error("SchemaFatalError", ValidatingXMLStreamReader.this.getClass(), getArtifactName( exception.getSystemId() ), 
                       	exception.getLineNumber(), exception.getColumnNumber(), exception.getMessage());               
            }
            
            public void warning(SAXParseException exception) throws SAXException {
            	if (ValidatingXMLStreamReader.this.monitor == null)
            		logger.warning(getMessage(exception));
            	else
            		ValidatingXMLStreamReader.this.warning("SchemaWarning", ValidatingXMLStreamReader.this.getClass(), getArtifactName( exception.getSystemId() ), 
                       	exception.getLineNumber(), exception.getColumnNumber(), exception.getMessage());                
            }
            
            private String getArtifactName( String input ) {
            	String artifactName = null;
            	if( ValidatingXMLStreamReader.this.monitor != null ) {
            		artifactName = ValidatingXMLStreamReader.this.monitor.getArtifactName();
            	}
            	if (artifactName == null){
            	    artifactName = input;
            	}
            	return artifactName;
            }
        });
        return handler;
    }
    
    /**
     * Report a warning.
     * 
     * @param problems
     * @param message
     * @param model
     */
    private void warning(String message, Object model, Object... messageParameters) {
        if (monitor != null) {
            Problem problem = monitor.createProblem(this.getClass().getName(), "contribution-validation-messages", Severity.WARNING, model, message, (Object[])messageParameters);
            monitor.problem(problem);
    	}
    }
    
    /**
     * Report a error.
     * 
     * @param problems
     * @param message
     * @param model
     */
    private void error(String message, Object model, Object... messageParameters) {
        if (monitor != null) {
            Problem problem = monitor.createProblem(this.getClass().getName(), "contribution-validation-messages", Severity.ERROR, model, message, (Object[])messageParameters);
            monitor.problem(problem);
        }
    }

    @Override
    public int next() throws XMLStreamException {
        if (getHandler() == null) {
            return super.next();
        }

        int event = super.getEventType();
        try {
            if (event == START_DOCUMENT) {
                // We need to trigger the startDocument()
                handler.startDocument();
            }
            event = super.next();
            validate(event);
        } catch (SAXException e) {
            XMLStreamException xse = new XMLStreamException(e.getMessage(), e);
            error("XMLStreamException", handler, xse);
            throw xse;
        }
        return event;
    }

    private void validate(int event) throws SAXException {
        switch (event) {
            case START_DOCUMENT:
                handler.startDocument();
                break;
            case START_ELEMENT:
                handleStartElement();
                break;
            case PROCESSING_INSTRUCTION:
                handler.processingInstruction(super.getPITarget(), super.getPIData());
                break;
            case CHARACTERS:
            case CDATA:
            case SPACE:
            case ENTITY_REFERENCE:
                handler.characters(super.getTextCharacters(), super.getTextStart(), super.getTextLength());
                break;
            case END_ELEMENT:
                handleEndElement();
                break;
            case END_DOCUMENT:
                handler.endDocument();
                break;
        }
    }
    
    @Override
    public int nextTag() throws XMLStreamException {
        if (getHandler() == null) {
            return super.nextTag();
        }
        while (true) {
            int event = super.getEventType();
            try {
                if (event == START_DOCUMENT) {
                    // We need to trigger the startDocument()
                    handler.startDocument();
                }
                event = super.next();
                validate(event);
            } catch (SAXException e) {
                XMLStreamException xse = new XMLStreamException(e);
                error("XMLStreamException", handler, xse);
                throw xse;
            }

            if ((event == CHARACTERS && isWhiteSpace()) // skip whitespace
                || (event == CDATA && isWhiteSpace())
                // skip whitespace
                || event == SPACE
                || event == PROCESSING_INSTRUCTION
                || event == COMMENT) {
                continue;
            }
            if (event != START_ELEMENT && event != END_ELEMENT) {
                throw new XMLStreamException("expected start or end tag", getLocation());
            }
            return event;
        }
    }
    
    @Override
    public String getElementText() throws XMLStreamException {
        if (getHandler() == null) {
            return super.getElementText();
        }

        if (getEventType() != START_ELEMENT) {
            return super.getElementText();
        }
        StringBuffer text = new StringBuffer();

        for (;;) {
            int event = next();
            switch (event) {
                case END_ELEMENT:
                    return text.toString();
                    
                case COMMENT:
                case PROCESSING_INSTRUCTION:
                    continue;
                    
                case CHARACTERS:
                case CDATA:
                case SPACE:
                case ENTITY_REFERENCE:
                    text.append(getText());
                    break;
                    
                default:
                    break;
            }
        }
    }
    
    @Override
    public NamespaceContext getNamespaceContext(){
    	return super.getNamespaceContext();
    }
    
    /**
     * Handle a start element event.
     * 
     * @throws SAXException
     */
    private void handleStartElement() throws SAXException {

        // send startPrefixMapping events immediately before startElement event
        int nsCount = super.getNamespaceCount();
        for (int i = 0; i < nsCount; i++) {
            String prefix = super.getNamespacePrefix(i);
            if (prefix == null) { // true for default namespace
                prefix = "";
            }
            handler.startPrefixMapping(prefix, super.getNamespaceURI(i));
        }

        // fire startElement
        QName qname = super.getName();
        String prefix = qname.getPrefix();
        String rawname;
        if (prefix == null || prefix.length() == 0) {
            rawname = qname.getLocalPart();
        } else {
            rawname = prefix + ':' + qname.getLocalPart();
        }
        Attributes attrs = getAttributes();
        handler.startElement(qname.getNamespaceURI(), qname.getLocalPart(), rawname, attrs);
    }

    /**
     * Handle an endElement event.
     * 
     * @throws SAXException
     */
    private void handleEndElement() throws SAXException {

        // fire endElement
        QName qname = super.getName();
        handler.endElement(qname.getNamespaceURI(), qname.getLocalPart(), qname.toString());

        // send endPrefixMapping events immediately after endElement event
        // we send them in the opposite order to that returned but this is not
        // actually required by SAX
        int nsCount = super.getNamespaceCount();
        for (int i = nsCount - 1; i >= 0; i--) {
            String prefix = super.getNamespacePrefix(i);
            if (prefix == null) { // true for default namespace
                prefix = "";
            }
            handler.endPrefixMapping(prefix);
        }
    }

    /**
     * Get the attributes associated with the current START_ELEMENT event.
     * 
     * @return the StAX attributes converted to org.xml.sax.Attributes
     */
    private Attributes getAttributes() {
        AttributesImpl attrs = new AttributesImpl();

        // add namespace declarations
        for (int i = 0; i < super.getNamespaceCount(); i++) {
            String prefix = super.getNamespacePrefix(i);
            String uri = super.getNamespaceURI(i);
            if (prefix == null) {
                attrs.addAttribute("", "", "xmlns", "CDATA", uri);
            } else {
                attrs.addAttribute("", "", "xmlns:" + prefix, "CDATA", uri);
            }
        }

        // Regular attributes
        for (int i = 0; i < super.getAttributeCount(); i++) {
            String uri = super.getAttributeNamespace(i);
            if (uri == null) {
                uri = "";
            }
            String localName = super.getAttributeLocalName(i);
            String prefix = super.getAttributePrefix(i);
            String qname;
            if (prefix == null || prefix.length() == 0) {
                qname = localName;
            } else {
                qname = prefix + ':' + localName;
            }
            String type = super.getAttributeType(i);
            String value = super.getAttributeValue(i);

            attrs.addAttribute(uri, localName, qname, type, value);
        }

        return attrs;
    }

    /**
     * Adapter for mapping Locator information.
     */
    private final class LocatorAdapter implements Locator {

        private LocatorAdapter() {
        }

        public int getColumnNumber() {
            Location location = getLocation();
            return location == null ? 0 : location.getColumnNumber();
        }

        public int getLineNumber() {
            Location location = getLocation();
            return location == null ? 0 : location.getLineNumber();
        }

        public String getPublicId() {
            Location location = getLocation();
            return location == null ? "" : location.getPublicId();
        }

        public String getSystemId() {
            Location location = getLocation();
            return location == null ? "" : location.getSystemId();
        }
    }
    
}