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

📄 textcommandparserhelper.java

📁 关于 RFID 读写器的相关内容
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
/*
 * 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.command;

import java.math.BigInteger;
import java.util.Calendar;
import java.util.Stack;
import java.util.Vector;

import javax.xml.bind.JAXBException;
import javax.xml.datatype.XMLGregorianCalendar;

import org.fosstrak.reader.rprm.core.msg.command.ReaderDeviceCommand.GetDataSelector;
import org.fosstrak.reader.rprm.core.msg.command.ReaderDeviceCommand.GetNotificationChannel;
import org.fosstrak.reader.rprm.core.msg.command.ReaderDeviceCommand.GetReadPoint;
import org.fosstrak.reader.rprm.core.msg.command.ReaderDeviceCommand.GetSource;
import org.fosstrak.reader.rprm.core.msg.command.ReaderDeviceCommand.GetTagField;
import org.fosstrak.reader.rprm.core.msg.command.ReaderDeviceCommand.GetTagSelector;
import org.fosstrak.reader.rprm.core.msg.command.ReaderDeviceCommand.GetTrigger;
import org.fosstrak.reader.rprm.core.msg.command.ReaderDeviceCommand.RemoveDataSelectors;
import org.fosstrak.reader.rprm.core.msg.command.ReaderDeviceCommand.RemoveNotificationChannels;
import org.fosstrak.reader.rprm.core.msg.command.ReaderDeviceCommand.RemoveSources;
import org.fosstrak.reader.rprm.core.msg.command.ReaderDeviceCommand.RemoveTagFields;
import org.fosstrak.reader.rprm.core.msg.command.ReaderDeviceCommand.RemoveTagSelectors;
import org.fosstrak.reader.rprm.core.msg.command.ReaderDeviceCommand.RemoveTriggers;
import org.fosstrak.reader.rprm.core.msg.command.ReaderDeviceCommand.SetCurrentDataSelector;
import org.fosstrak.reader.rprm.core.msg.command.ReaderDeviceCommand.SetCurrentSource;
import org.fosstrak.reader.rprm.core.msg.command.ReaderDeviceCommand.SetHandle;
import org.fosstrak.reader.rprm.core.msg.command.ReaderDeviceCommand.SetName;
import org.fosstrak.reader.rprm.core.msg.command.ReaderDeviceCommand.SetRole;
import org.fosstrak.reader.rprm.core.msg.command.ReaderDeviceCommand.SetTimeUTC;
import org.fosstrak.reader.rprm.core.msg.command.SourceCommand.AddReadPoints;
import org.fosstrak.reader.rprm.core.msg.command.SourceCommand.RemoveReadPoints;
import org.fosstrak.reader.rprm.core.msg.util.HexUtil;
import org.apache.log4j.Logger;

import com.sun.org.apache.xerces.internal.jaxp.datatype.XMLGregorianCalendarImpl;


/**
 * TextCommandParserHelper provides general utilities for the text parser.
 * In particular it generates the tree of the intermediate representation (IR).<br />
 * <br />
 * How parameters are parsed:<br />
 * During the parsing process the <code>TextCommandParser</code> puts parameters
 * to the parameter queue (FIFO). While creating the IR the parameters are again read from
 * the parameter queue. To be able to handle cases where there are nested parameter lists
 * the parser distincts different parsing states which can be modified by <code>pushState()</code>, 
 * <code>popState()</code> and <code>topState()</code>. If the parser is in the state 
 * <code>STATE_LIST_PARAMETER</code> (parsing a parameter list) it works on a so called
 * working list. Parameters are added to the working list and the whole working list is
 * added to the local parameter queue. In this way it's possible to handle recursive (nested)
 * lists of parameters.
 * 
 * @author Andreas F黵er, ETH Zurich Switzerland, Winter 2005/06
 * 
 * @see org.fosstrak.reader.rprm.core.msg.command.TextCommandParser
 */
public class TextCommandParserHelper {
	
	/** The logger. */
	private Logger log;
	
	/** state: normal state (no special meaning) */
	public static final int STATE_NONE = -1;
	
	/** state: parsing a list */
	public static final int STATE_LIST_PARAMETER = 1;
	
	/** state: parsing a parameter pair */
	public static final int STATE_PAIR_PARAMETER = 2;
	
	/** the factory object to create the IR tree */
	public static ObjectFactory cmdFactory = new ObjectFactory(); 
	
