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

📄 address.java

📁 关于 RFID 读写器的相关内容
💻 JAVA
字号:
/*
 * Copyright (C) 2007 ETH Zurich
 *
 * This file is part of Fosstrak (www.fosstrak.org).
 *
 * Fosstrak is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software Foundation.
 *
 * Fosstrak is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Fosstrak; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301  USA
 */

package org.fosstrak.reader.rprm.core.msg;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.StringTokenizer;

/**
 * Implementation of addresses used in the EPC Message/Transport Bindings (MTB).
 * Similar to <code>java.net.URL</code> an EPC address has a protocol, a host,
 * a port and a query.
 * <p>
 * Format: address = protocol "://" (host (":" port)+ )+ ( "?" (query)* )
 * 
 * 
 * @author Andreas F黵er, ETH Zurich Switzerland, Winter 2005/06
 * 
 */
public class Address {
	/* ------------------ URL CONSTANTS ------------------ */
	/** the delimiter used in URL (protocol://host:port/file?query1&query2).*/
	public static final String QUERY_DELIMITER = "&";
	
	/** the delimiter used in URL (protocol://host:port/file?key1=val1). */
	public static final String KEY_VALUE_DELIMITER = "=";
	
	/** the constant for the mode used in addresses of NotificationChannel's. */
	public static final String MODE = "mode";
	
	/**
	 * specifies to use the connect mode (used in a NotificationChannel
	 * address).
	 */
	public static final String MODE_CONNECT = "connect";

	/** specifies to use the listen mode (used in a NotificationChannel 
	 * address). */
	public static final String MODE_LISTEN = "listen";
	
	/* ------------------ FIELDS ------------------ */

	/** The transport protocol. */
	private String protocol;
	
	/** The host. */
	private String host;
	
	/** The port. */
	private int port = -1;
	
	/** The query parameters. */
	private HashMap urlParameters;
	
	/* ------------------ CONSTRUCTORS ------------------ */
	/**
	 * Creates a new Address object.
	 * 
	 * @param address
	 *            The address
	 * @throws MalformedURLException
	 *            If the URI couldn't be built out of the address parameter.
	 */
	public Address(final String address) throws MalformedURLException {
		try {
			URI uri = new URI(address);
			urlParameters = new HashMap();
			protocol = uri.getScheme();
			host = uri.getHost();
			port = uri.getPort();
			parseQuery(uri.getQuery());
			
		} catch (URISyntaxException e) {
			throw new MalformedURLException("Malformed address.");
		}
		
	}
	
	/* ------------------ METHODS ------------------ */

	/**
	 * @return Returns the host.
	 */
	public String getHost() {
		return host;
	}

	/**
	 * @param host
	 *            The host to set.
	 */
	public void setHost(final String host) {
		this.host = host;
	}

	/**
	 * @return Returns the port.
	 */
	public int getPort() {
		return port;
	}

	/**
	 * @param port
	 *            The port to set.
	 */
	public void setPort(final int port) {
		this.port = port;
	}

	/**
	 * @return Returns the protocol.
	 */
	public String getProtocol() {
		return protocol;
	}

	/**
	 * @param protocol
	 *            The protocol to set.
	 */
	public void setProtocol(final String protocol) {
		this.protocol = protocol;
	}

	/**
	 * Gets the mode of a address (listen|connect).
	 * 
	 * @return The mode used in NotificationChannel addresses or
	 *         <code>null</code> if the mode is not specified.
	 */
	public String getMode() {
		return (String) urlParameters.get(MODE);
	}

	/**
	 * Represents an address as a string.
	 * @return The address as a string.
	 */
	public String toString() {
		StringBuffer buffer = new StringBuffer();
		buffer.append(protocol);
		buffer.append("://");
		if (host != null) {
			buffer.append(host);
		}
		if (port != -1) {
			buffer.append(":");
			buffer.append(port);
		}

		return buffer.toString();
	}

	/**
	 * Parses the query from an URL (part after the "?") and stores the
	 * key-value-pairs in the <code>HashMap</code> urlParameters.
	 * 
	 * @param query
	 *            The query from the URL.
	 */
	private void parseQuery(final String query) {
		if (query != null && query != "") {
			StringTokenizer tokenizer = new StringTokenizer(query,
					QUERY_DELIMITER);
			while (tokenizer.hasMoreTokens()) {
				String token = tokenizer.nextToken();
				String[] pair = token.split(KEY_VALUE_DELIMITER);
				urlParameters.put(pair[0], pair[1]);

			}
		}
	}
	
}

⌨️ 快捷键说明

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