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

📄 minml.java

📁 实现了一个基于j2me移动gps定位系统
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
// Copyright (c) 2000, 2001 The Wilson Partnership.
// All Rights Reserved.
// @(#)MinML.java, 1.7, 18th November 2001
// Author: John Wilson - tug@wilson.co.uk

/*
 * $Id: MinML.java 140 2007-10-05 07:17:58Z khanhlnq $
 * $URL: https://jvnmobilegis.googlecode.com/svn/trunk/src/uk/co/wilson/xml/MinML.java $
 * $Author: khanhlnq $
 * $Revision: 140 $
 * $Date: 2007-10-05 14:17:58 +0700 (Fri, 05 Oct 2007) $
 *
 * ====================================================================
 *
 * Copyright (C) 2006-2007 by JVNGIS
 *
 * All copyright notices regarding JVNMobileGIS MUST remain
 * intact in the Java codes and resource files.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Support can be obtained from project homepage at:
 * http://code.google.com/p/jvnmobilegis/
 *
 * Correspondence and Marketing Questions can be sent to:
 * khanh.lnq AT gmail.com
 * 
 * @version: 1.0
 * @author: Khanh Le
 * @Date Created: 22 Jun 2007
 */
package 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 provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer. Redistributions in binary
 * form must reproduce the above copyright notice, this list of conditions and
 * the following disclaimer in the documentation and/or other materials provided
 * with the distribution. All advertising materials mentioning features or use
 * of 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 promote products derived from this software without
 * specific prior written permission. THIS SOFTWARE IS PROVIDED BY JOHN WILSON
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JOHN WILSON BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
 */

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.Writer;
import java.util.EmptyStackException;
import java.util.Stack;
import java.util.Vector;

import org.xml.sax.AttributeList;
import org.xml.sax.DTDHandler;
import org.xml.sax.EntityResolver;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

import uk.org.xml.sax.DocumentHandler;
import uk.org.xml.sax.Parser;

/**
 * @author khanhlnq
 */
public class MinML implements Parser, Locator, DocumentHandler, ErrorHandler {

    private static final int endStartName = 0;
    private static final int emitStartElement = 1;
    private static final int emitEndElement = 2;
    private static final int possiblyEmitCharacters = 3;
    private static final int emitCharacters = 4;
    private static final int emitCharactersSave = 5;
    private static final int saveAttributeName = 6;
    private static final int saveAttributeValue = 7;
    private static final int startComment = 8;
    private static final int endComment = 9;
    private static final int incLevel = 10;
    private static final int decLevel = 11;
    private static final int startCDATA = 12;
    private static final int endCDATA = 13;
    private static final int processCharRef = 14;
    private static final int writeCdata = 15;
    private static final int exitParser = 16;
    private static final int parseError = 17;
    private static final int discardAndChange = 18;
    private static final int discardSaveAndChange = 19;
    private static final int saveAndChange = 20;
    private static final int change = 21;

    private 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 MinML(final int initialBufferSize, final int bufferIncrement) {
        this.initialBufferSize = initialBufferSize;
        this.bufferIncrement = bufferIncrement;
    }

    public MinML() {
        this(256, 128);
    }

    private void parse(final Reader in) throws SAXException, IOException {
        final Vector attributeNames = new Vector();
        final Vector attributeValues = new Vector();

        final AttributeList attrs = new AttributeList() {

            public int getLength() {
                return attributeNames.size();
            }

            public String getName(final int i) {
                return (String) attributeNames.elementAt(i);
            }

            public String getType(final int i) {
                return "CDATA";
            }

            public String getValue(final int i) {
                return (String) attributeValues.elementAt(i);
            }

            public String getType(final String name) {
                return "CDATA";
            }

            public String getValue(final String name) {
                final int index = attributeNames.indexOf(name);

                return (index == -1) ? null : (String) attributeValues
                        .elementAt(index);
            }
        };

        final MinMLBuffer buffer = new MinMLBuffer(in);
        int currentChar, charCount = 0;
        int level = 0;
        int mixedContentLevel = -1;
        String elementName = null;
        String state = operands[inSkipping];

        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();
                    if (currentChar != '>' && currentChar != '/')
                        break; // change state to operand
                    // drop through to emit start element (we have no
                    // attributes)

                case emitStartElement:
                    // emit start element

                    final Writer newWriter = this.extDocumentHandler
                            .startElement(elementName, attrs, (this.tags
                                    .empty()) ? this.extDocumentHandler
                                    .startDocument(buffer) : buffer.getWriter());

                    buffer.pushWriter(newWriter);
                    this.tags.push(elementName);

                    attributeValues.removeAllElements();
                    attributeNames.removeAllElements();

                    if (mixedContentLevel != -1)
                        mixedContentLevel++;

                    if (currentChar != '/')
                        break; // change state to operand

                    // <element/> drop through

                case emitEndElement:
                    // emit end element

                    try {
                        final String begin = (String) this.tags.pop();

                        buffer.popWriter();
                        elementName = buffer.getString();

                        if (currentChar != '/' && !elementName.equals(begin)) {
                            fatalError("end tag </" + elementName
                                    + "> does not match begin tag <" + begin
                                    + ">", this.lineNumber, this.columnNumber);
                        } else {
                            this.documentHandler.endElement(begin);

                            if (this.tags.empty()) {
                                this.documentHandler.endDocument();
                                return;

⌨️ 快捷键说明

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