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

📄 robotconfdoc.java

📁 j2me实现的移动机器人代码(Java实现)
💻 JAVA
字号:
package name.lxm.robot.arch;

import org.jdom.input.SAXBuilder;
import org.jdom.*;
import org.jdom.output.*;
import java.io.*;
import java.util.*;

/**
 * <p>The robot configuration XML document object.</p>
 * <p>This class is mainly in charge of reading module and wire configurations from the XML configration file (generally named ``mobile.xml'' in the root directory of the program, but it's not defined in this class), and servicing these information to the whole system (generally the modules of the system) .</p>
 * <p>To fully understand that this class is only a bridge between the system and the configuration file may contribute a lot to the comprehention of the whole architecture.</p>
 */
public class RobotConfDoc
{
	private String robot_name;
	private HashMap modules = new HashMap();
	private HashMap wires = new HashMap();
	private boolean requireUI = false;
	/**
	 * Default constructor
	 */
	public RobotConfDoc()
	{
	}
	
	/**
	 * Construct from a reader
	 * @param r Reader - a reader
	 */
	public RobotConfDoc(Reader r) 
		throws IOException, IllegalXMLFormatException, 
		       JDOMException
	{
		SAXBuilder builder = new SAXBuilder();
		Document xmldoc = builder.build(r);
		loadFromXML(xmldoc);
	}
	
	/**
	 * Constrcut from a xml document
	 * @param doc Document - an xml document
	 * @throws IllegalXMLFormatException 
	 */
	public RobotConfDoc(Document doc) throws IllegalXMLFormatException
	{
		loadFromXML(doc);
	}
	
	/**
	 * load configuration from xml document
	 * @param doc Document - an XML Document
	 * @throws IllegalXMLFormatException
	 */
	public void loadFromXML(Document doc) throws IllegalXMLFormatException
	{
		Element root = doc.getRootElement();
		robot_name = root.getAttributeValue("name");
		String s = root.getAttributeValue("requireui");
		if(s != null && s.equals("yes"))
		{
			requireUI = true;
		}
		if(root.getName().equals("mobile"))
		{
			//get module defination
			List ele_list = root.getChildren("module");
			for(int i=0; i<ele_list.size(); i++)
			{
				Element e = (Element) ele_list.get(i);
				ModuleDoc md = new ModuleDoc(e);
				modules.put(md.getName(), md);
			}
			ele_list = root.getChildren("wire");
			for(int i=0; i<ele_list.size(); i++)
			{
				Element e = (Element) ele_list.get(i);
				WireConfig wc = new WireConfig(e);
				wires.put(wc.getID(), wc);
			}
		}
		else
			throw new IllegalXMLFormatException("Not element name with ``mobile'', please check the xml segment.");
	}

	/**
	 * Serialize to a XML Document
	 * @return Document - an XML Document
	 */
	public Document toXMLDocument()
	{
		
		Element e =  new Element("mobile");
		Document doc = new Document(e);
		e.setAttribute("name", robot_name);
		Iterator it = modules.values().iterator();
		while(it.hasNext())
		{
			ModuleDoc md = (ModuleDoc) it.next();
			e.addContent(md.toXMLElement());
		}
		return doc;
	}

	/**
	 * Get the robot name
	 * @return String - name of the robot
	 */
	public String getRobotName()
	{
		return robot_name;
	}

	public HashMap getWireConfigs()
	{
		return wires;
	}

	/**
	 * Get the list of module
	 * @return List - the list of module
	 */
	public Iterator getModuleDocs()
	{
		return modules.values().iterator();
	}

	public ModuleDoc getModuleDoc(String name)
	{
		return (ModuleDoc) modules.get(name);
	}

	
	/*
	 * For Test and Demo Only
	 */
	public static void main(String[] args)
	{
		try{
			File f = new File("mobile.xml");	
			FileReader fr = new FileReader(f);
			RobotConfDoc conf = new RobotConfDoc(fr);
			System.out.println("Robot is " + conf.getRobotName());
			Iterator i = conf.getModuleDocs();
			while(i.hasNext())
			{
				ModuleDoc md = (ModuleDoc) i.next();
				System.out.println("Module:");
				System.out.println("\t"+md.getName() + "\n\t" + md.getClassName());
			}
			i = conf.getWireConfigs().values().iterator();
			while(i.hasNext())
			{
				WireConfig wc = (WireConfig) i.next();
				System.out.println("Wire:");
				System.out.println("\t"+wc.getID() + "\n\t" + wc.getSourceModule() + "\n\t" + wc.getSourcePort());
			}			
			fr.close();
			Writer w = new StringWriter();
			Document doc = conf.toXMLDocument();
			Format format = Format.getPrettyFormat();
			format.setLineSeparator("\r\n");
			XMLOutputter outputter = new XMLOutputter(format);
			outputter.output(doc, w);
			System.out.println(w.toString());
			w.close();
		}catch(Exception e)
		{
			e.printStackTrace();
		}
	}

	public boolean requireUI()
	{
		return requireUI;
	}
}

⌨️ 快捷键说明

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