Logo
Distributed Genetic Programming Framework
print print

File org.dgpf.gui.utils.HostListUtils.java

Here you can find all the information about the file org.dgpf.gui.utils.HostListUtils.java. You may explore it here or download it onto your local disk.
/**
 * 
 */

package org.dgpf.gui.utils;

import java.io.File;
import java.io.IOException;
import java.util.Vector;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 * This class handles arrays of <link>org.dgpf.gui.DGPFHost</link> It cann
 * read such arrays from a xml file or write it out.
 * 
 * @author Lado Kumsiashvili
 */

public class HostListUtils
  {

  /**
   * @param p_file
   *          xml file containing hostlist.
   * @return Array of Hosts
   * @throws IOException
   * @throws SAXException
   * @throws ParserConfigurationException
   */

  public static Vector<Host> getHosts(File p_file) throws IOException,
      ParserConfigurationException, SAXException
    {
    Vector<Host> l_hosts = new Vector<Host>();
    Document l_doc;
    l_doc = parseDocument(p_file);
    l_doc.getDocumentElement().normalize();
    NodeList l_listOfHosts = l_doc.getElementsByTagName("host");

    for(int i = 0; i < l_listOfHosts.getLength(); ++i)
      {
      Node l_host = l_listOfHosts.item(i);
      NodeList l_hostData = l_host.getChildNodes();
      Node l_node;
      Element l_el;
      String l_hostname = "";
      String l_description = "";
      int l_port = -1;
      for(int j = 0; j < l_hostData.getLength(); ++j)
        {
        l_node = l_hostData.item(j);

        if(l_node.getNodeType() == Node.ELEMENT_NODE)
          {
          l_el = (Element) l_node;
          String l_nodeName = l_el.getNodeName();
          String l_nodeContent = l_el.getTextContent().trim();

          if(l_nodeName.equals("hostname"))
            {
            l_hostname = l_nodeContent;
            }
          else
            if(l_nodeName.equals("port"))
              {
              l_port = Integer.parseInt(l_nodeContent);
              }
            else
              {
              l_description = l_nodeContent;
              }
          }
        }
      l_hosts.add(new Host(l_hostname, l_port, l_description));
      }

    return l_hosts;
    }

  /**
   * Writes the list of hosts into an xml file.
   * 
   * @param p_hosts
   *          are the hosts to be written.
   * @param p_file
   *          to fill with hosts.
   * @throws IOException
   * @throws SAXException
   * @throws ParserConfigurationException
   * @throws TransformerFactoryConfigurationError
   * @throws TransformerException
   */

  public static void writeHosts(Vector<Host> p_hosts, File p_file)
      throws ParserConfigurationException, SAXException, IOException,
      TransformerFactoryConfigurationError, TransformerException
    {
    Document l_doc = createDocument();
    // creating a root element
    Element l_root = l_doc.createElement("hosts");
    // add newline(we format our xml Documnts)
    l_root.appendChild(l_doc.createTextNode("\n"));
    // Filling the Document xmldoc with values
    for(Host ll_host : p_hosts)
      {
      Element l_host = l_doc.createElement("host");
      l_host.appendChild(l_doc.createTextNode("\n"));
      l_host.appendChild(createHostDataUnit("hostname",
          ll_host.getHostname(), l_doc));
      l_host.appendChild(l_doc.createTextNode("\n"));
      l_host.appendChild(createHostDataUnit("port"new Integer(ll_host
          .getPort()), l_doc));
      l_host.appendChild(l_doc.createTextNode("\n"));
      l_host.appendChild(createHostDataUnit("description", ll_host
          .getDescription(), l_doc));
      l_host.appendChild(l_doc.createTextNode("\n"));
      l_root.appendChild(l_host);
      l_root.appendChild(l_doc.createTextNode("\n"));
      }
    // the Document ist completed
    l_doc.appendChild(l_root);
    l_doc.normalize();
    // Prepare the DOM document for writing
    Source l_source = new DOMSource(l_doc);
    Result l_result = new StreamResult(p_file);
    // Write the DOM document to the file
    Transformer l_xformer = TransformerFactory.newInstance()
        .newTransformer();
    l_xformer.transform(l_source, l_result);
    }

  /**
   *  Create a Host Instanz
   * 
   * @param p_name
   *          node name to set.
   * @param p_o
   *          value to add to node.
   * @param p_doc
   *          source.
   * @return A Node.
   */

  private static Node createHostDataUnit(String p_name, Object p_o,
      Document p_doc)
    {
    Element p_el = p_doc.createElement(p_name);
    p_el.setTextContent(p_o.toString());
    // el.appendChild(doc.createTextNode("\n"));
    return p_el;
    }

  /**
   * Parses a xml File and creates a DOM Document.
   * 
   * @param p_f File to parse.
   * @return  The Document
   * 
   * @throws ParserConfigurationException
   * @throws SAXException
   * @throws IOException
   */

  private static Document parseDocument(File p_f)
      throws ParserConfigurationException, SAXException, IOException
    {

    // Initialise
    DocumentBuilderFactory l_docBuilderFactory = DocumentBuilderFactory
        .newInstance();
    DocumentBuilder l_docBuilder = l_docBuilderFactory.newDocumentBuilder();

    return l_docBuilder.parse(p_f);

    }

  /**
   * Creats an empty DOM Document.
   * 
   * @return new <code>Document</code>.
   * @throws ParserConfigurationException
   * @throws SAXException
   * @throws IOException
   */

  private static Document createDocument()
      throws ParserConfigurationException, SAXException, IOException
    {

    // Initialise
    DocumentBuilderFactory l_docBuilderFactory = DocumentBuilderFactory
        .newInstance();
    DocumentBuilder l_docBuilder = l_docBuilderFactory.newDocumentBuilder();

    return l_docBuilder.newDocument();

    }
  }

File Information:

file name:HostListUtils.java
package:org.dgpf.gui.utils
qualified name:org.dgpf.gui.utils.HostListUtils.java
file type:Java Source File
download location:download http://dgpf.sourceforge.net/source/org/dgpf/gui/utils/HostListUtils.java
size:5.987 KB (6131 B)
uploaded: 2015-07-22 04:10:59 GMT+0000
last update: 2006-09-24 23:47:04 GMT+0000
last access: 2017-11-19 03:04:29 GMT+0000

statistics online since 2006-01-02.   RSS Feed
Contact us by sending an email to tweise@gmx.de to receive further information, to report errors, or to join our project.
All content on this site (http://dgpf.sourceforge.net/) is LGPL-licensed.
http://dgpf.sourceforge.net/scripts/source/source.php last modified at 2015-07-22 04:10:53 GMT+0000 served at 2017-11-19 03:04:29 GMT+0000.
Valid CSS Valid XHTML 1.1
Valid RSS SourceForge.net Logo