1/*
2 * Copyright (c) 2003, 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 javax.xml.bind.helpers;
27
28import java.text.MessageFormat;
29
30import javax.xml.bind.ValidationEvent;
31import javax.xml.bind.ValidationEventLocator;
32
33/**
34 * Default implementation of the ValidationEvent interface.
35 *
36 * <p>
37 * JAXB providers are allowed to use whatever class that implements
38 * the ValidationEvent interface. This class is just provided for a
39 * convenience.
40 *
41 * @author <ul><li>Kohsuke Kawaguchi, Sun Microsystems, Inc.</li></ul>
42 * @see javax.xml.bind.Validator
43 * @see javax.xml.bind.ValidationEventHandler
44 * @see javax.xml.bind.ValidationEvent
45 * @see javax.xml.bind.ValidationEventLocator
46 * @since 1.6, JAXB 1.0
47 */
48public class ValidationEventImpl implements ValidationEvent
49{
50
51    /**
52     * Create a new ValidationEventImpl.
53     *
54     * @param _severity The severity value for this event.  Must be one of
55     * ValidationEvent.WARNING, ValidationEvent.ERROR, or
56     * ValidationEvent.FATAL_ERROR
57     * @param _message The text message for this event - may be null.
58     * @param _locator The locator object for this event - may be null.
59     * @throws IllegalArgumentException if an illegal severity field is supplied
60     */
61    public ValidationEventImpl( int _severity, String _message,
62                                 ValidationEventLocator _locator ) {
63
64        this(_severity,_message,_locator,null);
65    }
66
67    /**
68     * Create a new ValidationEventImpl.
69     *
70     * @param _severity The severity value for this event.  Must be one of
71     * ValidationEvent.WARNING, ValidationEvent.ERROR, or
72     * ValidationEvent.FATAL_ERROR
73     * @param _message The text message for this event - may be null.
74     * @param _locator The locator object for this event - may be null.
75     * @param _linkedException An optional linked exception that may provide
76     * additional information about the event - may be null.
77     * @throws IllegalArgumentException if an illegal severity field is supplied
78     */
79    public ValidationEventImpl( int _severity, String _message,
80                                 ValidationEventLocator _locator,
81                                 Throwable _linkedException ) {
82
83        setSeverity( _severity );
84        this.message = _message;
85        this.locator = _locator;
86        this.linkedException = _linkedException;
87    }
88
89    private int severity;
90    private String message;
91    private Throwable linkedException;
92    private ValidationEventLocator locator;
93
94    public int getSeverity() {
95        return severity;
96    }
97
98
99    /**
100     * Set the severity field of this event.
101     *
102     * @param _severity Must be one of ValidationEvent.WARNING,
103     * ValidationEvent.ERROR, or ValidationEvent.FATAL_ERROR.
104     * @throws IllegalArgumentException if an illegal severity field is supplied
105     */
106    public void setSeverity( int _severity ) {
107
108        if( _severity != ValidationEvent.WARNING &&
109            _severity != ValidationEvent.ERROR &&
110            _severity != ValidationEvent.FATAL_ERROR ) {
111                throw new IllegalArgumentException(
112                    Messages.format( Messages.ILLEGAL_SEVERITY ) );
113        }
114
115        this.severity = _severity;
116    }
117
118    public String getMessage() {
119        return message;
120    }
121    /**
122     * Set the message field of this event.
123     *
124     * @param _message String message - may be null.
125     */
126    public void setMessage( String _message ) {
127        this.message = _message;
128    }
129
130    public Throwable getLinkedException() {
131        return linkedException;
132    }
133    /**
134     * Set the linked exception field of this event.
135     *
136     * @param _linkedException Optional linked exception - may be null.
137     */
138    public void setLinkedException( Throwable _linkedException ) {
139        this.linkedException = _linkedException;
140    }
141
142    public ValidationEventLocator getLocator() {
143        return locator;
144    }
145    /**
146     * Set the locator object for this event.
147     *
148     * @param _locator The locator - may be null.
149     */
150    public void setLocator( ValidationEventLocator _locator ) {
151        this.locator = _locator;
152    }
153
154    /**
155     * Returns a string representation of this object in a format
156     * helpful to debugging.
157     *
158     * @see Object#equals(Object)
159     */
160    public String toString() {
161        String s;
162        switch(getSeverity()) {
163        case WARNING:   s="WARNING";break;
164        case ERROR: s="ERROR";break;
165        case FATAL_ERROR: s="FATAL_ERROR";break;
166        default: s=String.valueOf(getSeverity());break;
167        }
168        return MessageFormat.format("[severity={0},message={1},locator={2}]",
169            new Object[]{
170                s,
171                getMessage(),
172                getLocator()
173            });
174    }
175}
176