001: /*
002:
003: Licensed to the Apache Software Foundation (ASF) under one or more
004: contributor license agreements. See the NOTICE file distributed with
005: this work for additional information regarding copyright ownership.
006: The ASF licenses this file to You under the Apache License, Version 2.0
007: (the "License"); you may not use this file except in compliance with
008: the License. You may obtain a copy of the License at
009:
010: http://www.apache.org/licenses/LICENSE-2.0
011:
012: Unless required by applicable law or agreed to in writing, software
013: distributed under the License is distributed on an "AS IS" BASIS,
014: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: See the License for the specific language governing permissions and
016: limitations under the License.
017:
018: */
019: package org.apache.batik.bridge;
020:
021: import org.apache.batik.dom.svg.LiveAttributeException;
022: import org.apache.batik.gvt.GraphicsNode;
023:
024: import org.w3c.dom.Element;
025: import org.w3c.dom.svg.SVGDocument;
026:
027: /**
028: * Thrown when the bridge has detected an error.
029: *
030: * @author <a href="mailto:tkormann@apache.org">Thierry Kormann</a>
031: * @version $Id: BridgeException.java 501922 2007-01-31 17:47:47Z dvholten $
032: */
033: public class BridgeException extends RuntimeException {
034:
035: /** The element on which the error occured. */
036: protected Element e;
037:
038: /** The error code. */
039: protected String code;
040:
041: /**
042: * The message.
043: */
044: protected String message;
045:
046: /** The paramters to use for the error message. */
047: protected Object[] params;
048:
049: /** The line number on which the error occured. */
050: protected int line;
051:
052: /** The graphics node that represents the current state of the GVT tree. */
053: protected GraphicsNode node;
054:
055: /**
056: * Constructs a new <tt>BridgeException</tt> based on the specified
057: * <tt>LiveAttributeException</tt>.
058: *
059: * @param ctx the bridge context to use for determining the element's
060: * source position
061: * @param ex the {@link LiveAttributeException}
062: */
063: public BridgeException(BridgeContext ctx, LiveAttributeException ex) {
064: switch (ex.getCode()) {
065: case LiveAttributeException.ERR_ATTRIBUTE_MISSING:
066: this .code = ErrorConstants.ERR_ATTRIBUTE_MISSING;
067: break;
068: case LiveAttributeException.ERR_ATTRIBUTE_MALFORMED:
069: this .code = ErrorConstants.ERR_ATTRIBUTE_VALUE_MALFORMED;
070: break;
071: case LiveAttributeException.ERR_ATTRIBUTE_NEGATIVE:
072: this .code = ErrorConstants.ERR_LENGTH_NEGATIVE;
073: break;
074: default:
075: throw new IllegalStateException(
076: "Unknown LiveAttributeException error code "
077: + ex.getCode());
078: }
079: this .e = ex.getElement();
080: this .params = new Object[] { ex.getAttributeName(),
081: ex.getValue() };
082: if (e != null && ctx != null) {
083: this .line = ctx.getDocumentLoader().getLineNumber(e);
084: }
085: }
086:
087: /**
088: * Constructs a new <tt>BridgeException</tt> with the specified parameters.
089: *
090: * @param ctx the bridge context to use for determining the element's
091: * source position
092: * @param e the element on which the error occurred
093: * @param code the error code
094: * @param params the parameters to use for the error message
095: */
096: public BridgeException(BridgeContext ctx, Element e, String code,
097: Object[] params) {
098:
099: this .e = e;
100: this .code = code;
101: this .params = params;
102: if (e != null && ctx != null) {
103: this .line = ctx.getDocumentLoader().getLineNumber(e);
104: }
105: }
106:
107: /**
108: * Constructs a new <tt>BridgeException</tt> with the specified parameters.
109: *
110: * @param ctx the bridge context to use for determining the element's
111: * source position
112: * @param e the element on which the error occurred
113: * @param ex the exception which was the root-cause for this exception
114: * @param code the error code
115: * @param params the parameters to use for the error message
116: */
117: public BridgeException(BridgeContext ctx, Element e, Exception ex,
118: String code, Object[] params) {
119:
120: // todo ex can be chained in jdk >= 1.4
121: this .e = e;
122:
123: message = ex.getMessage();
124: this .code = code;
125: this .params = params;
126: if (e != null && ctx != null) {
127: this .line = ctx.getDocumentLoader().getLineNumber(e);
128: }
129: }
130:
131: /**
132: * Constructs a new <tt>BridgeException</tt> with the specified parameters.
133: *
134: * @param ctx the bridge context to use for determining the element's
135: * source position
136: * @param e the element on which the error occurred
137: * @param message the error message
138: */
139: public BridgeException(BridgeContext ctx, Element e, String message) {
140: this .e = e;
141: this .message = message;
142: if (e != null && ctx != null) {
143: this .line = ctx.getDocumentLoader().getLineNumber(e);
144: }
145: }
146:
147: /**
148: * Returns the element on which the error occurred.
149: */
150: public Element getElement() {
151: return e;
152: }
153:
154: /**
155: * Sets the graphics node that represents the current GVT tree built.
156: *
157: * @param node the graphics node
158: */
159: public void setGraphicsNode(GraphicsNode node) {
160: this .node = node;
161: }
162:
163: /**
164: * Returns the graphics node that represents the current GVT tree built.
165: */
166: public GraphicsNode getGraphicsNode() {
167: return node;
168: }
169:
170: /**
171: * Returns the error message according to the error code and parameters.
172: */
173: public String getMessage() {
174: if (message != null) {
175: return message;
176: }
177:
178: String uri;
179: String lname = "<Unknown Element>";
180: SVGDocument doc = null;
181: if (e != null) {
182: doc = (SVGDocument) e.getOwnerDocument();
183: lname = e.getLocalName();
184: }
185: if (doc == null)
186: uri = "<Unknown Document>";
187: else
188: uri = doc.getURL();
189: Object[] fullparams = new Object[params.length + 3];
190: fullparams[0] = uri;
191: fullparams[1] = new Integer(line);
192: fullparams[2] = lname;
193: System.arraycopy(params, 0, fullparams, 3, params.length);
194: return Messages.formatMessage(code, fullparams);
195: }
196:
197: /**
198: * Returns the exception's error code
199: */
200: public String getCode() {
201: return code;
202: }
203: }
|