📄 oaamapper.java
字号:
collection = new LinkedList();
else if(parameterType.equals(Set.class))
collection = new HashSet();
else
collection = (Collection)parameterType.newInstance();
int length = Array.getLength(functionParameter);
for(int j = 0; j < length; j++) collection.add(Array.get(functionParameter, j));
functionParameters[i] = collection;
}
else if(parameterType.isPrimitive() && (Number.class.isAssignableFrom(functionParameter.getClass()) ||
Character.class.isAssignableFrom(functionParameter.getClass()) ||
Boolean.class.isAssignableFrom(functionParameter.getClass())))
{
}
else if(!parameterType.isAssignableFrom(functionParameter.getClass()))
{
throw new MappingException(
"Parameter " + i + " is of type " + parameterType.getName() + " and the argument " + functionParameter + " is not assignable to " +
functionParameter.getClass());
}
}
}
private ClassMapping findClassMapping(Class type)
{
for(int i = 0; i < iclMapping.getClassMappings().length; i++)
if(iclMapping.getClassMappings()[i].getMappingClass().equals(type))
return iclMapping.getClassMappings()[i];
return null;
}
private FunctionBinding findFunctionBinding(IclTerm icl)
throws MappingException
{
Map varMappings = null;
FunctionMapping functionMapping = null;
for(int i = 0; i < iclMapping.getFunctionMappings().length && varMappings == null; i++)
{
functionMapping = iclMapping.getFunctionMappings()[i];
VarMappingFinder varMappingFinder = new VarMappingFinder(functionMapping);
varMappingFinder.traverse(functionMapping.getIcl(), icl);
if(!varMappingFinder.isMatchFailed()) varMappings = varMappingFinder.getVarMapping();
}
if(varMappings != null)
{
FunctionBinding functionBinding = new FunctionBinding();
functionBinding.setFunctionMapping(functionMapping);
functionBinding.setVarMapping(varMappings);
return functionBinding;
}
throw new MappingException("No valid function mapping found for icl " + icl);
}
private String getFunctor(IclTerm icl)
{
if(icl != null && icl.isStruct()) return ((IclStruct)icl).getFunctor();
return null;
}
private static String getValue(IclTerm icl)
{
if(icl.isStr())
return ((IclStr)icl).toUnquotedString();
else if(icl.isInt())
return ((IclInt)icl).toInteger().toString();
else if(icl.isFloat())
return ((IclFloat)icl).toFloatObject().toString();
else
return null;
}
/**
* 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;
}
////////////////////////////////////////////////////////////////////
private class VarMappingFinder
extends IclDualTraversal
{
private Map varMapping = new HashMap();
private AbstractMapping mapping;
public VarMappingFinder(AbstractMapping mapping)
{
this.mapping = mapping;
}
public Map getVarMapping()
{
return varMapping;
}
public boolean visit(IclTerm template, IclTerm icl)
throws MappingException
{
try
{
// found a property mapping:
if(template.isVar())
{
varMapping.put(fixPropertyName(template.toString()), icl);
return false;
}
// parse a list:
else if(template.isList() && icl.isList())
{
// Try to match up elements of the template list with elements of
// the submitted list. If a match cannot be found for a child element, register an error:
for(int i = 0; i < icl.getNumChildren() && !isMatchFailed(); i++)
{
IclTerm iclChild = icl.getTerm(i);
boolean matchFound = false;
for(int j = 0; j < template.getNumChildren() && !matchFound; j++)
{
IclTerm templateChild = template.getTerm(j);
// we found a property in the template array, look ahead and see if its template will match the icl:
if(templateChild.isVar())
{
String templateChildName = fixPropertyName(templateChild.toString());
Class templateChildType = mapping.getPropertyType(templateChildName);
ClassMapping templateChildMapping = findClassMapping(templateChildType);
if(templateChildMapping != null)
{
if(templateChildMapping.getIcl() != null)
{
IclTerm templateChileIcl = templateChildMapping.getIcl();
if(templateChileIcl.isStruct() && iclChild.isStruct() &&
getFunctor(templateChileIcl).equals(getFunctor(iclChild)) &&
templateChileIcl.getNumChildren() == iclChild.getNumChildren())
{
varMapping.put(templateChildName, iclChild);
matchFound = true;
}
}
else if(templateChildMapping.getMapper() != null) matchFound = true;
}
}
// compare and map the template subtree:
else
{
VarMappingFinder listMappingFinder = new VarMappingFinder(mapping);
listMappingFinder.traverse(templateChild, iclChild);
if(!listMappingFinder.isMatchFailed())
{
varMapping.putAll(listMappingFinder.getVarMapping());
matchFound = true;
}
}
}
// no matching template was found:
if(!matchFound)
{
matchFailed("Icl match could not be found for " + iclChild);
}
}
return false;
}
return true;
}
catch(MappingException me)
{
throw me;
}
catch(Exception e)
{
throw new MappingException("Exception caught: " + e, e);
}
}
}
private class FunctionBinding
{
private FunctionMapping functionMapping;
private Map varMapping;
public FunctionMapping getFunctionMapping()
{
return functionMapping;
}
public void setFunctionMapping(FunctionMapping functionMapping)
{
this.functionMapping = functionMapping;
}
public Map getVarMapping()
{
return varMapping;
}
public void setVarMapping(Map varMapping)
{
this.varMapping = varMapping;
}
}
private class ResultSetter
extends IclDualTraversal
{
private IclTerm result;
public ResultSetter(IclTerm result)
{
this.result = result;
}
protected boolean visit(IclTerm template, IclTerm icl)
throws MappingException
{
if(template.isVar() && template.toString().equals("Result"))
{
super.replace(result);
return false;
}
return true;
}
}
private class PropertySetter
extends IclTraversal
{
private Map propertyMap;
private List unsetProperties = new LinkedList();
public PropertySetter(Map propertyMap)
{
this.propertyMap = propertyMap;
}
public List getUnsetProperties()
{
return unsetProperties;
}
/**
* Replace variables with properties. If the property is null,
* add it to the unset properties list.
*/
protected boolean visit(IclVar icl)
throws MappingException
{
String propertyName = fixPropertyName(icl.toString());
Object propertyValue = propertyMap.get(propertyName);
if(propertyValue != null)
super.replace(javaToIcl(propertyValue));
else
unsetProperties.add(propertyName);
return false;
}
/**
* Go through the elements of the list and set properties. If a list
* element has unset (null) properties, remove it from the list.
*/
protected boolean visit(IclList icl)
throws MappingException
{
PropertySetter listSetter = new PropertySetter(propertyMap);
Iterator iterator = icl.listIterator();
int index = 0;
while(iterator.hasNext())
{
IclTerm childIcl = (IclTerm)iterator.next();
listSetter.getUnsetProperties().clear();
listSetter.traverse(childIcl, icl, index);
if(listSetter.getUnsetProperties().size() > 0)
iterator.remove();
else
index++;
}
return false;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -