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

📄 totorrentxmldeserialiser.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:   TOTorrentXMLDeserialiser.java

package org.gudy.azureus2.core3.torrent.impl;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;
import org.gudy.azureus2.core3.torrent.*;
import org.gudy.azureus2.core3.util.Debug;
import org.gudy.azureus2.core3.xml.simpleparser.SimpleXMLParserDocumentFactory;
import org.gudy.azureus2.plugins.utils.xml.simpleparser.*;

// Referenced classes of package org.gudy.azureus2.core3.torrent.impl:
//			TOTorrentFileImpl, TOTorrentImpl

public class TOTorrentXMLDeserialiser
{
	protected class mapEntry
	{

		String name;
		Object value;
		final TOTorrentXMLDeserialiser this$0;

		mapEntry(String _name, Object _value)
		{
			this$0 = TOTorrentXMLDeserialiser.this;
			super();
			name = _name;
			value = _value;
		}
	}


	public TOTorrentXMLDeserialiser()
	{
	}

	public TOTorrent deserialise(File file)
		throws TOTorrentException
	{
		TOTorrent res;
		SimpleXMLParserDocument doc = SimpleXMLParserDocumentFactory.create(file);
		res = decodeRoot(doc);
		return res;
		SimpleXMLParserDocumentException e;
		e;
		throw new TOTorrentException((new StringBuilder()).append("XML Parse Fails: ").append(e.getMessage()).toString(), 6);
	}

	protected TOTorrent decodeRoot(SimpleXMLParserDocument doc)
		throws TOTorrentException
	{
		String root_name = doc.getName();
		if (root_name.equalsIgnoreCase("TORRENT"))
		{
			TOTorrentImpl torrent = new TOTorrentImpl();
			SimpleXMLParserDocumentNode kids[] = doc.getChildren();
			URL announce_url = null;
			byte torrent_hash[] = null;
			byte torrent_hash_override[] = null;
			for (int i = 0; i < kids.length; i++)
			{
				SimpleXMLParserDocumentNode kid = kids[i];
				String name = kid.getName();
				if (name.equalsIgnoreCase("ANNOUNCE_URL"))
				{
					try
					{
						announce_url = new URL(kid.getValue());
					}
					catch (MalformedURLException e)
					{
						throw new TOTorrentException("ANNOUNCE_URL malformed", 6);
					}
					continue;
				}
				if (name.equalsIgnoreCase("ANNOUNCE_LIST"))
				{
					SimpleXMLParserDocumentNode set_nodes[] = kid.getChildren();
					TOTorrentAnnounceURLGroup group = torrent.getAnnounceURLGroup();
					TOTorrentAnnounceURLSet sets[] = new TOTorrentAnnounceURLSet[set_nodes.length];
					for (int j = 0; j < sets.length; j++)
					{
						SimpleXMLParserDocumentNode url_nodes[] = set_nodes[j].getChildren();
						URL urls[] = new URL[url_nodes.length];
						for (int k = 0; k < urls.length; k++)
							try
							{
								urls[k] = new URL(url_nodes[k].getValue());
							}
							catch (MalformedURLException e)
							{
								throw new TOTorrentException("ANNOUNCE_LIST malformed", 6);
							}

						sets[j] = group.createAnnounceURLSet(urls);
					}

					group.setAnnounceURLSets(sets);
					continue;
				}
				if (name.equalsIgnoreCase("COMMENT"))
				{
					torrent.setComment(readLocalisableString(kid));
					continue;
				}
				if (name.equalsIgnoreCase("CREATED_BY"))
				{
					torrent.setCreatedBy(readLocalisableString(kid));
					continue;
				}
				if (name.equalsIgnoreCase("CREATION_DATE"))
				{
					torrent.setCreationDate(readGenericLong(kid).longValue());
					continue;
				}
				if (name.equalsIgnoreCase("TORRENT_HASH"))
				{
					torrent_hash = readGenericBytes(kid);
					continue;
				}
				if (name.equalsIgnoreCase("TORRENT_HASH_OVERRIDE"))
				{
					torrent_hash_override = readGenericBytes(kid);
					continue;
				}
				if (name.equalsIgnoreCase("INFO"))
				{
					decodeInfo(kid, torrent);
				} else
				{
					mapEntry entry = readGenericMapEntry(kid);
					torrent.addAdditionalProperty(entry.name, entry.value);
				}
			}

			if (announce_url == null)
				throw new TOTorrentException("ANNOUNCE_URL missing", 6);
			torrent.setAnnounceURL(announce_url);
			if (torrent_hash_override != null)
				try
				{
					torrent.setHashOverride(torrent_hash_override);
				}
				catch (Throwable e)
				{
					Debug.printStackTrace(e);
				}
			if (torrent_hash != null && !Arrays.equals(torrent.getHash(), torrent_hash))
				throw new TOTorrentException("Hash differs - declared TORRENT_HASH and computed hash differ. If this really is the intent (unlikely) then remove the TORRENT_HASH element", 6);
			else
				return torrent;
		} else
		{
			throw new TOTorrentException("Invalid root element", 6);
		}
	}

