All Downloads are FREE. Search and download functionalities are using the official Maven repository.

Alachisoft.NCache.Common.Configuration.ConfigurationBuilder Maven / Gradle / Ivy

package Alachisoft.NCache.Common.Configuration;

import com.alachisoft.ncache.runtime.exceptions.ConfigurationException;
import org.w3c.dom.*;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * [Author: Taimoor Haider] This class is the key component of the tag based generic configuration handling framework. The framework handles cofiugration setting specified in XML
 * format. All the properties should be specified in the form of an attributes e.g       The cofiguration framework can load a configuration from XML to an equivalted .Net object model and can convert back into the
 * XML. The equivalent .Net object should have the same skeleton as XML do have i.e. hierarchy. .Net class which represents the configuration in object form need to specify certain
 * custom attributes called cofiguration attributes. These custom attributes help the framework undersand which .Net object is equivalent to the a specific XML tag. To see . Below is example .Net class which is populated by the framework from the the above XML. 
 * [ConfigurationRoot("Cache")]
 */
//public class CacheClientConfig
//{
//    private string _id;
//    private int _retryInterval;
//    private ServerCofig[] _serverConfig;
//    [ConfigurationAttribute("id")]
//    public string Id
//    {
//        get { return _id; }
//        set { _id = value; }
//    }
//    [ConfigurationAttribute("retry-interval", "sec")]
//    public int RetryInterval
//    {
//        get { return _retryInterval; }
//        set { _retryInterval = value; }
//    }
//    [ConfigurationSection("server")]
//    public ServerCofig[] ServerConifg
//    {
//        get { return _serverConfig; }
//        set { _serverConfig = value; }
//    }
//}
//public class ServerCofig
//{
//    private string _serverName;
//    private string _priority;
//    [ConfigurationAttribute("name")]
//    public string ServerName
//    {
//        get { return _serverName; }
//        set { _serverName = value; }
//    }
//    [ConfigurationAttribute("priority")]
//    public string Priority
//    {
//        get { return _priority; }
//        set { _priority = value; }
//    }
//}

/**
 * 
 */
public class ConfigurationBuilder {

    private static final String DYNAMIC_CONFIG_SECTION = "dynamic-config-object";
    private static String[] _excludedText = new String[]
            {
                    "sec", "%", "mb"
            };
    private java.util.HashMap _baseConfigurationMap = new HashMap();
    private java.util.HashMap _dynamicSectionTypeMap = new java.util.HashMap();
    private java.util.ArrayList _lastLoadedConfiugration = new java.util.ArrayList();
    private String _file;
    private String _path = null;

    public ConfigurationBuilder(Object[] configuration) throws Exception {
        setConfiguration(configuration);

    }

    public ConfigurationBuilder(String file) {
        _file = file;
    }

    /**
     * In this case user will provide the xml data at the time of Reading
     */
    public ConfigurationBuilder() {
    }

    public ConfigurationBuilder(String file, String path) {
        _file = file;
        _path = path;

    }

    private static String ValidateForRootConfiguration(java.lang.Class type) {
        String rootAttrib = null;
        Annotation[] customAttributes = type.getAnnotations();

        if (customAttributes != null) {
            for (Annotation attrib : customAttributes) {
                if (attrib instanceof ConfigurationRootAnnotation) {
                    rootAttrib = ((ConfigurationRootAnnotation) attrib).value();
                    break;
                }
            }
        }
        return rootAttrib;
    }
//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
//#if VS2005

    public final Object[] getConfiguration() {
        return _lastLoadedConfiugration.toArray();
    }

    public final void setConfiguration(Object[] value) throws Exception {


        synchronized (_lastLoadedConfiugration) {
            _lastLoadedConfiugration.clear();
            for (int i = 0; i < value.length; i++) {
                String rootAttrib = ValidateForRootConfiguration(value[i].getClass());
                if (rootAttrib != null) {
                    _lastLoadedConfiugration.add(value[i]);
                } else {
                    throw new Exception(value[i].getClass() + " is not marked as RootConfiguration");
                }
            }
        }


    }

    /**
     * Registers a type to be matched for root configuration. ConfigurationBuilder map an XML config to a .Net class if it is registered with the framework.
     *
     * @param type type of the object which is to be mapped to a XML section. Registering object should have a ConfigurationRootAttribute.
     */
    public final void RegisterRootConfigurationObject(java.lang.Class type) throws Exception {
        String rootConfiguratinAttrib = ValidateForRootConfiguration(type);
        if (rootConfiguratinAttrib == null) {
            throw new Exception(type.toString() + " is not marked as RootConfiguration");
        } else {
            _baseConfigurationMap.put(rootConfiguratinAttrib.toLowerCase(), type);
        }
    }

