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.util.exception;
27
28import com.sun.istack.internal.NotNull;
29import com.sun.xml.internal.ws.resources.UtilMessages;
30import org.xml.sax.Locator;
31import org.xml.sax.helpers.LocatorImpl;
32
33import javax.xml.stream.Location;
34import javax.xml.stream.XMLStreamReader;
35import javax.xml.ws.WebServiceException;
36import java.util.Arrays;
37import java.util.List;
38
39/**
40 * {@link WebServiceException} with source location informaiton.
41 *
42 * <p>
43 * This exception should be used wherever the location information is available,
44 * so that the location information is carried forward to users (to assist
45 * error diagnostics.)
46 *
47 * @author Kohsuke Kawaguchi
48 */
49public class LocatableWebServiceException extends WebServiceException {
50    /**
51     * Locations related to error.
52     */
53    private final Locator[] location;
54
55    public LocatableWebServiceException(String message, Locator... location) {
56        this(message,null,location);
57    }
58
59    public LocatableWebServiceException(String message, Throwable cause, Locator... location) {
60        super(appendLocationInfo(message,location), cause);
61        this.location = location;
62    }
63
64    public LocatableWebServiceException(Throwable cause, Locator... location) {
65        this(cause.toString(),cause,location);
66    }
67
68    public LocatableWebServiceException(String message, XMLStreamReader locationSource) {
69        this(message,toLocation(locationSource));
70    }
71
72    public LocatableWebServiceException(String message, Throwable cause, XMLStreamReader locationSource) {
73        this(message,cause,toLocation(locationSource));
74    }
75
76    public LocatableWebServiceException(Throwable cause, XMLStreamReader locationSource) {
77        this(cause,toLocation(locationSource));
78    }
79
80    /**
81     * Locations related to this exception.
82     *
83     * @return
84     *      Can be empty but never null.
85     */
86    public @NotNull List<Locator> getLocation() {
87        return Arrays.asList(location);
88    }
89
90    private static String appendLocationInfo(String message, Locator[] location) {
91        StringBuilder buf = new StringBuilder(message);
92        for( Locator loc : location )
93            buf.append('\n').append(UtilMessages.UTIL_LOCATION( loc.getLineNumber(), loc.getSystemId() ));
94        return buf.toString();
95    }
96
97    private static Locator toLocation(XMLStreamReader xsr) {
98        LocatorImpl loc = new LocatorImpl();
99        Location in = xsr.getLocation();
100        loc.setSystemId(in.getSystemId());
101        loc.setPublicId(in.getPublicId());
102        loc.setLineNumber(in.getLineNumber());
103        loc.setColumnNumber(in.getColumnNumber());
104        return loc;
105    }
106}
107