Author: DartPeng
Date: 2008-08-29 05:11:24 -0400 (Fri, 29 Aug 2008)
New Revision: 9956
Modified:
workspace/dart/plugins/trunk/org.jboss.tools.smooks.javabean/src/org/jboss/tools/smooks/javabean/analyzer/JavaBeanAnalyzer.java
workspace/dart/plugins/trunk/org.jboss.tools.smooks.javabean/src/org/jboss/tools/smooks/javabean/model/JavaBeanModel.java
Log:
add some function for analyze smooks config file to graphical model
Modified:
workspace/dart/plugins/trunk/org.jboss.tools.smooks.javabean/src/org/jboss/tools/smooks/javabean/analyzer/JavaBeanAnalyzer.java
===================================================================
---
workspace/dart/plugins/trunk/org.jboss.tools.smooks.javabean/src/org/jboss/tools/smooks/javabean/analyzer/JavaBeanAnalyzer.java 2008-08-29
09:09:48 UTC (rev 9955)
+++
workspace/dart/plugins/trunk/org.jboss.tools.smooks.javabean/src/org/jboss/tools/smooks/javabean/analyzer/JavaBeanAnalyzer.java 2008-08-29
09:11:24 UTC (rev 9956)
@@ -10,30 +10,42 @@
******************************************************************************/
package org.jboss.tools.smooks.javabean.analyzer;
+import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IProject;
import org.eclipse.emf.common.command.BasicCommandStack;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.command.CommandStack;
import org.eclipse.emf.ecore.resource.Resource;
+import org.eclipse.emf.ecore.util.ExtendedMetaData;
+import org.eclipse.emf.ecore.xml.type.AnyType;
+import org.eclipse.emf.ecore.xml.type.XMLTypePackage;
import org.eclipse.emf.edit.command.AddCommand;
import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
import org.eclipse.emf.edit.provider.resource.ResourceItemProviderAdapterFactory;
+import org.eclipse.jdt.core.JavaCore;
+import org.eclipse.jdt.core.JavaModelException;
import org.jboss.tools.smooks.analyzer.IAnalyzer;
import org.jboss.tools.smooks.analyzer.SmooksAnalyzerException;
+import org.jboss.tools.smooks.graphical.GraphInformations;
import org.jboss.tools.smooks.javabean.model.JavaBeanModel;
+import org.jboss.tools.smooks.javabean.model.JavaBeanModelFactory;
import org.jboss.tools.smooks.ui.gef.model.AbstractStructuredDataModel;
import org.jboss.tools.smooks.ui.gef.model.GraphRootModel;
import org.jboss.tools.smooks.ui.gef.model.IConnectableModel;
import org.jboss.tools.smooks.ui.gef.model.LineConnectionModel;
import org.jboss.tools.smooks.ui.gef.model.TreeItemRelationModel;
import org.jboss.tools.smooks.ui.modelparser.SmooksConfigurationFileGenerateContext;
+import org.jboss.tools.smooks.utils.ProjectClassLoader;
import org.jboss.tools.smooks.utils.UIUtils;
+import org.milyn.xsd.smooks.AbstractResourceConfig;
import org.milyn.xsd.smooks.ParamType;
import org.milyn.xsd.smooks.ResourceConfigType;
import org.milyn.xsd.smooks.ResourceType;
@@ -51,12 +63,20 @@
public static final String BEANPOPULATOR =
"org.milyn.javabean.BeanPopulator";
+ public static final String PRO_CLASS_NAME = "__pro_class_name_";
+
+ public static final Object PRO_PROJECT_NAME = "__pro_project_name_";
+
private List usedConnectionList = new ArrayList();
private ComposedAdapterFactory adapterFactory;
private AdapterFactoryEditingDomain editingDomain;
+ private HashMap userdResourceTypeMap = new HashMap();
+
+ private HashMap usedBeanIDMap = new HashMap();
+
public JavaBeanAnalyzer() {
adapterFactory = new ComposedAdapterFactory(
@@ -75,26 +95,37 @@
return new BasicCommandStack();
}
- public SmooksResourceListType analyzeGraphicalModel(
- AbstractStructuredDataModel root) {
+ public void analyzeGraphicalModel(AbstractStructuredDataModel root,
+ SmooksResourceListType resouceList) {
- SmooksResourceListType resourceList = SmooksFactory.eINSTANCE
- .createSmooksResourceListType();
-
List children = root.getChildren();
for (Iterator iterator = children.iterator(); iterator.hasNext();) {
TreeItemRelationModel dataModel = (TreeItemRelationModel) iterator
.next();
List targetConnections = dataModel.getModelTargetConnections();
if (targetConnections != null && !targetConnections.isEmpty()) {
- this.analyzeStructuredDataModel(resourceList, root, dataModel,
+ this.analyzeStructuredDataModel(resouceList, root, dataModel,
null, null);
}
}
+ }
- return resourceList;
+ protected void setSelectorIsUsed(String selector) {
+ userdResourceTypeMap.put(selector, new Object());
}
+ protected boolean beanIDIsUsed(String beanID) {
+ return (usedBeanIDMap.get(beanID) != null);
+ }
+
+ protected void setBeanIDIsUsed(String beanID) {
+ usedBeanIDMap.put(beanID, new Object());
+ }
+
+ protected boolean isSelectorIsUsed(String resourceType) {
+ return (userdResourceTypeMap.get(resourceType) != null);
+ }
+
private boolean connectionIsUsed(Object connection) {
return (usedConnectionList.indexOf(connection) != -1);
}
@@ -252,7 +283,8 @@
.getReferenceEntityModel();
JavaBeanModel currentbean = (JavaBeanModel) target
.getReferenceEntityModel();
- if (sourcebean.getParent() == currentRootModel.getReferenceEntityModel()) {
+ if (sourcebean.getParent() == currentRootModel
+ .getReferenceEntityModel()) {
if (!currentbean.isPrimitive()) {
return "${" + currentbean.getName() + "}";
} else {
@@ -260,17 +292,21 @@
+ sourcebean.getName();
}
} else {
- JavaBeanModel jbParent =
((JavaBeanModel)source.getReferenceEntityModel()).getParent();
- AbstractStructuredDataModel parent =
UIUtils.findGraphModel(currentRootModel.getParent(), jbParent);
+ JavaBeanModel jbParent = ((JavaBeanModel) source
+ .getReferenceEntityModel()).getParent();
+ AbstractStructuredDataModel parent = UIUtils.findGraphModel(
+ currentRootModel.getParent(), jbParent);
String returnString = sourcebean.getName();
while (parent != currentRootModel && parent != null) {
JavaBeanModel jbm = (JavaBeanModel) parent
.getReferenceEntityModel();
if (jbm != null)
returnString = jbm.getName() + " " + returnString;
-
- JavaBeanModel jb = ((JavaBeanModel)parent.getReferenceEntityModel()).getParent();
- parent = UIUtils.findGraphModel(currentRootModel.getParent(), jb);
+
+ JavaBeanModel jb = ((JavaBeanModel) parent
+ .getReferenceEntityModel()).getParent();
+ parent = UIUtils.findGraphModel(currentRootModel.getParent(),
+ jb);
}
// if no property
if (returnString.equals(sourcebean.getName())) {
@@ -293,11 +329,151 @@
}
- public SmooksResourceListType analyzeMappingGraphModel(
+ public void analyzeMappingGraphModel(
SmooksConfigurationFileGenerateContext context)
throws SmooksAnalyzerException {
GraphRootModel root = context.getDataMappingRootModel();
- SmooksResourceListType listType = this.analyzeGraphicalModel(root);
- return listType;
+ SmooksResourceListType listType = context.getSmooksResourceListModel();
+ this.analyzeGraphicalModel(root, listType);
}
+
+ public List<LineConnectionModel> analyzeMappingSmooksModel(
+ SmooksResourceListType listType) {
+ return null;
+ }
+
+ public Object buildSourceInputObjects(GraphInformations graphInfo,
+ SmooksResourceListType listType, IFile sourceFile)
+ throws InvocationTargetException {
+ List resourceConfigList = listType.getAbstractResourceConfig();
+ String rootClassName = null;
+ ResourceConfigType current = null;
+ for (Iterator iterator = resourceConfigList.iterator(); iterator
+ .hasNext();) {
+ AbstractResourceConfig ar = (AbstractResourceConfig) iterator
+ .next();
+ if (ar instanceof ResourceConfigType) {
+ ResourceConfigType rc = (ResourceConfigType) ar;
+ ResourceType resourceType = rc.getResource();
+ String resource = resourceType.getValue();
+ if (BEANPOPULATOR.equals(resource)) {
+ // create root beanmodel
+ rootClassName = rc.getSelector();
+ current = rc;
+ break;
+ }
+ }
+ }
+
+ if (rootClassName == null)
+ return null;
+ IProject project = sourceFile.getProject();
+ boolean isWarning = false;
+ boolean isError = false;
+ Class clazz = null;
+ try {
+ ProjectClassLoader loader = new ProjectClassLoader(JavaCore
+ .create(project));
+ clazz = loader.loadClass(rootClassName);
+ } catch (Exception e) {
+ // ignore
+ }
+ JavaBeanModel model = null;
+ if (clazz != null) {
+ model = JavaBeanModelFactory.getJavaBeanModelWithLazyLoad(clazz);
+ } else {
+ model = new JavaBeanModel(null, rootClassName);
+ model.setRootClassModel(true);
+ model.setError(true);
+ isError = true;
+ }
+ if (model != null) {
+ this.setSelectorIsUsed(rootClassName);
+ buildSourceInputProperties(listType, model, false, isError, current);
+ }
+ return null;
+ }
+
+ protected void buildSourceInputProperties(SmooksResourceListType listType,
+ JavaBeanModel beanModel, boolean rootIsWarning,
+ boolean rootIsError, ResourceConfigType currentResourceConfigType) {
+ if (currentResourceConfigType != null) {
+ List paramList = currentResourceConfigType.getParam();
+ for (Iterator iterator = paramList.iterator(); iterator.hasNext();) {
+ ParamType param = (ParamType) iterator.next();
+ if ("bindings".equals(param.getName())) {
+ if (param.eContents().isEmpty())
+ continue;
+ List bindingList = (List) param.getMixed().get(
+ SmooksModelUtils.ELEMENT_BINDING, false);
+ for (Iterator iterator2 = bindingList.iterator(); iterator2
+ .hasNext();) {
+ AnyType binding = (AnyType) iterator2.next();
+ String selector = (String) binding
+ .getAnyAttribute()
+ .get(SmooksModelUtils.ATTRIBUTE_SELECTOR, false);
+ }
+ }
+ }
+ }
+ }
+
+ protected void analyzeBindingSelector(String selector,
+ JavaBeanModel currentModel, SmooksResourceListType listType) {
+ if (selector.startsWith("${") && selector.endsWith("}")) {
+ // should get the bean properties
+ // memory out???
+ currentModel.setProperties(null);
+ currentModel.getProperties();
+ selector = selector.substring(2, selector.length() - 1);
+ JavaBeanModel model = findTheChildJavaBeanModel(selector,
+ currentModel);
+ // something wrong
+ if (model == null) {
+ JavaBeanModel errorModel = new JavaBeanModel(null, selector);
+ currentModel.addProperty(errorModel);
+ errorModel.setError("don't exist");
+ List rl = listType.getAbstractResourceConfig();
+ for (Iterator iterator = rl.iterator(); iterator.hasNext();) {
+ ResourceConfigType rct = (ResourceConfigType) iterator
+ .next();
+ if (this.isSelectorIsUsed(rct.getSelector()))
+ continue;
+ String beanId = getBeanIDFromParam(rct);
+ if (selector.equals(beanId)) {
+ buildSourceInputProperties(listType, errorModel, false,
+ true, rct);
+ }
+ }
+ }
+ }
+ }
+
+ protected String getBeanIDFromParam(ResourceConfigType config) {
+ List list = config.getParam();
+ for (Iterator iterator = list.iterator(); iterator.hasNext();) {
+ ParamType p = (ParamType) iterator.next();
+ if ("beanId".equals(p.getName())) {
+ Object obj = p.getMixed().get(
+ XMLTypePackage.Literals.XML_TYPE_DOCUMENT_ROOT__TEXT,
+ true);
+ if (obj != null)
+ return obj.toString().trim();
+ }
+
+ }
+ return null;
+ }
+
+ protected JavaBeanModel findTheChildJavaBeanModel(String name,
+ JavaBeanModel parentModel) {
+ List list = parentModel.getProperties();
+ for (Iterator iterator = list.iterator(); iterator.hasNext();) {
+ JavaBeanModel child = (JavaBeanModel) iterator.next();
+ if (name.equals(child.getName())) {
+ return child;
+ }
+ }
+ return null;
+ }
}
Modified:
workspace/dart/plugins/trunk/org.jboss.tools.smooks.javabean/src/org/jboss/tools/smooks/javabean/model/JavaBeanModel.java
===================================================================
---
workspace/dart/plugins/trunk/org.jboss.tools.smooks.javabean/src/org/jboss/tools/smooks/javabean/model/JavaBeanModel.java 2008-08-29
09:09:48 UTC (rev 9955)
+++
workspace/dart/plugins/trunk/org.jboss.tools.smooks.javabean/src/org/jboss/tools/smooks/javabean/model/JavaBeanModel.java 2008-08-29
09:11:24 UTC (rev 9956)
@@ -19,13 +19,18 @@
import java.util.List;
import org.apache.commons.beanutils.PropertyUtils;
+import org.jboss.tools.smooks.analyzer.IValidatable;
/**
* @author Dart Peng
*
*/
-public class JavaBeanModel {
+public class JavaBeanModel implements IValidatable {
+ private Object waring = null;
+
+ private Object error = null;
+
private String name = "";
private Class typeRef = null;
@@ -37,7 +42,7 @@
private boolean isPrimitive = false;
private Class<? extends Object> beanClass = null;
-
+
private JavaBeanModel parent = null;
public Class getBeanClass() {
@@ -122,6 +127,8 @@
boolean lazyLoadProperties) {
this.lazyLoadProperties = lazyLoadProperties;
this.beanClass = beanClass;
+ if (beanClass == null)
+ return;
if (beanName == null) {
beanName = beanClass.getSimpleName();
}
@@ -177,6 +184,10 @@
this(beanClass, null, null, null, true);
}
+ public JavaBeanModel(Class beanClass, String name) {
+ this(beanClass, name, null, null, true);
+ }
+
JavaBeanModel(Class beanClass, boolean lazyLoadProperties) {
this(beanClass, null, null, null, lazyLoadProperties);
}
@@ -222,7 +233,7 @@
}
public void addProperty(JavaBeanModel property) {
- if(properties != null){
+ if (properties != null) {
properties.add(property);
property.setParent(this);
}
@@ -279,10 +290,27 @@
}
/**
- * @param parent the parent to set
+ * @param parent
+ * the parent to set
*/
public void setParent(JavaBeanModel parent) {
this.parent = parent;
}
+ public Object getWarning() {
+ return waring;
+ }
+
+ public void setWarning(Object waring) {
+ this.waring = waring;
+ }
+
+ public Object getError() {
+ return error;
+ }
+
+ public void setError(Object error) {
+ this.error = error;
+ }
+
}