View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  package javax.xml.bind.helpers;
18  
19  import java.net.URL;
20  
21  import javax.xml.bind.ValidationEventHandler;
22  import javax.xml.bind.ValidationEvent;
23  import javax.xml.bind.ValidationEventLocator;
24  
25  import org.w3c.dom.Node;
26  
27  public class DefaultValidationEventHandler implements ValidationEventHandler {
28  
29      public boolean handleEvent(ValidationEvent event) {
30          if (event == null) {
31              throw new IllegalArgumentException();
32          }
33          String severity = null;
34          boolean retVal = false;
35          switch(event.getSeverity()) {
36          case ValidationEvent.WARNING:
37              severity = "[WARNING]: ";
38              retVal = true;
39              break;
40  
41          case ValidationEvent.ERROR:
42              severity = "[ERROR]: ";
43              retVal = false;
44              break;
45  
46          case ValidationEvent.FATAL_ERROR:
47              severity = "[FATAL_ERROR]: ";
48              retVal = false;
49              break;
50          }
51          String location = getLocation(event);
52          System.out.println("DefaultValidationEventHandler " + severity + " " + event.getMessage() + "\n     Location: " + location);
53          return retVal;
54      }
55  
56      private String getLocation(ValidationEvent event) {
57          StringBuffer msg = new StringBuffer();
58          ValidationEventLocator locator = event.getLocator();
59          if (locator != null) {
60              URL url = locator.getURL();
61              Object obj = locator.getObject();
62              Node node = locator.getNode();
63              int line = locator.getLineNumber();
64              if(url != null || line != -1) {
65                  msg.append("line ").append(line);
66                  if(url != null) {
67                      msg.append(" of ").append(url);
68                  }
69              } else {
70                  if (obj != null) {
71                      msg.append(" obj: ").append(obj.toString());
72                  } else if(node != null) {
73                      msg.append(" node: ").append(node.toString());
74                  }
75              }
76          } else {
77              msg.append("unavailable");
78          }
79          return msg.toString();
80      }
81  
82  }