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

📄 xmlelement.java

📁 java 文件下载器。可自定义
💻 JAVA
字号:
// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   XMLElement.java

package org.gudy.azureus2.core3.xml.util;

import java.io.PrintWriter;
import java.util.*;

public class XMLElement
{
	private static class ContentComparator
		implements Comparator
	{

		public int compare(Object o1, Object o2)
		{
			if (o1 instanceof XMLElement)
				if (o2 instanceof XMLElement)
				{
					XMLElement xe1 = (XMLElement)o1;
					XMLElement xe2 = (XMLElement)o2;
					int result = String.CASE_INSENSITIVE_ORDER.compare(xe1.getTag(), xe2.getTag());
					if (result == 0)
					{
						int xe1_index = 0;
						int xe2_index = 0;
						try
						{
							xe1_index = Integer.parseInt(xe1.getAttribute("index"));
							xe2_index = Integer.parseInt(xe2.getAttribute("index"));
						}
						catch (NullPointerException ne)
						{
							xe1_index = xe2_index = 0;
						}
						catch (NumberFormatException ne)
						{
							xe1_index = xe2_index = 0;
						}
						if (xe1_index != xe2_index)
							return xe1_index - xe2_index;
						else
							throw new RuntimeException((new StringBuilder()).append("Shouldn't be using sorting for contents if you have tags with same name and no index attribute! (e.g. ").append(o1).append(")").toString());
					} else
					{
						return result;
					}
				} else
				{
					return -1;
				}
			if (o2 instanceof XMLElement)
				return 1;
			int result = String.CASE_INSENSITIVE_ORDER.compare((String)o1, (String)o2);
			if (result == 0)
				return -1;
			else
				return result;
		}

		private ContentComparator()
		{
		}

	}


	protected Object single_content;
	protected Map attributes;
	protected Collection contents;
	protected String tag_name;
	protected boolean auto_order;
	private static Comparator ATTRIBUTE_COMPARATOR;
	private static Comparator CONTENT_COMPARATOR = new ContentComparator();

	public XMLElement(String tag_name)
	{
		this(tag_name, false);
	}

	public XMLElement(String tag_name, boolean auto_order)
	{
		single_content = null;
		attributes = null;
		contents = null;
		this.tag_name = tag_name;
		this.auto_order = auto_order;
	}

	public String getTag()
	{
		return tag_name;
	}

	public String getAttribute(String key)
	{
		if (attributes == null)
			return null;
		else
			return (String)attributes.get(key);
	}

	public void addAttribute(String key, String value)
	{
		if (attributes == null)
			attributes = new TreeMap(ATTRIBUTE_COMPARATOR);
		attributes.put(key, value);
	}

	public void addAttribute(String key, int value)
	{
		addAttribute(key, String.valueOf(value));
	}

	public void addAttribute(String key, boolean value)
	{
		addAttribute(key, value ? "yes" : "no");
	}

	public void addContent(String s)
	{
		addContent(s);
	}

	public void addContent(XMLElement e)
	{
		addContent(e);
	}

	protected void addContent(Object o)
	{
		if (o == null)
			throw new NullPointerException();
		if (contents == null && single_content != null)
		{
			if (!auto_order)
				contents = new ArrayList();
			else
				contents = new TreeSet(CONTENT_COMPARATOR);
			contents.add(single_content);
			single_content = null;
		}
		if (contents == null)
			single_content = o;
		else
			contents.add(o);
	}

	public void printTo(PrintWriter pw)
	{
		printTo(pw, 0, false);
	}

	public void printTo(PrintWriter pw, boolean spaced_out)
	{
		printTo(pw, 0, spaced_out);
	}

	public void printTo(PrintWriter pw, int indent)
	{
		printTo(pw, indent, false);
	}

	public void printTo(PrintWriter pw, int indent, boolean spaced_out)
	{
		for (int i = 0; i < indent; i++)
			pw.print(" ");

		if (attributes == null && contents == null && single_content == null)
		{
			if (!spaced_out)
			{
				pw.print("<");
				pw.print(tag_name);
				pw.print(" />");
			}
			return;
		}
		pw.print("<");
		pw.print(tag_name);
		Iterator itr = null;
		if (attributes != null)
			for (itr = attributes.entrySet().iterator(); itr.hasNext(); pw.print("\""))
			{
				java.util.Map.Entry entry = (java.util.Map.Entry)itr.next();
				pw.print(" ");
				pw.print(entry.getKey());
				pw.print("=\"");
				pw.print(quote((String)entry.getValue()));
			}

		boolean needs_indented_close = contents != null || (single_content instanceof XMLElement);
		boolean needs_close_tag = needs_indented_close || single_content != null;
		needs_indented_close = needs_indented_close || spaced_out;
		needs_close_tag = needs_close_tag || spaced_out;
		if (needs_indented_close)
			pw.println(">");
		else
		if (needs_close_tag)
			pw.print(">");
		else
			pw.print(" />");
		itr = null;
		if (contents != null)
			itr = contents.iterator();
		else
		if (single_content != null)
			itr = Collections.singletonList(single_content).iterator();
		else
			itr = Collections.singletonList("").iterator();
		Object content_element = null;
		if (itr != null)
			while (itr.hasNext()) 
			{
				content_element = itr.next();
				if (content_element instanceof XMLElement)
					((XMLElement)content_element).printTo(pw, indent + 2, spaced_out);
				else
				if (spaced_out)
				{
					for (int i = 0; i < indent + 2; i++)
						pw.print(" ");

					pw.print(quote((String)content_element));
					pw.println();
				} else
				{
					pw.print(quote((String)content_element));
				}
			}
		if (needs_indented_close)
		{
			for (int i = 0; i < indent; i++)
				pw.print(" ");

		}
		if (needs_close_tag)
		{
			pw.print("</");
			pw.print(tag_name);
			pw.println(">");
		}
	}

	private String quote(String text)
	{
		text = text.replaceAll("&", "&amp;");
		text = text.replaceAll(">", "&gt;");
		text = text.replaceAll("<", "&lt;");
		text = text.replaceAll("\"", "&quot;");
		text = text.replaceAll("--", "&#45;&#45;");
		return text;
	}

	public XMLElement makeContent(String tag_name)
	{
		return makeContent(tag_name, false);
	}

	public XMLElement makeContent(String tag_name, boolean auto_order)
	{
		XMLElement content = new XMLElement(tag_name, auto_order);
		addContent(content);
		return content;
	}

	public void clear()
	{
		single_content = null;
		attributes = null;
		contents = null;
	}

	public void setAutoOrdering(boolean mode)
	{
		if (mode == auto_order)
			return;
		auto_order = mode;
		if (contents == null)
			return;
		Collection previous_contents = contents;
		if (auto_order)
		{
			contents = new TreeSet(CONTENT_COMPARATOR);
			contents.addAll(previous_contents);
		} else
		{
			contents = new ArrayList(previous_contents);
		}
	}

	public String toString()
	{
		return (new StringBuilder()).append("XMLElement[").append(tag_name).append("]@").append(Integer.toHexString(System.identityHashCode(this))).toString();
	}

	static 
	{
		ATTRIBUTE_COMPARATOR = String.CASE_INSENSITIVE_ORDER;
	}
}

⌨️ 快捷键说明

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