	protected void decodeInfo(SimpleXMLParserDocumentNode doc, TOTorrentImpl torrent)
		throws TOTorrentException
	{
		SimpleXMLParserDocumentNode kids[] = doc.getChildren();
		byte torrent_name[] = null;
		long torrent_length = 0L;
		SimpleXMLParserDocumentNode file_nodes[] = null;
		for (int i = 0; i < kids.length; i++)
		{
			SimpleXMLParserDocumentNode kid = kids[i];
			String name = kid.getName();
			if (name.equalsIgnoreCase("PIECE_LENGTH"))
			{
				torrent.setPieceLength(readGenericLong(kid).longValue());
				continue;
			}
			if (name.equalsIgnoreCase("LENGTH"))
			{
				torrent.setSimpleTorrent(true);
				torrent_length = readGenericLong(kid).longValue();
				continue;
			}
			if (name.equalsIgnoreCase("NAME"))
			{
				torrent.setName(readLocalisableString(kid));
				continue;
			}
			if (name.equalsIgnoreCase("FILES"))
			{
				torrent.setSimpleTorrent(false);
				file_nodes = kid.getChildren();
				continue;
			}
			if (name.equalsIgnoreCase("PIECES"))
			{
				SimpleXMLParserDocumentNode piece_nodes[] = kid.getChildren();
				byte pieces[][] = new byte[piece_nodes.length][];
				for (int j = 0; j < pieces.length; j++)
					pieces[j] = readGenericBytes(piece_nodes[j]);

				torrent.setPieces(pieces);
			} else
			{
				mapEntry entry = readGenericMapEntry(kid);
				torrent.addAdditionalInfoProperty(entry.name, entry.value);
			}
		}

		if (torrent.isSimpleTorrent())
		{
			torrent.setFiles(new TOTorrentFileImpl[] {
				new TOTorrentFileImpl(torrent, 0L, torrent_length, new byte[][] {
					torrent.getName()
				})
			});
		} else
		{
			TOTorrentFileImpl files[] = new TOTorrentFileImpl[file_nodes.length];
			long offset = 0L;
			for (int j = 0; j < files.length; j++)
			{
				SimpleXMLParserDocumentNode file_node = file_nodes[j];
				SimpleXMLParserDocumentNode file_entries[] = file_node.getChildren();
				long file_length = 0L;
				boolean length_entry_found = false;
				byte path_comps[][] = (byte[][])null;
				Vector additional_props = new Vector();
				for (int k = 0; k < file_entries.length; k++)
				{
					SimpleXMLParserDocumentNode file_entry = file_entries[k];
					String entry_name = file_entry.getName();
					if (entry_name.equalsIgnoreCase("LENGTH"))
					{
						file_length = readGenericLong(file_entry).longValue();
						length_entry_found = true;
						continue;
					}
					if (entry_name.equalsIgnoreCase("PATH"))
					{
						SimpleXMLParserDocumentNode path_nodes[] = file_entry.getChildren();
						path_comps = new byte[path_nodes.length][];
						for (int n = 0; n < path_nodes.length; n++)
							path_comps[n] = readLocalisableString(path_nodes[n]);

					} else
					{
						additional_props.addElement(readGenericMapEntry(file_entry));
					}
				}

				if (!length_entry_found || path_comps == null)
					throw new TOTorrentException((new StringBuilder()).append("FILE element invalid (file length = ").append(file_length).append(")").toString(), 6);
				files[j] = new TOTorrentFileImpl(torrent, offset, file_length, path_comps);
				offset += file_length;
				for (int k = 0; k < additional_props.size(); k++)
				{
					mapEntry entry = (mapEntry)additional_props.elementAt(k);
					files[j].setAdditionalProperty(entry.name, entry.value);
				}

			}

			torrent.setFiles(files);
		}
	}

