home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / desvs7nu / src / xbmparser.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  3.6 KB  |  159 lines

  1.  
  2. import java.io.*;
  3.  
  4. /**
  5.  * X11 bitmap (xbm) parser class.
  6.  *
  7.  * Parse files of the form:
  8.  * <pre>
  9.  * #define foo_width w
  10.  * #define foo_height h
  11.  * static char foo_bits[] = {
  12.  * 0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,
  13.  * 0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,
  14.  * 0xnn,0xnn,0xnn,0xnn};
  15.  * </pre>
  16.  *
  17.  * @version    1.2 96/02/20
  18.  * @author     Jan Andersson, Torpa Konsult AB. (janne@torpa.se)
  19.  */
  20. public class XbmParser {
  21.    private static final int GET_WIDTH = 0;
  22.    private static final int GET_HEIGHT = 1;
  23.    private static final int GET_START = 2;
  24.    private static final int GET_BYTES = 3;
  25.    
  26.    private StreamTokenizer tokenizer;
  27.    private int width = 0;
  28.    private int height = 0;
  29.    private int[] bitmap = null;
  30.    
  31.    /**
  32.     * Construct and XpmParser from an InputStream
  33.     * @param is the imput stream to parse
  34.     */
  35.    public XbmParser(InputStream is) {
  36.       tokenizer = new StreamTokenizer(is);
  37.       tokenizer.slashStarComments(true);
  38.       tokenizer.ordinaryChar('/');
  39.    }
  40.  
  41.    /**
  42.     * Parse input stream.
  43.     * @return true on success.
  44.     */
  45.    public boolean parse() {
  46.       try {
  47.      parseInput();
  48.      return true;
  49.       }
  50.       catch (Exception e) {
  51.      return false;
  52.       }
  53.    }
  54.  
  55.    /**
  56.     * Get image width.
  57.     * @return width in pixels.
  58.     */
  59.    public int getWidth() {
  60.       return width;
  61.    }
  62.  
  63.    /**
  64.     * Get image height.
  65.     * @return height in pixels.
  66.     */
  67.    public int getHeight() {
  68.       return height;
  69.    }
  70.  
  71.    /**
  72.     * Get pixmap.
  73.     * @return pixmap array.
  74.     */
  75.    public int[] getBitmap() {
  76.       return bitmap;
  77.    }
  78.    
  79.    /**
  80.     * Parse input stream.
  81.     * @exception Exception on input errors.
  82.     */
  83.    private void parseInput() throws Exception {
  84.       int state = GET_WIDTH;
  85.       int token = StreamTokenizer.TT_EOF;
  86.       int index = 0;
  87.  
  88.       token = tokenizer.nextToken();
  89.       while (token != StreamTokenizer.TT_EOF) {
  90.      switch(state) {
  91.         case GET_WIDTH:
  92.            if (token == StreamTokenizer.TT_NUMBER) {
  93.           width = (int) tokenizer.nval;
  94.           state = GET_HEIGHT;
  95.            }
  96.            break;
  97.         case GET_HEIGHT:
  98.            if (token == StreamTokenizer.TT_NUMBER) {
  99.           height = (int) tokenizer.nval;
  100.           state = GET_START;
  101.            }
  102.            break;
  103.         case GET_START:
  104.            if (token == '{') {
  105.           // reset tokinizer to handle bytes
  106.           resetTokenizer();
  107.           // allocate bitmap
  108.           bitmap = new int[width*height/8];
  109.           state = GET_BYTES;
  110.            }
  111.            break;
  112.         case GET_BYTES:
  113.            // waiting for 0xnn, or '}'
  114.            if (token == StreamTokenizer.TT_WORD) {
  115.           if (tokenizer.sval.length() > 2 &&
  116.               tokenizer.sval.charAt(0) == '0' &&
  117.               (tokenizer.sval.charAt(1) == 'x' ||
  118.                tokenizer.sval.charAt(1) == 'x')) {
  119.              try {
  120.             int val = Integer.parseInt(
  121.                tokenizer.sval.substring(2), 16);
  122.             bitmap[index++] = val;
  123.              }
  124.              catch (Exception e) {
  125.             token = StreamTokenizer.TT_EOF;
  126.              }    
  127.           }
  128.            }
  129.            else if (token == '}') {
  130.           // done!
  131.           token = StreamTokenizer.TT_EOF;
  132.            }
  133.      }
  134.      token = tokenizer.nextToken();
  135.       }
  136.       if (width == 0 || height == 0 || index != width*height/8)
  137.      throw(new Exception());
  138.    }
  139.  
  140.    /**
  141.     * Reset input tokenizer to handle hexadecimal values
  142.     */
  143.    private void resetTokenizer()
  144.    {
  145.       tokenizer.resetSyntax();
  146.       tokenizer.wordChars('a', 'f');
  147.       tokenizer.wordChars('A', 'F');
  148.       tokenizer.wordChars('0', '9');
  149.       tokenizer.wordChars('x', 'x');
  150.       tokenizer.wordChars('X', 'X');
  151.       tokenizer.whitespaceChars(0, ' ');
  152.       tokenizer.slashStarComments(true);
  153.    }
  154.    
  155.       
  156. }
  157.  
  158.    
  159.