home *** CD-ROM | disk | FTP | other *** search
/ Internet News 1999 October / INEWS_10_CD.ISO / pc / jdk / jdk1.2.2 / docs / tooldocs / javadoc / source / MessageRetriever.java < prev    next >
Encoding:
Java Source  |  1999-09-19  |  7.4 KB  |  264 lines

  1. /*
  2.  * @(#)MessageRetriever.java    1.4 98/09/22
  3.  *
  4.  * Copyright 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package com.sun.tools.doclets;
  16.  
  17. import com.sun.javadoc.*;
  18. import java.io.*;
  19. import java.util.*;
  20. import java.lang.*;
  21. import java.text.MessageFormat;
  22.  
  23.  
  24. /** 
  25.  * Retrieve and format messages stored in a resource.
  26.  *
  27.  * @since JDK1.2
  28.  * @author Atul M Dambalkar
  29.  * @author Robert Field
  30. */
  31. public class MessageRetriever {
  32.     private ResourceBundle messageRB;
  33.  
  34.     /**
  35.      * Initilize the ResourceBundle with the given resource.
  36.      *
  37.      * @param resourcelocation Resource.
  38.      */
  39.     public MessageRetriever(String resourcelocation) {
  40.     try {
  41.         messageRB = ResourceBundle.getBundle(resourcelocation);
  42.     } catch (MissingResourceException e) {
  43.             throw new Error("Fatal: Resource for javadoc doclets is missing: "+
  44.                              resourcelocation);
  45.     }
  46.     }
  47.  
  48.     /**
  49.      * get and format message string from resource
  50.      *
  51.      * @param key selects message from resource
  52.      */
  53.     public String getText(String key) {
  54.     return getText(key, (String)null);
  55.     }
  56.  
  57.     /**
  58.      * Get and format message string from resource
  59.      *
  60.      * @param key selects message from resource
  61.      * @param a1 Argument, to be repalced in the message.
  62.      */
  63.     public String getText(String key, String a1) {
  64.     return getText(key, a1, null);
  65.     }
  66.  
  67.     /**
  68.      * Get and format message string from resource
  69.      *
  70.      * @param key selects message from resource
  71.      * @param a1 first argument to be replaced in the message.
  72.      * @param a2 second argument to be replaced in the message.
  73.      */
  74.     public String getText(String key, String a1, String a2) {
  75.     return getText(key, a1, a2, null);
  76.     }
  77.  
  78.     /**
  79.      * Get and format message string from resource
  80.      *
  81.      * @param key selects message from resource
  82.      * @param a1 first argument to be replaced in the message.
  83.      * @param a2 second argument to be replaced in the message.
  84.      * @param a3 third argument to be replaced in the message.
  85.      */
  86.     public String getText(String key, String a1, String a2, String a3) {
  87.     try {
  88.         String message = messageRB.getString(key);
  89.         String[] args = new String[3];
  90.         args[0] = a1;
  91.         args[1] = a2;
  92.         args[2] = a3;
  93.         return MessageFormat.format(message, args);
  94.     } catch (MissingResourceException e) {
  95.         throw new Error("Fatal: Resource for javadoc is broken. There is no " + key + " key in resource.");
  96.     }
  97.     }
  98.     
  99.     /**
  100.      * Print error message, increment error count.
  101.      *
  102.      * @param msg message to print
  103.      */
  104.    static void printError(String msg) {
  105.     if (Configuration.root != null) {
  106.             Configuration.root.printError(msg);
  107.         } else {
  108.             System.err.println(/* Main.program + ": " + */ msg);
  109.         }
  110.     }    
  111.  
  112.     /**
  113.      * Print warning message, increment warning count.
  114.      *
  115.      * @param msg message to print
  116.      */
  117.     static void printWarning(String msg) {
  118.     if (Configuration.root != null) {
  119.             Configuration.root.printWarning(msg);
  120.         } else {
  121.             System.err.println(/* Main.program +  ": warning - " + */ "Warning: " + msg);
  122.         }
  123.     }    
  124.  
  125.     /**
  126.      * Print a message.
  127.      *
  128.      * @param msg message to print
  129.      */
  130.     static void printNotice(String msg) {
  131.     if (Configuration.root != null) {
  132.             Configuration.root.printNotice(msg);
  133.         } else {
  134.             System.err.println(msg);
  135.         }
  136.     }    
  137.  
  138.     /**
  139.      * Print error message, increment error count.
  140.      *
  141.      * @param key selects message from resource
  142.      */
  143.     public void error(String key) { 
  144.         printError(getText(key)); 
  145.     }
  146.  
  147.     /**
  148.      * Print error message, increment error count.
  149.      *
  150.      * @param key selects message from resource
  151.      * @param a1 first argument to be replaced in the message.
  152.      */
  153.     public void error(String key, String a1) { 
  154.         printError(getText(key, a1)); 
  155.     }
  156.  
  157.     /**
  158.      * Print error message, increment error count.
  159.      *
  160.      * @param key selects message from resource
  161.      * @param a1 first argument to be replaced in the message.
  162.      * @param a2 second argument to be replaced in the message.
  163.      */
  164.     public void error(String key, String a1, String a2) { 
  165.         printError(getText(key, a1, a2)); 
  166.     }
  167.  
  168.     /**
  169.      * Print error message, increment error count.
  170.      *
  171.      * @param key selects message from resource
  172.      * @param a1 first argument to be replaced in the message.
  173.      * @param a2 second argument to be replaced in the message.
  174.      * @param a3 third argument to be replaced in the message.
  175.      */
  176.     public void error(String key, String a1, String a2, String a3) { 
  177.         printError(getText(key, a1, a2, a3)); 
  178.     }
  179.  
  180.     /**
  181.      * Print warning message, increment warning count.
  182.      *
  183.      * @param key selects message from resource
  184.      */
  185.     public void warning(String key) { 
  186.         printWarning(getText(key)); 
  187.     }
  188.  
  189.     /**
  190.      * Print warning message, increment warning count.
  191.      *
  192.      * @param key selects message from resource
  193.      * @param a1 first argument to be replaced in the message.
  194.      */
  195.     public void warning(String key, String a1) { 
  196.         printWarning(getText(key, a1)); 
  197.     }
  198.  
  199.     /**
  200.      * Print warning message, increment warning count.
  201.      *
  202.      * @param key selects message from resource
  203.      * @param a1 first argument to be replaced in the message.
  204.      * @param a2 second argument to be replaced in the message.
  205.      */
  206.     public void warning(String key, String a1, String a2) { 
  207.         printWarning(getText(key, a1, a2)); 
  208.     }
  209.  
  210.     /**
  211.      * Print warning message, increment warning count.
  212.      *
  213.      * @param key selects message from resource
  214.      * @param a1 first argument to be replaced in the message.
  215.      * @param a2 second argument to be replaced in the message.
  216.      * @param a3 third argument to be replaced in the message.
  217.      */
  218.     public void warning(String key, String a1, String a2, String a3) { 
  219.         printWarning(getText(key, a1, a2, a3)); 
  220.     }
  221.  
  222.     /**
  223.      * Print a message.
  224.      *
  225.      * @param key selects message from resource
  226.      */
  227.     public void notice(String key) { 
  228.         printNotice(getText(key)); 
  229.     }
  230.  
  231.     /**
  232.      * Print a message.
  233.      *
  234.      * @param key selects message from resource
  235.      * @param a1 first argument to be replaced in the message.
  236.      */
  237.     public void notice(String key, String a1) { 
  238.         printNotice(getText(key, a1)); 
  239.     }
  240.  
  241.     /**
  242.      * Print a message.
  243.      *
  244.      * @param key selects message from resource
  245.      * @param a1 first argument to be replaced in the message.
  246.      * @param a2 second argument to be replaced in the message.
  247.      */
  248.     public void notice(String key, String a1, String a2) { 
  249.         printNotice(getText(key, a1, a2)); 
  250.     }
  251.  
  252.     /**
  253.      * Print a message.
  254.      *
  255.      * @param key selects message from resource
  256.      * @param a1 first argument to be replaced in the message.
  257.      * @param a2 second argument to be replaced in the message.
  258.      * @param a3 third argument to be replaced in the message.
  259.      */
  260.     public void notice(String key, String a1, String a2, String a3) { 
  261.         printNotice(getText(key, a1, a2, a3)); 
  262.     }
  263. }
  264.