	/** the object type */
	private int objectType;
	
	/** the type of the command */
	private int commandType;
	
	/** the command id */
	private String id;
	
	/** the target name (identiefier for object instances) */
	private String targetName;
	
	/** the parameter queue */
	private Vector parameters = new Vector();
	
	/** the working list stack */
	private Stack listStack = new Stack();
	
	/** the parsing state stack */
	private Stack stateStack = new Stack();
	
	/**
	 * Constructor for the TextCommandParserHelper.
	 */
	public TextCommandParserHelper() {
		log = Logger.getLogger(getClass().getName());
	}
	
	/**
	 * Set the command id.
	 * @param id The unique id of the command.
	 */
	public void setId(String id) {
		this.id = id;
	}
	
	/**
	 * Get the command id.
	 * @return the id of the command used to identify corresponding request-response pairs.
	 */
	public String getId() {
		return this.id;
	}
	
	/**
	 * Get the targetName which identifies an object instance.
	 * @return Returns the targetName.
	 */
	public String getTargetName() {
		return targetName;
	}

	/**
	 * Set the targetName which identifies an object instance.
	 * @param targetName The targetName to set.
	 */
	public void setTargetName(String targetName) {
		this.targetName = targetName;
	}
	
	/**
	 * Sets the object defined by the constants in <code>TextCommandParserTokenTypes</code><br />
	 * e.g., TextCommandParserTokenTypes.READERDEVICE, TextCommandParserTokenTypes.SOURCE etc.
	 * @param objectType the object type
	 * @see TextCommandParserTokenTypes
	 */
	public void setObject(int objectType) {
		this.objectType = objectType;
	}

	/**
	 * Sets the command method defined by constants in <code>TextCommandParserTokenTypes</code><br />
	 * e.g., TextCommandParserTokenTypes.CMD_CREATE, TextCommandParserTokenTypes.GET_NAME etc.
	 * @param commandType the command method
	 */
	public void setCommand(int commandType) {
		this.commandType = commandType;
	}
	
	/**
	 * Puts a <code>Parameter</code> to the parameter queue.
	 * @param param The <code>Parameter</code> to be pushed to the stack.
	 */
	public void writeParameter(Parameter param) {
		parameters.add(param);
	}
	
	/**
	 * Puts a integer parameter to the parameter queue. Internally integer
	 * parameters are pushed as <code>ValueParameter</code>s.
	 * @param i The value of the integer parameter
	 */
	public void writeParameter(int i) {
		Parameter param = new ValueParameter(i);
		writeParameter(param);
	}
	
	/**
	 * Puts a string parameter to the parameter queue. Internally string
	 * parameters are pushed as <code>ValueParameter</code>s.
	 * @param s The value of the string parameter
	 */
	public void writeParameter(String s) {
		Parameter param = new ValueParameter(s);
		writeParameter(param);
	}
	
	/**
	 * Puts a boolean parameter to the parameter queue. Internally boolean
	 * parameters are pushed as <code>ValueParameter</code>s.
	 * @param b The value of the boolean parameter
	 */
	public void writeParameter(boolean b) {
		Parameter param = new ValueParameter(b);
		writeParameter(param);
	}
	
	/**
	 * Pushes a parsing state to the local state stack.
	 * These states are used by the parser to identify different
	 * parsing states.
	 * @param state The state
	 */
	public void pushState(int state) {
		stateStack.push(new Integer(state));
	}
	
	/**
	 * Reads top element of the parsing state stack without removing it.
	 * @return the top element of the parsing state.
	 */
	public int topState() {
		if (stateStack.isEmpty()) {
			return STATE_NONE;
		} else {
			Integer i = (Integer)stateStack.peek();
			return i.intValue();
		}
	}
	
	/**
	 * Pops the parsing state stack and removes the element.
	 * @return the top of the parsing stack
	 */
	public int popState() {
		if (stateStack.isEmpty()) {
			return STATE_NONE;
		} else {
			Integer i = (Integer)stateStack.pop();
			return i.intValue();
		}
	}
	
