1/*
2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.xml.internal.ws.runtime.config;
27
28import com.sun.istack.internal.logging.Logger;
29import com.sun.xml.internal.ws.config.metro.dev.FeatureReader;
30import com.sun.xml.internal.ws.config.metro.util.ParserUtil;
31
32import javax.xml.namespace.QName;
33import javax.xml.stream.XMLEventReader;
34import javax.xml.stream.XMLStreamConstants;
35import javax.xml.stream.XMLStreamException;
36import javax.xml.stream.events.Attribute;
37import javax.xml.stream.events.EndElement;
38import javax.xml.stream.events.StartElement;
39import javax.xml.stream.events.XMLEvent;
40import javax.xml.ws.WebServiceException;
41import java.util.Iterator;
42
43/**
44 *
45 * @author Fabian Ritzmann
46 */
47public class TubelineFeatureReader implements FeatureReader {
48
49    private static final Logger LOGGER = Logger.getLogger(TubelineFeatureReader.class);
50    private static final QName NAME_ATTRIBUTE_NAME = new QName("name");
51
52    // TODO implement
53    public TubelineFeature parse(XMLEventReader reader) throws WebServiceException {
54        try {
55            final StartElement element = reader.nextEvent().asStartElement();
56            boolean attributeEnabled = true;
57            final Iterator iterator = element.getAttributes();
58            while (iterator.hasNext()) {
59                final Attribute nextAttribute = (Attribute) iterator.next();
60                final QName attributeName = nextAttribute.getName();
61                if (ENABLED_ATTRIBUTE_NAME.equals(attributeName)) {
62                    attributeEnabled = ParserUtil.parseBooleanValue(nextAttribute.getValue());
63                } else if (NAME_ATTRIBUTE_NAME.equals(attributeName)) {
64                    // TODO use name attribute
65                } else {
66                    // TODO logging message
67                    throw LOGGER.logSevereException(new WebServiceException("Unexpected attribute"));
68                }
69            }
70            return parseFactories(attributeEnabled, element, reader);
71        } catch (XMLStreamException e) {
72            throw LOGGER.logSevereException(new WebServiceException("Failed to unmarshal XML document", e));
73        }
74    }
75
76    private TubelineFeature parseFactories(final boolean enabled, final StartElement element, final XMLEventReader reader)
77            throws WebServiceException {
78        int elementRead = 0;
79        loop:
80        while (reader.hasNext()) {
81            try {
82                final XMLEvent event = reader.nextEvent();
83                switch (event.getEventType()) {
84                    case XMLStreamConstants.COMMENT:
85                        break; // skipping the comments and start document events
86                    case XMLStreamConstants.CHARACTERS:
87                        if (event.asCharacters().isWhiteSpace()) {
88                            break;
89                        }
90                        else {
91                            // TODO: logging message
92                            throw LOGGER.logSevereException(new WebServiceException("No character data allowed, was " + event.asCharacters()));
93                        }
94                    case XMLStreamConstants.START_ELEMENT:
95                        // TODO implement
96                        elementRead++;
97                        break;
98                    case XMLStreamConstants.END_ELEMENT:
99                        elementRead--;
100                        if (elementRead < 0) {
101                            final EndElement endElement = event.asEndElement();
102                            if (!element.getName().equals(endElement.getName())) {
103                                // TODO logging message
104                                throw LOGGER.logSevereException(new WebServiceException("End element does not match " + endElement));
105                            }
106                            break loop;
107                        }
108                        else {
109                            break;
110                        }
111                    default:
112                        // TODO logging message
113                        throw LOGGER.logSevereException(new WebServiceException("Unexpected event, was " + event));
114                }
115            } catch (XMLStreamException e) {
116                // TODO logging message
117                throw LOGGER.logSevereException(new WebServiceException("Failed to unmarshal XML document", e));
118            }
119        }
120
121        // TODO implement
122        return new TubelineFeature(enabled);
123    }
124
125}
126