    public final void ReadConfiguration() throws ParserConfigurationException, SAXException, IOException, InstantiationException, IllegalAccessException, Exception {
        ReadConfiguration(_file, _path);
    }

    public final void ReadConfiguration(String xml) throws ParserConfigurationException, SAXException, IOException, InstantiationException, IllegalAccessException, Exception {
        ReadConfiguration(_file, _path);
    }

    public final void ReadConfiguration(Document bridges) throws InstantiationException, IllegalAccessException, Exception {
        NodeList nodeList = bridges.getChildNodes();
        if ((nodeList != null) && (nodeList.getLength() > 0)) {
            for (int i = 0; i < nodeList.getLength(); i++) {
                Node node = nodeList.item(i);
                if (((node.getNodeType() != Node.CDATA_SECTION_NODE) && (node.getNodeType() != Node.COMMENT_NODE))) //&& (node.getNodeType() != Node. .XmlDeclaration))
                {
                    this.ReadConfigurationForNode(node);
                }
            }
        }
    }

    private void ReadConfiguration(String file, String path) throws ParserConfigurationException, SAXException, IOException, InstantiationException, IllegalAccessException, Exception {
        if (file == null) {
            throw new Exception("File name can not be null");
        }

        path = path == null ? "" : path;

        _lastLoadedConfiugration = new java.util.ArrayList();
        String fileName = path + file;
        if (!(new java.io.File(fileName)).isFile()) {
            throw new Exception("File " + fileName + " not found");
        }

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

        Document document = null;
        //XmlDocument document = new XmlDocument();
        try {
            DocumentBuilder db = dbf.newDocumentBuilder();
            document = db.parse(new File(fileName));
            //document.Load(fileName);
        } catch (Exception e) {
            throw new Exception("Can not open " + fileName + " Error:" + e.toString());
        }

        ReadConfiguration(document);

    }

    /*
     * public final void ReadConfiguration(XmlDocument xmlDocument) { XmlNodeList nodeList = xmlDocument.ChildNodes; if (nodeList != null && nodeList.size() > 0) { for (int i = 0;
     * i < nodeList.size(); i++) { XmlNode node = nodeList[i];
     *
     * if (node.NodeType == XmlNodeType.CDATA || node.NodeType == XmlNodeType.Comment || node.NodeType == XmlNodeType.XmlDeclaration) { continue; } ReadConfigurationForNode(node);
     * } } }
     */
    private Object GetConfigurationObject(String cofingStr) throws InstantiationException, IllegalAccessException {
        Object cfgObject = null;
        if (_baseConfigurationMap.containsKey(cofingStr.toLowerCase())) {
            java.lang.Class type = (java.lang.Class) ((_baseConfigurationMap.get(cofingStr.toLowerCase()) instanceof java.lang.Class) ? _baseConfigurationMap.get(cofingStr.toLowerCase()) : null);
            cfgObject = Activator.createInstance(type);

            _lastLoadedConfiugration.add(cfgObject);

        }
        return cfgObject;
    }

