1/*
2 * Copyright (c) 1997, 2012, 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.bind.v2.runtime.unmarshaller;
27
28import javax.xml.bind.ValidationEventLocator;
29import javax.xml.bind.helpers.ValidationEventLocatorImpl;
30import javax.xml.namespace.NamespaceContext;
31import javax.xml.stream.Location;
32import javax.xml.stream.XMLStreamException;
33
34import org.xml.sax.SAXException;
35
36/**
37 * @author Kohsuke Kawaguchi
38 */
39abstract class StAXConnector {
40    public abstract void bridge() throws XMLStreamException;
41
42
43    // event sink
44    protected final XmlVisitor visitor;
45
46    protected final UnmarshallingContext context;
47    protected final XmlVisitor.TextPredictor predictor;
48
49    private final class TagNameImpl extends TagName {
50        public String getQname() {
51            return StAXConnector.this.getCurrentQName();
52        }
53    }
54
55    protected final TagName tagName = new TagNameImpl();
56
57    protected StAXConnector(XmlVisitor visitor) {
58        this.visitor = visitor;
59        context = visitor.getContext();
60        predictor = visitor.getPredictor();
61    }
62
63    /**
64     * Gets the {@link Location}. Used for implementing the line number information.
65     * @return must not null.
66     */
67    protected abstract Location getCurrentLocation();
68
69    /**
70     * Gets the QName of the current element.
71     */
72    protected abstract String getCurrentQName();
73
74    protected final void handleStartDocument(NamespaceContext nsc) throws SAXException {
75        visitor.startDocument(new LocatorEx() {
76            public ValidationEventLocator getLocation() {
77                return new ValidationEventLocatorImpl(this);
78            }
79            public int getColumnNumber() {
80                return getCurrentLocation().getColumnNumber();
81            }
82            public int getLineNumber() {
83                return getCurrentLocation().getLineNumber();
84            }
85            public String getPublicId() {
86                return getCurrentLocation().getPublicId();
87            }
88            public String getSystemId() {
89                return getCurrentLocation().getSystemId();
90            }
91        },nsc);
92    }
93
94    protected final void handleEndDocument() throws SAXException {
95        visitor.endDocument();
96    }
97
98    protected static String fixNull(String s) {
99        if(s==null) return "";
100        else        return s;
101    }
102
103    protected final String getQName(String prefix, String localName) {
104        if(prefix==null || prefix.length()==0)
105            return localName;
106        else
107            return prefix + ':' + localName;
108    }
109}
110