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

📄 devsfiletodom.java

📁 这是一个用java和xml编写的流媒体服务器管理软件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * ====================================================================
 * The Vovida Software License, Version 1.0
 * 
 * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 
 * 1. Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in
 * the documentation and/or other materials provided with the
 * distribution.
 * 
 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
 * and "Vovida Open Communication Application Library (VOCAL)" must
 * not be used to endorse or promote products derived from this
 * software without prior written permission. For written
 * permission, please contact vocal@vovida.org.
 * 
 * 4. Products derived from this software may not be called "VOCAL", nor
 * may "VOCAL" appear in their name, without prior written
 * permission of Vovida Networks, Inc.
 * 
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
 * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 * 
 * ====================================================================
 * 
 * This software consists of voluntary contributions made by Vovida
 * Networks, Inc. and many individuals on behalf of Vovida Networks,
 * Inc.  For more information on Vovida Networks, Inc., please see
 * <http://www.vovida.org/>.
 * 
 */

package vocal.data;

import vocal.ui.AdministrativeLogin;
import vocal.comm.VPPException;
import vocal.comm.VPPNoSuchFileException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

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

import java.util.Vector;
import java.util.Hashtable;
import java.util.Enumeration;
import java.io.IOException;
import java.io.StringReader;

/**
 * $RCSfile: FileToDOM.java,v $
 * 
 * @author $Author: bko $, $Date: 2002/09/26 01:14:01 $
 * @version $Revision: 1.33 $
 */
public class DevsFileToDOM implements DevsDataDefaults, TreeNodeTypes
{
  private static Document baseDocument;
  private static Element rootElement;
  private static Element systemElement;
  private static Element serversElement;
  private static Element uaclientElement;

