1 /* Copyright 2004 The Apache Software Foundation 2 * 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 package org.apache.xmlbeans; 17 18 import java.util.List; 19 import java.util.Collections; 20 import java.util.Collection; 21 import java.util.ArrayList; 22 23 /** 24 * An unchecked XML exception. 25 * May contain any number of {@link XmlError} objects. 26 * 27 * @see XmlError 28 * @see XmlException 29 */ 30 public class XmlRuntimeException extends RuntimeException 31 { 32 private static final long serialVersionUID = 1L; 33 34 /** 35 * Constructs an XmlRuntimeException from a message. 36 */ 37 public XmlRuntimeException ( String m ) { super( m ); } 38 39 /** 40 * Constructs an XmlRuntimeException from a message and a cause. 41 */ 42 public XmlRuntimeException ( String m, Throwable t ) { super( m, t ); } 43 44 /** 45 * Constructs an XmlRuntimeException from a cause. 46 */ 47 public XmlRuntimeException ( Throwable t ) { super( t ); } 48 49 /** 50 * Constructs an XmlRuntimeException from a message, a cause, and a collection of XmlErrors. 51 */ 52 public XmlRuntimeException ( String m, Throwable t, Collection errors ) 53 { 54 super( m, t ); 55 56 if (errors != null) 57 _errors = Collections.unmodifiableList( new ArrayList(errors) ); 58 } 59 60 /** 61 * Constructs an XmlRuntimeException from an XmlError. 62 */ 63 public XmlRuntimeException ( XmlError error ) 64 { 65 this( error.toString(), null, error ); 66 } 67 68 /** 69 * Constructs an XmlRuntimeException from a message, a cause, and an XmlError. 70 */ 71 public XmlRuntimeException ( String m, Throwable t, XmlError error ) 72 { 73 this( m, t, Collections.singletonList( error ) ); 74 } 75 76 /** 77 * Constructs an XmlRuntimeException from an {@link XmlException}. 78 */ 79 public XmlRuntimeException ( XmlException xmlException ) 80 { 81 super( xmlException.getMessage(), xmlException.getCause() ); 82 83 Collection errors = xmlException.getErrors(); 84 85 if (errors != null) 86 _errors = Collections.unmodifiableList( new ArrayList( errors ) ); 87 } 88 89 /** 90 * Returns the first {@link XmlError} that caused this exception, if any. 91 */ 92 public XmlError getError ( ) 93 { 94 if (_errors == null || _errors.size() == 0) 95 return null; 96 97 return (XmlError) _errors.get( 0 ); 98 } 99 100 /** 101 * Returns the collection of {@link XmlError XmlErrors} that caused this exception, if any. 102 */ 103 public Collection getErrors ( ) 104 { 105 return _errors; 106 } 107 108 private List _errors; 109 }