settingsxml.java

来自「java语言开发的P2P流媒体系统」· Java 代码 · 共 959 行 · 第 1/3 页

JAVA
959
字号
	    				  serverUsesVerifiedBandwidth = false;
	    		  }
	    		  Element preset = findTag(server, "preset");
	    		  if (preset != null)
	    			  this.preset = Integer.parseInt(firstChild(preset));
	    		  Element mMode = findTag(server, "message_mode");
	    		  if (mMode != null)
	    			  messageMode = Integer.parseInt(firstChild(mMode));
	    		  Element sMode = findTag(server, "stream_mode");
	    		  if (sMode != null)
	    			  streamMode = Integer.parseInt(firstChild(sMode));
	    		  Element ttl = findTag(server, "multicast_ttl");
	    		  if (ttl != null)
	    			  this.ttl = Integer.parseInt(firstChild(ttl));
	    		  Element multicastAddress = findTag(server, "multicast_address");
	    		  if (multicastAddress != null)
	    			  this.multicastAddress = firstChild(multicastAddress);
	    		  Element allowConnects = findTag(server, "allow_remote_shoutcast_connections");
	    		  if (allowConnects != null)
	    		  {
	    			  sBool = firstChild(allowConnects);
	    			  if (sBool.equals(TRUE))
	    				  Allow_Connections_From_Remote_Hosts = true;
	    			  else if (sBool.equals(FALSE))
	    				  Allow_Connections_From_Remote_Hosts = false;
	    		  }
	    		  
	    		  
		      }
		      if (last != null)
		      {
		    	  Element volume = findTag(last, "volume");
		    	  if (volume != null)
		    	  {
		    		  String svolume = firstChild(volume);
		    		  if (svolume != "-1")
		    			  playerGain = Double.parseDouble(svolume);
		    	  }
		    	  Element choice = findTag(last, "station_panel_choice");
		    	  if (choice != null)
		    		  stationPanelChoice = Integer.parseInt(firstChild(choice));
		      }
		      Logger.info("SettingsXML", "SettingsXML.SETTINGS_LOADED");
		      isValid = true;
			}
		    catch (SAXException e) {
		      Logger.info("SettingsXML", "SettingsXML.SETTINGS_MALFORMED");
		    }
		    catch (IOException e) { 
		    	Logger.info("SettingsXML", "SettingsXML.SETTINGS_NOT_FOUND", ServerOrClient);
		    	isValid = true;
		    	createNew();
		    	bTest = true;
		    	
		    }
		    catch (FactoryConfigurationError e) { 
		      // JAXP suffers from excessive brain-damage caused by 
		      // intellectual in-breeding at Sun. (Basically the Sun 
		      // engineers spend way too much time talking to each other
		      // and not nearly enough time talking to people outside 
		      // Sun.) Fortunately, you can happily ignore most of the 
		      // JAXP brain damage and not be any the poorer for it.
		      
		      // This, however, is one of the few problems you can't 
		      // avoid if you're going to use JAXP at all. 
		      // DocumentBuilderFactory.newInstance() should throw a 
		      // ClassNotFoundException if it can't locate the factory
		      // class. However, what it does throw is an Error,
		      // specifically a FactoryConfigurationError. Very few 
		      // programs are prepared to respond to errors as opposed
		      // to exceptions. You should catch this error in your 
		      // JAXP programs as quickly as possible even though the
		      // compiler won't require you to, and you should 
		      // never rethrow it or otherwise let it escape from the 
		      // method that produced it. 
		    	Logger.info("SettingsXML", "SettingsXML.FACTORY_CONFIGURATION_ERROR");
		    }
		    catch (ParserConfigurationException e) { 
		    	Logger.info("SettingsXML", "SettingsXML.PARSER_CONFIGURATION_EXCEPTION");
		    }
		  }
	private String firstChild(Element element)
	{
		if (element != null && element.hasChildNodes())
		{
			String child = element.getFirstChild().getNodeValue();
			return child;
		}
		return "-1"; //Not Found
	}
	
	private Element Find_Or_Make_Way_To_Descendant(String[] tree)
	{
		Element currentElement = doc.getDocumentElement();
		int i;
		for (i = 0; i < (tree.length - 1); i++)
		{
			Element newElement = findTag(currentElement, tree[i]);
			if (newElement == null)
			{
				newElement = doc.createElement(tree[i]);
				currentElement.appendChild(newElement);
				currentElement = newElement;
			}
			else
			{
				currentElement = newElement;
			}
		}
		return currentElement;
	}
	public void overwrite(String[] tree){
		if (!isValid)
			return;
		Element descendant = Find_Or_Make_Way_To_Descendant(tree);
		Text content = doc.createTextNode(tree[tree.length - 1]);
		if (descendant.hasChildNodes())
			descendant.replaceChild(content, descendant.getFirstChild());
		else
			descendant.appendChild(content);
	}
	public void deleteElement(String[] tree)
	{
		if (!isValid)
			return;
		
		String beDeleted = tree[tree.length - 1];
		Element descendant = Find_Or_Make_Way_To_Descendant(tree);
		Element deleted = findTag(descendant, beDeleted);
		if (deleted != null)
			descendant.removeChild(deleted);
	}
	public void deleteElements(String[] tree)
	{
		if (!isValid)
			return;
		
		String beDeleted = tree[tree.length - 1];
		Element descendant = Find_Or_Make_Way_To_Descendant(tree);
		NodeList toBeDeleted = descendant.getElementsByTagName(beDeleted);
		deleteNodeList(descendant, toBeDeleted);
	}
	private void deleteNodeList(Element parent, NodeList delete)
	{
		if (delete == null || parent == null)
			return;
		int deleteLength = delete.getLength();
		for(int i = deleteLength - 1; i >= 0; i--)
		{
			try{
			Node child = delete.item(i);
			parent.removeChild(child);
			}
			catch (DOMException e)
			{
				System.out.println("notfound or readonly");
			}
		}
	}
	private Element findTag (Element parent, String Tag){
		NodeList Children = parent.getElementsByTagName(Tag);
		if (Children == null)
			return null;
		else
			return (Element) Children.item(0);
	}
	public void writeLink(String stationName, String url)
	{
		if (!isValid)
			return;
		Element linkTag = doc.createElement("link");
		Element urlTag = doc.createElement("url");
		Element stationnameTag = doc.createElement("stationname");
		history.appendChild(linkTag);
		linkTag.appendChild(urlTag);
		linkTag.appendChild(stationnameTag);
		Text urlContent = doc.createTextNode(url);
		Text stationnameContent = doc.createTextNode(stationName);
		urlTag.appendChild(urlContent);
		stationnameTag.appendChild(stationnameContent);
	}
	public void deleteLink(String url, String stationname)
	{
		
	}
	public void deleteLinks(Map deleteUrlToStationname)
	{
		if (!isValid)
			return;
		TreeMap xmlUrlToStationname = discoverLinks(history);
		Set xmlKeys = xmlUrlToStationname.keySet();
		Iterator xmlIter = xmlKeys.iterator();
		TreeMap toBeDeleted = new TreeMap();
		while (xmlIter.hasNext())
		{
			Object xmlKey = xmlIter.next();
			Object xmlValue = xmlUrlToStationname.get(xmlKey);
			Object deleteValue = deleteUrlToStationname.get(xmlKey);
			if (deleteValue != null && deleteValue.equals(xmlValue))
			{
				toBeDeleted.put(xmlKey, xmlValue);
			}
		}
		
		NodeList links = history.getElementsByTagName("link");
		for (int i = links.getLength() - 1; i >= 0; i--)
		{
			Element link;
			try{
				link = (Element) links.item(i);
			}
			catch (ClassCastException c)
			{
				System.out.println("Not an element");
				continue;
			}
			Element urlTag = findTag(link, "url");
			Element stationTag = findTag(link, "stationname");
			if (urlTag != null && stationTag != null)
			{
				String urlFound = firstChild(urlTag);
				String stationFound = firstChild(stationTag);
				Object deletedStation = toBeDeleted.get(urlFound);
				if (deletedStation != null && deletedStation.equals(stationFound))
					{
						history.removeChild(link);
					}
			}
			
			
		}
		
		
	}
	public void flush()
	{
		try {
			TransformerFactory xformFactory 
		       = TransformerFactory.newInstance();  
		      idTransform = xformFactory.newTransformer();
		      input = new DOMSource(doc);
		      output = new StreamResult(xmlFile);
		      idTransform.transform(input, output);
		} catch (TransformerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	private void createNew()
	{
		try {
		      // Find the implementation
		      DocumentBuilderFactory factory 
		       = DocumentBuilderFactory.newInstance();
		      factory.setNamespaceAware(true);
		      DocumentBuilder builder = factory.newDocumentBuilder();
		      DOMImplementation impl = builder.getDOMImplementation();
		      
		      doc = impl.createDocument(null, "Stream2Stream", null);

		      // Fill the document
		      Node rootElement = doc.getDocumentElement();
		      /*ProcessingInstruction xmlstylesheet 
		       = doc.createProcessingInstruction("xml-stylesheet",
		       "type=\"text/css\" href=\"standard.css\"");*/
		      Comment comment = doc.createComment(
		       "Stream2Stream Settings");
		      doc.insertBefore(comment, rootElement);
		     // doc.insertBefore(xmlstylesheet, rootElement);
		      
		      
		      
		     history = doc.createElement("history");
		      rootElement.appendChild(history);
		      
		      general = doc.createElement("general");
		      rootElement.appendChild(general);
		      
		      advanced = doc.createElement("advanced");
		      rootElement.appendChild(advanced);
		      
		      network = doc.createElement("network");
		      rootElement.appendChild(network);
		      
		      last = doc.createElement("last");
		      rootElement.appendChild(last);
		      
		      
		      if (isServer)
		      {
		    	  	server = doc.createElement("server");
		    	  	rootElement.appendChild(server);
		    	  	setMonitorAddress(monitorAddress);
					setUseVerifiedBandwidth(serverUsesVerifiedBandwidth);
					setMinimumChildrenClientsMustServe(MinimumChildrenClientsMustServe);
					
					setMulticastAddress(multicastAddress);
					setTTL(ttl);
					setPreset(preset);
					setMessageMode(messageMode);
					setStreamMode(streamMode);
					Set_Allow_Connections_From_Remote_Hosts(Allow_Connections_From_Remote_Hosts);
		      }
		      setStationPanelChoice(stationPanelChoice);
		      setLogAs(logFilename);
		      setLANUpload(LAN_Max_Upload);
		      setLANDownload(LAN_Max_Download);
		      setPort(port);
		      setMediaPlayerStartup(startMediaPlayerAtStartup);
		      setUseIntegratedPlayer(useIntegratedPlayer);
		      setDisplayDebug(displayDebug);
		      setLogByDate(logByDate);
		      setSignOrVerify(signOrVerify);
		      setNegativeLagEnabled(negativeLag);
		      setEnableMonitor(enableMonitor);
		      
		      
		      setEnableMonitor(enableMonitor);
		      
		      

⌨️ 快捷键说明

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