001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.spi.xml.cookies;
043:
044: import javax.xml.transform.*;
045:
046: import org.xml.sax.SAXException;
047: import org.xml.sax.SAXParseException;
048:
049: import org.netbeans.api.xml.cookies.XMLProcessorDetail;
050:
051: /**
052: * Default XML processor observer message implementation.
053: * It supports direct wrapping of {@link SAXParseException}s and
054: * {@link TransformerException}s.
055: *
056: * @author Petr Kuzel
057: * @since 0.5
058: */
059: public class DefaultXMLProcessorDetail extends XMLProcessorDetail {
060:
061: private int columnNumber;
062:
063: private int lineNumber;
064:
065: private String publicId;
066:
067: private String systemId;
068:
069: private Exception exception;
070:
071: /**
072: * Create new DefaultXMLProcessorMessage based on SAXParseException.
073: * @param spex SAX exception to be wrapped (never <code>null</code>).
074: */
075: public DefaultXMLProcessorDetail(SAXParseException spex) {
076: if (spex == null)
077: throw new NullPointerException();
078:
079: this .exception = spex;
080: this .columnNumber = spex.getColumnNumber();
081: this .lineNumber = spex.getLineNumber();
082: this .publicId = spex.getPublicId();
083: this .systemId = spex.getSystemId();
084:
085: if (Util.THIS.isLoggable()) /* then */{
086: Util.THIS
087: .debug("DefaultXMLProcessorDetail: SAXParseException");
088: Util.THIS.debug(" exception= " + exception);
089: Util.THIS.debug(" columnNumber= " + columnNumber);
090: Util.THIS.debug(" lineNumber= " + lineNumber);
091: Util.THIS.debug(" publicId= " + publicId);
092: Util.THIS.debug(" systemId= " + systemId);
093: }
094: }
095:
096: /**
097: * Create new DefaultXMLProcessorMessage based on TransformerException.
098: * @param trex TrAX exception to be wrapped (never <code>null</code>).
099: */
100: public DefaultXMLProcessorDetail(TransformerException trex) {
101: if (trex == null)
102: throw new NullPointerException();
103:
104: this .exception = trex;
105:
106: // some locators disapper immediately we must sample it now!
107:
108: SourceLocator locator = trex.getLocator();
109: if (locator != null) {
110: this .columnNumber = locator.getColumnNumber();
111: this .lineNumber = locator.getLineNumber();
112: this .publicId = locator.getPublicId();
113: this .systemId = locator.getSystemId();
114:
115: //
116: if (lineNumber == -1) {
117: tryWrappedLocator(trex);
118: }
119: } else {
120:
121: // default
122: this .columnNumber = -1;
123: this .lineNumber = -1;
124: this .publicId = null;
125: this .systemId = null;
126:
127: // may be better
128: tryWrappedLocator(trex);
129: }
130:
131: if (Util.THIS.isLoggable()) /* then */{
132: Util.THIS
133: .debug("DefaultXMLProcessorDetail: TransformerException");
134: Util.THIS.debug(" exception= " + exception);
135: Util.THIS.debug(" columnNumber= " + columnNumber);
136: Util.THIS.debug(" lineNumber= " + lineNumber);
137: Util.THIS.debug(" publicId= " + publicId);
138: Util.THIS.debug(" systemId= " + systemId);
139: }
140: }
141:
142: // use location information from wrapped exception or do nothing
143: private void tryWrappedLocator(Exception ex) {
144: if (Util.THIS.isLoggable()) /* then */{
145: Util.THIS
146: .debug("DefaultXMLProcessorDetail.tryWrappedLocator: "
147: + ex);
148: }
149:
150: // I saw SAXException wrapped in TransformerException and vice versa
151:
152: Throwable wrapped = null;
153: if (ex instanceof TransformerException) {
154: wrapped = ((TransformerException) ex).getException();
155: } else if (ex instanceof SAXException) {
156: wrapped = ((SAXException) ex).getException();
157: } else {
158: return;
159: }
160:
161: // look if wrapped exception does not provide location info
162:
163: if (wrapped instanceof SAXParseException) {
164: SAXParseException pex = (SAXParseException) wrapped;
165: if (pex.getLineNumber() == -1) {
166: tryWrappedLocator(pex);
167: } else {
168: this .columnNumber = pex.getColumnNumber();
169: this .lineNumber = pex.getLineNumber();
170: this .publicId = pex.getPublicId();
171: this .systemId = pex.getSystemId();
172: }
173: } else if (wrapped instanceof TransformerException) {
174: TransformerException wrappedTransformerEx = (TransformerException) wrapped;
175: SourceLocator locator = wrappedTransformerEx.getLocator();
176: if (locator == null) {
177: tryWrappedLocator(wrappedTransformerEx);
178: } else {
179: if (locator.getLineNumber() == -1) {
180: tryWrappedLocator(wrappedTransformerEx);
181: } else {
182: this .columnNumber = locator.getColumnNumber();
183: this .lineNumber = locator.getLineNumber();
184: this .publicId = locator.getPublicId();
185: this .systemId = locator.getSystemId();
186: }
187: }
188: } else if (wrapped instanceof SAXException) {
189: tryWrappedLocator((SAXException) wrapped);
190: }
191: }
192:
193: public int getColumnNumber() {
194: return columnNumber;
195: }
196:
197: public int getLineNumber() {
198: return lineNumber;
199: }
200:
201: public String getPublicId() {
202: return publicId;
203: }
204:
205: public String getSystemId() {
206: return systemId;
207: }
208:
209: public Exception getException() {
210: return exception;
211: }
212:
213: }
|