	/**
	 * Gets the head of the parameter queue and removes the element.
	 * @return the first parameter in the queue as a <code>ValueParameter</code>.
	 * @throws TextCommandParserException if no more parameters are available or the parameter is not of type <code>ValueParameter</code>
	 */
	public ValueParameter readValueParameter() throws TextCommandParserException {
		if (!hasParameters()) {
			throw new ParameterMissingException("No more parameters available. Parameter queue is empty.");
		}
		Parameter ret = (Parameter)parameters.get(0);
		if (ret instanceof ValueParameter) {
			parameters.remove(0);
			return (ValueParameter)ret;
		}
		throw new ParameterWrongTypeException("Wrong parameter type. Expected a ValueParameter instead of a " + ret.getClass());
	}
	
	/**
	 * Gets the head of the parameter queue and removes the element.
	 * @return the first parameter in the queue as a <code>ListParameter</code>.
	 * @throws TextCommandParserException if no more parameters are available or the parameter is not of type <code>ListParameter</code>
	 */
	public ListParameter readListParameter() throws TextCommandParserException {
		if (!hasParameters()) {
			throw new ParameterMissingException("No more parameters available. Parameter queue is empty.");
		}
		Parameter ret = (Parameter)parameters.get(0);
		if (ret instanceof ListParameter) {
			parameters.remove(0);
			return (ListParameter)ret;
		}
		throw new ParameterWrongTypeException("Wrong parameter type. Expected a ListParameter instead of a " + ret.getClass());
	}
	
	/**
	 * Gets the head of the parameter queue and removes the element.
	 * @return the first parameter in the queue as a <code>Parameter</code>.
	 * @throws TextCommandParserException if no more parameters are available or the parameter is not of type <code>Parameter</code>
	 */
	public Parameter readParameter() throws TextCommandParserException {
		if (!hasParameters()) {
			throw new ParameterMissingException("No more parameters available. Parameter queue is empty.");
		}
		Parameter ret = (Parameter)parameters.get(0);
		parameters.remove(0);
		return ret;
	}
	
	/**
	 * Flag indicating if there are more parameters available in the parameter queue.
	 * @return <code>true</code> if the parameter queue is not empty, <code>false</code> otherwise.
	 */
	public boolean hasParameters() {
		return (parameters.size() != 0);
	}
	
	/**
	 * Pushes a working list to the working list stack.
	 * @param p The current working list
	 */
	public void pushList(ListParameter p) {
		listStack.push(p);
	}
	
	/**
	 * Pops a working list from the working list stack.
	 * @return the current working list
	 * @throws TextCommandParserException if the working list stack is empty.
	 */
	public ListParameter popList() throws TextCommandParserException {
		if (listStack.isEmpty()) {
			throw new ParameterMissingException("List paramater stack is empty. Could not parse the parameter lists correctly.");
		}
		return (ListParameter)listStack.pop();
	}
	
	/**
	 * Builds the IR for a text command.
	 * @return The IR of a parsed command.
	 * @throws TextCommandParserException
	 */
	public Command buildCommandTree() throws TextCommandParserException {
		Command command = cmdFactory.createCommand();
		command.setId(id);
		command.setTargetName(targetName);
		switch (objectType) {
		case TextCommandParserTokenTypes.READERDEVICE: 
		{
			command.setReaderDevice(getReaderDeviceCommand());
			break;
		}
		case TextCommandParserTokenTypes.SOURCE:
		{
			command.setSource(getSourceCommand());
			break;
		}
		case TextCommandParserTokenTypes.READPOINT:
		{
			command.setReadPoint(getReadPointCommand());
			break;
		}
		case TextCommandParserTokenTypes.TAGSELECTOR:
		{
			command.setTagSelector(getTagSelectorCommand());
			break;
		}
		case TextCommandParserTokenTypes.DATASELECTOR:
		{
			command.setDataSelector(getDataSelectorCommand());
			break;
		}
		case TextCommandParserTokenTypes.NOTIFICATIONCHANNEL:
		{
			command.setNotificationChannel(getNotificationChannelCommand());
			break;
		}
		case TextCommandParserTokenTypes.TRIGGER:
		{
			command.setTrigger(getTriggerCommand());
			break;
		}
		case TextCommandParserTokenTypes.EVENTTYPE:
		{
			command.setEventType(getEventTypeCommand());
			break;
		}
		case TextCommandParserTokenTypes.TRIGGERTYPE:
		{
			command.setTriggerType(getTriggerTypeCommand());
			break;
		}
		case TextCommandParserTokenTypes.FIELDNAME:
		{
			command.setFieldName(getFieldNameCommand());
			break;
		}
		case TextCommandParserTokenTypes.TAGFIELD:
		{

⌨️ 快捷键说明

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