home *** CD-ROM | disk | FTP | other *** search
/ Popular Software (Premium Edition) / mycd.iso / INTERNET / NETSCAP4.06 / CP32E406.EXE / netcast.z / ncjava10.jar / netscape / palomar / util / CascadedException.class (.txt) next >
Encoding:
Java Class File  |  1998-02-26  |  8.4 KB  |  239 lines

  1. package netscape.palomar.util;
  2.  
  3. import java.util.Enumeration;
  4. import java.util.Vector;
  5.  
  6. public class CascadedException extends Exception {
  7.    public static final int unknownArgument = 0;
  8.    public static final int unknownProperty = 1;
  9.    public static final int unknownMethod = 2;
  10.    public static final int unknownTrigger = 3;
  11.    public static final int fileNotFound = 4;
  12.    public static final int cantWriteToFile = 5;
  13.    public static final int invalidProjectElementLocation = 6;
  14.    public static final int CANT_LOAD_PALETTE_FILE = 7;
  15.    public static final int CANT_SAVE_PALETTE_FILE = 8;
  16.    public static final int CLONE_NOT_SUPPORTED = 9;
  17.    public static final int HTML_PARSER_FAILURE = 10;
  18.    public static final int HTML_PARSER_NOT_TEXT = 11;
  19.    public static final int HTML_PARSER_TEXT_FAILURE = 12;
  20.    public static final int HTML_PARSER_NOT_TOKEN = 13;
  21.    public static final int HTML_PARSER_TOKEN_FAILURE = 14;
  22.    public static final int HTML_PARSER_TIL_TOKEN_FAILURE = 15;
  23.    public static final int HTML_PARSER_CLOSE = 16;
  24.    public static final int HTML_PARSER_WRITE = 17;
  25.    public static final int HTML_PARSER_READ = 18;
  26.    public static final int CANT_VIEW_OBJECT = 19;
  27.    public static final int INVALID_ARGUMENT = 20;
  28.    public static final int NO_PROPERTY_TYPE = 21;
  29.    public static final int CANT_LOAD_SELECTED_ELEMENT = 22;
  30.    public static final int CANT_GET_ARCHIVE = 23;
  31.    public static final int IO_ERROR = 24;
  32.    public static final int SECURITY_EXCEPTION = 25;
  33.    public static final int CANT_GET_TWISTER_INSTALL_DIR = 26;
  34.    public static final int NO_PROJECT_IS_ACTIVE = 27;
  35.    public static final int NO_PROJECT_ELEMENT_IS_SELECTED = 28;
  36.    public static final int CANT_INSERT_BEFORE_OR_AFTER_ROOT_ELEMENT = 29;
  37.    public static final int UNDO_NOT_AVAILABLE = 30;
  38.    public static final int REDO_NOT_AVAILABLE = 31;
  39.    public static final int NOTHING_IN_CLIPBOARD_TO_PASTE = 32;
  40.    public static final int CANT_CUT_THIS_ITEM = 33;
  41.    public static final int CANT_DELETE_THIS_ITEM = 34;
  42.    public static final int CANT_PASTE_THAT_TYPE_HERE = 35;
  43.    public static final int CANT_COPY_THIS_ITEM = 36;
  44.    public static final int HAS_PARENT = 37;
  45.    public static final int SET_PARENT = 38;
  46.    public static final int NOT_MY_CHILD = 39;
  47.    public static final int PARENT_CLONE = 40;
  48.    public static final int CHILD_CLONE = 41;
  49.    public static final int END_OF_TREE = 42;
  50.    public static final int ADD_FILE_TO_ARCHIVE = 43;
  51.    public static final int CANT_VIEW_PAGE = 100;
  52.    protected Exception previous;
  53.    protected int errorID;
  54.    private Vector tokens = new Vector();
  55.  
  56.    public CascadedException(int id) {
  57.       this.errorID = id;
  58.    }
  59.  
  60.    public CascadedException(int id, Exception e) {
  61.       this.previous = e;
  62.       this.errorID = id;
  63.    }
  64.  
  65.    public int getErrorID() {
  66.       return this.errorID;
  67.    }
  68.  
  69.    public Exception getPrevious() {
  70.       return this.previous;
  71.    }
  72.  
  73.    public void addToken(String name, String value) {
  74.       this.tokens.addElement(new CascadedExceptionToken(name, value));
  75.    }
  76.  
  77.    public String singleLevelString() {
  78.       StringBuffer s = new StringBuffer();
  79.       s.append(this.getTextForId(this.errorID));
  80.       s.append("\n");
  81.       s.append(this.getClass().getName());
  82.       s.append("[#" + this.errorID + "]");
  83.       return s.toString();
  84.    }
  85.  
  86.    public String getMessage() {
  87.       return this.toString();
  88.    }
  89.  
  90.    public String toString() {
  91.       StringBuffer s = new StringBuffer();
  92.       s.append(this.singleLevelString());
  93.       if (this.previous != null) {
  94.          s.append("\n     because:\n");
  95.          s.append(this.previous.toString());
  96.          if (this.previous.getMessage() == null) {
  97.             s.append(": <null message>");
  98.          }
  99.       }
  100.  
  101.       return s.toString();
  102.    }
  103.  
  104.    protected String getTextForId(int id) {
  105.       String s = this.lookup(id);
  106.       return this.substituteTokens(s);
  107.    }
  108.  
  109.    protected String substituteTokens(String message) {
  110.       String returnString = new String(message);
  111.       Enumeration e = this.tokens.elements();
  112.  
  113.       while(e.hasMoreElements()) {
  114.          CascadedExceptionToken t = (CascadedExceptionToken)e.nextElement();
  115.          String token = "$" + t.name;
  116.          int startOfToken = returnString.indexOf(token);
  117.          if (startOfToken == -1) {
  118.             returnString = returnString + "\n" + t.name + " = " + t.value;
  119.          } else {
  120.             String firstHalf = returnString.substring(0, startOfToken);
  121.             int endOfToken = startOfToken + token.length();
  122.             String secondHalf;
  123.             if (endOfToken >= returnString.length()) {
  124.                secondHalf = "";
  125.             } else {
  126.                secondHalf = returnString.substring(endOfToken);
  127.             }
  128.  
  129.             returnString = firstHalf + t.value + secondHalf;
  130.          }
  131.       }
  132.  
  133.       return returnString;
  134.    }
  135.  
  136.    protected String lookup(int id) {
  137.       switch (id) {
  138.          case 0:
  139.             return "unknown argument $name in the method";
  140.          case 1:
  141.             return "unknown property $name";
  142.          case 2:
  143.             return "unknown method $name";
  144.          case 3:
  145.             return "unknown trigger $name";
  146.          case 4:
  147.             return "file '$filename' does not exist";
  148.          case 5:
  149.             return "can't write to file '$filename'";
  150.          case 6:
  151.             return "$projectElement's can't be placed there.";
  152.          case 7:
  153.             return "Can't load palette file $filename";
  154.          case 8:
  155.             return "Can't save palette file $filename";
  156.          case 9:
  157.             return "Clone not supported; must be reimplemented in subclass";
  158.          case 10:
  159.             return "Unable to parse the HTML at this level";
  160.          case 11:
  161.             return "Attept to read a text block when a token is next";
  162.          case 12:
  163.             return "Unable to retrieve a text block from HTML stream";
  164.          case 13:
  165.             return "Attept to read a token when a text block is next";
  166.          case 14:
  167.             return "Unable to find a valid token";
  168.          case 15:
  169.             return "Unable to find the ending token";
  170.          case 16:
  171.             return "Unable to close the HTML parse stream";
  172.          case 17:
  173.             return "Unable to write HTML to file $filename";
  174.          case 18:
  175.             return "Unable to read HTML file $filename";
  176.          case 19:
  177.             return "View of type $classname1 cannot view object of type $classname2";
  178.          case 20:
  179.             return "Value \"$argval\" not valid for argument \"$arg\" in method $methodname";
  180.          case 21:
  181.             return "Unable to retrieve the class file $classname for property $property";
  182.          case 22:
  183.             return "Unable to import the selected element";
  184.          case 23:
  185.             return "Unable to create or open archive $name";
  186.          case 24:
  187.             return "Error $opname file $filename";
  188.          case 25:
  189.             return "Security exception trying to $opname";
  190.          case 26:
  191.             return "Can't find Twister installation directory, must have class path with 'twister' in it";
  192.          case 27:
  193.             return "No project is currently active.";
  194.          case 28:
  195.             return "No project element us currently selected.";
  196.          case 29:
  197.             return "You can't insert elements before or after the root element.";
  198.          case 30:
  199.             return "Nothing to undo";
  200.          case 31:
  201.             return "Nothing to redo";
  202.          case 32:
  203.             return "Nothing in the clipboard to paste.";
  204.          case 33:
  205.             return "You can't cut this item.";
  206.          case 34:
  207.             return "You can't delete this item.";
  208.          case 35:
  209.             return "You can't paste this type of item here.";
  210.          case 36:
  211.             return "You can't copy this item.";
  212.          case 37:
  213.             return "Unable to re-parent, parent already set on $child";
  214.          case 38:
  215.             return "Failure setting the parent of tag $tag";
  216.          case 39:
  217.             return "Attempt to add child $child to parent, but it already has different parent";
  218.          case 40:
  219.             return "Clone failure on Tree element, unable to clone $parent";
  220.          case 41:
  221.             return "Clone failure on Tree element, unable to initialize newly cloned $parent";
  222.          case 42:
  223.             return "Invalid call to TreeEnumeration.nextElement() after all elements already fetched";
  224.          case 43:
  225.             return "Can't add file $filename to archive $archname";
  226.          case 100:
  227.             return "Can't view page.";
  228.          default:
  229.             return "identifer " + id + " not in CascadedException lookup table!";
  230.       }
  231.    }
  232.  
  233.    public static void printException(Exception e, String location) {
  234.       Warning w = new Warning(0, e);
  235.       ((CascadedException)w).addToken("msg", location);
  236.       Environment.getWarningList().addWarning(w);
  237.    }
  238. }
  239.