src/share/jaxws_classes/javax/xml/bind/helpers/DefaultValidationEventHandler.java

changeset 286
f50545b5e2f1
child 397
b99d7e355d4b
equal deleted inserted replaced
284:88b85470e72c 286:f50545b5e2f1
1 /*
2 * Copyright (c) 2003, 2010, 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
26 package javax.xml.bind.helpers;
27
28 import org.w3c.dom.Node;
29
30 import javax.xml.bind.ValidationEvent;
31 import javax.xml.bind.ValidationEventHandler;
32 import javax.xml.bind.ValidationEventLocator;
33 import java.net.URL;
34
35 /**
36 * <p>
37 * JAXB 1.0 only default validation event handler. This is the default
38 * handler for all objects created from a JAXBContext that is managing
39 * schema-derived code generated by a JAXB 1.0 binding compiler.
40 *
41 * <p>
42 * This handler causes the unmarshal and validate operations to fail on the first
43 * error or fatal error.
44 *
45 * <p>
46 * This handler is not the default handler for JAXB mapped classes following
47 * JAXB 2.0 or later versions. Default validation event handling has changed
48 * and is specified in {@link javax.xml.bind.Unmarshaller} and
49 * {@link javax.xml.bind.Marshaller}.
50 *
51 * @author <ul><li>Ryan Shoemaker, Sun Microsystems, Inc.</li></ul>
52 * @see javax.xml.bind.Unmarshaller
53 * @see javax.xml.bind.Validator
54 * @see javax.xml.bind.ValidationEventHandler
55 * @since JAXB1.0
56 */
57 public class DefaultValidationEventHandler implements ValidationEventHandler {
58
59 public boolean handleEvent( ValidationEvent event ) {
60
61 if( event == null ) {
62 throw new IllegalArgumentException();
63 }
64
65 // calculate the severity prefix and return value
66 String severity = null;
67 boolean retVal = false;
68 switch ( event.getSeverity() ) {
69 case ValidationEvent.WARNING:
70 severity = Messages.format( Messages.WARNING );
71 retVal = true; // continue after warnings
72 break;
73 case ValidationEvent.ERROR:
74 severity = Messages.format( Messages.ERROR );
75 retVal = false; // terminate after errors
76 break;
77 case ValidationEvent.FATAL_ERROR:
78 severity = Messages.format( Messages.FATAL_ERROR );
79 retVal = false; // terminate after fatal errors
80 break;
81 default:
82 assert false :
83 Messages.format( Messages.UNRECOGNIZED_SEVERITY,
84 event.getSeverity() );
85 }
86
87 // calculate the location message
88 String location = getLocation( event );
89
90 System.out.println(
91 Messages.format( Messages.SEVERITY_MESSAGE,
92 severity,
93 event.getMessage(),
94 location ) );
95
96 // fail on the first error or fatal error
97 return retVal;
98 }
99
100 /**
101 * Calculate a location message for the event
102 *
103 */
104 private String getLocation(ValidationEvent event) {
105 StringBuffer msg = new StringBuffer();
106
107 ValidationEventLocator locator = event.getLocator();
108
109 if( locator != null ) {
110
111 URL url = locator.getURL();
112 Object obj = locator.getObject();
113 Node node = locator.getNode();
114 int line = locator.getLineNumber();
115
116 if( url!=null || line!=-1 ) {
117 msg.append( "line " + line );
118 if( url!=null )
119 msg.append( " of " + url );
120 } else if( obj != null ) {
121 msg.append( " obj: " + obj.toString() );
122 } else if( node != null ) {
123 msg.append( " node: " + node.toString() );
124 }
125 } else {
126 msg.append( Messages.format( Messages.LOCATION_UNAVAILABLE ) );
127 }
128
129 return msg.toString();
130 }
131 }

mercurial