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

📄 iclmappingparser.java

📁 SRI international 发布的OAA框架软件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            String arg = fixPropertyName((String)iterator.next());
            try
            {
                int index = Integer.parseInt(arg.substring("Arg".length()));
                if(index < 0 || index >= varMappings.length || varMappings[index] != null)
                    throw new MappingException("ICL mapping for function " + functionName +
                            " has malformed argument " + arg);
                ParameterMapping varMapping = new ParameterMapping();
                varMapping.setName(arg);
                varMappings[index] = varMapping;
            }
            catch(NumberFormatException nfe)
            {
                throw new MappingException("ICL mapping for function " + functionName +
                            " has malformed argument " + arg, nfe);
            }
        }

        // now that we know the arity of the function, find the method descriptor:
        Method[] methods = functionMapping.getFunctionClass().getMethods();
        for(int i = 0; i < methods.length && functionMapping.getFunctionSignature() == null; i++)
        {
            Method method = methods[i];
            if(method.getName().equals(functionName) &&
               method.getParameterTypes().length == varMappings.length &&
               (!functionMapping.isReturnsResult() || (method.getReturnType() != null)))
                functionMapping.setFunctionSignature(method);
        }
        if(functionMapping.getFunctionSignature() == null)
        {
            throw new MappingException("No matching function " + functionName +
                        " found with " + varMappings.length + " arguments");
        }

        // fill in parameter types:
        Class[] argTypes = functionMapping.getFunctionSignature().getParameterTypes();
        for(int i = 0; i < argTypes.length; i++) functionMapping.getParameters()[i].setType(argTypes[i]);


        // see if any parameter types have been overridden by XML:
        List varElements = functionElement.selectNodes("./var");
        for(Iterator iterator = varElements.iterator(); iterator.hasNext();)
        {
            Element varElement = (Element)iterator.next();
            String name = fixPropertyName(varElement.attributeValue("name"));
            Class varType = getClass(varElement.attributeValue("type"));
            IclMapper iclMapper = getMapper(varElement);
            for(int i = 0; i < varMappings.length; i++)
            {
                ParameterMapping varMapping = varMappings[i];
                if(varMapping.getName().equals(name))
                {
                    if(varType != null) varMapping.setType(varType);
                    varMapping.setMapper(iclMapper);
                }
            }
        }

        return functionMapping;
    }

    private IclMapper getMapper(Element e) throws ClassNotFoundException, MappingException, IllegalAccessException, InstantiationException
    {
        String mapper = e.attributeValue("mapper");
        if(mapper != null)
        {
            Class mapperClass = getClass(mapper);
            if(IclMapper.class.isAssignableFrom(mapperClass)) return (IclMapper)mapperClass.newInstance();
            else throw new MappingException("Mapper " + mapper + " doesn't implement IclMapper interface");
        }
        return null;
    }

    private List getVarNames(IclTerm icl) throws MappingException
    {
        FindVarNames findVarNames = new FindVarNames();
        findVarNames.traverse(icl);
        return findVarNames.getVarNames();
    }

    /**
     * Translates an IclVar to a bean property name by making the first letter lower case.
     *
     * @param property
     * @return
     */
    private String fixPropertyName(String property)
    {
        if(property != null && property.length() > 0)
        {
            return (Character.toLowerCase(property.charAt(0))) + property.substring(1);
        }
        else return property;
    }

    /**
     * Retrieves Class objects and also handles primitive and Array references.
     *
     * @param className
     * @return
     */
    private Class getClass(String className) throws ClassNotFoundException
    {
        if(className == null) return null;
        className = className.trim();

        // handle arrays:
        if(className.endsWith("[]"))
        {
            // get dimensions:
            int arrayDimensions = 0;
            int index = 0;
            while(index < className.length() && index != -1)
            {
                index = className.indexOf("[]", index);
                arrayDimensions++;
                index += 2;
            }

            // get component type:
            String componentType = className.substring(0, className.indexOf("[]"));
            Class componentClass = getClass(componentType);

            // get the class:
            Object array = Array.newInstance(componentClass, arrayDimensions);
            return array.getClass();
        }
        else if(className.equals("int")) return Integer.TYPE;
        else if(className.equals("float")) return Float.TYPE;
        else if(className.equals("long")) return Long.TYPE;
        else if(className.equals("double")) return Double.TYPE;
        else if(className.equals("char")) return Character.TYPE;
        else if(className.equals("byte")) return Byte.TYPE;
        else if(className.equals("short")) return Short.TYPE;
        else return Class.forName(className);
    }

    private void validateMapping(IclMapping iclMapping) throws MappingException
    {
        FunctionMapping[] functions = iclMapping.getFunctionMappings();
        ClassMapping[] classes = iclMapping.getClassMappings();
        for(int i = 0; i < functions.length; i++)
        {
            FunctionMapping functionMapping = functions[i];
            for(int j = 0; j < functionMapping.getParameters().length; j++)
            {
                ParameterMapping parameterMapping = functionMapping.getParameters()[j];
                Class paramType = getComponentType(parameterMapping.getType());
                if(!isBasicType(paramType))
                {
                    boolean found = false;
                    for(int k = 0; k < classes.length && !found; k++)
                    {
                        ClassMapping classMapping = classes[k];
                        if(classMapping.getMappingClass().equals(paramType)) found = true;
                    }
                    if(!found) throw new MappingException("Function " + functionMapping.getFunctionSignature().getName() +
                            " has no mapping for parameter " + j + " of type " + paramType.getName());
                }
            }
        }

        for(int i = 0; i < classes.length; i++)
        {
            ClassMapping searchMapping = classes[i];
            if(searchMapping.getMapper() == null)
            {
                for(int j = 0; j < searchMapping.getProperties().length; j++)
                {
                    PropertyMapping propertyMapping = searchMapping.getProperties()[j];
                    Class propertyType = getComponentType(propertyMapping.getType());
                    if(!isBasicType(propertyType))
                    {
                        boolean found = false;
                        for(int k = 0; k < classes.length && !found; k++)
                        {
                            ClassMapping classMapping = classes[k];
                            if(classMapping.getMappingClass().equals(propertyType)) found = true;
                        }
                        if(!found) throw new MappingException("Class " + searchMapping.getMappingClass().getName() +
                                " has no mapping for property " + propertyMapping.getProperty().getName() + " of type " + propertyType.getName());
                    }
                }
            }
        }
    }

    private boolean isBasicType(Class type)
    {
        return (type.equals(Integer.class) || type.equals(Integer.TYPE) ||
                type.equals(Float.class) || type.equals(Float.TYPE) ||
                type.equals(Long.class) || type.equals(Long.TYPE) ||
                type.equals(Double.class) || type.equals(Double.TYPE) ||
                type.equals(Byte.class) || type.equals(Byte.TYPE) ||
                type.equals(Short.class) || type.equals(Short.TYPE) ||
                type.equals(Boolean.class) || type.equals(Boolean.TYPE) ||
                type.equals(String.class));
    }

    private Class getComponentType(Class type)
    {
        while(type.isArray()) type = type.getComponentType();
        return type;
    }
}

⌨️ 快捷键说明

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