KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > xs > parser > impl > LocSAXException


1 /*
2  * Copyright 2003, 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15
16  */

17 package org.apache.ws.jaxme.xs.parser.impl;
18
19 import java.io.PrintStream JavaDoc;
20 import java.io.PrintWriter JavaDoc;
21
22 import org.xml.sax.Locator JavaDoc;
23 import org.xml.sax.SAXException JavaDoc;
24 import org.xml.sax.SAXParseException JavaDoc;
25
26
27 /** <p>The <code>locatable SAXException</code>, a subclass of the SAXParseException,
28  * provides a human readable representation of the location.</p>
29  *
30  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
31  */

32 public class LocSAXException extends SAXParseException JavaDoc {
33   public static String JavaDoc formatMsg(String JavaDoc pMsg, String JavaDoc pPublicId, String JavaDoc pSystemId, int pLineNumber, int pColNumber) {
34     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
35     if (pSystemId != null) {
36       sb.append(pSystemId);
37     }
38     if (pLineNumber != -1) {
39       if (sb.length() > 0) {
40         sb.append(", ");
41       }
42       sb.append("line ").append(pLineNumber);
43     }
44     if (pColNumber != -1) {
45       if (sb.length() > 0) {
46         sb.append(", ");
47       }
48       sb.append("column ").append(pColNumber);
49     }
50     if (sb.length() == 0) {
51       return "" + pMsg;
52     } else {
53       return "At " + sb + ": " + pMsg;
54     }
55   }
56
57   private static String JavaDoc formatMsg(String JavaDoc pMsg, Locator JavaDoc pLocator) {
58     if (pLocator == null) {
59       return pMsg;
60     } else {
61       return formatMsg(pMsg, pLocator.getPublicId(), pLocator.getSystemId(), pLocator.getLineNumber(), pLocator.getColumnNumber());
62     }
63   }
64
65   public LocSAXException(String JavaDoc pMsg, String JavaDoc pPublicId, String JavaDoc pSystemId,
66                                 int pLineNumber, int pColumnNumber, Exception JavaDoc pException) {
67     super(formatMsg(pMsg, pPublicId, pSystemId, pLineNumber, pColumnNumber),
68            pPublicId, pSystemId, pLineNumber, pColumnNumber, pException);
69   }
70
71   public LocSAXException(String JavaDoc pMsg, String JavaDoc pPublicId, String JavaDoc pSystemId,
72                                 int pLineNumber, int pColumnNumber) {
73     super(formatMsg(pMsg, pPublicId, pSystemId, pLineNumber, pColumnNumber),
74            pPublicId, pSystemId, pLineNumber, pColumnNumber);
75   }
76
77   public LocSAXException(String JavaDoc pMsg, Locator JavaDoc pLocator, Exception JavaDoc pException) {
78     super(formatMsg(pMsg, pLocator), pLocator, pException);
79   }
80
81   public LocSAXException(String JavaDoc pMsg, Locator JavaDoc pLocator) {
82     super(formatMsg(pMsg, pLocator), pLocator);
83   }
84
85   public void printStackTrace(PrintStream JavaDoc pStream) {
86     super.printStackTrace(pStream);
87     Exception JavaDoc e = getException();
88     while (e != null) {
89       pStream.println("Caused by:");
90       e.printStackTrace(pStream);
91       if (e instanceof SAXException JavaDoc) {
92         e = ((SAXException JavaDoc) e).getException();
93       } else {
94         e = null;
95       }
96     }
97   }
98
99   public void printStackTrace(PrintWriter JavaDoc pWriter) {
100     super.printStackTrace(pWriter);
101     Exception JavaDoc e = getException();
102     while (e != null) {
103       pWriter.println("Caused by:");
104       e.printStackTrace(pWriter);
105       if (e instanceof SAXException JavaDoc) {
106         e = ((SAXException JavaDoc) e).getException();
107       } else {
108         e = null;
109       }
110     }
111   }
112 }
113
Popular Tags