【Java开源代码栏目提醒】:网学会员为需要Java开源代码的朋友们搜集整理了ClassMapping.java相关资料,希望对各位网友有所帮助!
//$Id: ClassMapping.java,v 1.9 2003/09/27 11:17:10 maxcsaucdk Exp $
package net.sf.hibernate.tool.hbm2java;
import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import net.sf.hibernate.CompositeUserType;
import net.sf.hibernate.UserType;
import net.sf.hibernate.type.TypeFactory;
import net.sf.hibernate.type.PrimitiveType;
import net.sf.hibernate.type.Type;
import net.sf.hibernate.util.ReflectHelper;
import net.sf.hibernate.util.StringHelper;
import org.apache.commons.collections.MultiMap;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jdom.Attribute;
import org.jdom.Element;
public class ClassMapping {
static private Log log = LogFactory.getLog(ClassMapping.class);
private ClassName name = null;
private ClassName generatedName = null;
private String superClass = null;
private ClassMapping superClassMapping = null;
private String proxyClass = null;
private List fields = new ArrayList();
private TreeSet imports = new TreeSet();
private List subclasses = new ArrayList();
private static final Map components = new HashMap();
private boolean mustImplementEquals = false;
private MultiMap metaattribs;
private boolean shouldBeAbstract = false;
public ClassMapping(ClassName superClass, ClassMapping superClassMapping, Element classElement, MultiMap inheritedMeta) {
this(superClass, classElement, inheritedMeta);
this.superClassMapping = superClassMapping;
if(this.superClassMapping!=null) {
List l = this.superClassMapping.getAllFieldsForFullConstructor();
for (Iterator iter = l.iterator(); iter.hasNext();) {
Field element = (Field) iter.next();
ClassName ct = element.getClassType();
if(ct!=null) { // add imports for superclasses possible fields.
addImport(ct);
} else {
addImport(element.getType());
}
}
}
}
public ClassMapping(ClassName superClass, Element classElement, MultiMap inheritedMeta) {
initWith(superClass, classElement, false, inheritedMeta);
}
public ClassMapping(Element classElement, MultiMap inheritedMeta) {
initWith(null, classElement, false, inheritedMeta);
}
public ClassMapping(Element classElement, boolean component, MultiMap inheritedMeta) {
initWith(null, classElement, component, inheritedMeta);
}
protected void initWith(ClassName superClass, Element classElement, boolean component, MultiMap inheritedMeta) {
String fullyQualifiedName = classElement.getAttributeValue(component?"class":"name");
log.debug("Processing mapping for class: " + fullyQualifiedName);
setMetaAttribs(MetaAttributeHelper.loadAndMergeMetaMap(classElement, inheritedMeta));
// class & package names
name = new ClassName();
name.setFullyQualifiedName(fullyQualifiedName);
if(getMeta("generated-class")!=null) {
generatedName = new ClassName();
generatedName.setFullyQualifiedName(getMetaAsString("generated-class").trim());
shouldBeAbstract = true;
log.warn("Generating " + generatedName + " instead of " + name);
} else {
generatedName = name;
}
if(superClass!=null) {
this.superClass = superClass.getName();
addImport(superClass); // can only be done AFTER this class gets its own name.
}
// get the properties defined for this class
List propertyList = new ArrayList();
propertyList.addAll( classElement.getChildren("property") );
propertyList.addAll( classElement.getChildren("version") );
propertyList.addAll( classElement.getChildren("timestamp") );
propertyList.addAll( classElement.getChildren("key-property") );
propertyList.addAll( classElement.getChildren("any"));
// get all many-to-one associations defined for the class
List manyToOneList = new ArrayList();
manyToOneList.addAll( classElement.getChildren("many-to-one") );
manyToOneList.addAll( classElement.getChildren("key-many-to-one") );
Attribute att = classElement.getAttribute("proxy");
if (att!=null) proxyClass = att.getValue();
Element id = classElement.getChild("id");
if (id != null) {
propertyList.add(0, id);
implementEquals();
}
// composite id
Element cmpid = classElement.getChild("composite-id");
if (cmpid != null) {
implementEquals();
String cmpname = cmpid.getAttributeValue("name");
String cmpclass = cmpid.getAttributeValue("class");
if ( cmpclass==null || cmpclass.equals(StringHelper.EMPTY_STRING) ) {
//Embedded composite id
//implementEquals();
propertyList.addAll(0, cmpid.getChildren("key-property") );
manyToOneList.addAll(0, cmpid.getChildren("key-many-to-one") );
}
else {
//Composite id class
ClassMapping mapping = new ClassMapping(cmpid, true, metaattribs);
MultiMap metaForCompositeid = MetaAttributeHelper.loadAndMergeMetaMap(cmpid, metaattribs);
mapping.implementEquals();
ClassName classType = new ClassName();
classType.setFullyQualifiedName(cmpclass);
// add an import and field for this property
addImport(classType);
Field cmpidfield = new Field(cmpname, classType, false, true, false, metaForCompositeid);
fields.add(cmpidfield);
components.put( mapping.getCanonicalName(), mapping);
}
}
// derive the class imports and fields from the properties
for (Iterator properties = propertyList.iterator(); properties.hasNext();) {
Element property = (Element) properties.next();
MultiMap metaForProperty = MetaAttributeHelper.loadAndMergeMetaMap(property, metaattribs);
String name = property.getAttributeValue("name");
if ( name == null || name.trim().equals(StringHelper.EMPTY_STRING) ) {
continue; //since an id doesn't necessarily need a name
}
// ensure that the type is specified
String type = property.getAttributeValue("type");
if (type == null && cmpid != null) { // for composite-keys
type = property.getAttributeValue("class");
}
if("timestamp".equals(property.getName())){
type = "java.util.Date";
}
if("any".equals(property.getName())) {
type = "java.lang.Object";
}
if ( type == null || type.trim().equals(StringHelper.EMPTY_STRING) ) {
log.warn("property \"" + name + "\" in class " + getName() + " is missing a type attribute");
continue;
}
// handle in a different way id and properties...
// ids may be generated and may need to be of object type in order to support
// the unsaved-value "null" value.
// Properties may be nullable (ids may not)
if (property == id) {
Element generator = property.getChild("generator");
String unsavedValue = property.getAttri
上一篇:
Classmanytoone.java
下一篇:
致享和威驰哪个好点?冲着安全和空间 推荐致享