  public static Document readFiles() 
    throws NoSuchNodeException, IOException, SAXException, VPPException
  {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder parser = null;
    try
    {
      parser = dbf.newDocumentBuilder();
    }
    catch(ParserConfigurationException e1)
    {
      e1.printStackTrace();
      return null;
    }
    StringReader baseReader = new StringReader(BASE_XML);

    baseDocument = parser.parse(new InputSource(baseReader));
    rootElement = baseDocument.getDocumentElement();
    systemElement = XMLUtils.getChildByName(rootElement, "system");
    serversElement = XMLUtils.getChildByName(rootElement, "servers");
    uaclientElement = XMLUtils.getChildByName(rootElement,"uaclient");
    // get the global config
    Document globalConfigDoc = null;

    try
    {
      try
      {
        StringReader reader = 
          new StringReader(AdministrativeLogin.getConnection().doGet(SYSTEM_DIR, 
                GLOBAL_CONFIG_FILE));

        try
        {
          globalConfigDoc
            = parser.parse(new InputSource(reader));
        }
        catch (SAXException e)
        {
          System.out.println("bad XML: " + reader.toString());
          e.printStackTrace();
        }

      }
      catch (VPPNoSuchFileException e)
      {
        System.out.println("Creating new " + GLOBAL_CONFIG_FILE);

        StringReader reader = new StringReader(GLOBAL_CONFIG_XML);

        globalConfigDoc = parser.parse(new InputSource(reader));

        DOMToFile.writeFileFromNode(globalConfigDoc.getDocumentElement(), 
                GLOBAL, SYSTEM_DIR, GLOBAL_CONFIG_FILE);
      }

      Element globalConfigNode = 
        (Element) (baseDocument.importNode(globalConfigDoc.getDocumentElement(), 
              true));

      globalConfigNode.setAttribute("fileDir", SYSTEM_DIR);
      globalConfigNode.setAttribute("fileName", GLOBAL_CONFIG_FILE);
      globalConfigNode.setAttribute("fileType", Integer.toString(GLOBAL));
      systemElement.appendChild(globalConfigNode);

      // get the OSP server info
      Document ospServerDoc = null;

      try
      {
        System.out.println("looking for " + SYSTEM_DIR + OSP_SERVER_FILE);

        StringReader reader = 
          new StringReader(AdministrativeLogin.getConnection().doGet(SYSTEM_DIR, 
                OSP_SERVER_FILE));

        try
        {
          ospServerDoc = parser.parse(new InputSource(reader));
          
        }
        catch (SAXException e)
        {
          System.out.println("bad XML: " + reader.toString());
          e.printStackTrace();
        }

      }
      catch (VPPNoSuchFileException e)
      {
        System.out.println("Creating new " + OSP_SERVER_FILE);

        StringReader reader = new StringReader(OSP_SERVER_XML);

        ospServerDoc = parser.parse(new InputSource(reader));

        DOMToFile.writeFileFromNode(ospServerDoc.getDocumentElement(), 
                OSP_SERVER, SYSTEM_DIR, OSP_SERVER_FILE);
      }

      Element ospServerNode = 
        (Element) (baseDocument.importNode(ospServerDoc.getDocumentElement(), 
              true));

      ospServerNode.setAttribute("fileDir", SYSTEM_DIR);
      ospServerNode.setAttribute("fileName", OSP_SERVER_FILE);
      ospServerNode.setAttribute("fileType", Integer.toString(OSP_SERVER));
      systemElement.appendChild(ospServerNode);
      System.out.println("after appending OSP, whole tree is " 
                         + new XMLUtils().buildXMLString(rootElement));

      // get the ip dial plan
      Document ipPlanDoc = null;

      try
      {
        System.out.println("looking for " + SYSTEM_DIR 
                           + DEFAULT_IP_PLAN_FILE);

        StringReader reader = 
          new StringReader(AdministrativeLogin.getConnection().doGet(SYSTEM_DIR, 
                DEFAULT_IP_PLAN_FILE));

        try
        {
          ipPlanDoc = parser.parse(new InputSource(reader));
          
        }
        catch (SAXException e)
        {
          System.out.println("bad XML: " + reader.toString());
          e.printStackTrace();
        }

      }
      catch (VPPNoSuchFileException e)
      {
        System.out.println("Creating new " + DEFAULT_IP_PLAN_FILE);

        StringReader reader = new StringReader(DEFAULT_IP_PLAN_XML);

        ipPlanDoc = parser.parse(new InputSource(reader));

        DOMToFile.writeFileFromNode(ipPlanDoc.getDocumentElement(), 
                IP_DIAL_PLAN, SYSTEM_DIR, DEFAULT_IP_PLAN_FILE);
      }

      Element ipPlanNode = 
        (Element) (baseDocument.importNode(ipPlanDoc.getDocumentElement(), 
              true));

      ipPlanNode.setAttribute("fileDir", SYSTEM_DIR);
      ipPlanNode.setAttribute("fileName", DEFAULT_IP_PLAN_FILE);
      ipPlanNode.setAttribute("fileType", Integer.toString(IP_DIAL_PLAN));
      systemElement.appendChild(ipPlanNode);

      // get the phone dial plan
      Document phonePlanDoc = null;

      try
      {
        System.out.println("looking for " + SYSTEM_DIR 
                           + DEFAULT_PHONE_PLAN_FILE);

        StringReader reader = 
          new StringReader(AdministrativeLogin.getConnection().doGet(SYSTEM_DIR, 
                DEFAULT_PHONE_PLAN_FILE));

        try
        {
          phonePlanDoc = parser.parse(new InputSource(reader));
          
        }
        catch (SAXException e)
        {
          System.out.println("bad XML: " + reader.toString());
          e.printStackTrace();
        }

      }
      catch (VPPNoSuchFileException e)
      {
        System.out.println("Creating new " + DEFAULT_PHONE_PLAN_FILE);

        StringReader reader = new StringReader(DEFAULT_PHONE_PLAN_XML);

        phonePlanDoc = parser.parse(new InputSource(reader));

        DOMToFile.writeFileFromNode(phonePlanDoc.getDocumentElement(), 
                DIGITAL_DIAL_PLAN, SYSTEM_DIR, DEFAULT_PHONE_PLAN_FILE);
      }

      Element phonePlanNode = 
        (Element) (baseDocument.importNode(phonePlanDoc.getDocumentElement(), 
              true));

      phonePlanNode.setAttribute("fileDir", SYSTEM_DIR);
      phonePlanNode.setAttribute("fileName", DEFAULT_PHONE_PLAN_FILE);
      phonePlanNode.setAttribute("fileType", 
              Integer.toString(DIGITAL_DIAL_PLAN));
      systemElement.appendChild(phonePlanNode);

      /*
       * Because the list of features keeps expanding, we
       * parse the default string for what a ListOfFeatureServers
       * should look like. Then we go through each type node in
       * default, and if it is missing in the file version we add it.
       */

      Document featureDoc = null;
      Document defaultDoc = null;

      try
      {

        // start by parsing the default string.
        StringReader reader = new StringReader(LIST_OF_FEATURE_SERVERS_XML);

        defaultDoc = parser.parse(new InputSource(reader));

        System.out.println("looking for " + SYSTEM_DIR 
                           + LIST_OF_FEATURE_SERVERS_FILE);

        reader = 
          new StringReader(AdministrativeLogin.getConnection().doGet(SYSTEM_DIR, 
                LIST_OF_FEATURE_SERVERS_FILE));

        try
        {
          featureDoc = parser.parse(new InputSource(reader));
        }
        catch (SAXException e)
        {
          System.out.println("bad XML: " + reader.toString());
          e.printStackTrace();
        }

        NodeList defaultTypeNodes = 
          defaultDoc.getElementsByTagName("serverType");
        NodeList typeNodes = featureDoc.getElementsByTagName("serverType");

        // if the two nodelists are the same length, we assume they are the same.
        // we also assume that new types get added to the end.
        if (defaultTypeNodes.getLength() > typeNodes.getLength())
        {
          System.out.println("extra serverType nodes being added to " 
                             + "ListOfFeatureServers");

          // find the element where new types get appended.
          // we start the index at length of typeNodes because we want
          // to start adding after the last element.
          for (int i = typeNodes.getLength(); 
               i < defaultTypeNodes.getLength(); i++)
          {
            Element currentType = (Element) defaultTypeNodes.item(i);
            Element featureNode = (Element) featureDoc.importNode(currentType, 
                    true);

            System.out.println("imported " + featureNode.getNodeName() 
                               + " into featureDoc");
            featureDoc.getDocumentElement().appendChild(featureNode);
          }
        }
        else
        {
          System.out.println("no extra server types to add to ListOfFeatureServers");
        }
      }
      catch (VPPNoSuchFileException e)
      {
        System.out.println("Creating new " + LIST_OF_FEATURE_SERVERS_FILE);

        featureDoc = defaultDoc;

        DOMToFile.writeFileFromNode(featureDoc.getDocumentElement(), 
                FEATURE_SERVERS, SYSTEM_DIR, LIST_OF_FEATURE_SERVERS_FILE);
      }

      System.out.println("after adding new server types, featureDoc is\n" 
                         + new XMLUtils().buildXMLString(featureDoc));

      Element featureNode = 
        (Element) (baseDocument.importNode(featureDoc.getDocumentElement(), 
              true));

      featureNode.setAttribute("fileDir", SYSTEM_DIR);
      featureNode.setAttribute("fileName", LIST_OF_FEATURE_SERVERS_FILE);
      featureNode.setAttribute("fileType", Integer.toString(FEATURE_SERVERS));
      serversElement.appendChild(featureNode);

      // fill in the feature hosts
      fillInServers((Element) featureNode, FEATURE_DIR);

      // get the list of marshal servers
      Document marshalDoc = null;

      try
      {
        System.out.println("looking for " + SYSTEM_DIR 
                           + LIST_OF_MARSHAL_SERVERS_FILE);

        StringReader reader = 
          new StringReader(AdministrativeLogin.getConnection().doGet(SYSTEM_DIR, 
                LIST_OF_MARSHAL_SERVERS_FILE));

        try
        {
          marshalDoc = parser.parse(new InputSource(reader));
        }
        catch (SAXException e)
        {
          System.out.println("bad XML: " + reader.toString());
          e.printStackTrace();
        }

      }
      catch (VPPNoSuchFileException e)
      {
        System.out.println("Creating new " + LIST_OF_MARSHAL_SERVERS_FILE);

        StringReader reader = new StringReader(LIST_OF_MARSHAL_SERVERS_XML);

        marshalDoc = parser.parse(new InputSource(reader));

        DOMToFile.writeFileFromNode(marshalDoc.getDocumentElement(), 
                MARSHAL_SERVERS, SYSTEM_DIR, LIST_OF_MARSHAL_SERVERS_FILE);
      }

      Element marshalNode = 
        (Element) (baseDocument.importNode(marshalDoc.getDocumentElement(), 
              true));

      marshalNode.setAttribute("fileDir", SYSTEM_DIR);
      marshalNode.setAttribute("fileName", LIST_OF_MARSHAL_SERVERS_FILE);
      marshalNode.setAttribute("fileType", Integer.toString(MARSHAL_SERVERS));
      serversElement.appendChild(marshalNode);

      // fill in the marshal hosts

⌨️ 快捷键说明

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