下面列出了怎么用org.eclipse.emf.ecore.EFactory的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
public void testFillIdToEObjectMap() {
EPackage pack = EcoreFactory.eINSTANCE.createEPackage();
EClass root = createEClass(pack, "Root");
EClass someType = createEClass(pack, "SomeType");
EReference ref1 = addEReference(root, someType, "ref1", false);
EReference ref2 = addEReference(root, someType, "ref2", true);
EFactory factory = pack.getEFactoryInstance();
EObject rootObject = factory.create(root);
EObject someTypeObject1 = factory.create(someType);
EObject someTypeObject2 = factory.create(someType);
rootObject.eSet(ref1, someTypeObject1);
rootObject.eSet(ref2, someTypeObject2);
List<EObject> map = new ArrayList<>();
SerializationUtil.fillIdToEObjectMap(rootObject, map);
assertTrue(map.contains(rootObject));
assertTrue(map.contains(someTypeObject1));
assertFalse(map.contains(someTypeObject2));
assertEquals(2, map.size());
}
@Before
public void init() {
EcoreFactory modelFactory = EcoreFactory.eINSTANCE;
testModelPackage = modelFactory.createEPackage();
testModelPackage.setName("TypeProviderTestEPackage");
testModelPackage.setNsPrefix("typeprovidertestpackage");
testModelPackage.setNsURI("http://testabstracttype");
EFactory instanceFactory = testModelPackage.getEFactoryInstance();
EClass clazz = createEClass("ExpressionContainer");
expressionContainerReference = modelFactory.createEReference();
clazz.getEStructuralFeatures().add(expressionContainerReference);
expressionContainerReference.setName("expression");
expressionContainerReference.setEType(typeModelPackage.getIExpression());
expressionContainerReference.setContainment(true);
expression1Container = instanceFactory.create(clazz);
expression1Container.eSet(expressionContainerReference, expression1);
expression2Container = instanceFactory.create(clazz);
expression2Container.eSet(expressionContainerReference, expression2);
expression3Container = instanceFactory.create(clazz);
expression3Container.eSet(expressionContainerReference, expression3);
}
private void registerFactory(IConfigurationElement configurationElement) {
try {
final String nsURI = configurationElement.getAttribute(NS_URI);
final String bundle = configurationElement.getContributor().getName();
final String className = configurationElement.getAttribute(CLASS);
final Class<? extends EFactory> clazz = loadClass(bundle, className);
if (nsURI == null || clazz == null) {
return;
}
final boolean useWildcards = Boolean.parseBoolean(configurationElement.getAttribute(USE_WILDCARDS));
nsURIToFactoryMap.put(nsURI, clazz);
wildcardsUsageMap.put(nsURI, useWildcards);
} catch (final ClassNotFoundException e) {
MigrationPlugin.INSTANCE.log(e);
}
}
/**
* Returns the {@link EFactory} registered for the given nsURI, taking wildcards into account, if such a factory
* exists.
*
* @param nsURI
* @return if there exists a factory registered for a matching nsURI, without any wildcards, it will be returned
* first. Otherwise, the method will return the first factory that it can match (using wildcards) to the
* given nsURI
* May return null.
*/
private Class<? extends EFactory> getEFactoryFromMap(String nsURI) {
if (nsURI == null) {
return null;
}
// if we have an exact match, that doesn't use wildcards, return the value from the factory map
if (wildcardsUsageMap.get(nsURI) != null && !wildcardsUsageMap.get(nsURI)) {
return nsURIToFactoryMap.get(nsURI);
}
// else, search if we can find a match using wildcards
// if multiple matches exist, the first one will be returned
for (final String uri : wildcardsUsageMap.keySet()) {
if (wildcardsUsageMap.get(uri) && nsURI.matches(uri.replace("*", ".*"))) { //$NON-NLS-1$ //$NON-NLS-2$
return nsURIToFactoryMap.get(uri);
}
}
// fallback to factory map
return nsURIToFactoryMap.get(nsURI);
}
protected String formatAttributeValue(EObject object, EAttribute feature,
Object value) {
if (value == null)
return "null";
EFactory factory = feature.getEAttributeType().getEPackage()
.getEFactoryInstance();
String stringVal = factory.convertToString(feature.getEAttributeType(),
value);
return "'" + stringVal + "'";
}
@SuppressWarnings("unchecked")
private static Class<? extends EFactory> loadClass(String bundleName,
String clazz) throws ClassNotFoundException {
final Bundle bundle = Platform.getBundle(bundleName);
if (bundle == null) {
MigrationPlugin.INSTANCE.log("Could not get bundle " + bundleName + " from platform."); //$NON-NLS-1$ //$NON-NLS-2$
}
return (Class<? extends EFactory>) bundle.loadClass(clazz);
}
@Override
public EFactory getEFactory() {
return WfgraphFactory.eINSTANCE;
}
@Test public void testGetUnknownValue() {
EFactory fact = factory.createEFactory();
String name = nameResolver.getValue(fact);
assertNull(name);
}
/** {@inheritDoc} */
@Override
public EFactory getEFactory(String nsURI) {
return delegate.getEFactory(nsURI);
}
private FactoryHelper() {
nsURIToFactoryMap = new LinkedHashMap<String, Class<? extends EFactory>>();
wildcardsUsageMap = new HashMap<String, Boolean>();
readExtensionPoint();
}
/**
* Returns the EFactory for this {@link GraphExtension}
*
* @return
*/
EFactory getEFactory();