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

📄 dataaccredit.java

📁 这是一个用java和xml编写的流媒体服务器管理软件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

      case ENV_CLIENT:
      {
        fragment = buildDocumentFragment(doc, ENV_CLIENT_STRING);

        getElement("group").setAttribute("value",
                   parent.getAttribute("value"));

        Element rootElement = getRootElement();

        rootElement.setAttribute("fileDir", ENV_DIR);
        rootElement.setAttribute("fileType", Integer.toString(type));

        break;
      }
      */
      case DVR:
      {
        fragment = buildDocumentFragment(doc, DVR_CLIENT_STRING);

        getElement("group").setAttribute("value",
                   parent.getAttribute("value"));

        Element rootElement = getRootElement();

        rootElement.setAttribute("fileDir", DVR_DIR);
        rootElement.setAttribute("fileType", Integer.toString(type));

        break;
      }
      case ENV:
      {
        fragment = buildDocumentFragment(doc, ENV_CLIENT_STRING);

        getElement("group").setAttribute("value",
                   parent.getAttribute("value"));

        Element rootElement = getRootElement();

        rootElement.setAttribute("fileDir", ENV_DIR);
        rootElement.setAttribute("fileType", Integer.toString(type));

        break;
      }
      case FEATURE_GROUP:

      case MARSHAL_GROUP:
      {
        fragment = buildDocumentFragment(doc, GROUP_STRING);

        break;
      }

      default:
      {
        System.out.println("cannot build new node of type "
                           + TreeNodeTypes.strings[type] + " ("
                           + Integer.toString(type) + ")");
        new Exception().printStackTrace();

      }
    }
  }


  // public methods

  // methods from HostPortData

  /**
   *
   */
  public String getHostData() throws InvalidRequestException
  {
    checkValidity("getHostData");

    Element rootElement = getRootElement();

    if (rootElement == null)
    {
      System.out.println("Error in getHostData: root element is null");

      return "";
    }

    return rootElement.getAttribute("host");
  }

  /**
   *
   * @param value
   *
   * @throws InvalidRequestException
   */
  public void setHostData(String value) throws InvalidRequestException
  {
    checkValidity("setHostData");

    if (configTree != null)
    {
      Element duplicate = configTree.hostPortExists(value, getPortData());
      if (duplicate != null)
      {
        JOptionPane.showMessageDialog(new JFrame(),
        value + ":" + getPortData() + " already exists", "Duplicate host:port",
        JOptionPane.ERROR_MESSAGE);
        throw new InvalidRequestException(value + ":" + getPortData()
        + " already exists in " + duplicate);
      }
    }
    getRootElement().setAttribute("host", value);
  }

  /**
   *
   */
  public String getPortData() throws InvalidRequestException
  {
    checkValidity("getPortData");
    return getRootElement().getAttribute("port");
  }

  /**
   *
   */
  public void setPortData(String value) throws InvalidRequestException
  {
    checkValidity("setPortData");

    if (configTree != null)
    {
      Element duplicate = configTree.hostPortExists(getHostData(), value);
      if (duplicate != null)
      {
        JOptionPane.showMessageDialog(new JFrame(),
        getHostData() + ":" + value + " already exists", "Duplicate host:port",
        JOptionPane.ERROR_MESSAGE);
        throw new InvalidRequestException(getHostData() + ":" + value
        + " already exists in " + duplicate);
      }
    }
    getRootElement().setAttribute("port", value);
  }
