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

📄 stdxmlreader.java

📁 Nano的XML解析器
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* StdXMLReader.java                                               NanoXML/Java * * $Revision: 1.4 $ * $Date: 2002/01/04 21:03:28 $ * $Name: RELEASE_2_2_1 $ * * This file is part of NanoXML 2 for Java. * Copyright (C) 2000-2002 Marc De Scheemaecker, All Rights Reserved. * * This software is provided 'as-is', without any express or implied warranty. * In no event will the authors be held liable for any damages arising from the * use of this software. * * Permission is granted to anyone to use this software for any purpose, * including commercial applications, and to alter it and redistribute it * freely, subject to the following restrictions: * *  1. The origin of this software must not be misrepresented; you must not *     claim that you wrote the original software. If you use this software in *     a product, an acknowledgment in the product documentation would be *     appreciated but is not required. * *  2. Altered source versions must be plainly marked as such, and must not be *     misrepresented as being the original software. * *  3. This notice may not be removed or altered from any source distribution. */package net.n3.nanoxml;import java.io.InputStream;import java.io.InputStreamReader;import java.io.IOException;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.LineNumberReader;import java.io.PushbackReader;import java.io.PushbackInputStream;import java.io.Reader;import java.io.StringReader;import java.io.UnsupportedEncodingException;import java.net.MalformedURLException;import java.net.URL;import java.util.Stack;/** * StdXMLReader reads the data to be parsed. * * @author Marc De Scheemaecker * @version $Name: RELEASE_2_2_1 $, $Revision: 1.4 $ */public class StdXMLReader   implements IXMLReader{   /**    * A stacked reader.    *    * @author Marc De Scheemaecker    * @version $Name: RELEASE_2_2_1 $, $Revision: 1.4 $    */   private class StackedReader   {         PushbackReader pbReader;         LineNumberReader lineReader;         URL systemId;         String publicId;      }   /**    * The stack of readers.    */   private Stack readers;   /**    * The current push-back reader.    */   private StackedReader currentReader;   /**    * Creates a new reader using a string as input.    *    * @param str the string containing the XML data    */   public static IXMLReader stringReader(String str)   {      return new StdXMLReader(new StringReader(str));   }   /**    * Creates a new reader using a file as input.    *    * @param filename the name of the file containing the XML data    *    * @throws java.io.FileNotFoundException    *     if the file could not be found    * @throws java.io.IOException    *     if an I/O error occurred    */   public static IXMLReader fileReader(String filename)      throws FileNotFoundException,             IOException   {      StdXMLReader r = new StdXMLReader(new FileInputStream(filename));      r.setSystemID(filename);      for (int i = 0; i < r.readers.size(); i++) {         StackedReader sr = (StackedReader) r.readers.elementAt(i);         sr.systemId = r.currentReader.systemId;      }      return r;   }   /**    * Initializes the reader from a system and public ID.    *    * @param publicID the public ID which may be null.    * @param systemID the non-null system ID.    *    * @throws MalformedURLException    *     if the system ID does not contain a valid URL    * @throws FileNotFoundException    *     if the system ID refers to a local file which does not exist    * @throws IOException    *     if an error occurred opening the stream    */   public StdXMLReader(String publicID,                       String systemID)      throws MalformedURLException,             FileNotFoundException,             IOException   {      URL systemIDasURL = null;      try {         systemIDasURL = new URL(systemID);      } catch (MalformedURLException e) {         systemID = "file:" + systemID;         try {            systemIDasURL = new URL(systemID);         } catch (MalformedURLException e2) {            throw e;         }      }      this.currentReader = new StackedReader();      this.readers = new Stack();      Reader reader = this.openStream(publicID, systemIDasURL.toString());      this.currentReader.lineReader = new LineNumberReader(reader);      this.currentReader.pbReader         = new PushbackReader(this.currentReader.lineReader, 2);   }   /**    * Initializes the XML reader.    *    * @param reader the input for the XML data.    */   public StdXMLReader(Reader reader)   {      this.currentReader = new StackedReader();      this.readers = new Stack();      this.currentReader.lineReader = new LineNumberReader(reader);      this.currentReader.pbReader         = new PushbackReader(this.currentReader.lineReader, 2);      this.currentReader.publicId = "";      try {         this.currentReader.systemId = new URL("file:.");      } catch (MalformedURLException e) {         // never happens      }   }   /**    * Cleans up the object when it's destroyed.    */   protected void finalize()      throws Throwable   {      this.currentReader.lineReader = null;      this.currentReader.pbReader = null;      this.currentReader.systemId = null;      this.currentReader.publicId = null;      this.currentReader = null;      this.readers.clear();      super.finalize();   }   /**    * Scans the encoding from an &lt;?xml...?&gt; tag.    *    * @param str the first tag in the XML data.    *    * @return the encoding, or null if no encoding has been specified.    */   protected String getEncoding(String str)   {      if (! str.startsWith("<?xml")) {         return null;      }      int index = 5;      while (index < str.length()) {         StringBuffer key = new StringBuffer();         while ((index < str.length()) && (str.charAt(index) <= ' ')) {            index++;         }         while ((index < str.length())                && (str.charAt(index) >= 'a')                && (str.charAt(index) <= 'z')) {            key.append(str.charAt(index));            index++;         }         while ((index < str.length()) && (str.charAt(index) <= ' ')) {            index++;         }         if ((index >= str.length()) || (str.charAt(index) != '=')) {            break;         }         while ((index < str.length()) && (str.charAt(index) != '\'')                && (str.charAt(index) != '"')) {            index++;         }         if (index >= str.length()) {            break;         }         char delimiter = str.charAt(index);         index++;         int index2 = str.indexOf(delimiter, index);         if (index2 < 0) {            break;         }         if (key.toString().equals("encoding")) {            return str.substring(index, index2);         }         index = index2 + 1;      }      return null;   }   /**    * Converts a stream to a reader while detecting the encoding.    *    * @param stream    the input for the XML data.    * @param charsRead buffer where to put characters that have been read    *    * @throws java.io.IOException    *     if an I/O error occurred    */   protected Reader stream2reader(InputStream  stream,                                  StringBuffer charsRead)      throws IOException   {      PushbackInputStream pbstream = new PushbackInputStream(stream);      int b = pbstream.read();      switch (b) {         case 0x00:         case 0xFE:         case 0xFF:            pbstream.unread(b);            return new InputStreamReader(pbstream, "UTF-16");         case 0xEF:            for (int i = 0; i < 2; i++) {               pbstream.read();            }            return new InputStreamReader(pbstream, "UTF-8");         case 0x3C:            b = pbstream.read();            charsRead.append('<');            while ((b > 0) && (b != 0x3E)) {               charsRead.append((char) b);               b = pbstream.read();            }            if (b > 0) {               charsRead.append((char) b);            }

⌨️ 快捷键说明

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