	protected mapEntry readGenericMapEntry(SimpleXMLParserDocumentNode node)
		throws TOTorrentException
	{
		if (!node.getName().equalsIgnoreCase("KEY"))
			throw new TOTorrentException("Additional property invalid, must be KEY node", 6);
		String name = node.getAttribute("name").getValue();
		SimpleXMLParserDocumentNode kids[] = node.getChildren();
		if (kids.length != 1)
		{
			throw new TOTorrentException("Additional property invalid, KEY must have one child", 6);
		} else
		{
			String type = kids[0].getName();
			Object value = readGenericValue(kids[0]);
			return new mapEntry(name, value);
		}
	}

	protected Object readGenericValue(SimpleXMLParserDocumentNode node)
		throws TOTorrentException
	{
		String name = node.getName();
		if (name.equalsIgnoreCase("BYTES"))
			return readGenericBytes(node);
		if (name.equalsIgnoreCase("LONG"))
			return readGenericLong(node);
		if (name.equalsIgnoreCase("LIST"))
			return readGenericList(node);
		if (name.equalsIgnoreCase("MAP"))
			return readGenericMap(node);
		else
			throw new TOTorrentException((new StringBuilder()).append("Additional property invalid, sub-key '").append(name).append("' not recognised").toString(), 6);
	}

	protected byte[] readGenericBytes(SimpleXMLParserDocumentNode node)
		throws TOTorrentException
	{
		String value = node.getValue();
		byte res[] = new byte[value.length() / 2];
		for (int i = 0; i < res.length; i++)
			res[i] = (byte)Integer.parseInt(value.substring(i * 2, i * 2 + 2), 16);

		return res;
	}

	protected Long readGenericLong(SimpleXMLParserDocumentNode node)
		throws TOTorrentException
	{
		String value = node.getValue();
		return new Long(value);
		Throwable e;
		e;
		throw new TOTorrentException((new StringBuilder()).append("long value invalid for '").append(node.getName()).append("'").toString(), 6);
	}

	protected Map readGenericMap(SimpleXMLParserDocumentNode node)
		throws TOTorrentException
	{
		Map res = new HashMap();
		SimpleXMLParserDocumentNode kids[] = node.getChildren();
		for (int i = 0; i < kids.length; i++)
		{
			mapEntry entry = readGenericMapEntry(kids[i]);
			res.put(entry.name, entry.value);
		}

		return res;
	}

	protected byte[] readLocalisableString(SimpleXMLParserDocumentNode kid)
		throws TOTorrentException
	{
		SimpleXMLParserDocumentAttribute attr = kid.getAttribute("encoding");
		if (attr == null || attr.getValue().equalsIgnoreCase("bytes"))
			return readGenericBytes(kid);
		return kid.getValue().getBytes("UTF8");
		UnsupportedEncodingException e;
		e;
		throw new TOTorrentException("bytes invalid - unsupported encoding", 6);
	}

	protected List readGenericList(SimpleXMLParserDocumentNode node)
		throws TOTorrentException
	{
		List res = new ArrayList();
		SimpleXMLParserDocumentNode kids[] = node.getChildren();
		for (int i = 0; i < kids.length; i++)
			res.add(readGenericValue(kids[i]));

		return res;
	}
}

⌨️ 快捷键说明

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