home *** CD-ROM | disk | FTP | other *** search
/ Dynamic HTML in Action / Dynamicke-HTML-v-akci-covermount.bin / XML / PARSER / XMLINST.EXE / classes / com / ms / xml / parser / EntityReader.java < prev    next >
Encoding:
Java Source  |  1997-12-01  |  2.6 KB  |  123 lines

  1. /*
  2.  * @(#)EntityReader.java 1.0 6/3/97
  3.  * 
  4.  * Copyright (c) 1997 Microsoft, Corp. All Rights Reserved.
  5.  * 
  6.  */
  7.  
  8. package com.ms.xml.parser;
  9.  
  10. import java.io.*;
  11.  
  12. /**
  13.  * This is a wrapper class on entities to provide transparent
  14.  * inclusion of entity text
  15.  *
  16.  * @version 1.0, 6/3/97
  17.  */
  18. class EntityReader 
  19. {
  20.     EntityReader(InputStream in, int line, int column, EntityReader prev, Object owner)
  21.     {        
  22.         this.line = line;
  23.         this.column = column;
  24.         this.prev = prev;
  25.         this.owner = owner;
  26.         this.pos = -1;
  27.         this.input = in;
  28.     }
  29.  
  30.     public int read() throws ParseException
  31.     {
  32.         int c =  readChar();
  33.  
  34.         // Proper end-of-line handling.
  35.  
  36.         if (c == 0xd || c == 0xa)
  37.         {
  38.             if (c == 0xd)
  39.             {
  40.                 int d = readChar();
  41.                 if (d != 0xa)
  42.                 {
  43.                     // Hmmm.  not the usual 0xd, 0xa pair.
  44.                     // Put the d back
  45.                     push((char)d);
  46.                 }
  47.                 c = 0xa; // normalize to 0xa
  48.             }
  49.             line++;
  50.             column = 1;
  51.         }
  52.         else
  53.         {
  54.             column++;
  55.         }
  56.         return c;
  57.     }
  58.  
  59.     int readChar() throws ParseException
  60.     {
  61.         int c;
  62.         if( pos >= 0 )
  63.         {
  64.             c = next[pos--];
  65.         }
  66.         else
  67.         {
  68.             try
  69.             {
  70.                 c = input.read();
  71.             }
  72.             catch( IOException e )
  73.             {
  74.                 String s = owner.toString();
  75.                 c = -1;
  76.                 throw new ParseException( "Error reading " + s + " at (" + line + "," + column + ") : " + e,
  77.                     line, column, owner);
  78.             }
  79.         }
  80.         return c;
  81.     }
  82.  
  83.     void push(char next) throws ParseException
  84.     {
  85.         if (pos == 1) {
  86.             throw new ParseException("Error unreading '" + next + "' at (" + line + "," + column + ") : ",
  87.                     line, column, owner);
  88.         }
  89.         this.next[++pos] = next;
  90.     }
  91.  
  92.     /**
  93.      * line number
  94.      */    
  95.     int line;
  96.     
  97.     /**
  98.      * character pos
  99.      */    
  100.     int column;
  101.  
  102.     /**
  103.      * previous reader to go back to when this is done
  104.      */
  105.     EntityReader prev;
  106.     
  107.     /**
  108.      * Entity reading from
  109.      */    
  110.     Object owner;
  111.  
  112.     /**
  113.      * For limited character push-back support.
  114.      */
  115.     int next[] = new int[3];
  116.     int pos;
  117.  
  118.     /**
  119.      * The input stream for reading characters
  120.      */
  121.     InputStream input;
  122. }
  123.