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

📄 xmlelement.java

📁 Skin Look And Feel 1.2.10, 给你的java程序换肤, 支持大量通用皮肤文件.
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
/* ====================================================================
 *
 * Skin Look And Feel 1.2.10 License.
 *
 * Copyright (c) 2000-2004 L2FProd.com.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. 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.
 *
 * 3. The end-user documentation included with the redistribution, if
 *    any, must include the following acknowlegement:
 *       "This product includes software developed by L2FProd.com
 *        (http://www.L2FProd.com/)."
 *    Alternately, this acknowlegement may appear in the software itself,
 *    if and wherever such third-party acknowlegements normally appear.
 *
 * 4. The names "Skin Look And Feel", "SkinLF" and "L2FProd.com" must not
 *    be used to endorse or promote products derived from this software
 *    without prior written permission. For written permission, please
 *    contact info@L2FProd.com.
 *
 * 5. Products derived from this software may not be called "SkinLF"
 *    nor may "SkinLF" appear in their names without prior written
 *    permission of L2FProd.com.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED 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 L2FPROD.COM OR ITS CONTRIBUTORS 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.
 * ====================================================================
 */
package com.l2fprod.contrib.nanoxml;

import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.io.PrintWriter;
import java.io.Serializable;
import java.io.StringWriter;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Properties;
import java.util.Vector;

/**
 * XMLElement is a representation of an XML object. The object is able to parse
 * XML code. <P>
 *
 * Note that NanoXML is not 100% XML 1.0 compliant:
 * <UL>
 *   <LI> The parser is non-validating.
 *   <LI> The DTD is fully ignored, including <CODE>&lt;!ENTITY...&gt;</CODE>.
 *
 *   <LI> There is no support for mixed content (elements containing both
 *   subelements and CDATA elements)
 * </UL>
 * <P>
 *
 * You can opt to use a SAX compatible API, by including both <CODE>nanoxml.jar</CODE>
 * and <CODE>nanoxml-sax.jar</CODE> in your classpath and setting the property
 * <CODE>org.xml.sax.parser</CODE> to <CODE>nanoxml.sax.SAXParser</CODE> <P>
 *
 * $Revision: 1.2 $<BR>
 * $Date: 2003/12/06 21:46:16 $<P>
 *
 *
 *
 * @author    Marc De Scheemaecker &lt;<A
 *      HREF="mailto:Marc.DeScheemaecker@advalvas.be" >
 *      Marc.DeScheemaecker@advalvas.be</A> &gt;
 * @created   27 avril 2002
 * @see       nanoxml.XMLParseException
 * @version   1.6
 */
