📄 datatodom.java
字号:
/* * ==================================================================== * 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 org.w3c.dom.Element;import org.w3c.dom.Document;import org.w3c.dom.DocumentFragment;import org.w3c.dom.NodeList;import org.w3c.dom.Node;import java.util.Vector;import java.util.StringTokenizer;/** * This class takes care of saving existing GUI data to the DOM tree * and to file. It also takes data from the DOM tree and gives * it to the GUI. By "existing" I mean that the data in this node * has beeen written once and is now being changed. * * This class and its sublclass(es) implement the set of interfaces * used by the GUI to read and write data. */public class DataToDOM extends DataGetSet implements DialPlanData, GlobalData{ protected ConfigTree configTree; // set to true when a server name has changed. protected boolean serverNameChanged = false; /** * * @param tree */ public DataToDOM(ConfigTree tree) { super(); configTree = tree; } // methods from HostPortData /** * */ public String getHostData() { Element element = configTree.getCurrentElement(); return element.getAttribute("host"); } /** * * @param data */ public void setHostData(String data) throws InvalidRequestException { Element element = configTree.getCurrentElement(); element.setAttribute("host", data); int type = configTree.getSelectedNodeType(); { element.setAttribute("fileName", data + ":" + element.getAttribute("port")); System.out.println("in setHostData, set fileName of " + element.getNodeName() + " to " + element.getAttribute("fileName")); } serverNameChanged = true; } /** * */ public String getPortData() { Element element = configTree.getCurrentElement(); return element.getAttribute("port"); } /** * * @param data */ public void setPortData(String data) throws InvalidRequestException { /* Element duplicate = configTree.hostPortExists(getHostData(), data); if (duplicate != null) { throw new InvalidRequestException(getHostData() + ":" + data + " already exists in " + duplicate); } */ Element element = configTree.getCurrentElement(); element.setAttribute("port", data); int type = configTree.getSelectedNodeType(); { // need to delete existing file element.setAttribute("fileName", element.getAttribute("host") + ":" + data); System.out.println("in setPortData, set fileName of " + element.getNodeName() + " to " + element.getAttribute("fileName")); } serverNameChanged = true; } // methods from SaveDeleteData /** * If this is a group with things in it, * then we cannot save a new name for it. */ public void saveData() { int nodeType = configTree.getSelectedNodeType(); Element element = configTree.getCurrentElement(); switch (nodeType) { case FEATURE_GROUP: case MARSHAL_GROUP: case CDR_GROUP: case PDP_GROUP: case REDIRECT_GROUP: case HEARTBEAT_GROUP: { if (element.hasChildNodes()) { System.out.println("cannot change group with members"); return; } configTree.saveSelectedNode(); break; } case FEATURE_SERVER: case MARSHAL_SERVER: case REDIRECT_SERVER: case CDR_SERVER: case PDP_SERVER: case HEARTBEAT_SERVER: { configTree.saveSelectedNode(); break; } case DIGITAL_DIAL_PLAN: case IP_DIAL_PLAN: { break; } case OSP_SERVER: case GLOBAL: { configTree.saveChildrenOfSelectedNode(); break; } default: { break; } } // write to file try { if (serverNameChanged) { DOMToFile.writeAllFilesInPath(configTree.getCurrentElement()); serverNameChanged = false; } else { DOMToFile.tryToWriteFile(configTree.getCurrentElement()); } } catch (FailedToWriteFileException e) { // there is nothing we can do in this case. } } /** * We can only do this if the node has no children. * @return true if the node could be deleted, false otherwise. */ public boolean deleteData() { Element element = configTree.getCurrentElement(); int elementType = configTree.getSelectedNodeType(); if ((elementType == FEATURE_GROUP) || (elementType == MARSHAL_GROUP) ) { if (!element.hasChildNodes()) { configTree.deleteSelectedNode(); return true; } } return false; } // methods from GroupData public String getGroupName() { Element element = configTree.getCurrentElement(); return element.getAttribute("value"); } public void setGroupName(String value) { Element element = configTree.getCurrentElement(); if (element.hasChildNodes()) { System.out.println("error: name change requested on group with members"); return; } element.setAttribute("value", value); } // methods from GlobalData public String getExpiryTimerData() throws InvalidRequestException { return getValue("expiryTimer"); } public void setExpiryTimerData(String data) throws InvalidRequestException { Element rootElement = configTree.getRootElement(); setValue("expiryTimer", data); } public String getMulticastHostData() throws InvalidRequestException { Element rootElement = configTree.getRootElement(); return getChildAttribute(rootElement, "multicastHost", "host"); } public void setMulticastHostData(String data) throws InvalidRequestException { Element rootElement = configTree.getRootElement(); setChildAttribute(rootElement, "multicastHost", "host", data); } public String getMulticastPortData() throws InvalidRequestException { Element rootElement = configTree.getRootElement(); return getChildAttribute(rootElement, "multicastHost", "port"); } public void setMulticastPortData(String data) throws InvalidRequestException { Element rootElement = configTree.getRootElement(); setChildAttribute(rootElement, "multicastHost", "port", data); } public String getMissedHeartbeatsData() throws InvalidRequestException { return getValue("maxMissedHeartbeats"); } public void setMissedHeartbeatsData(String data) throws InvalidRequestException { setValue("maxMissedHeartbeats", data); } public String getHeartbeatIntervalData() throws InvalidRequestException { return getValue("heartbeatInterval"); } public void setHeartbeatIntervalData(String data) throws InvalidRequestException { setValue("heartbeatInterval", data); } public String getProxyAuthKeyData() throws InvalidRequestException { return getValue("proxyAuthKey"); } public void setProxyAuthKeyData(String data) throws InvalidRequestException { Element proxyAuthKey = getElement("proxyAuthKey"); if (proxyAuthKey == null) { Element currentElement = configTree.getCurrentElement(); Document doc = currentElement.getOwnerDocument(); proxyAuthKey = doc.createElement("proxyAuthKey"); currentElement.appendChild(proxyAuthKey); } proxyAuthKey.setAttribute("value", data); } public boolean getRedirectReasonData() throws InvalidRequestException { String data = getValue("redirectReasonInSIP"); if (data.equals("true")) { return true; } return false; } public void setRedirectReasonData(boolean data) throws InvalidRequestException { Element redirectReason = getElement("redirectReasonInSIP"); if (redirectReason == null) { Element currentElement = configTree.getCurrentElement(); Document doc = currentElement.getOwnerDocument(); redirectReason = doc.createElement("redirectReasonInSIP"); currentElement.appendChild(redirectReason); } if (data) { redirectReason.setAttribute("value", "true"); } else { redirectReason.setAttribute("value", "false"); } } public String getLowestUIDData() throws InvalidRequestException { return getValue("startRange"); } public void setLowestUIDData(String data) throws InvalidRequestException { Element startRange = getElement("startRange"); if (startRange == null) { Element currentElement = configTree.getCurrentElement(); Document doc = currentElement.getOwnerDocument(); startRange = doc.createElement("startRange"); currentElement.appendChild(startRange); } startRange.setAttribute("value", data); } public String getHighestUIDData() throws InvalidRequestException { return getValue("endRange"); } public void setHighestUIDData(String data) throws InvalidRequestException { Element endRange = getElement("endRange"); if (endRange == null) { Element currentElement = configTree.getCurrentElement(); Document doc = currentElement.getOwnerDocument(); endRange = doc.createElement("endRange"); currentElement.appendChild(endRange); } endRange.setAttribute("value", data); } /** * The Dial Plan table model now stores its data in a DOM. */ public Element getDialPlanData() { Element data = configTree.getCurrentElement(); return data; } public void setDialPlanData(Element data) { // need a reference to the DOM to create elements. Document theDOM = configTree.getTreeDOM(); Element currentElement = configTree.getCurrentElement(); if (currentElement == null) { System.out.println("current element is null"); return; } configTree.replaceSelectedNode((Node)data);// System.out.println("value of data is "// + data.getAttribute("value"));// System.out.println("value of current element is "// + currentElement.getAttribute("value"));// Node parentNode = currentElement.getParentNode();// if (parentNode == null)// {// System.out.println("parent of current element is null");// return;// }// parentNode.replaceChild(data, currentElement);// // the JTree is rebuilt in the Save step. } // protected methods protected void checkValidity(String methodName) throws InvalidRequestException { // does nothing in DataToDOM } protected Element getElement(String tagName) { Element element = configTree.getCurrentElement(); if (element.getNodeName().equals(tagName)) { return element; } NodeList nodeList = element.getElementsByTagName(tagName); if (nodeList.getLength() == 0) { System.out.println("Element " + element.getTagName() + " has no child named " + tagName); return null; } Element child = (Element) (nodeList.item(0)); return child; } protected String getValue(String tagName) { Element element = getElement(tagName); if (element == null) { return ""; } return element.getAttribute("value"); } protected void setValue(String tagName, String value) { Element element = getElement(tagName); if (element == null) { return; } element.setAttribute("value", value); } protected String getChildAttribute(Element element, String childName, String attributeName) { NodeList nodeList = element.getElementsByTagName(childName); if (nodeList.getLength() < 1) { return ""; } Element child = (Element) (nodeList.item(0)); return child.getAttribute(attributeName); } protected void setChildAttribute(Element element, String childName, String attributeName, String attributeValue) { NodeList nodeList = element.getElementsByTagName(childName); Element child = (Element) (nodeList.item(0)); child.setAttribute(attributeName, attributeValue); } /** * This method is only intended to be used from the DataFragment side. */ public DocumentFragment getFragment() throws InvalidRequestException { throw new InvalidRequestException("getFragment called on existing Node in the DOM"); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -