compactparser.java

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

JAVA
989
字号
      choice.addChild(pattern);      return choice;    }    else if (token == '+') {      GroupPattern group = new GroupPattern();      group.addChild(pattern);      group.addChild(new ZeroOrMorePattern(pattern));      return group;    }    else {      _peekToken = token;    }    return pattern;  }  /**   * Parses an element.   */  private Pattern parseElement(GrammarPattern grammar)    throws IOException, SAXException, RelaxException  {    String id = generateId();    ElementPattern elt = new ElementPattern(id);    grammar.setDefinition(id, elt);        elt.addNameChild(parseNameClass(grammar, true));    int token = parseToken();    if (token == '{') {      elt.addChild(parsePattern(grammar));      token = parseToken();      if (token != '}')        throw error(L.l("expected '}' at {0}", errorToken(token)));    }    return elt;  }  /**   * Parses an element.   */  private Pattern parseAttribute(GrammarPattern grammar)    throws IOException, SAXException, RelaxException  {    AttributePattern elt = new AttributePattern();    elt.addNameChild(parseNameClass(grammar, false));    int token = parseToken();    if (token == '{') {      token = parseToken();      if (token == '}')        return elt;      _peekToken = token;            elt.addChild(parsePattern(grammar));      token = parseToken();      if (token != '}')        throw error(L.l("expected '}' at {0}", errorToken(token)));    }    return elt;  }  /**   * Parses a name class.   */  private NameClassPattern parseNameClass(GrammarPattern grammar,                                          boolean isElement)    throws IOException, SAXException, RelaxException  {    NameClassPattern left = parseName(grammar, isElement);    ChoiceNamePattern choice = null;    int ch;    while ((ch = skipWhitespace()) == '|') {      NameClassPattern right = parseName(grammar, isElement);      if (choice == null) {        choice = new ChoiceNamePattern();        choice.addNameChild(left);      }      choice.addNameChild(right);    }    unread();    if (choice != null)      return choice;    else      return left;  }  /**   * Parses a name class.   */  private NameClassPattern parseName(GrammarPattern grammar, boolean isElement)    throws IOException, SAXException, RelaxException  {    int ch = skipWhitespace();    if (ch == '(') {      NameClassPattern name = parseNameClass(grammar, isElement);      ch = skipWhitespace();      if (ch != ')')        throw error(L.l("expected ')' at '{0}'", String.valueOf((char) ch)));      return name;    }    char []cbuf = _cb.getBuffer();    int i = 0;        while (ch > 0 && ch < 256 && NAME_CHAR[ch]) {      cbuf[i++] = (char) ch;      if (_offset < _length)	ch = _buffer[_offset++] & 0xff;      else	ch = read();    }    _cb.setLength(i);        if (ch == '*')      _cb.append('*');    else      unread();    if (_cb.length() == 0)      throw error(L.l("expected name at '{0}'", String.valueOf((char) ch)));    NameClassPattern pattern;    String lexeme = _cb.toString();    int p = lexeme.lastIndexOf(':');    String ns = _ns;    String localName;        if (p < 0) {      localName = lexeme;      if (! isElement)        ns = null;    }    else {      String prefix = lexeme.substring(0, p);      localName = lexeme.substring(p + 1);      ns = _nsMap.get(prefix);      if (ns == null && localName.equals("*"))        throw error(L.l("'{0}' does not match a defined namespace.", lexeme));            if (ns == null) {// && isElement) {	pattern = createNamePattern(lexeme, "");        return pattern;      }    }    if (lexeme.equals("*")) {      AnyNamePattern namePattern = new AnyNamePattern();            namePattern.setExcept(parseExcept(grammar, isElement));            return namePattern;    }    else if (localName.equals("*")) {      NsNamePattern namePattern = new NsNamePattern(lexeme, ns);            namePattern.setExcept(parseExcept(grammar, isElement));            return namePattern;    }    else if ("".equals(ns) || ns == null) {      pattern = createNamePattern(localName, "");      return pattern;    }    else {      pattern = createNamePattern(lexeme, ns);      return pattern;    }  }  private NamePattern createNamePattern(String localName, String namespace)  {    return new NamePattern(new QName(localName, namespace));  }  /**   * Parses a name class.   */  private NameClassPattern parseExcept(GrammarPattern grammar,                                       boolean isElement)    throws IOException, SAXException, RelaxException  {    int ch = skipWhitespace();    if (ch != '-') {      unread();      return null;    }    return parseName(grammar, isElement);  }  /**   * Parses a token.   */  private int parseToken()    throws IOException, SAXException, RelaxException  {    int ch = _peekToken;    if (ch >= 0) {      _peekToken = -1;      return ch;    }    _cb.clear();          while (true)  {      if (_offset < _length)	ch = _buffer[_offset++];      else	ch = read();      switch (ch) {      case ' ':      case '\t':      case '\n':      case '\r':	break;            case '?':      case '*':      case '+':      case ',':      case '|':      case '&':      case '{':      case '}':      case '(':      case ')':      case '=':	return ch;      case '\"':      case '\'':	unread();	_lexeme = parseLiteral();	return LITERAL;      case '#':	do {	  ch = read();	  if (ch != '#')	    throw error(L.l("expected '#' at '{0}'", String.valueOf((char) ch)));        	  if (_cb.length() > 0)	    _cb.append('\n');	  for (ch = read(); ch > 0 && ch != '\n' && ch != '\r'; ch = read())	    _cb.append((char) ch);	  if (ch == '\r') {	    ch = read();	    if (ch != '\n')	      unread();	  }	  ch = read();	} while (ch == '#');	unread();	return COMMENT;      case -1:	_cb.append("end of file");	return -1;      default:	if (XmlChar.isNameStart(ch)) {	  char []cbuf = _cb.getBuffer();	  int i = 0;	  	  while (ch > 0 && ch < 256 && NAME_CHAR[ch]) {	    cbuf[i++] = (char) ch;	    if (_offset < _length)	      ch = _buffer[_offset++] & 0xff;	    else	      ch = read();	  }	  _cb.setLength(i);	  unread();	  int token = _tokenMap.get(_cb);	  if (token > 0) {	    _lexeme = null;	    return token;	  }	  else {	    _lexeme = _cb.toString().intern();	    return IDENTIFIER;	  }	}	else if (ch < 0) {	  _cb.append("end of file");	  return -1;	}	else {	  throw error(L.l("Unknown character '{0}'", String.valueOf((char) ch)));	}      }    }  }  private String parseLiteral()    throws IOException, SAXException, RelaxException  {    int end = skipWhitespace();    if (end != '"' && end != '\'')      throw error(L.l("expected '\"' at '{0}'", String.valueOf((char) end)));    _cb.clear();    int ch = read();    for (; ch >= 0 && ch != end; ch = read()) {      _cb.append((char) ch);    }    if (ch != end)      throw error(L.l("expected '\"' at '{0}'", String.valueOf((char) ch)));    return _cb.toString();  }  private String parseIdentifier()    throws IOException, SAXException, RelaxException  {    int ch = skipWhitespace();    if (! XmlChar.isNameChar(ch))      throw error(L.l("expected identifier character at '{0}'", String.valueOf((char) ch)));    _cb.clear();    for (; XmlChar.isNameChar(ch); ch = read()) {      _cb.append((char) ch);    }    return _cb.toString();  }  /**   * Parses whitespace.   */  private int skipWhitespace()    throws IOException, SAXException  {    int ch;          for (ch = read(); XmlChar.isWhitespace(ch); ch = read()) {    }    return ch;  }  private String errorToken(int ch)  {    switch (ch) {    case -1:      return "end of file";          case '?':    case '*':    case '+':    case ',':    case '|':    case '&':    case '{':    case '}':    case '(':    case ')':    case '=':      return String.valueOf((char) ch);          default:      return _cb.toString();    }  }  /**   * Creates an error.   */  private SAXException error(String msg)  {    return new SAXException(_filename + ":" + _line + ": " + msg);  }  /**   * Returns the current location string.   */  /*  public String getLocation()  {    return _filename + ":" + _line;  }  */    /**   * Reads a character.   */  private int read()    throws IOException  {    if (_length <= _offset) {      fillBuffer();      if (_length < 0)	return -1;    }        int ch = _buffer[_offset++];        if (ch == '\n')      _line++;    else if (ch == '\r') {      _line++;      if (_length <= _offset)	fillBuffer();            ch = _buffer[_offset++];            if (ch != '\n') {        unread();        ch = '\n';      }    }    return ch;  }  private void fillBuffer()    throws IOException  {    _length = _is.read(_buffer, 0, _buffer.length);    _offset = 0;  }  private void unread()  {    if (_offset > 0) {      _offset--;      int ch = _buffer[_offset];      if (ch == '\n')	_line--;    }  }  static {    _tokenMap.put(new CharBuffer("namespace"), NAMESPACE);    _tokenMap.put(new CharBuffer("default"), DEFAULT);        _tokenMap.put(new CharBuffer("start"), START);    _tokenMap.put(new CharBuffer("div"), DIV);        _tokenMap.put(new CharBuffer("element"), ELEMENT);    _tokenMap.put(new CharBuffer("attribute"), ATTRIBUTE);        _tokenMap.put(new CharBuffer("text"), TEXT);    _tokenMap.put(new CharBuffer("string"), STRING);    _tokenMap.put(new CharBuffer("token"), TOKEN);        _tokenMap.put(new CharBuffer("empty"), EMPTY);        _tokenMap.put(new CharBuffer("include"), INCLUDE);    NAME_CHAR = new boolean[256];    for (int i = 0; i < NAME_CHAR.length; i++) {      if (XmlChar.isNameChar((char) i))	NAME_CHAR[i] = true;    }  }}

⌨️ 快捷键说明

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