public class XMLElement
     implements Serializable {

  /**
   * The attributes given to the object.
   */
  private Properties attributes;

  /**
   * Subobjects of the object. The subobjects are of class XMLElement
   * themselves.
   */
  private Vector children;

  /**
   * The class of the object (the name indicated in the tag).
   */
  private String tagName;

  /**
   * The #PCDATA content of the object. If there is no such content, this field
   * is null.
   */
  private String contents;

  /**
   * Conversion table for &amp;...; tags.
   */
  private Properties conversionTable;

  /**
   * Whether to skip leading whitespace in CDATA.
   */
  private boolean skipLeadingWhitespace;

  /**
   * The line number where the element starts.
   */
  private int lineNr;

  /**
   * Whether the parsing is case sensitive.
   */
  private boolean ignoreCase;

  /**
   * Major version of NanoXML.
   */
  public final static int NANOXML_MAJOR_VERSION = 1;

  /**
   * Minor version of NanoXML.
   */
  public final static int NANOXML_MINOR_VERSION = 6;

  /**
   * Serialization serial version ID.
   */
  final static long serialVersionUID = 6685035139346394777L;


  /**
   * Creates a new XML element. The following settings are used:
   * <DL>
   *   <DT> Conversion table</DT>
   *   <DD> Minimal XML conversions: <CODE>&amp;amp; &amp;lt; &amp;gt;
   *         &amp;apos; &amp;quot;</CODE></DD>
   *   <DT> Skip whitespace in contents</DT>
   *   <DD> <CODE>false</CODE></DD>
   *   <DT> Ignore Case</DT>
   *   <DD> <CODE>true</CODE></DD>
   * </DL>
   *
   *
   * @see   nanoxml.XMLElement#XMLElement(java.util.Properties)
   * @see   nanoxml.XMLElement#XMLElement(boolean)
   * @see   nanoxml.XMLElement#XMLElement(java.util.Properties,boolean)
   */
  public XMLElement() {
    this(new Properties(), false, true, true);
  }


  /**
   * Creates a new XML element. The following settings are used:
   * <DL>
   *   <DT> Conversion table</DT>
   *   <DD> <I>conversionTable</I> combined with the minimal XML conversions:
   *   <CODE>&amp;amp; &amp;lt; &amp;gt;
   *         &amp;apos; &amp;quot;</CODE></DD>
   *   <DT> Skip whitespace in contents</DT>
   *   <DD> <CODE>false</CODE></DD>
   *   <DT> Ignore Case</DT>
   *   <DD> <CODE>true</CODE></DD>
   * </DL>
   *
   *
   * @param conversionTable  Description of Parameter
   * @see                    nanoxml.XMLElement#XMLElement()
   * @see                    nanoxml.XMLElement#XMLElement(boolean)
   * @see                    nanoxml.XMLElement#XMLElement(java.util.Properties,boolean)
   */
  public XMLElement(Properties conversionTable) {
    this(conversionTable, false, true, true);
  }


  /**
   * Creates a new XML element. The following settings are used:
   * <DL>
   *   <DT> Conversion table</DT>
   *   <DD> Minimal XML conversions: <CODE>&amp;amp; &amp;lt; &amp;gt;
   *         &amp;apos; &amp;quot;</CODE></DD>
   *   <DT> Skip whitespace in contents</DT>
   *   <DD> <I>skipLeadingWhitespace</I> </DD>
   *   <DT> Ignore Case</DT>
   *   <DD> <CODE>true</CODE></DD>
   * </DL>
   *
   *
   * @param skipLeadingWhitespace  Description of Parameter
   * @see                          nanoxml.XMLElement#XMLElement()
   * @see                          nanoxml.XMLElement#XMLElement(java.util.Properties)
   * @see                          nanoxml.XMLElement#XMLElement(java.util.Properties,boolean)
   */
  public XMLElement(boolean skipLeadingWhitespace) {
    this(new Properties(), skipLeadingWhitespace, true, true);
  }


  /**
   * Creates a new XML element. The following settings are used:
   * <DL>
   *   <DT> Conversion table</DT>
   *   <DD> <I>conversionTable</I> combined with the minimal XML conversions:
   *   <CODE>&amp;amp; &amp;lt; &amp;gt;
   *         &amp;apos; &amp;quot;</CODE></DD>
   *   <DT> Skip whitespace in contents</DT>
   *   <DD> <I>skipLeadingWhitespace</I> </DD>
   *   <DT> Ignore Case</DT>
   *   <DD> <CODE>true</CODE></DD>
   * </DL>
   *
   *
   * @param conversionTable        Description of Parameter
   * @param skipLeadingWhitespace  Description of Parameter
   * @see                          nanoxml.XMLElement#XMLElement()
   * @see                          nanoxml.XMLElement#XMLElement(boolean)
   * @see                          nanoxml.XMLElement#XMLElement(java.util.Properties)
   */
  public XMLElement(Properties conversionTable,
      boolean skipLeadingWhitespace) {
    this(conversionTable, skipLeadingWhitespace, true, true);
  }


  /**
   * Creates a new XML element. The following settings are used:
   * <DL>
   *   <DT> Conversion table</DT>
   *   <DD> <I>conversionTable</I> , eventually combined with the minimal XML
   *   conversions: <CODE>&amp;amp; &amp;lt; &amp;gt;
   *         &amp;apos; &amp;quot;</CODE> (depending on <I>
   *   fillBasicConversionTable</I> )</DD>
   *   <DT> Skip whitespace in contents</DT>
   *   <DD> <I>skipLeadingWhitespace</I> </DD>
   *   <DT> Ignore Case</DT>
   *   <DD> <I>ignoreCase</I> </DD>
   * </DL>
   * <P>
   *
   * This constructor should <I>only</I> be called from XMLElement itself to
   * create child elements.
   *
   * @param conversionTable        Description of Parameter
   * @param skipLeadingWhitespace  Description of Parameter
   * @param ignoreCase             Description of Parameter
   * @see                          nanoxml.XMLElement#XMLElement()
   * @see                          nanoxml.XMLElement#XMLElement(boolean)
   * @see                          nanoxml.XMLElement#XMLElement(java.util.Properties)
   * @see                          nanoxml.XMLElement#XMLElement(java.util.Properties,boolean)
   */
  public XMLElement(Properties conversionTable,
      boolean skipLeadingWhitespace,
      boolean ignoreCase) {
    this(conversionTable, skipLeadingWhitespace, true, ignoreCase);
  }


  /**
   * Creates a new XML element. The following settings are used:
   * <DL>
   *   <DT> Conversion table</DT>
   *   <DD> <I>conversionTable</I> , eventually combined with the minimal XML
   *   conversions: <CODE>&amp;amp; &amp;lt; &amp;gt;
   *         &amp;apos; &amp;quot;</CODE> (depending on <I>
   *   fillBasicConversionTable</I> )</DD>
   *   <DT> Skip whitespace in contents</DT>
   *   <DD> <I>skipLeadingWhitespace</I> </DD>
   *   <DT> Ignore Case</DT>
   *   <DD> <I>ignoreCase</I> </DD>
   * </DL>
   * <P>
   *
   * This constructor should <I>only</I> be called from XMLElement itself to
   * create child elements.
   *
   * @param conversionTable           Description of Parameter
   * @param skipLeadingWhitespace     Description of Parameter
   * @param fillBasicConversionTable  Description of Parameter
   * @param ignoreCase                Description of Parameter
   * @see                             nanoxml.XMLElement#XMLElement()
   * @see                             nanoxml.XMLElement#XMLElement(boolean)
   * @see                             nanoxml.XMLElement#XMLElement(java.util.Properties)
   * @see                             nanoxml.XMLElement#XMLElement(java.util.Properties,boolean)
   */
  protected XMLElement(Properties conversionTable,
      boolean skipLeadingWhitespace,
      boolean fillBasicConversionTable,
      boolean ignoreCase) {
    this.ignoreCase = ignoreCase;
    this.skipLeadingWhitespace = skipLeadingWhitespace;
    this.tagName = null;
    this.contents = "";
    this.attributes = new Properties();
    this.children = new Vector();
    this.conversionTable = conversionTable;
    this.lineNr = 0;

    if (fillBasicConversionTable) {
      this.conversionTable.put("lt", "<");
      this.conversionTable.put("gt", ">");
      this.conversionTable.put("quot", "\"");
      this.conversionTable.put("apos", "'");
      this.conversionTable.put("amp", "&");
    }
  }


  /**
   * Changes the content string.
   *
   * @param content  The new content string.
   */
  public void setContent(String content) {
    this.contents = content;
  }


  /**
   * Changes the tag name.
   *
   * @param tagName  The new tag name.
   */
  public void setTagName(String tagName) {
    this.tagName = tagName;
  }


  /**
   * Returns the subobjects of the object.
   *
   * @return   The Children value
   */
  public Vector getChildren() {
    return this.children;
  }


  /**
   * Returns the #PCDATA content of the object. If there is no such content,
   * <CODE>null</CODE> is returned.
   *
   * @return   The Contents value
   */
  public String getContents() {
    return this.contents;
  }


  /**
   * Returns the line nr on which the element is found.
   *
   * @return   The LineNr value
   */
  public int getLineNr() {
    return this.lineNr;
  }


  /**
   * Returns a property by looking up a key in a hashtable. If the property
   * doesn't exist, the value corresponding to defaultValue is returned.
   *
   * @param key           Description of Parameter
   * @param valueSet      Description of Parameter
   * @param defaultValue  Description of Parameter
   * @return              The IntProperty value
   */
  public int getIntProperty(String key,
      Hashtable valueSet,
      String defaultValue) {
    String val = this.attributes.getProperty(key);
    Integer result;

    if (this.ignoreCase) {
      key = key.toUpperCase();
    }

    if (val == null) {
      val = defaultValue;
    }

    try {
      result = (Integer) (valueSet.get(val));
    } catch (ClassCastException e) {
      throw this.invalidValueSet(key);
    }

    if (result == null) {
      throw this.invalidValue(key, val, this.lineNr);
    }

    return result.intValue();
  }


  /**
   * Returns a property of the object. If there is no such property, this method
   * returns <CODE>null</CODE>.
   *
   * @param key  Description of Parameter
   * @return     The Property value
   */
  public String getProperty(String key) {
    if (this.ignoreCase) {
      key = key.toUpperCase();
    }

    return this.attributes.getProperty(key);
  }


  /**
   * Returns a property of the object. If the property doesn't exist, <I>
   * defaultValue</I> is returned.
   *
   * @param key           Description of Parameter
   * @param defaultValue  Description of Parameter
   * @return              The Property value
   */
  public String getProperty(String key,
      String defaultValue) {
    if (this.ignoreCase) {
      key = key.toUpperCase();
    }

    return this.attributes.getProperty(key, defaultValue);
  }


  /**
   * Returns an integer property of the object. If the property doesn't exist,
   * <I>defaultValue</I> is returned.
   *
   * @param key           Description of Parameter
   * @param defaultValue  Description of Parameter
   * @return              The Property value
   */
  public int getProperty(String key,
      int defaultValue) {
    if (this.ignoreCase) {
      key = key.toUpperCase();
    }

    String val = this.attributes.getProperty(key);

    if (val == null) {
      return defaultValue;
    }
    else {
      try {
        return Integer.parseInt(val);
      } catch (NumberFormatException e) {
        throw this.invalidValue(key, val, this.lineNr);
      }
    }
  }


  /**
   * Returns a floating point property of the object. If the property doesn't
   * exist, <I>defaultValue</I> is returned.
   *
   * @param key           Description of Parameter
   * @param defaultValue  Description of Parameter
   * @return              The Property value

⌨️ 快捷键说明

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