📄 xmltypecreator.java
字号:
return nextCreator.createClassInfo(m, index); } info.setTypeClass(m.getParameterTypes()[index]); //info.setAnnotations(m.getParameterAnnotations()[index]); Element parameter = manager.getParamter(bestMatch, index); readMetadata(info, mapping, parameter); } else { List nodes = getMatches(mapping, "./method[@name='" + m.getName() + "']/return-type/parent::*"); if(nodes.size() == 0) return nextCreator.createClassInfo(m, index); Element bestMatch = getBestMatch(mapping, m, nodes); if(bestMatch == null) { //no mapping for this method return nextCreator.createClassInfo(m, index); } info.setTypeClass(m.getReturnType()); //info.setAnnotations(m.getAnnotations()); Element rtElement = bestMatch.getChild("return-type"); readMetadata(info, mapping, rtElement); } return info; } protected void readMetadata(TypeClassInfo info, Element mapping, Element parameter) { info.setTypeName(createQName(parameter, parameter.getAttributeValue("typeName"))); info.setMappedName(createQName(parameter, parameter.getAttributeValue("mappedName"))); setComponentType(info, mapping, parameter); setKeyType(info, mapping, parameter); setType(info, parameter); String min = parameter.getAttributeValue("minOccurs"); if (min != null) info.setMinOccurs(Long.parseLong(min)); String max = parameter.getAttributeValue("maxOccurs"); if (max != null) info.setMaxOccurs(Long.parseLong(max)); String flat = parameter.getAttributeValue("flat"); if (flat != null) info.setFlat(Boolean.valueOf(flat.toLowerCase()).booleanValue()); } protected Type getOrCreateGenericType(TypeClassInfo info) { Type type = null; if (info.getGenericType() != null) type = createTypeFromGeneric(info.getGenericType()); if (type == null) type = super.getOrCreateGenericType(info); return type; } private Type createTypeFromGeneric(Object cType) { if (cType instanceof TypeClassInfo) return createTypeForClass((TypeClassInfo) cType); else if (cType instanceof Class) return createType((Class) cType); else return null; } protected Type getOrCreateMapKeyType(TypeClassInfo info) { Type type = null; if (info.getKeyType() != null) type = createTypeFromGeneric(info.getKeyType()); if (type == null) type = super.getOrCreateMapKeyType(info); return type; } protected Type getOrCreateMapValueType(TypeClassInfo info) { Type type = null; if (info.getGenericType() != null) type = createTypeFromGeneric(info.getGenericType()); if (type == null) type = super.getOrCreateMapValueType(info); return type; } protected void setComponentType(TypeClassInfo info, Element mapping, Element parameter) { String componentType = parameter.getAttributeValue("componentType"); if(componentType != null) { info.setGenericType(loadGeneric(info, mapping, componentType)); } } private Object loadGeneric(TypeClassInfo info, Element mapping, String componentType) { if (componentType.startsWith("#")) { String name = componentType.substring(1); Element propertyEl = manager.getComponent(mapping, name); if(propertyEl == null) { throw new XFireRuntimeException("Could not find <component> element in mapping named '" + name + "'"); } TypeClassInfo componentInfo = new TypeClassInfo(); readMetadata(componentInfo, mapping, propertyEl); String className = propertyEl.getAttributeValue("class"); if (className == null) throw new XFireRuntimeException("A 'class' attribute must be specified for <component> " + name); componentInfo.setTypeClass(loadComponentClass(className)); return componentInfo; } else { return loadComponentClass(componentType); } } private Class loadComponentClass(String componentType) { try { return ClassLoaderUtils.loadClass(componentType, getClass()); } catch(ClassNotFoundException e) { throw new XFireRuntimeException("Unable to load component type class " + componentType, e); } } protected void setType(TypeClassInfo info, Element parameter) { String type = parameter.getAttributeValue("type"); if(type != null) { try { info.setType(ClassLoaderUtils.loadClass(type, getClass())); } catch(ClassNotFoundException e) { throw new XFireRuntimeException("Unable to load type class " + type, e); } } } protected void setKeyType(TypeClassInfo info, Element mapping, Element parameter) { String componentType = parameter.getAttributeValue("keyType"); if(componentType != null) { info.setKeyType(loadGeneric(info, mapping, componentType)); } } private Element getBestMatch(Element mapping, Method method, List availableNodes) { //first find all the matching method names List nodes = getMatches(mapping, "./method[@name='" + method.getName() + "']"); //remove the ones that aren't in our acceptable set, if one is specified if(availableNodes != null) { nodes.retainAll(availableNodes); } //no name found, so no matches if(nodes.size() == 0) return null; //if the method has no params, then more than one mapping is pointless Class[] parameterTypes = method.getParameterTypes(); if(parameterTypes.length == 0) return (Element)nodes.get(0); //here's the fun part. //we go through the method parameters, ruling out matches for(int i = 0; i < parameterTypes.length; i++) { Class parameterType = parameterTypes[i]; for(Iterator iterator = nodes.iterator(); iterator.hasNext();) { Element element = (Element)iterator.next(); //first we check if the parameter index is specified //Element match = getMatch(element, "parameter[@index='" + i + "']"); Element match = manager.getParamter(element, i); if(match != null) { //we check if the type is specified and matches if(match.getAttributeValue("class") != null) { //if it doesn't match, then we can definitely rule out this result if(!match.getAttributeValue("class").equals(parameterType.getName())) { iterator.remove(); } } } } } //if we have just one node left, then it has to be the best match if(nodes.size() == 1) return (Element)nodes.get(0); //all remaining definitions could apply, so we need to now pick the best one //the best one is the one with the most parameters specified Element bestCandidate = null; int highestSpecified = 0; for(Iterator iterator = nodes.iterator(); iterator.hasNext();) { Element element = (Element)iterator.next(); int availableParameters = element.getChildren("parameter").size(); if(availableParameters > highestSpecified) { bestCandidate = element; highestSpecified = availableParameters; } } return bestCandidate; } private List getMatches(Object doc, String xpath) { try { XPath path = XPath.newInstance(xpath); List result = path.selectNodes(doc); return result; } catch(JDOMException e) { throw new XFireRuntimeException("Error evaluating xpath " + xpath, e); } } /** * Creates a QName from a string, such as "ns:Element". */ protected QName createQName(Element e, String value) { if (value == null || value.length() == 0) return null; int index = value.indexOf(":"); if (index == -1) { return new QName(getTypeMapping().getEncodingStyleURI(), value); } String prefix = value.substring(0, index); String localName = value.substring(index+1); Namespace ns = e.getNamespace(prefix); if (ns == null || localName == null) throw new XFireRuntimeException("Invalid QName in mapping: " + value); return new QName(ns.getURI(), localName, prefix); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -