import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.util.Collection; import java.util.Collections; 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 javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import net.ihe.gazelle.oasis.OASISPackage; import net.ihe.gazelle.oasis.impl.OASISPackageImpl; import net.ihe.gazelle.v3.V3Package; import net.ihe.gazelle.v3.impl.V3PackageImpl; import net.ihe.gazelle.xdw.DocumentRoot; import net.ihe.gazelle.xdw.TXDWWorkflowDocument; import net.ihe.gazelle.xdw.XDWPackage; import net.ihe.gazelle.xdw.impl.XDWPackageImpl; import net.ihe.gazelle.xdw.util.XDWResourceFactoryImpl; import org.eclipse.emf.common.notify.Adapter; import org.eclipse.emf.common.notify.Notification; import org.eclipse.emf.common.util.EList; import org.eclipse.emf.common.util.TreeIterator; import org.eclipse.emf.common.util.URI; import org.eclipse.emf.ecore.EAnnotation; import org.eclipse.emf.ecore.EAttribute; import org.eclipse.emf.ecore.EClass; import org.eclipse.emf.ecore.EClassifier; import org.eclipse.emf.ecore.EDataType; import org.eclipse.emf.ecore.EGenericType; import org.eclipse.emf.ecore.EObject; import org.eclipse.emf.ecore.EOperation; import org.eclipse.emf.ecore.EPackage; import org.eclipse.emf.ecore.EReference; import org.eclipse.emf.ecore.EStructuralFeature; import org.eclipse.emf.ecore.EcorePackage; import org.eclipse.emf.ecore.impl.EStructuralFeatureImpl; import org.eclipse.emf.ecore.resource.Resource; import org.eclipse.emf.ecore.resource.ResourceSet; import org.eclipse.emf.ecore.resource.impl.ResourceFactoryRegistryImpl; import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; import org.eclipse.emf.ecore.util.BasicExtendedMetaData; import org.eclipse.emf.ecore.util.EcoreUtil; import org.eclipse.emf.ecore.util.ExtendedMetaData; import org.eclipse.emf.ecore.util.FeatureMap; import org.eclipse.emf.ecore.util.FeatureMapUtil; import org.eclipse.emf.ecore.xmi.XMLResource; import org.eclipse.emf.ecore.xmi.impl.EcoreResourceFactoryImpl; import org.eclipse.emf.ecore.xmi.impl.GenericXMLResourceFactoryImpl; import org.eclipse.xsd.XSDAttributeDeclaration; import org.eclipse.xsd.XSDAttributeUse; import org.eclipse.xsd.XSDComplexTypeDefinition; import org.eclipse.xsd.XSDCompositor; import org.eclipse.xsd.XSDElementDeclaration; import org.eclipse.xsd.XSDFactory; import org.eclipse.xsd.XSDForm; import org.eclipse.xsd.XSDModelGroup; import org.eclipse.xsd.XSDParticle; import org.eclipse.xsd.XSDSchema; import org.eclipse.xsd.XSDSimpleTypeDefinition; import org.eclipse.xsd.ecore.XSDEcoreBuilder; import org.eclipse.xsd.util.XSDConstants; import org.eclipse.xsd.util.XSDResourceFactoryImpl; import org.openhealthtools.uml.xdw.XDWResource; import org.w3c.dom.Document; public class TESTLoad { public static void main(String[] args) throws Exception { // demonstrateSchema(); // testtt(); // kaka(); // gggg(); lastone(); } public static void mmmmm() throws IOException{ XDWResource resource = (XDWResource) XDWResource.Factory.INSTANCE.createResource(URI.createURI("/Users/aboufahj/Documents/eclipse-mdht/workspace/net.ihe.gazelle.xdw/sample/example.xml")); XDWPackageImpl.init(); V3PackageImpl.init(); OASISPackageImpl.init(); resource.load(new FileInputStream("/Users/aboufahj/Documents/eclipse-mdht/workspace/net.ihe.gazelle.xdw/sample/example.xml"), null); DocumentRoot root = (DocumentRoot) resource.getContents().get(0); TXDWWorkflowDocument clinicalDocument = root.getXDWWorkflowDocument(); System.out.println(root.getXMLNSPrefixMap()); System.out.println(root.getXSISchemaLocation()); Main.printDocRoot(clinicalDocument); } public static void lastone() throws Exception{ // XSDEcoreBuilder xsdEcoreBuilder = new XSDEcoreBuilder(); // ResourceSet resourceSet = new ResourceSetImpl(); // Collection eCorePackages = xsdEcoreBuilder.generate(URI.createFileURI("c:/temp/ipo.xsd")); //XDWResource resource = (XDWResource) XDWResource.Factory.INSTANCE.createResource(URI.createURI(XDWPackage.eNS_URI)); XDWResourceFactoryImpl xr = new XDWResourceFactoryImpl(); XDWPackageImpl.init(); Resource resource = xr.createResource(URI.createURI("/Users/aboufahj/Documents/eclipse-mdht/workspace/net.ihe.gazelle.xdw/sample/example.xml")); XDWPackageImpl.init(); V3PackageImpl.init(); OASISPackageImpl.init(); Set ss = EPackage.Registry.INSTANCE.keySet(); for (String string : ss) { System.out.println("ss = " + string); if (string == "urn:ihe:iti:2011:xdw"){ EPackage ep = EPackage.Registry.INSTANCE.getEPackage(string); System.out.println("\t" + ep.getName()); EList el = ep.eContents(); for (EObject eObject : el) { System.out.println("\t\t" + eObject.toString()); if (eObject.getClass().getName() == "XDWWorkflowDocument"){ System.out.println("eObject.eContents().size() == " + eObject.eContents().size()); //eAnnotation.eGet(new EStructuralFeatureImpl.) } } } } resource.load(new FileInputStream("/Users/aboufahj/Documents/eclipse-mdht/workspace/net.ihe.gazelle.xdw/sample/example.xml"), null); DocumentRoot root = (DocumentRoot) resource.getContents().get(0); TXDWWorkflowDocument clinicalDocument = root.getXDWWorkflowDocument(); System.out.println(root.getXMLNSPrefixMap()); System.out.println(root.getXSISchemaLocation()); Main.printDocRoot(clinicalDocument); // DocumentBuilder builder = newDocumentBuilder(); // Document document = builder.parse(new FileInputStream("/Users/aboufahj/Documents/eclipse-mdht/workspace/net.ihe.gazelle.xdw/sample/example.xml")); // // resource.load(new FileInputStream("/Users/aboufahj/Documents/eclipse-mdht/workspace/net.ihe.gazelle.xdw/sample/example.xml"), null); // DocumentRoot root = (DocumentRoot) resource.getContents().get(0); // TXDWWorkflowDocument clinicalDocument = root.getXDWWorkflowDocument(); // // Main.printDocRoot(clinicalDocument); // HashMap options = new HashMap(); // options.put(XMLResource.OPTION_EXTENDED_META_DATA, Boolean.TRUE); // Resource resource = resourceSet.createResource(URI.createFileURI("c:/temp/ipo.xml")); // resource.load(options); } private static DocumentBuilder newDocumentBuilder() throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); return factory.newDocumentBuilder(); } public static void zlot() throws IOException{ // generate EPackages from schemas URI schemaURI = URI.createFileURI("/Users/aboufahj/Documents/eclipse-mdht/workspace/net.ihe.gazelle.xdw/gen/xdw.xsd"); URI xmlURI = URI.createFileURI("/Users/aboufahj/Documents/eclipse-mdht/workspace/net.ihe.gazelle.xdw/sample/example.xml"); XSDEcoreBuilder xsdEcoreBuilder = new XSDEcoreBuilder(); Collection generatedPackages = xsdEcoreBuilder.generate(schemaURI); // register the packages loaded from XSD for (EObject generatedEObject : generatedPackages) { if (generatedEObject instanceof EPackage) { EPackage generatedPackage = (EPackage) generatedEObject; EPackage.Registry.INSTANCE.put(generatedPackage.getNsURI(), generatedPackage); } } // add file extension to registry ResourceFactoryRegistryImpl.INSTANCE.getExtensionToFactoryMap() .put("xml", new GenericXMLResourceFactoryImpl()); ResourceSet resourceSet = new ResourceSetImpl(); // resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(XDWPackage.eNS_PREFIX, new XDWResourceFactoryImpl()); // resourceSet.getPackageRegistry().put(XDWPackage.eNS_URI, XDWPackage.eINSTANCE); Resource serverXMLResource = resourceSet.getResource(xmlURI, true); serverXMLResource.load(Collections.EMPTY_MAP); serverXMLResource.getEObject("TXDWWorkflowDocument"); EObject root = serverXMLResource.getContents().get(0); EObject xdw = root.eContents().get(0); //System.out.println(xdw.e); // // ResourceSet resourceSet = ResourceSetFactory.createResourceSet(); // Resource resource = resourceSet.getResource(xmlURI, true); // resource.load(Collections.EMPTY_MAP); // EObject root = resource.getContents().get(0); } public static void gggg() throws IOException{ File serverDotXML = new File("/Users/aboufahj/Documents/eclipse-mdht/workspace/net.ihe.gazelle.xdw/sample/example.xml"); ResourceSet resourceSet = new ResourceSetImpl(); resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(XDWPackage.eNS_PREFIX, new XDWResourceFactoryImpl()); resourceSet.getPackageRegistry().put (XDWPackage.eNS_URI, XDWPackage.eINSTANCE); Resource serverXMLResource = resourceSet.createResource(URI.createFileURI(serverDotXML.getAbsolutePath())); serverXMLResource.load(Collections.EMPTY_MAP); serverXMLResource.getEObject("TXDWWorkflowDocument"); //Get the root configuration object //Get a contained object using a fragment //etc. } public static void testtt() throws Exception { // demonstrateSchema(); // demonstrateStaticLoadAndTraverse(); ResourceSet resourceSet = new ResourceSetImpl(); resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(Resource.Factory.Registry.DEFAULT_EXTENSION, new XDWResourceFactoryImpl()); resourceSet.getPackageRegistry().put(XDWPackage.eNS_URI, XDWPackage.eINSTANCE); resourceSet.getPackageRegistry().put(V3Package.eNS_URI, V3Package.eINSTANCE); resourceSet.getPackageRegistry().put(OASISPackage.eNS_URI, OASISPackage.eINSTANCE); final ExtendedMetaData extendedMetaData = new BasicExtendedMetaData(resourceSet.getPackageRegistry()); resourceSet.getLoadOptions().put(XMLResource.OPTION_EXTENDED_META_DATA, extendedMetaData); resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(Resource.Factory.Registry.DEFAULT_EXTENSION, new GenericXMLResourceFactoryImpl()); // Resource resource = resourceSet.getResource(URI.createFileURI("/Users/aboufahj/Documents/eclipse-mdht/workspace/net.ihe.gazelle.xdw/sample/example.xml"), true); // EObject documentRoot = (EObject)resource.getContents().get(0); XSDEcoreBuilder xsdEcoreBuilder = new XSDEcoreBuilder(extendedMetaData); URI schemaLocationURI = URI.createFileURI(new File("/Users/aboufahj/Documents/eclipse-mdht/workspace/net.ihe.gazelle.xdw/gen/xdw.xsd").getAbsolutePath()); xsdEcoreBuilder.generate(schemaLocationURI); EPackage ePackage = extendedMetaData.getPackage(XDWPackage.eNS_URI); System.out.println("package " + ePackage.getNsURI()); for (Iterator i = ePackage.getEClassifiers().iterator(); i.hasNext(); ) { // EClassifier eClassifier = (EClassifier)i.next(); if (eClassifier instanceof EClass) { // EClass eClass = (EClass)eClassifier; System.out.println(" class " + eClass.getName()); for (Iterator j = eClass.getEStructuralFeatures().iterator(); j.hasNext(); ) { EStructuralFeature eStructuralFeature = (EStructuralFeature)j.next(); if (eStructuralFeature instanceof EReference) { EReference eReference = (EReference)eStructuralFeature; System.out.println(" reference " + eReference.getName() + ":" + eReference.getEReferenceType().getName()); } else { EAttribute eAttribute = (EAttribute)eStructuralFeature; System.out.println(" attribute " + eAttribute.getName() + ":" + eAttribute.getEAttributeType().getName()); } } } else { EDataType eDataType = (EDataType)eClassifier; System.out.println(" data type " + eDataType.getName()); } } Resource ecoreResource = new EcoreResourceFactoryImpl().createResource(schemaLocationURI); ecoreResource.getContents().add(ePackage); Resource resource = resourceSet.getResource(URI.createFileURI("/Users/aboufahj/Documents/eclipse-mdht/workspace/net.ihe.gazelle.xdw/sample/example.xml"), true); resource.save(System.out, null); System.out.println(); EObject documentRoot = (EObject)resource.getContents().get(0); final EObject rootTreeNode = (EObject)documentRoot.eContents().get(0); System.err.println(rootTreeNode.eClass().getName()); System.err.println("dd" + extendedMetaData.getType(XDWPackage.eNS_URI, "XDW.WorkflowDocument")); if (rootTreeNode.eClass() != extendedMetaData.getType(XDWPackage.eNS_URI, "XDW.WorkflowDocument")) { throw new Exception("Bad meta data"); } System.out.println(resource.getEObject("DucmentRoot")); System.out.println("Traversing EMF EObject"); // //final EStructuralFeature mixedFeature = extendedMetaData.getMixedFeature(rootTreeNode.eClass()); //final EStructuralFeature labelAttribute = extendedMetaData.getAttribute(rootTreeNode.eClass(), null, "label"); // Visit all the tree nodes exactly as before, // but without assuming they are AnyType instances. // I.e., we use purely reflection on the EMF's data API. // new Object() { public void traverse(String indent, EObject eObject) { System.out.println(indent + "{" + extendedMetaData.getNamespace(eObject.eContainmentFeature()) + "}" + extendedMetaData.getName(eObject.eContainmentFeature())); //eObject.eContents(). //System.out.println(indent + " label=" + eObject.eGet(labelAttribute)); // Access the feature map reflectively. // // FeatureMap featureMap = (FeatureMap)eObject.eGet(mixedFeature); // for (int i = 0, size = featureMap.size(); i < size; ++i) // { // EStructuralFeature feature = featureMap.getEStructuralFeature(i); // if (FeatureMapUtil.isText(feature)) // { // System.out.println(indent + " '" + featureMap.getValue(i).toString().replaceAll("\n", "\\\\n") + "'"); // } // else if (FeatureMapUtil.isComment(feature)) // { // System.out.println(indent + " "); // } // else if (FeatureMapUtil.isCDATA(feature)) // { // System.out.println(indent + " "); // } // else if (feature instanceof EReference) // { // traverse(indent + " ", (EObject)featureMap.getValue(i)); // } // } } }.traverse("", rootTreeNode); //TXDWWorkflowDocument dd = (TXDWWorkflowDocument)rootTreeNode; } }