Word-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee" twffan="done">
import
java.io.IOException;
import java.util.Iterator;
import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.util.Diagnostician;
import org.eclipse.emf.ecore.xmi.XMLResource;
import org.eclipse.emf.ecore.xmi.impl.XMLResourceImpl;
import org.steeven.family.FamilyFactory;
import org.steeven.family.人物;
import org.steeven.family.家庭;
import org.steeven.family.性别;
public class TestMy
{
public static void main(String[] args) throws IOException
{
testFamily();
}
PRivate static void testFamily() throws IOException
{
家庭 family = FamilyFactory.eINSTANCE.create家庭();
family.setTitle( " steeven家 " );
family.set老公(FamilyFactory.eINSTANCE.create人物());
family.get老公().set姓名( " steeven " );
family.set老婆(FamilyFactory.eINSTANCE.create人物());
family.get老婆().set姓名( " stella " );
family.get老婆().set性别(性别.女_LITERAL);
人物 sophie = FamilyFactory.eINSTANCE.create人物();
sophie.set姓名( " sophie " );
sophie.set性别(性别.女_LITERAL);
family.get兔崽子().add(sophie);
dump(family);
validate(family);
}
private static void validate(EObject family)
{
Diagnostic diagnostic = Diagnostician.INSTANCE.validate(family);
System.out.println(diagnostic);
for (Iterator it = diagnostic.getChildren().iterator(); it.hasNext();)
{
Diagnostic diag = (Diagnostic) it.next();
System.out.println(diag.getMessage());
}
}
private static Resource dump(EObject
objs) throws IOException
{
// ResourceSet rs = new ResourceSetImpl();
// rs.getResourceFactoryRegistry().getExtensionToFactoryMap().put(
// Resource.Factory.Registry.DEFAULT_EXTENSION,
// new XMIResourceFactoryImpl());
// Resource resource = rs.createResource(URI
// .createFileURI("c://temp//test.xml"));
XMLResource resource = new XMLResourceImpl(URI
.createFileURI( " c://temp//test.xml " ));
resource.setEncoding( " GBK " );
for (EObject obj : objs)
resource.getContents().add(obj); // 目前版本不加入resource验证会报singling异常
resource.save(System.out, null );
return resource;
}
}