@SuppressWarnings({"unchecked", "rawtypes"}) public static void saveResources(final List<EObject> root, final URI uri) { final ResourceSet resourceSet = new ResourceSetImpl(); resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap() .put(Resource.Factory.Registry.DEFAULT_EXTENSION, new XMLResourceFactoryImpl()); final Resource resource = resourceSet.createResource(uri); resource.getContents().addAll(root); final Map options = new HashMap(); options.put(XMLResource.OPTION_SCHEMA_LOCATION, Boolean.TRUE); try { resource.save(options); } catch (final IOException e) { e.printStackTrace(); } }
private void serializeInstance(String fileName, Map<String, Boolean> options) { ResourceSet rscSet = new ResourceSetImpl(); EPackage topPck= (EPackage) modelReader.getResource().getContents().get(0); rscSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put( "xmi", new XMLResourceFactoryImpl()); EPackage.Registry.INSTANCE.put(topPck.getNsURI(), topPck); fileName+=".xmi"; Resource resource = rscSet.createResource(URI.createURI(fileName)); Iterator<EObject> objIt = objList.iterator(); while (objIt.hasNext()) { EObject obj = objIt.next(); if (isRoot(obj)) resource.getContents().add(obj); } try{ resource.save(options); }catch (IOException e) { e.printStackTrace(); } }
public static String getSkriptXml( Model pModel ) { Resource.Factory.Registry lRegistry = Resource.Factory.Registry.INSTANCE; Map<String,Object> lMap = lRegistry.getExtensionToFactoryMap(); lMap.put( "xml", new XMLResourceFactoryImpl() ); ResourceSet lResourceSet = new ResourceSetImpl(); Resource lResource = lResourceSet.createResource( URI.createFileURI( "*.xml" ) ); ((XMLResource)lResource).getDefaultSaveOptions(); lResource.getContents().add( pModel ); try { ByteArrayOutputStream lByteArrayOutputStream = new ByteArrayOutputStream(); lResource.save( lByteArrayOutputStream, Collections.EMPTY_MAP ); return new String( lByteArrayOutputStream.toByteArray() ); } catch( IOException e ) { throw new RuntimeException( e ); } }
@SuppressWarnings( "unchecked" ) public T loadModelXml( String pFilename, String pNamespaceUri, EPackage pEPackage ) { EPackage.Registry.INSTANCE.put( pNamespaceUri, pEPackage ); Resource.Factory.Registry lRegistry = Resource.Factory.Registry.INSTANCE; Map<String, Object> lMap = lRegistry.getExtensionToFactoryMap(); lMap.put( "xml", new XMLResourceFactoryImpl() ); ResourceSet lResourceSet = new ResourceSetImpl(); Resource lResource = lResourceSet.createResource( URI.createFileURI( pFilename ) ); ((XMLResource) lResource).getDefaultSaveOptions(); try { lResource.load( Collections.EMPTY_MAP ); } catch( IOException e ) { throw new RuntimeException( e ); } return (T) lResource.getContents().get( 0 ); }
private void doSave ( final String file ) throws IOException { final ResourceSet rs = new ResourceSetImpl (); rs.getResourceFactoryRegistry ().getExtensionToFactoryMap ().put ( "*", new XMLResourceFactoryImpl () ); //$NON-NLS-1$ final URI fileUri = URI.createFileURI ( file ); final Resource resource = rs.createResource ( fileUri ); resource.getContents ().add ( this.chart ); final Map<Object, Object> options = new HashMap<Object, Object> (); // options.put ( XMIResource., value ) resource.save ( options ); }
/** * Create the resource manager. */ public ResourceManager(EPackage... packageInstances) { setResourceSet(createResourceSet()); // register factories Resource.Factory.Registry.INSTANCE .getExtensionToFactoryMap().put("xml", new XMLResourceFactoryImpl()); // Add them to the resourceSet for (EPackage p : packageInstances) resourceSet.getPackageRegistry().put(p.getNsURI(), p); }
public EMFBuilder() { Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("typegraphbasic", new XMLResourceFactoryImpl()); resourceSet = new ResourceSetImpl(); TypeGraphBasicPackage.eINSTANCE.eClass(); TypeGraphTracePackage.eINSTANCE.eClass(); }
/** * @generated NOT */ @Override public void registerResourceFactories(ResourceSet resourceSet) { System.out.println("@MyGenerator::registerResourceFactories"); super.registerResourceFactories(resourceSet); resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put( "xmi", new org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl()); resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put( "ecore", new EcoreResourceFactoryImpl()); resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put( IAcceleoConstants.EMTL_FILE_EXTENSION, new EMtlResourceFactoryImpl()); if (QWT_SUPPORT_ENABLED){ resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("ui", new Qt48XmlschemaQwtEnhancedResourceFactoryImpl()); } else{ resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("ui", new Qt48XmlschemaResourceFactoryImpl()); } resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("xsd", new XMLResourceFactoryImpl()); }
public static String convertToXML(EObject eObject, Map<String, Object> options) { List<? extends EObject> contents = Collections.singletonList(eObject); options.put(XMLResource.OPTION_ROOT_OBJECTS, contents); boolean temporaryResource = false; Resource resource = eObject.eResource(); EObject eRoot = null; if (resource == null || !(resource instanceof XMLResource)) { ResourceSet resourceSet = createResourceSet(); resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(Resource.Factory.Registry.DEFAULT_EXTENSION, new XMLResourceFactoryImpl()); resource = resourceSet.createResource(URI.createURI("")); temporaryResource = true; eRoot = EcoreUtil.getRootContainer(eObject); resource.getContents().add(eRoot); } try { ByteArrayOutputStream stream = new ByteArrayOutputStream(); try { resource.save(stream, options); String string; if (options.get(XMLResource.OPTION_ENCODING) != null) { string = stream.toString(options.get(XMLResource.OPTION_ENCODING).toString()); } else { string = stream.toString(); } return string; } catch (IOException e) { throw new IllegalStateException("Error converting " + eObject + " to string", e); } } finally { if (temporaryResource) { resource.getContents().remove(eRoot); } } }
public static EObject createFromXML(String xml) { ResourceSet resourceSet = createResourceSet(); resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(Resource.Factory.Registry.DEFAULT_EXTENSION, new XMLResourceFactoryImpl()); Resource resource = resourceSet.createResource(URI.createURI("createFromString.xml")); InputStream stream = new ByteArrayInputStream(xml.getBytes()); try { resource.load(stream, null); } catch (IOException e) { throw new IllegalStateException("Error creating EObject from string " + xml, e); } return resource.getContents().remove(0); }
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { ResourceSet resourceSet = EMFUtils.createResourceSet(); resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(Resource.Factory.Registry.DEFAULT_EXTENSION, new XMLResourceFactoryImpl()); String string = (String)in.readObject(); ByteArrayInputStream stream = new ByteArrayInputStream(string.getBytes()); Resource resource = resourceSet.createResource(URI.createURI("EPlanElementImpl_readExternal.xmi")); resource.load(stream, null); EList<EObject> contents = resource.getContents(); @SuppressWarnings("unused") EObject newThis = contents.get(0); }
public void run() { EPackage.Registry.INSTANCE.put(CitygmlPackage.eINSTANCE.getNsURI(), CitygmlPackage.eINSTANCE); EPackage.Registry.INSTANCE.put(EcorePackage.eINSTANCE.getNsURI(), EcorePackage.eINSTANCE); Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("xml", new XMLResourceFactoryImpl()); Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("gml", new XMLResourceFactoryImpl()); ResourceSet rs = new ResourceSetImpl(); Map<String, Object> saveOptions = new HashMap<String, Object>(); saveOptions.put(XMLResource.OPTION_EXTENDED_META_DATA, Boolean.TRUE); rs.getLoadOptions().putAll(saveOptions); rs.getResource(URI.createURI("models/waldbruecke_v1.0.0.gml"), true); }
public void writeEMF(String fileName) { ResourceSet metaResourceSet = new ResourceSetImpl(); metaResourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("ecore", new XMLResourceFactoryImpl()); URI resUri = URI.createURI(fileName); Resource metaResource = metaResourceSet.createResource(resUri); metaResource.getContents().add(schemaPack); try { metaResource.save(null); } catch (Exception e) { LOGGER.error("", e); } }
public void writeEMF(String fileName) { ResourceSet metaResourceSet = new ResourceSetImpl(); metaResourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("ecore", new XMLResourceFactoryImpl()); URI resUri = URI.createURI(fileName); Resource metaResource = metaResourceSet.createResource(resUri); metaResource.getContents().add(ePackage); try { metaResource.save(null); } catch (Exception e) { e.printStackTrace(); } }
@Test public void testBasicXDS() { Collection<EObject> xsd = new XSDEcoreBuilder().generate(URI.createFileURI(LINK_STAT_XSD)); Collection<EPackage> xsdPackages = Collections2.transform(xsd, new Function<EObject, EPackage>() { @Override public EPackage apply(EObject input) { return (EPackage)input; } }); final ResourceSet rs = new ResourceSetImpl(); rs.getResourceFactoryRegistry().getExtensionToFactoryMap().put("xml", new XMLResourceFactoryImpl()); for (EPackage ePackage: xsdPackages) { rs.getPackageRegistry().put(ePackage.getNsURI(), ePackage); } Resource xmlResource = rs.createResource(URI.createFileURI(LINK_STAT_XML)); Map<String, Object> options = new HashMap<String, Object>(); options.put(XMLResource.OPTION_EXTENDED_META_DATA, Boolean.TRUE); options.put(XMLResource.OPTION_ENCODING, "UTF-8"); try { xmlResource.load(options); } catch (IOException e) { e.printStackTrace(); } EObject model = xmlResource.getContents().get(0); Iterator<EObject> it = model.eAllContents(); while (it.hasNext()) { assertTrue(! (it.next() instanceof AnyType)); } }
public void runCAML2TOSCATypes(URI camlProfile, String toscaModelPath) throws IOException { ExecEnv env = EmftvmFactory.eINSTANCE.createExecEnv(); ResourceSet rs = new ResourceSetImpl(); rs.getPackageRegistry().put(ToscaPackage.eINSTANCE.getNsURI(), ToscaPackage.eINSTANCE); rs.getResourceFactoryRegistry().getExtensionToFactoryMap().put("ecore", new XMIResourceFactoryImpl()); rs.getResourceFactoryRegistry().getExtensionToFactoryMap().put("genmodel", new XMIResourceFactoryImpl()); rs.getResourceFactoryRegistry().getExtensionToFactoryMap().put("xsd", new XMLResourceFactoryImpl()); rs.getPackageRegistry().put(UMLPackage.eINSTANCE.getNsURI(), UMLPackage.eINSTANCE); rs.getResourceFactoryRegistry().getExtensionToFactoryMap() .put(UMLResource.FILE_EXTENSION, UMLResource.Factory.INSTANCE); ToscaUtil.init(rs, "model/"); // TODO: Maybe there is a better solution for 'resetting' the extended metadata // We changed the annotation already in the tosca.ecore EClassifierExtendedMetaData.Holder holder = (EClassifierExtendedMetaData.Holder)ToscaPackage.eINSTANCE.getDefinitionsType(); EClassifierExtendedMetaData result = holder.getExtendedMetaData(); result.setName("Definitions"); // Load metamodels Metamodel umlMM = EmftvmFactory.eINSTANCE.createMetamodel(); umlMM.setResource(rs.getResource(URI.createURI("http://www.eclipse.org/uml2/4.0.0/UML"), true)); env.registerMetaModel("UMLMM", umlMM); Metamodel toscaMM = EmftvmFactory.eINSTANCE.createMetamodel(); toscaMM.setResource(rs.getResource(URI.createURI("http://docs.oasis-open.org/tosca/ns/2011/12"), true)); env.registerMetaModel("TOSCA", toscaMM); // ##### INPUT Models ##### // the CAML library Model cL = EmftvmFactory.eINSTANCE.createModel(); URI libraryURI = URI.createPlatformPluginURI("eu.artist.migration.caml", true); libraryURI = libraryURI.appendSegments(new String[]{"umllibraries", "CAMLLibrary.uml"}); cL.setResource(rs.getResource(libraryURI, true)); env.registerInputModel("CL", cL); // the CAML profile Model cP = EmftvmFactory.eINSTANCE.createModel(); cP.setResource(rs.getResource(camlProfile, true)); env.registerInputModel("CP", cP); // Model cCP = EmftvmFactory.eINSTANCE.createModel(); // profileURI = URI.createPlatformPluginURI("eu.artist.migration.caml", true); // profileURI = profileURI.appendSegments(new String[]{"umlprofiles", "CommonCloudProfile.profile.uml"}); // cCP.setResource(rs.getResource(profileURI, true)); // env.registerInputModel("CCP", cCP); // ##### OUTPUT Modles ##### // the TOSCA deployment model Model toscaM = EmftvmFactory.eINSTANCE.createModel(); toscaM.setResource(rs.createResource(URI.createFileURI(new File(toscaModelPath).getAbsolutePath()))); env.registerOutputModel("TM", toscaM); // Load and run module DefaultModuleResolver mr = new DefaultModuleResolver(moduleLocation, new ResourceSetImpl()); TimingData td = new TimingData(); env.loadModule(mr, typesTrafo); td.finishLoading(); env.run(td); td.finish(); toscaM.getResource().save(Collections.emptyMap()); }