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

📄 configurationinfo.java

📁 We intend to develop a wifi enabled p2p file sharing system on a linux platform using jxta and java.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package edu.uiuc.cs.cs327.linuxwifi.services;



import java.util.Vector.*;

import java.util.*;
import java.util.Vector;


import java.awt.*;

import java.io.*;

import  org.w3c.dom.*;
import org.apache.xerces.dom.TextImpl;
import org.apache.xerces.parsers.DOMParser;
import  org.apache.xerces.dom.DocumentImpl;
import  org.apache.xerces.dom.DOMImplementationImpl;
import  org.w3c.dom.Document;
import  org.apache.xml.serialize.OutputFormat;
import  org.apache.xml.serialize.Serializer;
import  org.apache.xml.serialize.SerializerFactory;
import  org.apache.xml.serialize.XMLSerializer;


    /**

     * Constructs a configuration information class that holds all

     * parameters relating to the users' preferences.

     */


public class ConfigurationInfo {



	private static final boolean DEBUG = true;

	// name of file used saving	linux-wifi configuration
	private static String ConfigFilename = "linux-wifi.xml";
	private static String xmlRootTag = "linuxwifi";
	private static String xmlUploadDirTag = "uploaddir";
	private static String xmlUploadDirsTag = "uploaddirectories";

	private static String xmlDownloadDirTag = "downloaddir";
	private static String xmlDownloadDirsTag = "downloaddirectories";

	private static String xmlMaxUploadsPerHourTag = "maxuploadsperhour";
	private static String xmlKnownNodesOnlyTag = "knownodesonly";
	private static String xmlAutoDownloadModeTag = "autodownloadmode";
	private static String xmlMusicProfilesTag = "musicprofiles";
	private static String xmlMusicProfileTag = "musicprofile";

	private static String DirListDelim = ";";
	private static int defMaxUploadsPerHour = 20;
	// -----------------------------------------------------------------------
	// Configuration Items --- Download/Upload General Restrictions

    private Vector musicProfileVector=null;		// Vector of MusicProfiles



	private int maxUploadsPerHour = defMaxUploadsPerHour;	// number of downloads permitted per hour


	private boolean knownNodesOnly = false;		// whether to allow only the known nodes
	private Vector knownNodesVector = null;		// list of know nodes


	private Vector uploadDirectories = null;	// list of directories to upload from
	private Vector downloadDirectories = new Vector();	// list of directories to download to



	private boolean autoDownloadMode = false;	// whether auto-download is enabled

	// -----------------------------------------------------------------------



	// -----------------------------------------------------------------------
    public ConfigurationInfo() {

		load();
    }
	// -----------------------------------------------------------------------

	// -----------------------------------------------------------------------
	/**

		 * Sets preferences variables based on the values in XML
		 * @return success

	 */
	public boolean setFromXML() {
		boolean success=false;
		DebugOut("ENTERED: setFromXML" );
		try {
			DOMParser parser = new DOMParser();
			Document doc = null;
			parser.parse(ConfigFilename);
            doc = parser.getDocument();

			String sVal;

			// upload directories
			NodeList elements = null;
			elements = doc.getElementsByTagName(xmlUploadDirTag);

			if (elements != null) {
				if (uploadDirectories != null)
					uploadDirectories.clear();
				else
					uploadDirectories = new Vector();
				int elementCount = elements.getLength();
				for (int i = 0; i < elementCount; i++) {
					Element element = (Element)elements.item(i);
					Node textNode = element.getFirstChild();
					sVal = "";
					if (textNode != null)
						sVal = textNode.getNodeValue();
					uploadDirectories.add(sVal);
            	}
			}

			// download directories
			elements = doc.getElementsByTagName(xmlDownloadDirTag);

			if (elements != null) {
				if (downloadDirectories != null)
					downloadDirectories.clear();
				else
					downloadDirectories = new Vector();
				int elementCount = elements.getLength();
				for (int i = 0; i < elementCount; i++) {
					Element element = (Element)elements.item(i);
					Node textNode = element.getFirstChild();
					sVal = "";
					if (textNode != null)
						sVal = textNode.getNodeValue();
					downloadDirectories.add(sVal);
            	}
			}


			// max uploads per hour
			sVal = getElementValue(doc, xmlMaxUploadsPerHourTag);
			maxUploadsPerHour = Integer.parseInt(sVal);

			// known nodes only
			sVal = getElementValue(doc, xmlKnownNodesOnlyTag);
			setKnownNodesOnly(false);
			DebugOut("Known sVal =" + sVal);
			if (sVal.compareTo("1") == 0)
				setKnownNodesOnly(true);

			// autodownload
			sVal = getElementValue(doc, xmlAutoDownloadModeTag);
			setModeToAutoDrive(false);
			if (sVal.compareTo("1") == 0)
				setModeToAutoDrive(true);


			// TODO: Load music profiles
			elements = doc.getElementsByTagName(xmlMusicProfileTag);
			if (elements != null) {
				if (musicProfileVector != null)
					musicProfileVector.clear();
				else
					musicProfileVector = new Vector();
				int elementCount = elements.getLength();
				for (int i = 0; i < elementCount; i++) {
					MusicProfile mp = new MusicProfile();
					Element element = (Element)elements.item(i);
					mp.setFromXmlNode(element);
					musicProfileVector.add(mp);
				}
			}

			//PopulateMP();

			OutputFormat    format  = new OutputFormat(doc);   //Serialize DOM
            StringWriter  stringOut = new StringWriter();        //Writer will be a String
			XMLSerializer    serial = new XMLSerializer(stringOut, format);
			serial.asDOMSerializer();                            // As a DOM Serializer
			serial.serialize(doc.getDocumentElement());
			DebugOut("setFromXML = " + stringOut.toString());

		}
		catch (Exception ex) {
			ex.printStackTrace();
		}
		success = true;
		return(success);
	}


