/*
* Copyright (C) 2006 http://www.chaidb.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
package org.chaidb.db.helper;
import org.apache.xerces.parsers.DOMParser;
import org.chaidb.db.exception.ChaiDBException;
import org.chaidb.db.exception.ErrorCode;
import org.w3c.dom.*;
import org.xml.sax.InputSource;
import java.io.*;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Vector;
/**
* @author Kurt Sung
*/
public class ConfigTool {
private Hashtable attributes;
private String name;
private String value;
private ElementSet children;
private static ConfigTool root;
// private final String UNSPPORT_ERROR="System can't support this initlization!";
private static boolean isOutput = false;
private final static String PATH_TYPE_WRONG = "WRONG";
private final static String PATH_TYPE_ROOT = "ROOT";
private final static String PATH_TYPE_NORMAL = "NORMAL";
private final static String ELEMENT_ID = "name";
private final static String NO_SUNCH_ELEMENT = "no such element was found!";
private static String tab = "";
private ConfigTool() {
this(null, null, null, null);
}
private ConfigTool(String name, Hashtable attributes, String value, ElementSet children) {
this.attributes = attributes;
this.name = name;
this.value = value;
this.children = children;
}
public static ConfigTool getRootElement() {
return root;
}
public Hashtable getAttributes() {
return this.attributes;
}
public String getName() {
return this.name;
}
public ElementSet getChildren() {
return this.children;
}
public String getValue() {
return this.value;
}
public void setAttributes(Hashtable attributes) {
this.attributes = attributes;
}
public void setName(String name) {
this.name = name;
}
public void setChildren(ElementSet children) {
this.children = children;
}
public void setValue(String value) {
this.value = value;
}
/**
*
*/
public static void init() {
root = new ConfigTool();
}
/**
* @param name
* @param attributes
* @param value
* @param childs
*/
public static void init(String name, Hashtable attributes, String value, ElementSet childs) {
root = new ConfigTool(name, attributes, value, childs);
}
/**
* @param filename config file path and name
*/
public static void load(String filename) throws ChaiDBException {
File configFile = new File(filename);
load(configFile);
}
/**
* @param directory config file parent directory path
* @param filename cofnig file name
*/
public static void load(String directory, String filename) throws ChaiDBException {
File configFile = new File(directory, filename);
load(configFile);
}
/**
* @param directory config file parent directory
* @param filename cofnig file name
*/
public static void load(File directory, String filename) throws ChaiDBException {
File configFile = new File(directory, filename);
load(configFile);
}
// /**
// *
// * @param configFile
// */
// public static void load(File configFile){
//
// //check whether file exists
// if(!configFile.exists()){
// log("The Configuration file " + configFile.getAbsolutePath() +
// " was not found!");
// return ;
// }
//
// root = new ConfigTool();
// try{
// DOMBuilder builder = new DOMBuilder();
// Document document = builder.build(configFile);
// Element rootElement = document.getRootElement();
//
// root = paseXML2Element(rootElement);
//
// }catch(Exception ex){
// ex.printStackTrace();
// }
// }
/**
* @param configFile cofig file
* @throws ChaiDBException
*/
public static void load(File configFile) throws ChaiDBException {
//check whether file exists
if (!configFile.exists()) {
throw new ChaiDBException(ErrorCode.PARSE_ERROR, "Config File does not exists!");
}
root = new ConfigTool();
FileInputStream fis;
try {
fis = new FileInputStream(configFile);
} catch (FileNotFoundException e) {
throw new ChaiDBException(ErrorCode.PARSE_ERROR, "Config File does not exists!");
}
load(fis);
}
public static void load(InputStream is) throws ChaiDBException {
try {
InputSource inputSource = new InputSource(is);
DOMParser parser = new DOMParser();
parser.parse(inputSource);
Document document = parser.getDocument();
if (document == null) {
throw new ChaiDBException(ErrorCode.PARSE_ERROR, "Config File is not well-formed!");
}
Element rootElement = document.getDocumentElement();
root = paseXML2Element(rootElement);
} catch (Exception ex) {
throw new ChaiDBException(ErrorCode.PARSE_ERROR, "Config File is not well-formed!");
} finally {
try {
is.close();
} catch (IOException e) {
throw new ChaiDBException(ErrorCode.PARSE_ERROR, "Close config file IO error!");
}
}
}
/**
* parase xml format to ConfigTool format
*
* @param XMLChild xmld node
*/
private static ConfigTool paseXML2Element(Node XMLChild) {
ConfigTool element = new ConfigTool();
//set name
log(XMLChild.getNodeName());
element.setName(XMLChild.getNodeName());
//set attributes
Hashtable attributes = new Hashtable();
if (XMLChild.hasAttributes()) {
NamedNodeMap XMLAttributes = XMLChild.getAttributes();
for (int i = 0; i < XMLAttributes.getLength(); i++) {
Node attribute = XMLAttributes.item(i);
log(attribute.getNodeName() + "is : " + attribute.getNodeValue());
attributes.put(attribute.getNodeName(), attribute.getNodeValue());
}
}
element.setAttributes(attributes);
//set text
NodeList children = XMLChild.getChildNodes();
for (int j = 0; j < children.getLength(); j++) {
if (children.item(j).getNodeType() == Node.TEXT_NODE) {
log("text is:" + children.item(j).getNodeValue());
element.setValue(children.item(j).getNodeValue());
break;
}
}
//set children
NodeList grandChildren = XMLChild.getChildNodes();
if (grandChildren != null) {
if (grandChildren.getLength() > 0) {
element.setChildren(parseXML2Children(grandChildren));
}
}
return element;
}
// /**
// *
// * @param XMLChild
// * @return
// */
// private static ConfigTool paseXML2Element(Element XMLChild){
//
// ConfigTool element = new ConfigTool();
// //set name
// element.setName(XMLChild.getName());
// //set attributes
// Hashtable attributes = new Hashtable();
// List XMLAttributes = XMLChild.getAttributes();
// for(int i=0; i<XMLAttributes.size(); i++){
// Attribute attribute = (Attribute)XMLAttributes.get(i);
// attributes.put(attribute.getName(),attribute.getValue());
// }
// element.setAttributes(attributes);
// //set text
// if(XMLChild.getText() != null){
// element.setValue(XMLChild.getTextTrim());
// }
// //set children
// List grandChildren = XMLChild.getChildren();
// if( grandChildren != null){
// if(grandChildren.size()>0){
// element.setChildren(parseXML2Children(grandChildren));
// }
// }
// return element;
//
// }
//
/**
* parse xml format to ElementSet formaent
*
* @param XMLChildren node list
*/
private static ElementSet parseXML2Children(NodeList XMLChildren) {
ElementSet children = new ElementSet();
for (int i = 0; i < XMLChildren.getLength(); i++) {
Node XMLChild = XMLChildren.item(i);
if (XMLChild.getNodeType() != Node.TEXT_NODE) children.add(paseXML2Element(XMLChild));
}
return children;
}
// /**
// *
// * @param XMLChildren
// * @return
// */
// private static ElementSet parseXML2Children(List XMLChildren){
//
// ElementSet children = new ElementSet();
// for(int i=0; i<XMLChildren.size(); i++){
// Element XMLChild = (Element)XMLChildren.get(i);
// children.add(paseXML2Element(XMLChild));
// }
// return children;
//
// }
// /**
// *
// * @param configFile
// */
// public static void save(File configFile){
//
// //check whether file exists
// if(!configFile.exists()){
// log("The Configuration file " + configFile.getAbsolutePath() +
// " was not found! new file has been created! ");
// try{
// configFile.createNewFile();
// }catch(Exception ex){
// ex.printStackTrace();
// }
// }
//
// try{
// FileWriter configfw = new FileWriter(configFile);
// Element rootElement = parseElement2XML(root);
// Document document = new Document(rootElement);
// XMLOutputter xmlOutput = new XMLOutputter();
// xmlOutput.output(document,configfw);
//
// }catch(Exception ex){
// ex.printStackTrace();
// }
//
// }
/**
* parse ConfigTool format to xml format
*
* @param element ConfigTool
*/
private static String parseElement2XML(ConfigTool element) {
StringBuffer content = new StringBuffer();
String currentTab = tab;
tab += " ";
//name
content.append(currentTab).append("<").append(element.getName()).append(" ");
//attributes
if (element.getAttributes() != null) {
for (Enumeration attributeNames = element.getAttributes().keys(); attributeNames.hasMoreElements();) {
String attributeName = (String) attributeNames.nextElement();
String attributeValue = (String) element.getAttributes().get(attributeName);
content.append(attributeName).append("=\"").append(attributeValue).append("\" ");
}
}
content.append(">\n");
//value
if (element.getValue() != null && !element.getValue().trim().equals("")) {
content.append(currentTab).append(element.getValue().trim()).append("\n");
}
//children
ElementSet children = element.getChildren();
if (children != null) {
content.append(parseChildren2XML(children));
}
tab = tab.substring(0, tab.length() - 4);
content.append(currentTab).append("</").append(element.getName()).append(">\n");
return content.toString();
}
// /**
// *
// * @param element
// * @return
// */
// private static Element parseElement2XML(ConfigTool element){
// Element XMLChild = null;
// //set XMLChild name
// XMLChild = new Element(element.getName());
// //set XMLChild attributes
// Hashtable attributes = element.getAttributes();
// List XMLAttributes = new Vector();
// for (Enumeration keys = attributes.keys(); keys.hasMoreElements() ;){
// String key = (String)keys.nextElement();
// Attribute XMLAttribute = new Attribute(key,(String)attributes.get(key));
// XMLAttributes.add(XMLAttribute);
// }
// XMLChild.setAttributes(XMLAttributes);
// //set XMLChild text
// if(element.getValue() != null){
// XMLChild.setText(element.getValue());
// }
// //set XMLChild children
// ElementSet children = element.getChildren();
// if(children != null ){
// XMLChild.setChildren(parseChildren2XML(children));
// }
// return XMLChild;
// }
/**
* parse elementSet format to xml format
*
* @param children
*/
private static String parseChildren2XML(ElementSet children) {
StringBuffer childContent = new StringBuffer();
for (Iterator childrenIter = children.iterator(); childrenIter.hasNext();) {
ConfigTool child = (ConfigTool) childrenIter.next();
childContent.append(parseElement2XML(child));
}
return childContent.toString();
}
// /**
// *
// * @param children
// * @return
// */
// private static List parseChildren2XML(ElementSet children){
// List XMLChildren = new Vector();
//
// for (Iterator childrenIter = children.iterator(); childrenIter.hasNext();){
// ConfigTool child = (ConfigTool)childrenIter.next();
// XMLChildren.add(parseElement2XML(child));
// }
// return XMLChildren;
// }
/**
* find element under current using element name.
* if current is null then null eas returned.
* if current has no sub (include recurise) element
* named elemenname then null was returned too.
*
* @param current current ConfigTool
* @param elementName
*/
private static ConfigTool findElement(ConfigTool current, String elementName) {
ConfigTool requriedElement = null;
if (current == null) return null;
if (current.getName().equalsIgnoreCase(elementName)) {
return current;
}
if (current.getChildren() == null || current.getChildren().size() == 0) {
return null;
}
Iterator iterater = current.getChildren().iterator();
while (iterater.hasNext()) {
ConfigTool nextElement = (ConfigTool) iterater.next();
requriedElement = findElement(nextElement, elementName);
if (requriedElement != null) {
break;
}
}
return requriedElement;
}
/**
* find element under root element
*
* @param elementName
*/
public static ConfigTool findElement(String elementName) {
return findElement(root, elementName);
}
/**
* find element under current using both element name
* and attributes.
* if current is null then null eas returned.
* if current has the sub (include recurise) element
* named elemenname and satify the finding condition-attriutes
* then the element was returned .
*/
private static ConfigTool findElement(ConfigTool current, String elementName, Hashtable attributes) {
ConfigTool requriedElement = null;
if (current == null) return null;
if (current.getName().equalsIgnoreCase(elementName)) {
Enumeration e = current.getAttributes().keys();
while (e.hasMoreElements()) {
String attributeName = (String) e.nextElement();
String attributeValue = (String) attributes.get(attributeName);
if (current.getAttributeValue(attributeName).equalsIgnoreCase(attributeValue)) return current;
}
}
if (current.getChildren() == null || current.getChildren().size() == 0) {
return null;
}
Iterator iterater = current.getChildren().iterator();
while (iterater.hasNext()) {
ConfigTool nextElement = (ConfigTool) iterater.next();
requriedElement = findElement(nextElement, elementName, attributes);
if (requriedElement != null) {
break;
}
}
return requriedElement;
}
/**
* find element under root using element name and
* attributes
*/
public static ConfigTool findElement(String elementName, Hashtable attributes) {
return ConfigTool.findElement(root, elementName, attributes);
}
/**
* find element under current using both element name
* and attributes.
* if current is null then null eas returned.
* if current has the sub (include recurise) element
* named elemenname and satify the finding condition-attriutes
* then the element was returned .
*/
public static ConfigTool findElement(String elementName, String attributeName, String attributeValue) throws ChaiDBException {
Hashtable attributes = new Hashtable();
try {
attributes.put(attributeName, attributeValue);
} catch (Exception ex) {
throw new ChaiDBException(ErrorCode.INVALID_INPUT_PARAM, "null can't be used as input!");
}
return findElement(root, elementName, attributes);
}
/**
* find element under root
*
* @param path
* @return
* @throws ChaiDBException
*/
public static ConfigTool findElementByPath(String path) throws ChaiDBException {
return findElementByPath(path, null);
}
/**
* find element by path and element ID
*
* @param path "/" or "//" mean root elemement
* for example "/view/driver"
* @param elementID if there are many element with the same path
* then elementID was used to identify them.
* if it is null, then the first element found
* using the path was returned.
*/
public static ConfigTool findElementByPath(String path, String elementID) throws ChaiDBException {
Vector parasedPath = parasePath(path);
String pathType = (String) parasedPath.elementAt(0);
if (pathType.equals(ConfigTool.PATH_TYPE_WRONG))
throw new ChaiDBException(ErrorCode.PARSE_ERROR, "Path is wrong!");
if (pathType.equals(ConfigTool.PATH_TYPE_ROOT)) return root;
if (pathType.equals(ConfigTool.PATH_TYPE_NORMAL)) {
ConfigTool parent = root;
for (int i = 1; i < parasedPath.size(); i++) {
String elementName = (String) parasedPath.elementAt(i);
ElementSet elementSet = parent.getChildren();
if (elementSet == null) break;
ConfigTool child;
for (Iterator childrenIter = elementSet.iterator(); childrenIter.hasNext();) {
child = (ConfigTool) childrenIter.next();
if (child.getName() != null && elementName.equals(child.getName())) {
if (i == parasedPath.size() - 1) {
if (elementID == null) {
return child;
}
if (child.getAttributes() != null && child.getAttributes().get(ELEMENT_ID) != null && ((String) child.getAttributes().get(ELEMENT_ID)).equalsIgnoreCase(elementID)) {
return child;
}
} else {
parent = child;
break;
}
}
}
}
}
throw new ChaiDBException(ErrorCode.INVALID_INPUT_PARAM, "No such Config was found! Input Param:path=" + path + "elementId=" + elementID);
}
/**
* parse path string to a vector,the first level
* path will be added into the first location of the
* vector.the rest may be deduced by analogy .
*/
private static Vector parasePath(String path) throws ChaiDBException {
if (path == null) {
throw new ChaiDBException(ErrorCode.INVALID_INPUT_PARAM, "null can't used as path!");
}
Vector parasedPath = new Vector();
final String SEPARATOR = "/";
final char SEPARATOR_CHAR = '/';
// path must start with /
if (!path.startsWith(SEPARATOR)) {
parasedPath.add(ConfigTool.PATH_TYPE_WRONG);
return parasedPath;
}
// root using // or /
if (path.length() == 1 || (path.length() == 2 && path.charAt(1) == SEPARATOR_CHAR)) {
parasedPath.add(ConfigTool.PATH_TYPE_ROOT);
return parasedPath;
}
parasedPath.add(ConfigTool.PATH_TYPE_NORMAL);
//add / to the end
if (!path.endsWith(SEPARATOR)) {
path += SEPARATOR;
}
int startIndex = 1;
int endIndex;
while ((endIndex = path.indexOf(SEPARATOR, startIndex)) > 0) {
String elementName = path.substring(startIndex, endIndex);
if (!elementName.trim().equals("")) {
parasedPath.add(elementName);
}
startIndex = endIndex + 1;
}
return parasedPath;
}
public String getAttributeValue(String attributeName) {
if (attributes.containsKey(attributeName)) return (String) attributes.get(attributeName);
log("no such attribute was found!");
return null;
}
public static String getElementAttributeValue(ConfigTool element, String attributeName) {
return element.getAttributeValue(attributeName);
}
/**
* @param path used to find element
* @param elementID used to find element
* @param attributeName
* @return
* @see ConfigTool:findelementbypath(String path,String elementID)
*/
public static String getElementAttributeValue(String path, String elementID, String attributeName) throws ChaiDBException {
ConfigTool element = findElementByPath(path, elementID);
if (element == null) {
log(ConfigTool.NO_SUNCH_ELEMENT);
return null;
}
return element.getAttributeValue(attributeName);
}
public static String getElementValue(ConfigTool element) {
return element.getValue();
}
/**
* get the element value which was found by path and elementID
*
* @param path
* @see ConfigTool:findelementbypath(String path,String elementID)
*/
public static String getElementValue(String path, String elementID) throws ChaiDBException {
ConfigTool element = findElementByPath(path, elementID);
if (element == null) {
log(ConfigTool.NO_SUNCH_ELEMENT);
return null;
}
return getElementValue(element);
}
/**
* modify value of element child's element named elementName
*
* @param elementName
* @param elementValue
*/
public void setChildElementValue(String elementName, String elementValue) {
for (Iterator i = children.iterator(); i.hasNext();) {
ConfigTool child = (ConfigTool) i.next();
String childName = child.getName();
if (childName != null && childName.equalsIgnoreCase(elementName)) {
child.setValue(elementValue);
break;
}
}
}
public static Hashtable getElementAllAttributes(ConfigTool element) {
return element.getAttributes();
}
/**
* get all attibutes of element found by path an delementID
*
* @param path
* @param elementID
* @return
* @see ConfigTool:findelementbypath(String path,String elementID)
*/
public static Hashtable getElementAllAttributes(String path, String elementID) throws ChaiDBException {
ConfigTool element = ConfigTool.findElementByPath(path, elementID);
if (element == null) {
log(ConfigTool.NO_SUNCH_ELEMENT);
return null;
}
return element.getAttributes();
}
/**
* get names of all attribute
*/
public Enumeration getAllAttributeNames() {
return attributes.keys();
}
/**
* get names of attributes of element
*/
public static Enumeration getElementAllAttributeNames(ConfigTool element) {
return element.getAllAttributeNames();
}
/**
* get the element's attributes name
*
* @see :findelementbypath(String path,String elementID)
*/
public static Enumeration getElementAllAttributeNames(String path, String elementID) throws ChaiDBException {
ConfigTool element = ConfigTool.findElementByPath(path, elementID);
if (element == null) {
log(ConfigTool.NO_SUNCH_ELEMENT);
return null;
}
return element.getAllAttributeNames();
}
/**
* get all name and ID of child element filted by filter
*
* @return hashtable name-vetor the vector include the elementID having same name
*/
public Hashtable getChildAllElementNamesAndID(Hashtable filter) {
Hashtable nameAndID = new Hashtable();
Vector IDs;
out:
for (Iterator i = children.iterator(); i.hasNext();) {
ConfigTool child = (ConfigTool) i.next();
String name = child.getName();
if (filter != null) {
for (Enumeration filterEu = filter.keys(); filterEu.hasMoreElements();) {
String attributeName = (String) filterEu.nextElement();
String attributeValue = (String) filter.get(attributeName);
String childAttribute = child.getAttributeValue(attributeName);
if (!attributeValue.equals(childAttribute) && childAttribute != null) continue out;
}
}
if (nameAndID.containsKey(name)) {
IDs = (Vector) nameAndID.get(name);
IDs.add(child.getAttributeValue(ConfigTool.ELEMENT_ID));
nameAndID.remove(name);
} else {
IDs = new Vector();
IDs.add(child.getAttributeValue(ConfigTool.ELEMENT_ID));
}
nameAndID.put(name, IDs);
}
return nameAndID;
}
/**
* get the element's attributes name
*
* @see :findelementbypath(String path,String elementID)
*/
public static Hashtable getElementChildAllElementNamesAndID(ConfigTool element) {
return element.getChildAllElementNamesAndID(null);
}
/**
* get the element's attributes name
*
* @see :findelementbypath(String path,String elementID)
*/
public static Hashtable getElementChildAllElementNamesAndID(String path, String elementID) throws ChaiDBException {
ConfigTool element = ConfigTool.findElementByPath(path, elementID);
if (element == null) {
log(ConfigTool.NO_SUNCH_ELEMENT);
return null;
}
return element.getChildAllElementNamesAndID(null);
}
/**
* get names of all the curren element's child elements
*/
public Enumeration getChildAllElementNames() {
//no dulipute name
return getChildAllElementNamesAndID(null).keys();
}
/**
* get names of all the child elements filted by filter
*/
public Enumeration getChildAllElementNames(Hashtable filter) {
return getChildAllElementNamesAndID(filter).keys();
}
/**
* get names of all the element's child elements
*/
public static Enumeration getElementChildAllElementNames(ConfigTool element) {
return element.getChildAllElementNames();
}
/**
* get names of all the element's child elements filted by filter
*/
public static Enumeration getElementChildAllElementNames(ConfigTool element, Hashtable filter) {
return element.getChildAllElementNames(filter);
}
/**
* get names of all the element child elements, the element was
* found by path and elementID
*
* @param path
* @param elementID
* @return
* @see :findelementbypath(String path,String elementID)
*/
public static Enumeration getElementChildAllElementNames(String path, String elementID) throws ChaiDBException {
return getElementChildAllElementNames(path, elementID, null);
}
/**
* get names of all the element child elements filtered by filter,
* the element was found by path and elementID
*
* @see :findelementbypath(String path,String elementID)
*/
public static Enumeration getElementChildAllElementNames(String path, String elementID, Hashtable filter) throws ChaiDBException {
ConfigTool element = ConfigTool.findElementByPath(path, elementID);
if (element == null) {
log(ConfigTool.NO_SUNCH_ELEMENT);
return null;
}
return element.getChildAllElementNames(filter);
}
/**
* get the element id of current element's child element named element name
*
* @param elementName
* @return vectort whose element is string
*/
public Enumeration getChildElementIDByChildElementName(String elementName) throws ChaiDBException {
if (elementName == null) {
throw new ChaiDBException(ErrorCode.INVALID_INPUT_PARAM, "null can't used as input!");
}
Hashtable nameAndID = getChildAllElementNamesAndID(null);
Vector IDs = (Vector) nameAndID.get(elementName);
if (IDs == null) {
// throw new ChaiDBException(ErrorCode.INVALID_INPUT_PARAM,
// elementName + " has not child!");
return new Vector().elements();
} else {
return IDs.elements();
}
}
/**
* get the element id of element's child element named element name
*
* @see :findelementbypath(String path,String elementID)
*/
public static Enumeration getElementChildElementIDByChildElementName(ConfigTool element, String childelEmentName) throws ChaiDBException {
return element.getChildElementIDByChildElementName(childelEmentName);
}
/**
* get the element id of element's child element named element name
* the element was found by path and elementname
*
* @see :findelementbypath(String path,String elementID)
*/
public static Enumeration getElementChildElementIDByChildElementName(String path, String elementName, String childelEmentName) throws ChaiDBException {
ConfigTool element = ConfigTool.findElementByPath(path, elementName);
if (element == null) {
log(ConfigTool.NO_SUNCH_ELEMENT);
return null;
}
return element.getChildElementIDByChildElementName(childelEmentName);
}
public boolean setAttributeValue(String attributeName, String attributeNewValue) throws ChaiDBException {
if (attributes.containsKey(attributeName)) {
attributes.remove(attributeName);
try {
attributes.put(attributeName, attributeNewValue);
} catch (Exception ex) {
throw new ChaiDBException(ErrorCode.INVALID_INPUT_PARAM, "attribute name or attribute" + " value should not be null");
}
return true;
}
throw new ChaiDBException(ErrorCode.INVALID_INPUT_PARAM, "the attribute " + attributeName + " has not existed!");
}
public static boolean setElementAttributeValue(ConfigTool element, String attributeName, String attributeNewValue) throws ChaiDBException {
return element.setAttributeValue(attributeName, attributeNewValue);
}
public static boolean setElementAttributeValue(String path, String elementID, String attributeName, String attributeNewValue) throws ChaiDBException {
ConfigTool element = ConfigTool.findElementByPath(path, elementID);
if (element == null) {
log(ConfigTool.NO_SUNCH_ELEMENT);
return false;
}
return element.setAttributeValue(attributeName, attributeNewValue);
}
/**
* @param element
* @param elementValue
*/
public static void setElementValue(ConfigTool element, String elementValue) {
element.setValue(elementValue);
}
public static void setElementValue(String path, String elementID, String elementValue) throws ChaiDBException {
ConfigTool element = ConfigTool.findElementByPath(path, elementID);
if (element == null) {
log(ConfigTool.NO_SUNCH_ELEMENT);
return;
}
element.setValue(elementValue);
}
public static boolean containElement(String path, String elementID) {
try {
return (ConfigTool.findElementByPath(path, elementID) != null);
} catch (ChaiDBException ie) {
return false;
}
}
public static boolean containChildElement(String path, String elementID, String childElement, String childElementID) {
try {
ConfigTool parent = findElementByPath(path, elementID);
if (parent != null) {
ElementSet children = parent.getChildren();
if (children == null) {
return false;
}
for (Iterator i = children.iterator(); i.hasNext();) {
ConfigTool child = (ConfigTool) i.next();
if (child.getName() != null && child.getName().equalsIgnoreCase(childElement) && child.getAttributeValue(ELEMENT_ID) != null && child.getAttributeValue(ELEMENT_ID).equalsIgnoreCase(childElementID)) {
return true;
}
}
}
} catch (ChaiDBException ie) {
ie.printStackTrace();
return false;
}
return false;
}
/**
* @param elementName
* @param elementID
* @param cannotRemoveFilter
* @return
* @throws ChaiDBException
*/
public boolean removeChildElement(String elementName, String elementID, Hashtable cannotRemoveFilter) throws ChaiDBException {
for (Iterator i = children.iterator(); i.hasNext();) {
ConfigTool child = (ConfigTool) i.next();
if (child.getName() != null && child.getName().equalsIgnoreCase(elementName)) {
if (elementID == null || (child.getAttributeValue(ConfigTool.ELEMENT_ID) != null && child.getAttributeValue(ConfigTool.ELEMENT_ID).equalsIgnoreCase(elementID))) {
if (cannotRemoveFilter == null) {
children.remove(child);
return true;
} else {
boolean canRemoved = false;
for (Enumeration e = cannotRemoveFilter.keys(); e.hasMoreElements();) {
String filterAttributeName = (String) e.nextElement();
String filterAttributeValue = (String) cannotRemoveFilter.get(filterAttributeName);
if (child.getAttributeValue(filterAttributeName) == null || !child.getAttributeValue(filterAttributeName).
equalsIgnoreCase(filterAttributeValue)) {
canRemoved = true;
break;
}
}
if (canRemoved) {
children.remove(child);
return true;
} else {
throw new ChaiDBException(ErrorCode.INVALID_INPUT_PARAM, "the " + elementName + " can't be removed!");
}
}
}
}
}
throw new ChaiDBException(ErrorCode.INVALID_INPUT_PARAM, "the config " + elementName + " has not exist!");
}
public boolean removeChildElement(String elementName, String elementID) throws ChaiDBException {
Hashtable filterCannotRemoved = null;
return removeChildElement(elementName, elementID, filterCannotRemoved);
}
/**
* remove element's child element named elementName and its
* element ID is elementID
*/
public static boolean removeElementChildElement(ConfigTool element, String childElementName, String childElementID) throws ChaiDBException {
return element.removeChildElement(childElementName, childElementID);
}
/**
* remove element's child element named elementName and its element
* ID is elementID, the element was found by path and elementID
*
* @see :findelementbypath(String path,String elementID)
*/
public static boolean removeElementChildElement(String path, String elementID, String childElementName, String childElementID) throws ChaiDBException {
Hashtable filterCannotRemoved = null;
return removeElementChildElement(path, elementID, childElementName, childElementID, filterCannotRemoved);
}
/**
* remove element's child element named elementName and its element
* ID is elementID and it does not satify the filter, the element
* was found by path and elementID
*
* @throws ChaiDBException
* @see :findelementbypath(String path,String elementID)
*/
public static boolean removeElementChildElement(String path, String elementID, String childElementName, String childElementID, Hashtable filterCannotRemoved) throws ChaiDBException {
ConfigTool element = ConfigTool.findElementByPath(path, elementID);
if (element == null) {
throw new ChaiDBException(ErrorCode.INVALID_INPUT_PARAM, "the config " + elementID + "has not exist!");
}
return element.removeChildElement(childElementName, childElementID, filterCannotRemoved);
}
/**
* remove all child elements of current element
*/
public void removeAllChildElements() throws ChaiDBException {
removeAllChildElements(null);
}
/**
* remove all child elements of current element if it does not
* satify the filter condition
*
* @param filterCannotRemoved
* @throws ChaiDBException
*/
public void removeAllChildElements(Hashtable filterCannotRemoved) throws ChaiDBException {
if (filterCannotRemoved == null) {
children.clear();
return;
}
for (Iterator c = children.iterator(); c.hasNext();) {
String childElementName = ((ConfigTool) c.next()).getName();
String childElementID = ((ConfigTool) c.next()).getAttributeValue(ELEMENT_ID);
try {
removeChildElement(childElementName, childElementID, filterCannotRemoved);
} catch (ChaiDBException ie) {
ie.printStackTrace();
}
}
}
/**
* remove all child elements of element if it does not
* satify the filter condition
*/
public static void removeElementAllChildElements(ConfigTool element) throws ChaiDBException {
element.removeAllChildElements();
}
/**
* remove all child elements of element ,
* the element eas found by parh and elementID
*
* @see :findelementbypath(String path,String elementID)
*/
public static void removeElementAllChildElements(String path, String elementID) throws ChaiDBException {
removeElementAllChildElements(path, elementID, null);
}
/**
* remove all child elements of element if it does not
* satify the filter condition,the element eas found by parh
* and elementID
*
* @see :findelementbypath(String path,String elementID)
*/
public static void removeElementAllChildElements(String path, String elementID, Hashtable filterCannotRemoved) throws ChaiDBException {
ConfigTool element = ConfigTool.findElementByPath(path, elementID);
if (element == null) {
log(ConfigTool.NO_SUNCH_ELEMENT);
return;
}
element.removeAllChildElements(filterCannotRemoved);
}
// /**
// *
// * @param elementName
// * @param ElementID
// */
// public void addChildElement(String elementName,String elementID){
//
// Hashtable attributes = new Hashtable();
// attributes.put(ConfigTool.ELEMENT_ID,elementID);
// addChildElement(elementName,attributes);
//
// }
/**
* add child element to current element ,and set its attributes as
* attributes
*
* @param elementName
* @param attributes
*/
public void addChildElement(String elementName, Hashtable attributes) {
ConfigTool child = new ConfigTool(elementName, attributes, null, null);
if (children == null) children = new ElementSet();
children.add(child);
}
/**
* add child element to element ,and set its attributes as
* attributes,the element was found by path and elementID
*
* @param path
* @param elementID
* @param addedElementName
* @param addedElementAttributes
* @see :findelementbypath(String path,String elementID)
*/
public static void addElementChildElement(String path, String elementID, String addedElementName, Hashtable addedElementAttributes) throws ChaiDBException {
ConfigTool element = ConfigTool.findElementByPath(path, elementID);
if (element == null) {
log(ConfigTool.NO_SUNCH_ELEMENT);
return;
}
element.addChildElement(addedElementName, addedElementAttributes);
}
/**
* add attribute whose name is attibuteName and value is attributeValue
* to the current element
*/
public void addAttribute(String attributeName, String attributeValue) throws ChaiDBException {
if (!attributes.containsKey(attributeName)) {
try {
attributes.put(attributeName, attributeValue);
} catch (Exception ex) {
throw new ChaiDBException(ErrorCode.INVALID_INPUT_PARAM, "adding attribute name or adding attribute " + "value can't be null");
}
} else {
throw new ChaiDBException(ErrorCode.INVALID_INPUT_PARAM, "the Attribute " + attributeName + "has existed!");
}
}
/**
* add attribute whose name is attibuteName and value is attributeValue
* to the element
*
* @param element
* @param attributeName
* @param attributeValue
*/
public static void addElementAttribute(ConfigTool element, String attributeName, String attributeValue) throws ChaiDBException {
element.addAttribute(attributeName, attributeValue);
}
/**
* add attribute whose name is attibuteName and value is attributeValue
* to the element found by path and elementID
*
* @param path
* @param elementID
* @param attributeName
* @param attributeValue
* @see :findelementbypath(String path,String elementID)
*/
public static void addElementAttribute(String path, String elementID, String attributeName, String attributeValue) throws ChaiDBException {
ConfigTool element = ConfigTool.findElementByPath(path, elementID);
if (element == null) {
log(ConfigTool.NO_SUNCH_ELEMENT);
return;
}
element.addAttribute(attributeName, attributeValue);
}
/**
* @param info
*/
private static void log(String info) {
if (isOutput) {
System.out.println(info);
}
}
}