001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package javax.xml.bind.helpers;
018
019 import java.net.URL;
020
021 import javax.xml.bind.ValidationEventHandler;
022 import javax.xml.bind.ValidationEvent;
023 import javax.xml.bind.ValidationEventLocator;
024
025 import org.w3c.dom.Node;
026
027 public class DefaultValidationEventHandler implements ValidationEventHandler {
028
029 public boolean handleEvent(ValidationEvent event) {
030 if (event == null) {
031 throw new IllegalArgumentException();
032 }
033 String severity = null;
034 boolean retVal = false;
035 switch(event.getSeverity()) {
036 case ValidationEvent.WARNING:
037 severity = "[WARNING]: ";
038 retVal = true;
039 break;
040
041 case ValidationEvent.ERROR:
042 severity = "[ERROR]: ";
043 retVal = false;
044 break;
045
046 case ValidationEvent.FATAL_ERROR:
047 severity = "[FATAL_ERROR]: ";
048 retVal = false;
049 break;
050 }
051 String location = getLocation(event);
052 System.out.println("DefaultValidationEventHandler " + severity + " " + event.getMessage() + "\n Location: " + location);
053 return retVal;
054 }
055
056 private String getLocation(ValidationEvent event) {
057 StringBuffer msg = new StringBuffer();
058 ValidationEventLocator locator = event.getLocator();
059 if (locator != null) {
060 URL url = locator.getURL();
061 Object obj = locator.getObject();
062 Node node = locator.getNode();
063 int line = locator.getLineNumber();
064 if(url != null || line != -1) {
065 msg.append("line ").append(line);
066 if(url != null) {
067 msg.append(" of ").append(url);
068 }
069 } else {
070 if (obj != null) {
071 msg.append(" obj: ").append(obj.toString());
072 } else if(node != null) {
073 msg.append(" node: ").append(node.toString());
074 }
075 }
076 } else {
077 msg.append("unavailable");
078 }
079 return msg.toString();
080 }
081
082 }