KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > spi > xml > cookies > DefaultXMLProcessorDetail


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.spi.xml.cookies;
21
22 import javax.xml.transform.*;
23
24 import org.xml.sax.SAXException JavaDoc;
25 import org.xml.sax.SAXParseException JavaDoc;
26
27 import org.netbeans.api.xml.cookies.XMLProcessorDetail;
28
29 /**
30  * Default XML processor observer message implementation.
31  * It supports direct wrapping of {@link SAXParseException}s and
32  * {@link TransformerException}s.
33  *
34  * @author Petr Kuzel
35  * @since 0.5
36  */

37 public class DefaultXMLProcessorDetail extends XMLProcessorDetail {
38
39     private int columnNumber;
40
41     private int lineNumber;
42
43     private String JavaDoc publicId;
44
45     private String JavaDoc systemId;
46
47     private Exception JavaDoc exception;
48
49     
50     /**
51      * Create new DefaultXMLProcessorMessage based on SAXParseException.
52      * @param spex SAX exception to be wrapped (never <code>null</code>).
53      */

54     public DefaultXMLProcessorDetail(SAXParseException JavaDoc spex) {
55         if (spex == null) throw new NullPointerException JavaDoc();
56
57         this.exception = spex;
58         this.columnNumber = spex.getColumnNumber();
59         this.lineNumber = spex.getLineNumber();
60         this.publicId = spex.getPublicId();
61         this.systemId = spex.getSystemId();
62
63         if ( Util.THIS.isLoggable() ) /* then */ {
64             Util.THIS.debug ("DefaultXMLProcessorDetail: SAXParseException");
65             Util.THIS.debug (" exception= " + exception);
66             Util.THIS.debug (" columnNumber= " + columnNumber);
67             Util.THIS.debug (" lineNumber= " + lineNumber);
68             Util.THIS.debug (" publicId= " + publicId);
69             Util.THIS.debug (" systemId= " + systemId);
70         }
71     }
72
73     /**
74      * Create new DefaultXMLProcessorMessage based on TransformerException.
75      * @param trex TrAX exception to be wrapped (never <code>null</code>).
76      */

77     public DefaultXMLProcessorDetail(TransformerException trex) {
78         if (trex == null) throw new NullPointerException JavaDoc();
79
80         this.exception = trex;
81
82         // some locators disapper immediately we must sample it now!
83

84         SourceLocator locator = trex.getLocator();
85         if (locator != null) {
86             this.columnNumber = locator.getColumnNumber();
87             this.lineNumber = locator.getLineNumber();
88             this.publicId = locator.getPublicId();
89             this.systemId = locator.getSystemId();
90
91             //
92
if (lineNumber == -1) {
93                 tryWrappedLocator(trex);
94             }
95         } else {
96
97             // default
98
this.columnNumber = -1;
99             this.lineNumber = -1;
100             this.publicId = null;
101             this.systemId = null;
102
103             // may be better
104
tryWrappedLocator(trex);
105         }
106
107         if ( Util.THIS.isLoggable() ) /* then */ {
108             Util.THIS.debug ("DefaultXMLProcessorDetail: TransformerException");
109             Util.THIS.debug (" exception= " + exception);
110             Util.THIS.debug (" columnNumber= " + columnNumber);
111             Util.THIS.debug (" lineNumber= " + lineNumber);
112             Util.THIS.debug (" publicId= " + publicId);
113             Util.THIS.debug (" systemId= " + systemId);
114         }
115     }
116
117     // use location information from wrapped exception or do nothing
118
private void tryWrappedLocator(Exception JavaDoc ex) {
119         if ( Util.THIS.isLoggable() ) /* then */ {
120             Util.THIS.debug ("DefaultXMLProcessorDetail.tryWrappedLocator: " + ex);
121         }
122
123         // I saw SAXException wrapped in TransformerException and vice versa
124

125         Throwable JavaDoc wrapped = null;
126         if (ex instanceof TransformerException) {
127             wrapped = ((TransformerException) ex).getException();
128         } else if (ex instanceof SAXException JavaDoc) {
129             wrapped = ((SAXException JavaDoc) ex).getException();
130         } else {
131             return;
132         }
133
134         // look if wrapped exception does not provide location info
135

136         if (wrapped instanceof SAXParseException JavaDoc) {
137             SAXParseException JavaDoc pex = (SAXParseException JavaDoc) wrapped;
138             if (pex.getLineNumber() == -1) {
139                 tryWrappedLocator(pex);
140             } else {
141                 this.columnNumber = pex.getColumnNumber();
142                 this.lineNumber = pex.getLineNumber();
143                 this.publicId = pex.getPublicId();
144                 this.systemId = pex.getSystemId();
145             }
146         } else if (wrapped instanceof TransformerException) {
147             TransformerException wrappedTransformerEx =
148                 (TransformerException) wrapped;
149             SourceLocator locator = wrappedTransformerEx.getLocator();
150             if (locator == null) {
151                 tryWrappedLocator(wrappedTransformerEx);
152             } else {
153                 if (locator.getLineNumber() == -1) {
154                     tryWrappedLocator(wrappedTransformerEx);
155                 } else {
156                     this.columnNumber = locator.getColumnNumber();
157                     this.lineNumber = locator.getLineNumber();
158                     this.publicId = locator.getPublicId();
159                     this.systemId = locator.getSystemId();
160                 }
161             }
162         } else if (wrapped instanceof SAXException JavaDoc) {
163             tryWrappedLocator((SAXException JavaDoc)wrapped);
164         }
165     }
166                                 
167     public int getColumnNumber() {
168         return columnNumber;
169     }
170
171     public int getLineNumber() {
172         return lineNumber;
173     }
174
175     public String JavaDoc getPublicId() {
176         return publicId;
177     }
178
179     public String JavaDoc getSystemId() {
180         return systemId;
181     }
182
183     public Exception JavaDoc getException() {
184         return exception;
185     }
186
187 }
188
Popular Tags