001: /*
002: * <copyright>
003: *
004: * Copyright 2001-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026: package org.cougaar.planning.servlet.data;
027:
028: import java.io.IOException;
029: import java.io.PrintWriter;
030: import java.io.Serializable;
031: import java.io.StringWriter;
032:
033: import org.cougaar.planning.servlet.data.xml.DeXMLable;
034: import org.cougaar.planning.servlet.data.xml.UnexpectedXMLException;
035: import org.cougaar.planning.servlet.data.xml.XMLWriter;
036: import org.cougaar.planning.servlet.data.xml.XMLable;
037: import org.xml.sax.Attributes;
038:
039: /**
040: * Represents an error that can be returned instead of the normal
041: * object
042: *
043: * @since 1/24/01
044: **/
045: public class Failure implements XMLable, DeXMLable, Serializable {
046:
047: //Variables:
048: ////////////
049:
050: //Tags:
051: public static final String NAME_TAG = "Error";
052: protected static final String MESSAGE_TAG = "Message";
053: protected static final String STACK_TAG = "StackTrace";
054: //Attr:
055:
056: protected String message;
057: protected String stackTrace;
058:
059: //Constructors:
060: ///////////////
061:
062: public Failure() {
063: }
064:
065: public Failure(String message) {
066: this .message = message;
067: this .stackTrace = "";
068: }
069:
070: public Failure(String message, String stackTrace) {
071: this .message = message;
072: this .stackTrace = stackTrace;
073: }
074:
075: public Failure(Exception e) {
076: message = e.toString();
077: StringWriter sw = new StringWriter();
078: PrintWriter pw = new PrintWriter(sw);
079: e.printStackTrace(pw);
080: pw.close();
081: stackTrace = sw.toString();
082: }
083:
084: //Members:
085: //////////
086:
087: public String getMessage() {
088: return message;
089: }
090:
091: public String getStackTrace() {
092: return stackTrace;
093: }
094:
095: public String toString() {
096: if (stackTrace == null || stackTrace.equals("")) {
097: return message;
098: }
099: return message + " [" + stackTrace + "]";
100: }
101:
102: //XMLable members:
103: //----------------
104:
105: /**
106: * Write this class out to the Writer in XML format
107: * @param w output Writer
108: **/
109: public void toXML(XMLWriter w) throws IOException {
110: w.optagln(NAME_TAG);
111: w.tagln(MESSAGE_TAG, message);
112: w.tagln(STACK_TAG, stackTrace);
113: w.cltagln(NAME_TAG);
114: }
115:
116: //DeXMLable members:
117: //------------------
118:
119: /**
120: * Report a startElement that pertains to THIS object, not any
121: * sub objects. Call also provides the elements Attributes and data.
122: * Note, that unlike in a SAX parser, data is guaranteed to contain
123: * ALL of this tag's data, not just a 'chunk' of it.
124: * @param name startElement tag
125: * @param attr attributes for this tag
126: * @param data data for this tag
127: **/
128: public void openTag(String name, Attributes attr, String data)
129: throws UnexpectedXMLException {
130:
131: if (name.equals(NAME_TAG)) {
132: } else if (name.equals(MESSAGE_TAG)) {
133: message = data;
134: } else if (name.equals(STACK_TAG)) {
135: stackTrace = data;
136: } else {
137: throw new UnexpectedXMLException("Unexpected tag: " + name);
138: }
139: }
140:
141: /**
142: * Report an endElement.
143: * @param name endElement tag
144: * @return true iff the object is DONE being DeXMLized
145: **/
146: public boolean closeTag(String name) throws UnexpectedXMLException {
147: return name.equals(NAME_TAG);
148: }
149:
150: /**
151: * This function will be called whenever a subobject has
152: * completed de-XMLizing and needs to be encorporated into
153: * this object.
154: * @param name the startElement tag that caused this subobject
155: * to be created
156: * @param obj the object itself
157: **/
158: public void completeSubObject(String name, DeXMLable obj)
159: throws UnexpectedXMLException {
160: throw new UnexpectedXMLException("Unknown object:" + name + ":"
161: + obj);
162: }
163:
164: //Inner Classes:
165:
166: private static final long serialVersionUID = 237888923872839741L;
167: }
|