⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmlparser.java

📁 该源码是一个J2ME项目
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                 (Xml.END_TAG, current, current.namespace, current.name);

    current = current.parent;

    return result;
  }

  /** precondition: <? consumed */

  ParseEvent parsePI() throws IOException
  {
    StringBuffer buf = new StringBuffer();
    readTo('?', buf);
    readChar(); // ?

    while (peekChar() != '>')
    {
      buf.append('?');

      int r = readChar();
      if (r == -1)
      {
        throw new DefaultParserException
              (UNEXPECTED_EOF, null);
      }

      buf.append( (char) r);
      readTo('?', buf);
      readChar();
    }

    readChar(); // consume >

    return new ParseEvent
          (Xml.PROCESSING_INSTRUCTION, buf.toString());
  }

  StartTag parseStartTag() throws IOException
  {
    //current = new StartTag (current, reader, relaxed);

    //prefixMap = parent == null ? new PrefixMap () : parent.prefixMap;

    String qname = readName();

    //System.out.println ("name: ("+name+")");

    Vector attributes = null;
    immediateClose = false;

    while (true)
    {
      skipWhitespace();

      int c = peekChar();

      if (c == '/')
      {
        immediateClose = true;
        readChar();
        skipWhitespace();
        if (readChar() != '>')
        {
          throw new DefaultParserException
                ("illegal element termination", null);
        }
        break;
      }

      if (c == '>')
      {
        readChar();
        break;
      }

      if (c == -1)
      {
        throw new DefaultParserException(UNEXPECTED_EOF, null);
      }

      String attrName = readName();

      if (attrName.length() == 0)
      {
        throw new DefaultParserException
              ("illegal char / attr", null);
      }

      skipWhitespace();

      if (readChar() != '=')
      {
        throw new DefaultParserException
              ("Attribute name " + attrName
               + "must be followed by '='!", null);
      }

      skipWhitespace();
      int delimiter = readChar();

      if (delimiter != '\'' && delimiter != '"')
      {
        if (!relaxed)
        {
          throw new DefaultParserException
                ("<" + qname + ">: invalid delimiter: "
                 + (char) delimiter, null);
        }

        delimiter = ' ';
      }

      StringBuffer buf = new StringBuffer();
      readText(buf, (char) delimiter);
      if (attributes == null)
      {
        attributes = new Vector();
      }
      attributes.addElement
            (new Attribute(null, attrName, buf.toString()));

      if (delimiter != ' ')
      {
        readChar(); // skip endquote
      }
    }

    try
    {
      current = new StartTag
                (current, Xml.NO_NAMESPACE, qname, attributes,
                 immediateClose, processNamespaces);
    }
    catch (Exception e)
    {
      throw new DefaultParserException(e.toString(), e);
    }

    //System.out.println ("tag: ("+next+")");

    if (!immediateClose)
    {
      qNames.addElement(qname);

    }
    return current;
  }

  static int wlmc1 = 0, wlmc2 = 0, wlmc3 = 0;

  int readText(StringBuffer buf, char delimiter) throws IOException
  {

    int type = Xml.WHITESPACE;
    int nextChar;
    int wlmChar1, wlmChar2;

    while (true)
    {
      nextChar = peekChar();

      if (nextChar == -1 ||
          nextChar == delimiter ||
          (delimiter == ' ' && (nextChar == '>' || nextChar < ' '))
          )
      {
        break;
      }
      //readChar ();
      wlmChar1 = readChar(); //skip '&'

      if (nextChar == '&')
      {
        String code = readTo(';', new StringBuffer()).toString();
        //readChar ();
        wlmChar2 = readChar(); //skip ';'
        if (code.charAt(0) == '#')
        {
          nextChar = (code.charAt(1) == 'x'
                      ? Integer.parseInt(code.substring(2), 16)
                      : Integer.parseInt(code.substring(1)));

          if (nextChar > ' ')
          {
            type = Xml.TEXT;
            // this is the original line, modify by wlm 2003-09-01
          }
          buf.append( (char) nextChar);
          //System.err.println( "unicode:"+nextChar );
          //the 4 lines add by wlm 2003-09-01
          //buf.append((char) wlmChar1);
          //buf.append(code);
          //buf.append((char) wlmChar2);
          //buf.append((char) ';');
        }
        else
        {
          if (code.equals("lt"))
          {
            buf.append('<');
          }
          else if (code.equals("gt"))
          {
            buf.append('>');
          }
          else if (code.equals("apos"))
          {
            buf.append('\'');
          }
          else if (code.equals("quot"))
          {
            buf.append('"');
          }
          else if (code.equals("amp"))
          {
            buf.append('&');
          }
          else
          {
            buf.append(resolveCharacterEntity(code));
          }
          type = Xml.TEXT;
        }
      }
      else
      {
        if (nextChar > ' ')
        {
          type = Xml.TEXT;
          //This is the original line, modify by wlm 2003-09-29
        }
        buf.append( (char) nextChar);

        //the if-else statement add by wlm 2003-09-29
        /*if( nextChar>0x80)		
            {
         nextChar=0x5f69;
         buf.append ((char) nextChar);
         System.err.println( "hz:"+nextChar );
            }
            else
            {
         if( nextChar=='?' )
         {
          System.err.println( "ascii:"+nextChar );
          if( wlmc1 == 0 )
          {
           wlmc1=nextChar;
          }
          else if( wlmc2 == 0 )
          {
           wlmc2 = nextChar;
          }
          else if( wlmc3 == 0 )
          {
           wlmc3 = nextChar;
           wlmc3 = wlmc1<<16+wlmc2<<8+wlmc3;
           buf.append ((char) wlmc3 );
           wlmc1=0;  wlmc2=0;  wlmc3=0;
          }
         }
         else
         {
          buf.append ((char) nextChar);
         }
            }//end if( nextChar>0x80)
         */
      }
    }

    return type;
  }

  /** precondition: &lt; consumed */

  ParseEvent parseSpecial() throws IOException
  {

    switch (peekChar())
    {
      case -1:
        throw new DefaultParserException
              (UNEXPECTED_EOF, null);

      case '!':
        readChar();
        switch (peekChar())
        {
          case '-':
            readChar();
            return parseComment();

          case '[':
            readChar();
            return parseCData();

          default:
            return parseDoctype();
        }

      case '?':
        readChar();
        return parsePI();

      case '/':
        readChar();
        return parseEndTag();

      default:
        return parseStartTag();
    }
  }

  public ParseEvent read() throws IOException
  {
    if (next == null)
    {
      peek();

    }
    ParseEvent result = next;
    next = null;
    return result;
  }

  public ParseEvent peek() throws IOException
  {

    if (next == null)
    {

      if (immediateClose)
      {
        next = new Tag
               (Xml.END_TAG, current, current.namespace, current.name);
        current = current.getParent();
        immediateClose = false;
      }
      else
      {
        switch (peekChar())
        {

          case '<':
            readChar();
            next = parseSpecial();
            break;

          case -1:
            if (current != null && !relaxed)
            {
              throw new DefaultParserException
                    ("End tag missing for: " + current, null);
            }
            next = new ParseEvent(Xml.END_DOCUMENT, null);
            break;

          default:
          {
            StringBuffer buf = new StringBuffer();
            int type = readText(buf, '<');
            next = new ParseEvent(type, buf.toString());

          }
        }
      }
    }
    return next;
  }

  /** default is false. Setting relaxed true
       allows CHTML parsing */

  public void setRelaxed(boolean relaxed)
  {
    this.relaxed = relaxed;
  }

  public int getLineNumber()
  {
    return line;
  }

  public int getColumnNumber()
  {
    return column;
  }

}

⌨️ 快捷键说明

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