    private void ReadConfigurationForNode(Node node) throws InstantiationException, IllegalAccessException, Exception {
        Object cfgObject = GetConfigurationObject(node.getNodeName().toLowerCase());
        NodeList nodeList = node.getChildNodes();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node1 = nodeList.item(i);
            if (node1.getNodeType() == Node.ELEMENT_NODE) {
                Element element = (Element) node1;
                if (node1.getNodeName().toLowerCase().equals(DYNAMIC_CONFIG_SECTION)) {
                    ExtractDyanamicConfigSectionObjectType(node1);
                }
            }
        }
        if (cfgObject != null) {
            PopulateConfiugrationObject(cfgObject, node);
        } else {
            for (int i = 0; i < nodeList.getLength(); i++) {
                node = nodeList.item(i);
                if (node.getNodeType() != Node.ELEMENT_NODE) {
                    continue;
                }
                ReadConfigurationForNode(node);
            }
        }
    }

    private void PopulateConfiugrationObject(Object config, Node node) throws Exception {
        if (node == null || config == null) {
            return;
        }

        Element element = (Element) node;
        NamedNodeMap attribs = element.getAttributes();

        for (int i = 0; i < attribs.getLength(); i++) {
            FillConfigWithAttribValue(config, attribs.item(i));
        }

        NodeList nodeList = node.getChildNodes();
        HashMap sameSections = new HashMap();

        for (int i = 0; i < nodeList.getLength(); i++) {
            if (nodeList.item(i).getNodeType() == Node.ELEMENT_NODE) {
                Element childElements = (Element) nodeList.item(i);
                Class sectionType = null;

                if (childElements.getNodeName().toLowerCase() == DYNAMIC_CONFIG_SECTION && childElements.hasAttributes()) {
                    //Not called as yet
                    ExtractDyanamicConfigSectionObjectType(childElements);
                }
            }
        }


        for (int i = 0; i < nodeList.getLength(); i++) {
            if (nodeList.item(i).getNodeType() != Node.ELEMENT_NODE) {
                continue;
            }
            Element sectionNode = (Element) nodeList.item(i);
            Class sectionType = null;
            if (sectionNode.getNodeName().toLowerCase().equals(DYNAMIC_CONFIG_SECTION)) {
                continue;
            }

            sectionType = GetConfigSectionObjectType(config, sectionNode.getNodeName());

            if (sectionType != null) {
                if (sectionType.isArray()) {
                    String nonArrayType = sectionType.getCanonicalName().replace("[]", "");
                    ArrayList sameSessionList = null;
                    HashMap tmp = null;
                    if (!sameSections.containsKey(sectionType)) {
                        tmp = new HashMap();
                        tmp.put("section-name", sectionNode.getNodeName());

                        sameSessionList = new ArrayList();
                        tmp.put("section-list", sameSessionList);
                        sameSections.put(sectionType, tmp);
                    } else {
                        tmp = sameSections.get(sectionType) instanceof HashMap ? (HashMap) sameSections.get(sectionType) : null;
                        sameSessionList = tmp.get("section-list") instanceof ArrayList ? (ArrayList) tmp.get("section-list") : null;
                    }

                    Class clas = Class.forName(nonArrayType);
                    Object singleSessionObject = Activator.createInstance(clas);
//                    ObjectHandle objHandle = Activator.CreateInstance(sectionType.Assembly.FullName, nonArrayType);
//                    object singleSessionObject = objHandle.Unwrap();
                    PopulateConfiugrationObject(singleSessionObject, sectionNode);
                    sameSessionList.add(singleSessionObject);
                } else {
//                    Object sectionConfig = GetConfigSectionObject(config, sectionNode.getNodeName());
                    Object objHandle = Activator.createInstance(sectionType);
//                    object sectionConfig = objHandle.Unwrap();
                    Object sectionConfig = objHandle;
                    PopulateConfiugrationObject(sectionConfig, sectionNode);
                    SetConfigSectionObject(config, sectionConfig, sectionNode.getNodeName());
                }
            }

            if (sameSections.size() > 0) {
                HashMap tmp;
                Iterator ide = sameSections.entrySet().iterator();
                while (ide.hasNext()) {
                    Map.Entry pair = (Map.Entry) ide.next();
                    Class classtype = pair.getKey() instanceof Class ? ((Class) pair.getKey()) : null;
                    String type = classtype.getCanonicalName().replace("[]", "");
                    tmp = pair.getValue() instanceof HashMap ? (HashMap) pair.getValue() : null;
                    ArrayList sameSessionList = tmp.get("section-list") instanceof ArrayList ? (ArrayList) tmp.get("section-list") : null;
                    String sectionName = tmp.get("section-name") instanceof String ? (String) tmp.get("section-name") : null;
//                    Object sessionArrayObj = Activator.CreateInstance(arrType, new object[] { sameSessionList.Count }) as object[];
                    Object[] sessionArrayObj = new Object[sameSessionList.size()];
                    for (int j = 0; j < sameSessionList.size(); j++) {
                        sessionArrayObj[j] = Activator.createInstance(Class.forName(classtype.getCanonicalName().replace("[]", "")));
                    }

                    if (sessionArrayObj != null) {
                        System.arraycopy(sameSessionList.toArray(), 0, sessionArrayObj, 0, sameSessionList.size());
                        Object obj = sessionArrayObj;
                        SetConfigSectionObject(config, obj, sectionName);
                    }
                }
            }
        }
        /*
         * if (node == null || config == null) { return; }
         *
         * NamedNodeMap attribColl = node.getAttributes(); for(int i=0; i< attribColl.getLength(); i++){ attribColl.item(i); //for (NamedNodeMap xmlAttrib : attribColl.) {
         * FillConfigWithAttribValue(config, node.getAttributes()); }
         *
         * NodeList nodeList = node.getChildNodes(); java.util.HashMap sameSections = new java.util.HashMap(); for (int i = 0; i < nodeList.getLength(); i++) { Node sectionNode
         * = nodeList.item(i); java.lang.Class sectionType = null;
         *
         * if (sectionNode.getNodeName().toLowerCase().equals(DYNAMIC_CONFIG_SECTION) && sectionNode.hasChildNodes()) { ExtractDyanamicConfigSectionObjectType(sectionNode); } } for
         * (int i = 0; i < nodeList.getLength(); i++) { Node sectionNode = nodeList.item(i); java.lang.Class sectionType = null; if
         * (sectionNode.getNodeName().toLowerCase().equals(DYNAMIC_CONFIG_SECTION)) { continue; }
         *
         * sectionType = GetConfigSectionObjectType(config, sectionNode.getNodeName());
         *
         * if (sectionType != null) { if (sectionType.isArray()) { String nonArrayType = sectionType.getName().replace("[]", ""); java.util.ArrayList sameSessionList = null;
         * java.util.HashMap tmp = null; if (!sameSections.contains(sectionType)) { tmp = new java.util.HashMap(); tmp.put("section-name", sectionNode.getNodeName());
         *
         * sameSessionList = new java.util.ArrayList(); tmp.put("section-list", sameSessionList); sameSections.put(sectionType, tmp); } else { tmp =
         * (java.util.HashMap)((sameSections.get(sectionType) instanceof java.util.HashMap) ? sameSections.get(sectionType) : null); sameSessionList =
         * (java.util.ArrayList)((tmp.get("section-list") instanceof java.util.ArrayList) ? tmp.get("section-list") : null); }
         *
         * ObjectHandle objHandle = Activator.CreateInstance(sectionType.Assembly.FullName,nonArrayType); Object singleSessionObject = objHandle.Unwrap();
         * PopulateConfiugrationObject(singleSessionObject, sectionNode); sameSessionList.add(singleSessionObject); } else { //Object sectionConfig = GetConfigSectionObject(config,
         * sectionNode.Name); ObjectHandle objHandle = Activator.CreateInstance(sectionType.Assembly.FullName, sectionType.FullName); Object sectionConfig = objHandle.Unwrap();
         * PopulateConfiugrationObject(sectionConfig, sectionNode); SetConfigSectionObject(config, sectionConfig, sectionNode.Name); } } } if (sameSections.size() > 0) {
         * java.util.HashMap tmp; IDictionaryEnumerator ide = sameSections.entrySet().iterator(); while (ide.MoveNext()) { java.lang.Class arrType = (java.lang.Class)((ide.Key
         * instanceof java.lang.Class) ? ide.Key : null); tmp = (java.util.HashMap)((ide.Value instanceof java.util.HashMap) ? ide.Value : null); java.util.ArrayList
         * sameSessionList = (java.util.ArrayList)((tmp.get("section-list") instanceof java.util.ArrayList) ? tmp.get("section-list") : null); String sectionName =
         * (String)((tmp.get("section-name") instanceof String) ? tmp.get("section-name") : null); Object tempVar = Activator.CreateInstance(arrType, new Object[]
         * {sameSessionList.size()}); Object[] sessionArrayObj = (Object[])((tempVar instanceof Object[]) ? tempVar : null); if (sessionArrayObj != null) { for (int i = 0; i <
         * sameSessionList.size(); i++) { sessionArrayObj[i] = sameSessionList.get(i); } SetConfigSectionObject(config, sessionArrayObj, sectionName); } } }
         */
    }

    private Object GetConfigSectionObject(Object config, String sectionName) {
        /*
         * java.lang.Class type = config.getClass(); PropertyInfo[] objProps = type.GetProperties();
         *
         * if (objProps != null) { for (int i = 0; i < objProps.length; i++) { PropertyInfo propInfo = objProps[i]; Object[] customAttribs =
         * propInfo.GetCustomAttributes(ConfigurationSectionAttribute.class, false); if (customAttribs != null && customAttribs.length > 0) { ConfigurationSectionAttribute
         * configSection = (ConfigurationSectionAttribute)((customAttribs[0] instanceof ConfigurationSectionAttribute) ? customAttribs[0] : null); if (configSection != null &&
         * sectionName.toLowerCase().equals(configSection.getSectionName())) { return Activator.CreateInstance(propInfo.PropertyType); } } } }
         */
        return null;
    }

    /**
     * Gets the type of the section object.
     *
     * @param config
     * @param sectionName
     * @return
     */
    private java.lang.Class GetConfigSectionObjectType(Object config, String sectionName) {
        java.lang.Class sectionType = null;
        Class type = config.getClass();
        Method[] objProps = type.getMethods();

        if (objProps != null) {
            for (int i = 0; i < objProps.length; i++) {
                Method fieldInfo = objProps[i];
                Object[] customAttribs = fieldInfo.getAnnotations();
                if (customAttribs != null && customAttribs.length > 0) {
                    ConfigurationSectionAnnotation configAttrib = (ConfigurationSectionAnnotation) ((customAttribs[0] instanceof ConfigurationSectionAnnotation) ? customAttribs[0] : null);
                    if (configAttrib != null && configAttrib.value().toLowerCase().equals(sectionName.toLowerCase())) {
                        //for simplicity get return type of the getter method, skip for the setter
                        if (fieldInfo.getParameterTypes().length > 0) {
                            continue;
                        }
                        sectionType = fieldInfo.getReturnType();
                        break;
//                            sectionType = fieldInfo.getType();
//                            fieldInfo.setValue(config, ConvertToPrimitive(fieldInfo.getType(), xmlAttrib.getNodeValue(), configAttrib.getAppendedText()), null);
                    }
                }
            }
        }

        if (sectionType == null) {
            if (_dynamicSectionTypeMap.containsKey(sectionName.toLowerCase())) {
                sectionType = _dynamicSectionTypeMap.get(sectionName.toLowerCase()).getType();
            }
        }

        return sectionType;
    }

    /**
     * Gets the type of the section object.
     *
     * @param config
     * @param sectionName
     * @return
     */
    private void ExtractDyanamicConfigSectionObjectType(Node node) {
        java.lang.Class sectionType = null;
        if (node != null) {
            String assemblyName = null;
            String className = null;
            boolean isArray = false;
            String sectionid = null;
            NamedNodeMap map = node.getAttributes();

            for (int i = 0; i < map.getLength(); i++) {
                Node attribute = map.item(i);
                if (attribute.getNodeName().toLowerCase().equals("assembly")) {
                    assemblyName = attribute.getNodeValue();
                }

                if (attribute.getNodeName().toLowerCase().equals("class")) {
                    className = attribute.getNodeValue();
                }

                if (attribute.getNodeName().toLowerCase().equals("section-id")) {
                    sectionid = attribute.getNodeValue();
                }

                if (attribute.getNodeName().toLowerCase().equals("is-array")) {
                    isArray = Boolean.parseBoolean(attribute.getNodeValue());
                }
            }

            if (className == null || sectionid == null) {
                return;
            }
            //Assembly qualified name ; for ref: http://msdn.microsoft.com/en-us/library/system.type.assemblyqualifiedname.aspx


            String assebmlyQualifiedName = null;
            if (assemblyName != null) {
                assebmlyQualifiedName = className + "," + assemblyName;
            } else {
                assebmlyQualifiedName = className;
            }

            try {
                sectionType = java.lang.Class.forName(assebmlyQualifiedName);
            } catch (ClassNotFoundException clex) {
                //clex.printStackTrace();
            }

            if (sectionType != null && !tangible.DotNetToJavaStringHelper.isNullOrEmpty(sectionid)) {
                _dynamicSectionTypeMap.put(sectionid, new DynamicConfigType(sectionType, isArray));
            }
        }
    }

    private void SetConfigSectionObject(Object config, Object sectionConfig, String sectionName) throws ConfigurationException {
        java.lang.Class type = config.getClass();
        Method[] objProps = type.getMethods();

        if (objProps != null) {
            for (int i = 0; i < objProps.length; i++) {
                Method fieldInfo = objProps[i];
                Annotation[] customAttribs = fieldInfo.getAnnotations();
                if (customAttribs != null && customAttribs.length > 0) {
                    ConfigurationSectionAnnotation configSection = (ConfigurationSectionAnnotation) ((customAttribs[0].annotationType().equals(ConfigurationSectionAnnotation.class) ? customAttribs[0] : null));
                    try {
                        if (configSection != null && configSection.value().equals(sectionName.toLowerCase())) {
                            if (fieldInfo.getParameterTypes().length == 0) {
                                continue;
                            }
                            fieldInfo.invoke(config, sectionConfig);
                            break;
//                            fieldInfo.setValue(config, ConvertToPrimitive(fieldInfo.getType(), xmlAttrib.getNodeValue(), configAttrib.getAppendedText()), null);
                        }
                    } catch (Exception e) {
                        throw new ConfigurationException(e.getMessage());
                    }

                }
            }
        }
        /*
         * PropertyInfo[] objProps = type.GetProperties(); try { if (objProps != null) { for (int i = 0; i < objProps.length; i++) { PropertyInfo propInfo = objProps[i]; Object[]
         * customAttribs = propInfo.GetCustomAttributes(ConfigurationSectionAttribute.class, false);
         *
         * if (customAttribs != null && customAttribs.length > 0) { ConfigurationSectionAttribute configSection = (ConfigurationSectionAttribute)((customAttribs[0] instanceof
         * ConfigurationSectionAttribute) ? customAttribs[0] : null); try { if (configSection != null && sectionName.toLowerCase().equals(configSection.getSectionName())) {
         * propInfo.SetValue(config, sectionConfig, null);
         *
         * }
         * } catch (Exception e) { throw new Alachisoft.NCache.Runtime.Exceptions.ConfigurationException("Duplicate cache entries in client.ncconf"); } }
         *
         *
         * customAttribs = propInfo.GetCustomAttributes(DynamicConfigurationSectionAttribute.class, false); if (customAttribs != null && customAttribs.length > 0) {
         * DynamicConfigurationSectionAttribute configSection = (DynamicConfigurationSectionAttribute)((customAttribs[0] instanceof DynamicConfigurationSectionAttribute) ?
         * customAttribs[0] : null); try { if (configSection != null && sectionName.toLowerCase().equals(configSection.getSectionName())) { propInfo.SetValue(config, sectionConfig,
         * null); } } catch (Exception e) { throw new ConfigurationException("Duplicate cache entries in client.ncconf"); } } } } } catch(Exception e) { throw e; }
         */
    }

    public Object ConvertToPrimitive(java.lang.Class targetType, String value, String appendedText) {
        Object primitiveValue = null;

        if (appendedText != null && !appendedText.equals("")) {
            value = value.toLowerCase().replace(appendedText.toLowerCase(), "");
        }

//        if (targetType.isPrimitive())
//        {
//C# TO JAVA CONVERTER NOTE: The following 'switch' operated on a string member and was converted to Java 'if-else' logic:
//			switch (targetType.FullName)
//ORIGINAL LINE: case "System.Byte":

        if (targetType.getCanonicalName().equals("byte")) {
            primitiveValue = Byte.parseByte(value);

        }
//ORIGINAL LINE: case "System.Int16":
        else if (targetType.getCanonicalName().equals("short")) {
            primitiveValue = Short.parseShort(value);

        }
        /*
         * //ORIGINAL LINE: case "System.Int32": else if (targetType.FullName.equals("System.Int32")) { primitiveValue = Integer.parseInt(value);
         *
         * }
         * //ORIGINAL LINE: case "System.Int64": else if (targetType.FullName.equals("System.Int64")) { primitiveValue = Long.parseLong(value);
         *
         * }
         * //ORIGINAL LINE: case "System.Single": else if (targetType.FullName.equals("System.Single")) { primitiveValue = Float.parseFloat(value);
         *
         * }
         *
         */
//ORIGINAL LINE: case "System.Double":
        else if (targetType.getCanonicalName().equals("double")) {
            primitiveValue = Double.parseDouble(value);

        }
//ORIGINAL LINE: case "System.Boolean":
        else if (targetType.getCanonicalName().equals("boolean")) {
            //+Asad [bug-id: 1434] in case of boolean we can ignore the the case as "True", "true" and "tRue" are the same
            primitiveValue = Boolean.parseBoolean(value.toLowerCase());

        }
//ORIGINAL LINE: case "System.Char":
        else if (targetType.getCanonicalName().equals("char")) {
            primitiveValue = value;
        } else if (targetType.getCanonicalName().equals("int")) {
            primitiveValue = Integer.parseInt(value);
        } else if (targetType.getCanonicalName().equals("long")) {
            primitiveValue = Long.decode(value);
        } else if (targetType.getCanonicalName().equals("float")) {
            primitiveValue = Float.parseFloat(value);
        } else if (targetType.getCanonicalName().equals("java.lang.Byte")) {
            primitiveValue = Byte.parseByte(value);
        } else if (targetType.getCanonicalName().equals("java.lang.Double")) {
            primitiveValue = Double.parseDouble(value);
        } else if (targetType.getCanonicalName().equals("java.lang.Integer")) {
            primitiveValue = Integer.parseInt(value);
        }
        if (targetType.getCanonicalName().equals("java.lang.Short")) {
            primitiveValue = Short.parseShort(value);

        } else if (targetType.getCanonicalName().equals("java.lang.Boolean")) {
            //+Asad [bug-id: 1434] in case of boolean we can ignore the the case as "True", "true" and "tRue" are the same
            primitiveValue = Boolean.parseBoolean(value.toLowerCase());

        }
//ORIGINAL LINE: case "System.Char":
        else if (targetType.getCanonicalName().equals("java.lang.Character")) {

            if (value.toCharArray().length == 1) {
                primitiveValue = value.toCharArray()[0];
            } else {
                //System.err.println("More than one Character");
            }
        } else if (targetType.getCanonicalName().equals("java.math.BigDecimal")) {
            primitiveValue = BigDecimal.valueOf(Double.parseDouble(value));
        } else if (targetType.getCanonicalName().equals("java.lang.Long")) {
            primitiveValue = Long.decode(value);
        }
        if (targetType.getCanonicalName().equals("java.lang.String")) {
            primitiveValue = value;
        } else if (targetType.getCanonicalName().equals("java.lang.Float")) {
            primitiveValue = Float.parseFloat(value);
        } else if (targetType.getCanonicalName().equals("java.lang.Object[]")) {
            primitiveValue = new Object[]{value};
        }


        return primitiveValue;
    }

    private String ExcludeExtraText(String input) {
        String output = input;
        if (input != null) {
            input = input.toLowerCase();
            for (int i = 0; i < _excludedText.length; i++) {
                if (input.indexOf(_excludedText[i]) >= 0) {
                    output = input.replace(_excludedText[i], "");
                    break;
                }
            }
        }
        return output;
    }

    private void FillConfigWithAttribValue(Object config, Node xmlAttrib) throws Exception {
        java.lang.Class type = config.getClass();
        Method[] objProps = type.getMethods();

        if (objProps != null) {
            for (int i = 0; i < objProps.length; i++) {
                Method fieldInfo = objProps[i];
                Annotation[] customAttribs = fieldInfo.getAnnotations();
                if (customAttribs != null && customAttribs.length > 0) {
                    ConfigurationAttributeAnnotation configAttrib = (ConfigurationAttributeAnnotation) ((customAttribs[0].annotationType().equals(ConfigurationAttributeAnnotation.class) ? customAttribs[0] : null));
                    try {
                        if (configAttrib != null && xmlAttrib.getNodeName().toLowerCase().equals(configAttrib.value())) {
                            if (fieldInfo.getParameterTypes().length == 0) {
                                continue;
                            }
                            Class[] typs = fieldInfo.getParameterTypes();
                            fieldInfo.invoke(config, ConvertToPrimitive(typs[0], xmlAttrib.getNodeValue(), configAttrib.appendText()));
                            break;
//                            fieldInfo.setValue(config, ConvertToPrimitive(fieldInfo.getType(), xmlAttrib.getNodeValue(), configAttrib.getAppendedText()), null);
                        }
                    } catch (Exception e) {
                        throw new Exception("Can not set the value for attribute " + configAttrib.value() + " Errror :" + e.toString());
                    }

                }
            }
        }
    }

    public final String GetXmlString() throws IllegalArgumentException, IllegalAccessException {
        StringBuilder sb = new StringBuilder();
        if (_lastLoadedConfiugration != null) {
            for (Object cfgObject : _lastLoadedConfiugration) {
                sb.append(GetXmlString(cfgObject));
            }
        }
        return sb.toString();
    }

    public final String GetXmlString(Object cfgObject) throws IllegalArgumentException, IllegalAccessException {
        StringBuilder sb = new StringBuilder();
        String rootXmlStr = null;
        java.lang.Class type = cfgObject.getClass();
        Annotation[] cfgObjCustomAttribs = type.getAnnotations();

        if (cfgObjCustomAttribs != null && cfgObjCustomAttribs.length > 0) {
            for (int i = 0; i < cfgObjCustomAttribs.length; i++) {
                if (cfgObjCustomAttribs[i].annotationType() == ConfigurationRootAnnotation.class) {
                    ConfigurationRootAnnotation rootAttrib = (ConfigurationRootAnnotation) cfgObjCustomAttribs[i];
                    if (rootAttrib != null) {
                        rootXmlStr = rootAttrib.value();
                    }
                }
            }
        }
        return GetSectionXml(cfgObject, rootXmlStr, 1);

    }

    private String getRightPadString(int padCount) {

        String padString = "";

        for (int i = 0; i < padCount; i++) {
            padString += " ";
        }
        return padString;
    }

    private String GetSectionXml(Object configSection, String sectionName, int indent) throws IllegalArgumentException, IllegalAccessException {
        //string strPropertyValue = "";
        String endStr = "\r\n";
        String preStr = getRightPadString(indent * 2); //(new String(""));

        StringBuilder sb = new StringBuilder(preStr + "<" + sectionName);
        java.lang.Class type = configSection.getClass();

        Method[] propertiesInfo = type.getMethods();

        if (propertiesInfo != null && propertiesInfo.length > 0) {
            for (int i = 0; i < propertiesInfo.length; i++) {
                Method property = propertiesInfo[i];
                if (property.getReturnType().equals(void.class)) {
                    continue;
                }

                Annotation[] customAttribs = property.getAnnotations();

                if (customAttribs != null && customAttribs.length > 0) {
                    for (int j = 0; j < customAttribs.length; j++) {
                        ConfigurationAttributeAnnotation attrib = (ConfigurationAttributeAnnotation) (customAttribs[j].annotationType() == ConfigurationAttributeAnnotation.class ? customAttribs[j] : null);
                        if (attrib != null) {
                            Object propertyValue = null;
                            try {
                                propertyValue = property.invoke(configSection, new Object[0]);
                            } catch (InvocationTargetException ex) {
                                // Logger.getLogger(ConfigurationBuilder.class.getName()).log(Level.SEVERE, null, ex);
                            }
                            String appendedText = attrib.appendText() != null ? attrib.appendText() : "";
                            if (propertyValue != null) {
                                //strPropertyValue = propertyValue.ToString();
                                //if (sectionName == "attribute" && strPropertyValue.Contains("<"))
                                //{
                                //    strPropertyValue = strPropertyValue.Replace("<", "<");
                                //    strPropertyValue = strPropertyValue.Replace(">", ">");
                                //}
                                sb.append(" " + attrib.value() + "=\"" + propertyValue.toString() + appendedText + "\"");
                            } else {
                                sb.append(" " + attrib.value() + "=\"\"");
                            }
                        }
                    }
                }
            }
        }
        boolean subsectionsFound = false;
        boolean firstSubSection = true;
        StringBuilder comments = null;

        //get xml string for sub-sections if exists
        if (propertiesInfo != null && propertiesInfo.length > 0) {
            for (int i = 0; i < propertiesInfo.length; i++) {
                Method property = propertiesInfo[i];
                if (property.getReturnType().equals(void.class)) {
                    continue;
                }

                Annotation[] customAttribs = property.getAnnotations();

                if (customAttribs != null && customAttribs.length > 0) {
                    for (int j = 0; j < customAttribs.length; j++) {

                        //Confi commentAttrib = (ConfigurationCommentAttribute)((customAttribs[j] instanceof ConfigurationCommentAttribute) ? customAttribs[j] : null);
                        if (property.isAnnotationPresent(ConfigurationCommentAnnotation.class)) {
                            Object propertyValue = null;
                            try {
                                propertyValue = property.invoke(configSection, new Object[0]);
                            } catch (InvocationTargetException ex) {
                                //Logger.getLogger(ConfigurationBuilder.class.getName()).log(Level.SEVERE, null, ex);
                            }
                            if (propertyValue != null) {
                                String propStr = (String) ((propertyValue instanceof String) ? propertyValue : null);
                                if (!tangible.DotNetToJavaStringHelper.isNullOrEmpty(propStr)) {
                                    if (comments == null) {
                                        comments = new StringBuilder();
                                    }
                                    comments.append(String.format("%1$s%3$s", preStr, propStr, endStr));
                                }
                            }
                        }

                        //ConfigurationSectionAttribute attrib = (ConfigurationSectionAttribute)((customAttribs[j] instanceof ConfigurationSectionAttribute) ? customAttribs[j] : null);
                        if (property.isAnnotationPresent(ConfigurationSectionAnnotation.class)) {
                            Object propertyValue = null;
                            try {
                                propertyValue = property.invoke(configSection, new Object[0]);
                            } catch (InvocationTargetException ex) {
                                //Logger.getLogger(ConfigurationBuilder.class.getName()).log(Level.SEVERE, null, ex);
                            }
                            if (propertyValue != null) {
                                subsectionsFound = true;
                                if (firstSubSection) {
                                    sb.append(">" + endStr);
                                    firstSubSection = false;
                                }
                                if (propertyValue.getClass().isArray()) {
                                    Object[] array = (Object[]) ((propertyValue instanceof Object[]) ? propertyValue : null);
                                    Object actualSectionObj;
                                    for (int k = 0; k < array.length; k++) {
                                        actualSectionObj = array[k];
                                        if (actualSectionObj != null) {
                                            sb.append(GetSectionXml(actualSectionObj, property.getAnnotation(ConfigurationSectionAnnotation.class).value(), indent + 1));
                                        }
                                    }
                                } else {
                                    sb.append(GetSectionXml(propertyValue, property.getAnnotation(ConfigurationSectionAnnotation.class).value(), indent + 1));
                                }
                            }
                        }
                    }
                }
            }
        }

        if (subsectionsFound) {
            sb.append(preStr + "" + endStr);
        } else {
            sb.append("/>" + endStr);
        }

        String xml = "";
        if (comments != null) {
            xml = comments.toString() + sb.toString();
        } else {
            xml = sb.toString();
        }

        return xml;

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy