📄 defaultentitytype.java
字号:
/*
* Created on 13-Apr-2005
*
* TODO All
*/
package ke.defaultimpl.core;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import ke.core.ConceptInstance;
import ke.core.CoreException;
import ke.core.EntityType;
import ke.defaultimpl.utils.Utils;
import ke.defaultimpl.utils.UtilsException;
import ke.defaultimpl.utils.xml.parsing.SchemaBean;
/**
* @author James Cunningham
*
* @version
*
* TODO All
*/
public class DefaultEntityType extends EntityType
{
/**
* @param id
* @param name
* an instance of a DefaultEntityType may have property and type pairs
*/
public DefaultEntityType(String id,
String name,
String schemaFile) throws CoreException {
super(id, name);
this.createPropertyClassMap(schemaFile);
}
/**
* @param id
* @param name
* @param propertyNameClassMap
* @throws CoreException
*/
public DefaultEntityType(String id,
String name,
HashMap propertyNameClassMap) throws CoreException {
super(id, name);
try {
for(Iterator i = propertyNameClassMap.keySet().iterator(); i.hasNext();) {
String key = (String)i.next();
if(!String.class.isAssignableFrom(propertyNameClassMap.get(key).getClass())) {
throw new CoreException("Error: Bad property name class map supplied");
}
else {
// property name must be the type which can be convered to string
Class c = Class.forName((String)propertyNameClassMap.get(key));
if(!String.class.isAssignableFrom(c) &&
!Number.class.isAssignableFrom(c) &&
!Character.class.isAssignableFrom(c)) {
throw new CoreException("Error: class type must convert to/from String");
}
}
}
this.propertyNameClassMap = propertyNameClassMap;
} catch (ClassNotFoundException cnfe) {
throw new CoreException("Error: bad class name in schema");
}
}
/**
* @param schemaFile, which defines property and type pairs
*/
private void createPropertyClassMap(String schemaFile) throws CoreException {
try {
SchemaBean sBean = Utils.getAttributesFromSchema(schemaFile);
String[] names = sBean.getAttributeName();
String[] types = sBean.getAttributeType();
this.propertyNameClassMap = new HashMap();
Class c;
for(int i = 0; i < names.length; i++) {
// test type - needs to be a fully qualified java
// class name in the classpath and String, Number or
// Character
c = Class.forName(types[i]);
if(!String.class.isAssignableFrom(c) &&
!Number.class.isAssignableFrom(c) &&
!Character.class.isAssignableFrom(c)) {
throw new CoreException("Error: class type must convert to/from String");
}
this.propertyNameClassMap.put(names[i], types[i]);
}
} catch (UtilsException ue) {
throw new CoreException("Error: " + ue);
} catch (ClassNotFoundException cnfe) {
throw new CoreException("Error: bad class name in schema");
}
}
/**
* @param id
* @return
* @see ke.core.ConceptType#createInstance(java.lang.String)
*/
public ConceptInstance createInstance(String id) {
HashMap propVals = new HashMap();
for(Iterator i = this.propertyNameClassMap.keySet().iterator(); i.hasNext();) {
String prop = (String)i.next();
try {
// we will instantiate an object for the instance's property
// with the default constructor if one exists, otherwise
// we will put a null object in the map
// TODO (is this the best behaviour?)
Class c = this.getPropertyClass(prop);
propVals.put(prop, c.newInstance());
} catch (InstantiationException ie) {
propVals.put(prop, null);
} catch (Exception e) {
// TODO add logging
System.out.print("Bad class name found for property " + prop);
System.out.print(" when creating instance of " + this.getName());
System.out.println("... ignoring property in instance.");
}
}
return new DefaultEntity(id, this, propVals);
}
/**
* @return
* @see ke.core.ConceptType#getPropertyNames()
*/
public String[] getPropertyNames() {
Set namesSet = this.propertyNameClassMap.keySet();
return (String[])namesSet.toArray(new String[namesSet.size()]);
}
/**
* @param propertyName
* @return
* @see ke.core.ConceptType#getPropertyClass(java.lang.String)
*/
public Class getPropertyClass(String propertyName) throws CoreException {
if(!this.propertyNameClassMap.keySet().contains(propertyName))
throw new CoreException("Error: bad property name");
try {
Class c = Class.forName((String)this.propertyNameClassMap.get(propertyName));
return c;
} catch (ClassNotFoundException cnfe) {
throw new CoreException("Error: bad class name in type definition!");
}
}
//------------------------------------
private HashMap propertyNameClassMap; //which is set of property name and type pairs (String,String)
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -