microburlapinput.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,032 行 · 第 1/2 页

JAVA
1,032
字号
    int ch = skipWhitespace();    peek = ch;        if (ch == '<') {      expectEndTag("length");      return -1;    }    int value = parseInt();          expectEndTag("length");        return value;  }  /**   * Resolves a remote object.   */  public Object resolveRemote(String type, String url)    throws IOException  {    return new BurlapRemote(type, url);  }  /**   * Reads a fault.   */  public Hashtable readFault()    throws IOException  {    expectStartTag("fault");        Hashtable map = new Hashtable();    while (parseTag()) {      peekTag = true;      Object key = readObject(null);      Object value = readObject(null);      if (key != null && value != null)        map.put(key, value);    }        if (! sbuf.toString().equals("fault"))      throw new BurlapProtocolException("expected </fault>");    return map;  }    /**   * Reads an object from the input stream.   *   * @param expectedClass the calling routine's expected class   * @param type the type from the stream   */  public Object readMap(Class expectedClass, String type)    throws IOException  {    Hashtable map = new Hashtable();    if (refs == null)      refs = new Vector();    refs.addElement(map);    while (parseTag()) {      peekTag = true;      Object key = readObject(null);      Object value = readObject(null);      map.put(key, value);    }    if (! sbuf.toString().equals("map"))      throw new BurlapProtocolException("expected </map>");    return map;  }  /**   * Reads object unknown to MicroBurlapInput.   */  protected Object readExtensionObject(Class expectedClass, String tag)    throws IOException  {    throw new BurlapProtocolException("unknown object tag <" + tag + ">");  }    /**   * Reads a list object from the input stream.   *   * @param expectedClass the calling routine's expected class   * @param type the type from the stream   * @param length the expected length, -1 for unspecified length   */  public Object readList(Class expectedClass, String type, int length)    throws IOException  {    Vector list = new Vector();    if (refs == null)      refs = new Vector();    refs.addElement(list);    while (parseTag()) {      peekTag = true;      Object value = readObject(null);      list.addElement(value);    }        if (! sbuf.toString().equals("list"))      throw new BurlapProtocolException("expected </list>");    return list;  }  /**   * Parses an integer value from the stream.   */  protected int parseInt()    throws IOException  {    int sign = 1;    int value = 0;    int ch = skipWhitespace();    if (ch == '+')      ch = read();    else if (ch == '-') {      sign = -1;      ch = read();    }    for (; ch >= '0' && ch <= '9'; ch = read())      value = 10 * value + ch - '0';        peek = ch;    return sign * value;  }  /**   * Parses a long value from the stream.   */  protected long parseLong()    throws IOException  {    long sign = 1;    long value = 0;    int ch = skipWhitespace();    if (ch == '+')      ch = read();    else if (ch == '-') {      sign = -1;      ch = read();    }    for (; ch >= '0' && ch <= '9'; ch = read()) {      value = 10 * value + ch - '0';    }    peek = ch;    return sign * value;  }  /**   * Parses a date value from the stream.   */  protected long parseDate(Calendar calendar)    throws IOException  {    int ch = skipWhitespace();        int year = 0;    for (int i = 0; i < 4; i++) {      if (ch >= '0' && ch <= '9')        year = 10 * year + ch - '0';      else        throw expectedChar("year", ch);      ch = read();    }    int month = 0;    for (int i = 0; i < 2; i++) {      if (ch >= '0' && ch <= '9')        month = 10 * month + ch - '0';      else        throw expectedChar("month", ch);      ch = read();    }    int day = 0;    for (int i = 0; i < 2; i++) {      if (ch >= '0' && ch <= '9')        day = 10 * day + ch - '0';      else        throw expectedChar("day", ch);      ch = read();    }    if (ch != 'T')      throw expectedChar("`T'", ch);    ch = read();    int hour = 0;    for (int i = 0; i < 2; i++) {      if (ch >= '0' && ch <= '9')        hour = 10 * hour + ch - '0';      else        throw expectedChar("hour", ch);      ch = read();    }    int minute = 0;    for (int i = 0; i < 2; i++) {      if (ch >= '0' && ch <= '9')        minute = 10 * minute + ch - '0';      else        throw expectedChar("minute", ch);      ch = read();    }    int second = 0;    for (int i = 0; i < 2; i++) {      if (ch >= '0' && ch <= '9')        second = 10 * second + ch - '0';      else        throw expectedChar("second", ch);      ch = read();    }    for (; ch > 0 && ch != '<'; ch = read()) {    }    peek = ch;    calendar.set(Calendar.YEAR, year);    calendar.set(Calendar.MONTH, month - 1);    calendar.set(Calendar.DAY_OF_MONTH, day);    calendar.set(Calendar.HOUR_OF_DAY, hour);    calendar.set(Calendar.MINUTE, minute);    calendar.set(Calendar.SECOND, second);    calendar.set(Calendar.MILLISECOND, 0);    return calendar.getTime().getTime();  }  /**   * Parses a string value from the stream.   * string buffer is used for the result.   */  protected String parseString()    throws IOException  {    StringBuffer sbuf = new StringBuffer();    return parseString(sbuf).toString();  }    /**   * Parses a string value from the stream.  The burlap object's   * string buffer is used for the result.   */  protected StringBuffer parseString(StringBuffer sbuf)    throws IOException  {    int ch = read();    for (; ch >= 0 && ch != '<'; ch = read()) {      if (ch == '&') {        ch = read();        if (ch == '#') {          ch = read();          if (ch >= '0' && ch <= '9') {            int v = 0;            for (; ch >= '0' && ch <= '9'; ch = read()) {              v = 10 * v + ch - '0';            }            sbuf.append((char) v);          }        }        else {          StringBuffer entityBuffer = new StringBuffer();          for (; ch >= 'a' && ch <= 'z'; ch = read())            entityBuffer.append((char) ch);          String entity = entityBuffer.toString();          if (entity.equals("amp"))            sbuf.append('&');          else if (entity.equals("apos"))            sbuf.append('\'');          else if (entity.equals("quot"))            sbuf.append('"');          else if (entity.equals("lt"))            sbuf.append('<');          else if (entity.equals("gt"))            sbuf.append('>');          else            throw new BurlapProtocolException("unknown XML entity &" + entity + "; at `" + (char) ch + "'");        }        if (ch != ';')          throw expectedChar("';'", ch);      }      else if (ch < 0x80)        sbuf.append((char) ch);      else if ((ch & 0xe0) == 0xc0) {        int ch1 = read();        int v = ((ch & 0x1f) << 6) + (ch1 & 0x3f);        sbuf.append((char) v);      }      else if ((ch & 0xf0) == 0xe0) {        int ch1 = read();        int ch2 = read();        int v = ((ch & 0x0f) << 12) + ((ch1 & 0x3f) << 6) + (ch2 & 0x3f);        sbuf.append((char) v);      }      else        throw new BurlapProtocolException("bad utf-8 encoding");    }    peek = ch;    return sbuf;  }    /**   * Parses a byte array.   */  protected byte []parseBytes()    throws IOException  {    ByteArrayOutputStream bos = new ByteArrayOutputStream();    parseBytes(bos);    return bos.toByteArray();  }    /**   * Parses a byte array.   */  protected ByteArrayOutputStream parseBytes(ByteArrayOutputStream bos)    throws IOException  {    int ch;    for (ch = read(); ch >= 0 && ch != '<'; ch = read()) {      int b1 = ch;      int b2 = read();      int b3 = read();      int b4 = read();      if (b4 != '=') {        int chunk = ((base64Decode[b1] << 18) +                     (base64Decode[b2] << 12) +                     (base64Decode[b3] << 6) +                     (base64Decode[b4]));        bos.write(chunk >> 16);        bos.write(chunk >> 8);        bos.write(chunk);      }      else if (b3 != '=') {        int chunk = ((base64Decode[b1] << 12) +                     (base64Decode[b2] << 6) +                     (base64Decode[b3]));        bos.write(chunk >> 8);        bos.write(chunk);      }      else {        int chunk = ((base64Decode[b1] << 6) +                     (base64Decode[b2]));        bos.write(chunk);      }    }    if (ch == '<')      peek = ch;        return bos;  }  protected void expectStartTag(String tag)    throws IOException  {    if (! parseTag())      throw new BurlapProtocolException("expected <" + tag + ">");    if (! sbuf.toString().equals(tag))      throw new BurlapProtocolException("expected <" + tag + "> at <" + sbuf + ">");  }  protected void expectEndTag(String tag)    throws IOException  {    if (parseTag())      throw new BurlapProtocolException("expected </" + tag + ">");    if (! sbuf.toString().equals(tag))      throw new BurlapProtocolException("expected </" + tag + "> at </" + sbuf + ">");  }  /**   * Parses a tag.  Returns true if it's a start tag.   */  protected boolean parseTag()    throws IOException  {    if (peekTag) {      peekTag = false;      return true;    }        int ch = skipWhitespace();    boolean isStartTag = true;    if (ch != '<')      throw expectedChar("'<'", ch);    ch = read();    if (ch == '/') {      isStartTag = false;      ch = is.read();    }        if (! isTagChar(ch))      throw expectedChar("tag", ch);          sbuf.setLength(0);    for (; isTagChar(ch); ch = read())      sbuf.append((char) ch);    if (ch != '>')      throw expectedChar("'>'", ch);    return isStartTag;  }  protected IOException expectedChar(String expect, int actualChar)  {    return new BurlapProtocolException("expected " + expect + " at " +                           (char) actualChar + "'");  }  protected IOException expectBeginTag(String expect, String tag)  {    return new BurlapProtocolException("expected <" + expect + "> at <" + tag + ">");  }  private boolean isTagChar(int ch)  {    return (ch >= 'a' && ch <= 'z' ||            ch >= 'A' && ch <= 'Z' ||            ch >= '0' && ch <= '9' ||            ch == ':' || ch == '-');  }  protected int skipWhitespace()    throws IOException  {    int ch = read();    for (;         ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r';         ch = read()) {    }    return ch;  }  protected boolean isWhitespace(int ch)    throws IOException  {    return ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r';  }  protected int read()    throws IOException  {    if (peek > 0) {      int value = peek;      peek = 0;      return value;    }        return is.read();  }  static {    base64Decode = new int[256];    for (int i = 'A'; i <= 'Z'; i++)      base64Decode[i] = i - 'A';    for (int i = 'a'; i <= 'z'; i++)      base64Decode[i] = i - 'a' + 26;    for (int i = '0'; i <= '9'; i++)      base64Decode[i] = i - '0' + 52;    base64Decode['+'] = 62;    base64Decode['/'] = 63;  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?