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;
018    
019    import java.io.PrintStream;
020    import java.io.PrintWriter;
021    
022    public class TypeConstraintException extends RuntimeException {
023    
024        private String errorCode;
025        private Throwable linkedException;
026    
027        public TypeConstraintException(String message) {
028            this(message, null, null);
029        }
030    
031        public TypeConstraintException(String message, String errorCode) {
032            this(message, errorCode, null);
033        }
034    
035        public TypeConstraintException(String message, String errorCode, Throwable cause) {
036            super(message);
037            this.errorCode = errorCode;
038            this.linkedException = cause;
039        }
040    
041        public TypeConstraintException(String message, Throwable cause) {
042            this(message, null, cause);
043        }
044    
045        public TypeConstraintException(Throwable cause) {
046            this(null, null, cause);
047        }
048    
049        public String getErrorCode() {
050            return errorCode;
051        }
052    
053        public Throwable getLinkedException() {
054            return getCause();
055        }
056    
057        public synchronized void setLinkedException(Throwable linkedException) {
058            this.linkedException = linkedException;
059        }
060    
061        public String toString() {
062            return linkedException != null ?
063                    super.toString() + "\n - with linked exception:\n[" + linkedException.toString() + "]" :
064                    super.toString();
065        }
066    
067        @Override
068        public Throwable getCause() {
069            return linkedException;
070        }
071    
072        @Override
073        public void printStackTrace(PrintStream s) {
074            synchronized (s) {
075                s.println(this);
076                StackTraceElement[] trace = getStackTrace();
077                for (int i=0; i < trace.length; i++) {
078                    s.println("\tat " + trace[i]);
079                }
080                Throwable ourCause = getCause();
081                if (ourCause != null) {
082                    ourCause.printStackTrace(s);
083                }
084            }
085        }
086    
087        @Override
088        public void printStackTrace(PrintWriter s) {
089            synchronized (s) {
090                s.println(this);
091                StackTraceElement[] trace = getStackTrace();
092                for (int i=0; i < trace.length; i++) {
093                    s.println("\tat " + trace[i]);
094                }
095                Throwable ourCause = getCause();
096                if (ourCause != null) {
097                    ourCause.printStackTrace(s);
098                }
099            }
100        }
101    
102    }