001: package org.andromda.metafacades.emf.uml2;
002:
003: import java.util.ArrayList;
004: import java.util.Collection;
005: import java.util.Collections;
006: import java.util.Iterator;
007:
008: import org.andromda.core.common.ExceptionUtils;
009: import org.andromda.core.configuration.Filters;
010: import org.andromda.core.metafacade.MetafacadeBase;
011: import org.andromda.core.metafacade.MetafacadeConstants;
012: import org.andromda.core.metafacade.MetafacadeFactory;
013: import org.andromda.core.metafacade.ModelAccessFacade;
014: import org.andromda.metafacades.uml.ModelElementFacade;
015: import org.andromda.metafacades.uml.PackageFacade;
016: import org.apache.commons.collections.CollectionUtils;
017: import org.apache.commons.collections.Predicate;
018: import org.apache.commons.lang.StringUtils;
019: import org.eclipse.emf.common.util.TreeIterator;
020: import org.eclipse.emf.ecore.EObject;
021: import org.eclipse.uml2.Element;
022: import org.eclipse.uml2.NamedElement;
023: import org.eclipse.uml2.util.UML2Resource;
024:
025: /**
026: * Model access facade implementation for the EMF/UML2 metafacades
027: *
028: * @author Steve Jerman
029: * @author Chad Brandon
030: */
031: public class UMLModelAccessFacade implements ModelAccessFacade {
032: /**
033: * Protected to improve performance.
034: */
035: protected Filters modelPackages = new Filters();
036:
037: /**
038: * @see org.andromda.core.metafacade.ModelAccessFacade#setModel(java.lang.Object)
039: */
040: public void setModel(final Object model) {
041: ExceptionUtils.checkNull("model", model);
042: ExceptionUtils.checkAssignable(UML2Resource.class,
043: "modelElement", model.getClass());
044: this .model = (UML2Resource) model;
045: }
046:
047: /**
048: * @see org.andromda.core.metafacade.ModelAccessFacade#getModel()
049: */
050: public Object getModel() {
051: return this .model;
052: }
053:
054: /**
055: * @see org.andromda.core.metafacade.ModelAccessFacade#getName(java.lang.Object)
056: */
057: public String getName(final Object modelElement) {
058: ExceptionUtils.checkNull("modelElement", modelElement);
059: ExceptionUtils.checkAssignable(ModelElementFacade.class,
060: "modelElement", modelElement.getClass());
061: return ((ModelElementFacade) modelElement).getName();
062: }
063:
064: /**
065: * @see org.andromda.core.metafacade.ModelAccessFacade#getPackageName(java.lang.Object)
066: */
067: public String getPackageName(final Object modelElement) {
068: ExceptionUtils.checkNull("modelElement", modelElement);
069: ExceptionUtils.checkAssignable(ModelElementFacade.class,
070: "modelElement", modelElement.getClass());
071: final ModelElementFacade modelElementFacade = (ModelElementFacade) modelElement;
072: final StringBuffer packageName = new StringBuffer(
073: modelElementFacade.getPackageName(true));
074:
075: // - if the model element is a package then the package name will be the
076: // name
077: // of the package with its package name
078: if (modelElement instanceof PackageFacade) {
079: final String name = modelElementFacade.getName();
080: if (StringUtils.isNotBlank(name)) {
081: packageName
082: .append(MetafacadeConstants.NAMESPACE_SCOPE_OPERATOR);
083: packageName.append(name);
084: }
085: }
086: return packageName.toString();
087: }
088:
089: /**
090: * The actual underlying model instance.
091: */
092: private UML2Resource model;
093:
094: /**
095: * @see org.andromda.core.metafacade.ModelAccessFacade#setPackageFilter(org.andromda.core.configuration.Filters)
096: */
097: public void setPackageFilter(final Filters modelPackages) {
098: this .modelPackages = modelPackages;
099: }
100:
101: /**
102: * @see org.andromda.core.metafacade.ModelAccessFacade#getStereotypeNames(java.lang.Object)
103: */
104: public Collection getStereotypeNames(final Object modelElement) {
105: ExceptionUtils.checkNull("modelElement", modelElement);
106:
107: Collection stereotypeNames = Collections.EMPTY_LIST;
108: if (modelElement instanceof Element) {
109: Element element = (Element) modelElement;
110: stereotypeNames = UmlUtilities.getStereotypeNames(element);
111: } else if (modelElement instanceof ModelElementFacade) {
112: stereotypeNames = ((ModelElementFacade) modelElement)
113: .getStereotypeNames();
114: }
115: return stereotypeNames;
116: }
117:
118: /**
119: * @see org.andromda.core.metafacade.ModelAccessFacade#findByStereotype(java.lang.String)
120: */
121: public Collection findByStereotype(final String name) {
122: final ArrayList elements = new ArrayList();
123: for (TreeIterator iterator = UmlUtilities.findModel(this .model)
124: .eAllContents(); iterator.hasNext();) {
125: final EObject object = (EObject) iterator.next();
126: if (object instanceof NamedElement) {
127: final NamedElement element = (NamedElement) object;
128: if (UmlUtilities.containsStereotype(element, name)) {
129: elements.add(MetafacadeFactory.getInstance()
130: .createMetafacade(element));
131: }
132: }
133: }
134: return elements;
135: }
136:
137: /**
138: * @see org.andromda.core.metafacade.ModelAccessFacade#getModelElements()
139: */
140: public Collection getModelElements() {
141: final ArrayList elements = new ArrayList();
142:
143: for (final Iterator iterator = UmlUtilities.findModel(
144: this .model).eAllContents(); iterator.hasNext();) {
145: final EObject object = (EObject) iterator.next();
146: if (object instanceof NamedElement) {
147: elements.add(object);
148: }
149: }
150:
151: // Don't forget to transform properties
152: CollectionUtils.transform(elements,
153: UmlUtilities.ELEMENT_TRANSFORMER);
154:
155: final Collection metafacades;
156:
157: if (elements.isEmpty()) {
158: metafacades = Collections.EMPTY_LIST;
159: } else {
160: metafacades = MetafacadeFactory.getInstance()
161: .createMetafacades(elements);
162: this .filterMetafacades(metafacades);
163: }
164:
165: return metafacades;
166: }
167:
168: /**
169: * Filters out those metafacades which <strong>should </strong> be
170: * processed.
171: *
172: * @param metafacades the Collection of metafacades.
173: */
174: private void filterMetafacades(final Collection metafacades) {
175: if (this .modelPackages != null && !this .modelPackages.isEmpty()) {
176: CollectionUtils.filter(metafacades, new Predicate() {
177: public boolean evaluate(final Object metafacade) {
178: boolean valid = false;
179: if (metafacade instanceof MetafacadeBase) {
180: final ModelElementFacade modelElementFacade = (ModelElementFacade) metafacade;
181: final StringBuffer packageName = new StringBuffer(
182: modelElementFacade.getPackageName(true));
183:
184: // - if the model element is a package then the package
185: // name will be the name
186: // of the package with its package name
187: if (metafacade instanceof PackageFacade) {
188: final String name = modelElementFacade
189: .getName();
190: if (StringUtils.isNotBlank(name)) {
191: packageName
192: .append(MetafacadeConstants.NAMESPACE_SCOPE_OPERATOR);
193: packageName.append(name);
194: }
195: }
196: valid = UMLModelAccessFacade.this.modelPackages
197: .isApply(packageName.toString());
198: }
199: return valid;
200: }
201: });
202: }
203: }
204: }
|