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

📄 minml2.java

📁 用J2ME开发的XHTML手机浏览器(for Nokia S60)
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
// Copyright (c) 2001 The Wilson Partnership.// All Rights Reserved.// @(#)MinML2.java, 0.3, 25 November 2000, 2001// Author: John Wilson - tug@wilson.co.ukpackage uk.co.wilson.xml;/*Copyright (c) 2000, 2001 John Wilson (tug@wilson.co.uk).All rights reserved.Redistribution and use in source and binary forms,with or without modification, are permitted providedthat the following conditions are met:Redistributions of source code must retain the abovecopyright notice, this list of conditions and thefollowing disclaimer.Redistributions in binary form must reproduce theabove copyright notice, this list of conditions andthe following disclaimer in the documentation and/orother materials provided with the distribution.All advertising materials mentioning features or useof this software must display the following acknowledgement:This product includes software developed by John Wilson.The name of John Wilson may not be used to endorse or promoteproducts derived from this software without specific priorwritten permission.THIS SOFTWARE IS PROVIDED BY JOHN WILSON ``AS IS'' AND ANYEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR APARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JOHN WILSONBE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITEDTO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED ANDON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICTLIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISINGIN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISEDOF THE POSSIBILITY OF SUCH DAMAGE*/import org.xml.sax.Attributes;import org.xml.sax.InputSource;import org.xml.sax.Locator;import org.xml.sax.ErrorHandler;import org.xml.sax.DTDHandler;import org.xml.sax.EntityResolver;import org.xml.sax.SAXException;import org.xml.sax.SAXParseException;import org.xml.sax.SAXNotSupportedException;import org.xml.sax.SAXNotRecognizedException;import uk.org.xml.sax.ContentHandler;import uk.org.xml.sax.XMLReader;import java.io.IOException;import java.io.InputStream;import java.io.Reader;import java.io.Writer;import java.io.InputStreamReader;import java.util.Hashtable;import java.util.Stack;import java.util.EmptyStackException;import java.util.Vector;import javax.microedition.io.Connector;import javax.microedition.io.HttpConnection;public class MinML2 implements XMLReader, ContentHandler, Locator, ErrorHandler {  public static final int endStartName = 0;  public static final int emitStartElement = 1;  public static final int emitEndElement = 2;  public static final int possiblyEmitCharacters = 3;  public static final int emitCharacters = 4;  public static final int emitCharactersSave = 5;  public static final int saveAttributeName = 6;  public static final int saveAttributeValue = 7;  public static final int startComment = 8;  public static final int endComment = 9;  public static final int incLevel = 10;  public static final int decLevel = 11;  public static final int startCDATA = 12;  public static final int endCDATA = 13;  public static final int processCharRef = 14;  public static final int writeCdata = 15;  public static final int exitParser = 16;  public static final int parseError = 17;  public static final int discardAndChange = 18;  public static final int discardSaveAndChange = 19;  public static final int saveAndChange = 20;  public static final int change = 21;  public static final int inSkipping = 0;  public static final int inSTag = 1;  public static final int inPossiblyAttribute = 2;  public static final int inNextAttribute = 3;  public static final int inAttribute = 4;  public static final int inAttribute1 = 5;  public static final int inAttributeValue = 6;  public static final int inAttributeQuoteValue = 7;  public static final int inAttributeQuotesValue = 8;  public static final int inETag = 9;  public static final int inETag1 = 10;  public static final int inMTTag = 11;  public static final int inTag = 12;  public static final int inTag1 = 13;  public static final int inPI = 14;  public static final int inPI1 = 15;  public static final int inPossiblySkipping = 16;  public static final int inCharData = 17;  public static final int inCDATA = 18;  public static final int inCDATA1 = 19;  public static final int inComment =20;  public static final int inDTD = 21;  public static final int defaultInitialBufferSize = 256;  public static final int defaultBufferIncrement = 256;  public MinML2(final int initialBufferSize, final int bufferIncrement) {    this.initialBufferSize = initialBufferSize;    this.bufferIncrement = bufferIncrement;  }  public MinML2() {    this(defaultInitialBufferSize, defaultBufferIncrement);  }  public void parse(final Reader in) throws SAXException, IOException {  final Vector attributeURIs = new Vector();  final Vector attributeLocalNames = new Vector();  final Vector attributeQNames = new Vector();  final Vector attributeValues = new Vector();  final Attributes attrs = new Attributes() {                                  public int getLength() {                                    return attributeValues.size();                                  }                                  public String getURI(final int i) {                                    return (String)attributeURIs.elementAt(i);                                  }                                  public String getLocalName(final int i) {                                    return (String)attributeLocalNames.elementAt(i);                                  }                                  public String getQName(final int i) {                                    return (String)attributeQNames.elementAt(i);                                  }                                  public String getType(final int i) {                                    return "CDATA";                                  }                                  public String getValue(final int i) {                                    return (String)attributeValues.elementAt(i);                                  }                                  public int getIndex(final String uri, final String localPart) {                                  int i = -1;                                    while (true) {                                      i = attributeLocalNames.indexOf(localPart, i + 1);                                      if (i == -1 || uri.equals(attributeURIs.elementAt(i))) return i;                                    }                                  }                                  public int getIndex(final String qName) {                                    return attributeQNames.indexOf(qName);                                  }                                  public String getType(final String uri, final String localName) {                                    return "CDATA";                                  }                                  public String getType(final String qName) {                                    return "CDATA";                                  }                                  public String getValue(final String uri, final String localName) {                                  final int index = this.getIndex(uri, localName);                                    return (index == -1) ? null : (String)attributeValues.elementAt(index);                                  }                                  public String getValue(final String qName) {                                  final int index = attributeQNames.indexOf(qName);                                    return (index == -1) ? null : (String)attributeValues.elementAt(index);                                  }                      };    this.namespaces.put("xml", "http://www.w3.org/XML/1998/namespace");    final MinMLBuffer buffer = new MinMLBuffer(in);    boolean firstElement = true;    int currentChar = 0, charCount = 0;    int level = 0;    int mixedContentLevel = -1;    String elementName = null;    String state = operands[inSkipping];    String attributeName = "";    this.lineNumber = 1;    this.columnNumber = 0;    try {      while(true) {        charCount++;        //        // this is to try and make the loop a bit faster        // currentChar = buffer.read(); is simpler but is a bit slower.        //        currentChar = (buffer.nextIn == buffer.lastIn) ? buffer.read() : buffer.chars[buffer.nextIn++];        final int transition;        if (currentChar > ']') {          transition = state.charAt(14);        } else {        final int charClass = charClasses[currentChar + 1];          if (charClass == -1) fatalError("Document contains illegal control character with value " + currentChar, this.lineNumber, this.columnNumber);          if (charClass == 12) {            if (currentChar == '\r') {              currentChar = '\n';              charCount = -1;            }            if (currentChar == '\n') {              if (charCount == 0) continue;  // preceeded by '\r' so ignore              if (charCount != -1) charCount = 0;              this.lineNumber++;              this.columnNumber = 0;            }          }          transition = state.charAt(charClass);       }        this.columnNumber++;        final String operand = operands[transition >>> 8];        switch (transition & 0XFF) {          case endStartName:          // end of start element name            elementName = buffer.getString();            this.stack.push(null);            if (currentChar != '>' && currentChar != '/') break;  // change state to operand            // drop through to emit start element (we have no attributes)          case emitStartElement: {          // emit start element            for (int i = 0; i != attributeURIs.size(); i++) {            final Object attributeNamespaceName = attributeURIs.elementAt(i);            final Object uri;              if (attributeNamespaceName != null) {                uri = this.namespaces.get(attributeNamespaceName);                if (uri == null) throw new SAXException("Namespace \"" + attributeNamespaceName + "\" is not defined");              } else {                uri = "";              }              attributeURIs.setElementAt(uri, i);            }            final String elementLocalName;            final String uri;            final int colonIndex = elementName.indexOf(':');            if (colonIndex != -1) {            final String namespaceName = elementName.substring(0, colonIndex);              uri = (String)this.namespaces.get(namespaceName);              if (uri == null) throw new SAXException("Namespace \"" + namespaceName + "\" is not defined");              elementLocalName = elementName.substring(colonIndex + 1);            } else {              elementLocalName = elementName;              uri = this.defaultNamespace;            }            this.stack.push(elementLocalName);            this.stack.push(uri);            this.stack.push(elementName);            buffer.pushWriter(this.extContentHandler.startElement(uri,                                                                  elementLocalName,                                                                  elementName,                                                                  attrs,                                                                  (firstElement) ?                                                                    this.extContentHandler.startDocument(buffer)                                                                  :                                                                    buffer.getWriter()));            firstElement = false;            attributeURIs.removeAllElements();            attributeLocalNames.removeAllElements();            attributeQNames.removeAllElements();            attributeValues.removeAllElements();            if (mixedContentLevel != -1) mixedContentLevel++;            if (currentChar != '/') break;  // change state to operand          }            // <element/> drop through          case emitEndElement:          // emit end element            try {              buffer.popWriter();              elementName = buffer.getString();              final String begin = (String)this.stack.pop();              if (currentChar != '/' && !elementName.equals(begin)) {

⌨️ 快捷键说明

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