	private String getElementValue(Document doc, String sTag) {
		String sVal = "";
		NodeList elements = doc.getElementsByTagName(sTag);
		if (elements != null) {
			if (elements.getLength() > 0) {
				Element element = (Element)elements.item(0);
				Node textNode = element.getFirstChild();
				if (textNode != null)
					sVal = textNode.getNodeValue();
			}
		}
		return sVal;
	}
	// -----------------------------------------------------------------------


	// -----------------------------------------------------------------------
	/**

	     * Read all configuration information from the config file and
	     * populate preferences variables

	     * @return success

     */
	public boolean load() {
		boolean success=false;
		FileIOHelper fileIOHelperObject = new FileIOHelper();
		if (fileIOHelperObject.FileRead(ConfigFilename)) {
			success = setFromXML();
			DebugOut(fileIOHelperObject.getFileBuf());
		}
		return(success);
	}
	// -----------------------------------------------------------------------

	// temporary routine
	public void PopulateMP() {
		if (musicProfileVector != null)
			musicProfileVector.clear();
		else
			musicProfileVector = new Vector();
		MusicProfile mf = new MusicProfile();
		mf.setFileFormat("mp3");
		mf.setMaxFileSize(100000);
		mf.setArtist("Billy Joel");
		mf.setSong("My Life");
		mf.setGenre("Rock");
		musicProfileVector.add(mf);

		mf = new MusicProfile();
		mf.setFileFormat("wav");
		mf.setMaxFileSize(150000);
		mf.setArtist("Nelly");
		mf.setSong("Hot in here");
		mf.setGenre("Rap");
		musicProfileVector.add(mf);
	}

	// -----------------------------------------------------------------------
	    /**

			 * get values of all preferences as an xml document

			 * @return success

	     */
		public String xml()
		{
			try {
				int i;
				Document doc= new DocumentImpl();
				Element root = doc.createElement(xmlRootTag);     // Create Root Element
				doc.appendChild( root );                        // Add Root to Document

				// upload directories
				Element elemDirs = doc.createElement(xmlUploadDirsTag);
				root.appendChild(elemDirs);
				String Dir;
				if (uploadDirectories != null) {
					Iterator directories = uploadDirectories.iterator();
					while(directories.hasNext()) {
						Dir = (String)directories.next();
						Element uploaddir = doc.createElement(xmlUploadDirTag);
						uploaddir.appendChild(doc.createTextNode(Dir));
						elemDirs.appendChild(uploaddir);
					}
				}

				// download directories
				elemDirs = doc.createElement(xmlDownloadDirsTag);
				root.appendChild(elemDirs);
				if (downloadDirectories != null) {
					Iterator directories = downloadDirectories.iterator();
					while(directories.hasNext()) {
						Dir = (String)directories.next();
						Element downloaddir = doc.createElement(xmlDownloadDirTag);
						downloaddir.appendChild(doc.createTextNode(Dir));
						elemDirs.appendChild(downloaddir);
					}
				}


⌨️ 快捷键说明

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