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

📄 stdxmlparser.java

📁 Nano的XML解析器
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* StdXMLParser.java                                               NanoXML/Java * * $Revision: 1.5 $ * $Date: 2002/03/24 11:37:00 $ * $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.IOException;import java.io.CharArrayReader;import java.io.Reader;import java.util.Enumeration;import java.util.Properties;import java.util.Vector;/** * StdXMLParser is the core parser of NanoXML. * * @author Marc De Scheemaecker * @version $Name: RELEASE_2_2_1 $, $Revision: 1.5 $ */public class StdXMLParser   implements IXMLParser{   /**    * The builder which creates the logical structure of the XML data.    */   private IXMLBuilder builder;   /**    * The reader from which the parser retrieves its data.    */   private IXMLReader reader;   /**    * The entity resolver.    */   private IXMLEntityResolver entityResolver;   /**    * The validator that will process entity references and validate the XML    * data.    */   private IXMLValidator validator;   /**    * Creates a new parser.    */   public StdXMLParser()   {      this.builder = null;      this.validator = null;      this.reader = null;      this.entityResolver = new XMLEntityResolver();   }   /**    * Cleans up the object when it's destroyed.    */   protected void finalize()      throws Throwable   {      this.builder = null;      this.reader = null;      this.entityResolver = null;      this.validator = null;      super.finalize();   }   /**    * Sets the builder which creates the logical structure of the XML data.    *    * @param builder the non-null builder    */   public void setBuilder(IXMLBuilder builder)   {      this.builder = builder;   }   /**    * Returns the builder which creates the logical structure of the XML data.    *    * @return the builder    */   public IXMLBuilder getBuilder()   {      return this.builder;   }   /**    * Sets the validator that validates the XML data.    *    * @param validator the non-null validator    */   public void setValidator(IXMLValidator validator)   {      this.validator = validator;   }   /**    * Returns the validator that validates the XML data.    *    * @return the validator    */   public IXMLValidator getValidator()   {      return this.validator;   }   /**    * Sets the entity resolver.    *    * @param resolver the non-null resolver    */   public void setResolver(IXMLEntityResolver resolver)   {      this.entityResolver = resolver;   }   /**    * Returns the entity resolver.    *    * @return the non-null resolver    */   public IXMLEntityResolver getResolver()   {      return this.entityResolver;   }   /**    * Sets the reader from which the parser retrieves its data.    *    * @param reader the reader    */   public void setReader(IXMLReader reader)   {      this.reader = reader;   }   /**    * Returns the reader from which the parser retrieves its data.    *    * @return the reader    */   public IXMLReader getReader()   {      return this.reader;   }   /**    * Parses the data and lets the builder create the logical data structure.    *    * @return the logical structure built by the builder    *    * @throws net.n3.nanoxml.XMLException    *		if an error occurred reading or parsing the data    */   public Object parse()      throws XMLException   {      try {         this.builder.startBuilding(this.reader.getSystemID(),                                    this.reader.getLineNr());         this.scanData();         return this.builder.getResult();      } catch (XMLException e) {         throw e;      } catch (Exception e) {         throw new XMLException(e);      }   }   /**    * Scans the XML data for elements.    *    * @throws java.lang.Exception    *     if something went wrong    */   protected void scanData()      throws Exception   {      while ((! this.reader.atEOF()) && (this.builder.getResult() == null)) {         String str = XMLUtil.read(this.reader, '&');         char ch = str.charAt(0);         if (ch == '&') {            XMLUtil.processEntity(str, this.reader, this.entityResolver);            continue;         }         switch (ch) {            case '<':               this.scanSomeTag(false, // don't allow CDATA                                null,  // no default namespace                                new Properties());               break;            case ' ':            case '\t':            case '\r':            case '\n':               // skip whitespace               break;            default:               XMLUtil.errorInvalidInput(reader.getSystemID(),                                         reader.getLineNr(),                                         "`" + ch + "' (0x"                                         + Integer.toHexString((int) ch)                                         + ')');         }      }   }   /**    * Scans an XML tag.    *    * @param allowCDATA true if CDATA sections are allowed at this point    * @param defaultNamespace the default namespace URI (or null)    * @param namespaces list of defined namespaces    *    * @throws java.lang.Exception    *     if something went wrong    */   protected void scanSomeTag(boolean    allowCDATA,                              String     defaultNamespace,                              Properties namespaces)      throws Exception   {      String str = XMLUtil.read(this.reader, '&');      char ch = str.charAt(0);      if (ch == '&') {         XMLUtil.errorUnexpectedEntity(reader.getSystemID(),                                       reader.getLineNr(),                                       str);      }      switch (ch) {         case '?':            this.processPI();            break;         case '!':            this.processSpecialTag(allowCDATA);            break;         default:            this.reader.unread(ch);            this.processElement(defaultNamespace, namespaces);      }   }   /**    * Processes a "processing instruction".    *    * @throws java.lang.Exception    *     if something went wrong    */   protected void processPI()      throws Exception   {      XMLUtil.skipWhitespace(this.reader, null);      String target = XMLUtil.scanIdentifier(this.reader);      XMLUtil.skipWhitespace(this.reader, null);      Reader reader = new PIReader(this.reader);      if (! target.equalsIgnoreCase("xml")) {         this.builder.newProcessingInstruction(target, reader);      }      reader.close();   }   /**    * Processes a tag that starts with a bang (&lt;!...&gt;).    *    * @param allowCDATA true if CDATA sections are allowed at this point    *    * @throws java.lang.Exception    *     if something went wrong    */   protected void processSpecialTag(boolean allowCDATA)      throws Exception   {      String str = XMLUtil.read(this.reader, '&');      char ch = str.charAt(0);      if (ch == '&') {         XMLUtil.errorUnexpectedEntity(reader.getSystemID(),                                       reader.getLineNr(),                                       str);      }      switch (ch) {         case '[':            if (allowCDATA) {               this.processCDATA();            } else {               XMLUtil.errorUnexpectedCDATA(reader.getSystemID(),                                            reader.getLineNr());            }

⌨️ 快捷键说明

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