/*
  public String getHostNameData() throws InvalidRequestException
  {
    checkValidity("getHostNameData");

    Element rootElement = getRootElement();

    if (rootElement == null)
    {
      System.out.println("Error in getHostData: root element is null");

      return "";
    }

    return rootElement.getAttribute("hostname");
  }
*/
  /**
   *
   * @param value
   *
   * @throws InvalidRequestException
   */
  /*
  public void setHostNameData(String value) throws InvalidRequestException
  {
    checkValidity("setHostNameData");
    
    getRootElement().setAttribute("hostname", value);
  }
*/
  /**
   *
   */
  public DocumentFragment getFragment() throws InvalidRequestException
  {
    isValid = false;

    return fragment;
  }

  /**
   *
   * @throws InvalidRequestException
   */
  public void saveData() throws InvalidRequestException
  {
    if (!isValid)
    {
      throw new InvalidRequestException("saveData on saved or deleted DataFragment");
    }

    System.out.println("saveData called on DataFragment");

    Element rootElement = getRootElement();

    System.out.println(new XMLUtils().buildXMLString(rootElement));
    System.out.println("Root element is " + rootElement.getNodeName());

    String dir = rootElement.getAttribute("fileDir");    
    String host = null;
    String port = null;

    if (!dir.equals(""))
    {
      System.out.println("dir is not empty");

      host = rootElement.getAttribute("host");
      port = rootElement.getAttribute("port");

      rootElement.setAttribute("fileName", host + ":" + port);
    }
    else
    {
      System.out.println("dir is empty");
    }

    if (configTree != null) // null if called by DeployServers
    {
      configTree.insertDocumentFragment(fragment, parentTreeNode);
    }

    // if this is a Call Return feature, then we need to generate some
    // cpl at this point.
    if (((Element) grandparentNode).getAttribute("value").equals("CallReturn"))
    {
      String fileDir = (FEATURE_DIR + "_" + host + "_" + port + "_Called");
      String xmlString = CplGenerator.buildCallReturnServerXML();

      // write the cpl script to file.
      try
      {
        DOMToFile.writeAsIs(xmlString, fileDir, "*69@vovida.com.cpl");
      }
      catch (FailedToWriteFileException e1)
      {

        // nothing much we can do here.
      }
    }

    if (configTree != null) // null if called by DeployServers
    {
      try
      {
        DOMToFile.writeAllFilesInPath(configTree.getCurrentElement());
      }
      catch (FailedToWriteFileException e2)
      {
        configTree.deleteData();
      }
    }

    isValid = false;
  }

  /**
   *
   */
  public boolean deleteData() throws InvalidRequestException
  {
    if (!isValid)
    {
      throw new InvalidRequestException("deleteData on saved or deleted DataFragment");
    }

    // do nothing because the data fragment was never put in the tree
    isValid = false;

    return true;
  }

  // methods from GroupData

  /**
   *
   */
  public String getGroupName() throws InvalidRequestException
  {
    checkValidity("getGroupName");

    return getRootElement().getAttribute("value");
  }

  /**
   *
   * @param data
   *
   * @throws InvalidRequestException
   */
  public void setGroupName(String data) throws InvalidRequestException
  {
    checkValidity("setGroupName");
    getRootElement().setAttribute("value", data);
  }


  /**
   *
   * @param methodName
   *
   * @throws InvalidRequestException
   */
  protected void checkValidity(String methodName)
    throws InvalidRequestException
  {
    if (!isValid)
    {
      throw new InvalidRequestException(methodName
              + " on saved or deleted DataFragment");
    }
  }

  /**
   *
   */
  protected Element getRootElement()
  {
    Node rootNode = fragment.getFirstChild();

    if (rootNode == null)
    {
      System.out.println("Error: Data Fragment root element is null");
      new Exception().printStackTrace();

    }

    return (Element) rootNode;
  }

  /**
   *
   */
  protected Element getElement(String elementName)
  {
    Element rootElement = getRootElement();

    if (rootElement.getNodeName().equals(elementName))
    {
      return rootElement;
    }

    NodeList nodes = rootElement.getElementsByTagName(elementName);
    Element requestedElement = null;

    if (nodes.getLength() < 1)
    {
      System.out.println("fragment does not contain element " + elementName);
      System.out.println(new XMLUtils().buildXMLString(fragment));

      return null;
    }

    try
    {
      requestedElement = (Element) (nodes.item(0));
    }
    catch (ClassCastException e)
    {
      System.out.println(elementName + " is not an Element: "
                         + nodes.item(0).getNodeType());
      e.printStackTrace();

    }

    return requestedElement;
  }

  /**
   *
   */
  protected String getValue(String tagName)
  {
    Element element = getElement(tagName);

    if (element == null)
    {
      return null;
    }

    return element.getAttribute("value");
  }

  /**
   *
   * @param tagName
   * @param data
   */
  protected void setValue(String tagName, String data)
  {
    Element element = getElement(tagName);

    if (element == null)
    {
      return;
    }

    element.setAttribute("value", data);
  }

  /**
   * Build a Document Fragment from a string representation of the XML.
   */
  protected DocumentFragment buildDocumentFragment(final Document doc,
          String fragString)
  {
    System.out.println("building document fragment: " + fragString);

    final DocumentFragment frag = doc.createDocumentFragment();

    // read the string in with a SAX parser to build the fragment
    StringReader fragReader = new StringReader(fragString);

    SAXParserFactory spf = SAXParserFactory.newInstance();
    SAXParser parser = null;
    try
    {
      parser = spf.newSAXParser();
    }
    catch (ParserConfigurationException parsEx)
    {
      parsEx.printStackTrace();
      return null;
    }
    catch(SAXException saxEx)
    {
      saxEx.printStackTrace();
      return null;
    }
    DefaultHandler handler = new DefaultHandler()
    {
      Stack elementStack = new Stack();

      /**
       *
       * @param ignore1
       * @param ignore2
       * @param rawName
       * @param attrs
       */
      public void startElement(String ignore1, String ignore2,
              String rawName, Attributes attrs)
      {
        if (rawName.equals("fragment"))
        {

          // the fragment root element is necessary for the
          // SAX parser to work.
          return;
        }

        int attrsLength = attrs.getLength();

        Element newElement = doc.createElement(rawName);

        for (int i = 0; i < attrsLength; i++)
        {
          newElement.setAttribute(attrs.getQName(i), attrs.getValue(i));
        }

        if (!elementStack.isEmpty())
        {
          Element lastElement = (Element) (elementStack.peek());

          lastElement.appendChild(newElement);
        }

        elementStack.push(newElement);
      }

      /**
       *
       * @param ignore1
       * @param ignore2
       * @param rawName
       */
      public void endElement(String ignore1, String ignore2, String rawName)
      {
        if (!rawName.equals("fragment"))
        {
          Element lastElement = (Element) (elementStack.pop());

          if (elementStack.isEmpty())
          {
            frag.appendChild(lastElement);
          }
        }
      }

    };

    try
    {
      parser.parse(new InputSource(fragReader), handler);
    }
    catch (Exception e)
    {
      e.printStackTrace();

    }

    return frag;
  }



}



⌨️ 快捷键说明

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