/** Helper method which uses xstream to depersist an object as xml from disk. */ <T> T depersist(XStreamPersister xp, Resource f, Class<T> clazz) throws IOException { try (InputStream in = new ByteArrayInputStream(f.getContents())) { return xp.load(in, clazz); } }
@Override public WorkspaceContents apply(Resource rd) throws IOException { Resource wr = rd.get("workspace.xml"); Resource nr = rd.get("namespace.xml"); if (Resources.exists(wr) && Resources.exists(nr)) { byte[] contents = wr.getContents(); byte[] nrContents = nr.getContents(); return new WorkspaceContents(rd, contents, nrContents); } else { LOGGER.warning("Ignoring workspace directory " + rd.path()); return null; } } }
@Override public LayerContents apply(Resource rd) throws IOException { Resource r = rd.get(resourceFileName); Resource lr = rd.get("layer.xml"); if (Resources.exists(r) && Resources.exists(lr)) { byte[] contents = r.getContents(); byte[] lrContents = lr.getContents(); return new LayerContents(rd, contents, lrContents); } else { LOGGER.warning("Ignoring " + resourceType + " directory " + rd.path()); return null; } } }
void loadLayerGroups(Resource layerGroups, Catalog catalog, XStreamPersister xp) { try (AsynchResourceIterator<byte[]> it = new AsynchResourceIterator<>(layerGroups, XML_FILTER, r -> r.getContents())) { while (it.hasNext()) { try { LayerGroupInfo lg = depersist(xp, it.next(), LayerGroupInfo.class); if (lg.getLayers() == null || lg.getLayers().size() == 0) { LOGGER.warning( "Skipping empty layer group '" + lg.getName() + "', it is invalid"); continue; } catalog.add(lg); LOGGER.info("Loaded layer group '" + lg.getName() + "'"); } catch (Exception e) { LOGGER.log(Level.WARNING, "Failed to load layer group", e); } } } }
void loadStyles(Resource styles, Catalog catalog, XStreamPersister xp) throws IOException { Filter<Resource> styleFilter = r -> XML_FILTER.accept(r) && !Resources.exists(styles.get(r.name() + ".xml")); try (AsynchResourceIterator<byte[]> it = new AsynchResourceIterator<>(styles, styleFilter, r -> r.getContents())) { while (it.hasNext()) { try { StyleInfo s = depersist(xp, it.next(), StyleInfo.class); catalog.add(s); if (LOGGER.isLoggable(Level.INFO)) { LOGGER.info("Loaded style '" + s.getName() + "'"); } } catch (Exception e) { LOGGER.log(Level.WARNING, "Failed to load style", e); } } } }
Resource f = sd.get("datastore.xml"); if (Resources.exists(f)) { return new StoreContents(f, f.getContents()); return new StoreContents(f, f.getContents()); return new StoreContents(f, f.getContents()); return new StoreContents(f, f.getContents());
ResourceSet resourceSet = ResourceSetFactory.createResourceSet(); Resource resource = resourceSet.getResource(xmlURI, true); resource.load(Collections.EMPTY_MAP); EObject root = resource.getContents().get(0);
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(UMLResource.FILE_EXTENSION, UMLResource.Factory.INSTANCE); resourceSet.getPackageRegistry().put("http://www.eclipse.org/uml2/2.0.0/UML", UMLPackage.eINSTANCE); Resource r = resourceSet.getResource(uriToYourFile,true); YourModelRootType root = (YourModelRootType) r.getContents().get(0);
private GeoServerTileLayerInfoImpl depersist(final Resource res) throws IOException { if (LOGGER.isLoggable(Level.FINE)) { LOGGER.fine("Depersisting GeoServerTileLayerInfo from " + res.path()); } GeoServerTileLayerInfoImpl info; try (Reader reader = new InputStreamReader(new ByteArrayInputStream(res.getContents()), "UTF-8")) { info = (GeoServerTileLayerInfoImpl) serializer.fromXML(reader); } return info; }
// creating the injector Injector injector = new MyLanguageStandaloneSetup().createInjectorAndDoEMFRegistration() // obtain a resource set XtextResourceSet resourceSet = injector.get(XtextResourceSet.class); // load file Resource resource = resourceSet.getResource(URI.create("path/to/file.mylanguage"), true); // obtain root AST element MyModel model = (MyModel) resource.getContents().get(0);
// "workspace" is a string that contains the path to the workspace containing the DSL program. new org.eclipse.emf.mwe.utils.StandaloneSetup().setPlatformUri(workspace); Injector injector = new MyDslStandaloneSetup().createInjectorAndDoEMFRegistration(); XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class); resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE); // "DSLProgram" is a string that contains the path to the file of the DSL program relative to the workspace set above. Resource resource = resourceSet.getResource(URI.createURI("platform:/resource/" + DSLProgram), true); Model model = (Model) resource.getContents().get(0);
private void exportXMI(String absuloteTargetFolderPath) { // change MyLanguage with your language name Injector injector = new MyLanguageStandaloneSetup() .createInjectorAndDoEMFRegistration(); XtextResourceSet resourceSet = injector .getInstance(XtextResourceSet.class); // .ext ist the extension of the model file String inputURI = "file:///" + absuloteTargetFolderPath + "/MyFile.ext"; String outputURI = "file:///" + absuloteTargetFolderPath + "/MyFile.xmi"; URI uri = URI.createURI(inputURI); Resource xtextResource = resourceSet.getResource(uri, true); EcoreUtil.resolveAll(xtextResource); Resource xmiResource = resourceSet .createResource(URI.createURI(outputURI)); xmiResource.getContents().add(xtextResource.getContents().get(0)); try { xmiResource.save(null); } catch (IOException e) { e.printStackTrace(); } }
//Generate your URI ResourceFactoryImpl factory = new ResourceFactoryImpl(); URI sourceURI = URI.createURI("your xml path"); Resource loadResource = (ResourceImpl)factory.createResource(sourceURI); System.out.println(sourceURI.path()); //Add loading options Map<String, Boolean> options = new HashMap<String, Boolean>(); options.put(XMLResource.OPTION_SUPPRESS_DOCUMENT_ROOT, true); //Load XML loadResource.load( options); //Create XMI output URI targetURI = URI.createURI("your xmi path"); Resource resourceOut = new ResourceImpl(targetURI); //Generating your EObjects from XML model EList listObj = loadResource.getContents(); EObject obj = (EObject) listObj.get(0); resourceOut.getContents().add(obj); resourceOut.save(options);
ResourceSet set = new ResourceSetImpl(); UMLResourcesUtil.init(set); Resource res = set.getResource(typesUri, true); EObject eobj = res.getContents().get(0);
/* * load existing EPackage */ EcorePackage.eINSTANCE.eClass(); /*Initialize your EPackage*/ final Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE; final Map<String, Object> m = reg.getExtensionToFactoryMap(); m.put(EcorePackage.eNAME, new XMIResourceFactoryImpl()); final ResourceSet resSet = new ResourceSetImpl(); Resource resource = null; try { resource = resSet.getResource(URI.createFileURI("/Your/Path/To/Directory/myTest.ecore"), true); } catch (Exception e) { e.printStackTrace(); } /*load root and cast to EPackage*/ final EPackage root = (EPackage) resource.getContents().get(0);
Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE; Map<String, Object> m = reg.getExtensionToFactoryMap(); m.put("key", new XMIResourceFactoryImpl()); ResourceSet resSet = new ResourceSetImpl(); Resource resource = resSet.createResource(URI.createFileURI(fileName)); resource.getContents().add(data); resource.save(Collections.EMPTY_MAP);
/* * Save your EPackage to file ecore file: */ /*Initialize your EPackage*/ myPackage.eClass(); Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE; Map<String, Object> m = reg.getExtensionToFactoryMap(); /*add default .ecore extension for ecore file*/ m.put(EcorePackage.eNAME, new XMIResourceFactoryImpl()); // Obtain a new resource set ResourceSet resSet = new ResourceSetImpl(); // create a resource Resource resource = null; try { resource = resSet.createResource(URI.createFileURI("/Your/Path/To/Directory/myTest.ecore")); } catch (Exception e) { e.printStackTrace(); } /*add your EPackage as root, everything is hierarchical included in this first node*/ resource.getContents().add(myPackage); // now save the content. try { resource.save(Collections.EMPTY_MAP); } catch (IOException e) { e.printStackTrace(); }
public static EObject loadEObjectFromString(String myModelXml, EPackage ePackage) throws IOException { // Create a ResourceSet ResourceSet resourceSet = new ResourceSetImpl(); // register XMIRegistryResourceFactoryIml resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put (Resource.Factory.Registry.DEFAULT_EXTENSION, new XMIResourceFactoryImpl()); // register your epackage to the resource set so it has a reference to your ecore // you can get an instance to your epackage by calling YourEPackageClass.getInstace(); resourceSet.getPackageRegistry().put(ePackage.getNsURI(), ePackage); Resource resource = resourceSet.createResource(URI.createURI("*.modelextension")); resource.load(new URIConverter.ReadableInputStream(myModelXml), null); // return the root model object and there you have it, all you need is to // cast it to the right EObject based on your model return resource.getContents().get(0); }
@Test public void testRawPutAsInvalidSLD() throws Exception { String xml = "This is not valid SLD"; MockHttpServletResponse response = putAsServletResponse( RestBaseController.ROOT_PATH + "/styles/Ponds?raw=true", xml, SLDHandler.MIMETYPE_10); assertEquals(200, response.getStatus()); StyleInfo styleInfo = catalog.getStyleByName("Ponds"); String fileName = styleInfo.getFilename(); GeoServerResourceLoader resources = getGeoServer().getCatalog().getResourceLoader(); Resource resource = resources.get("styles/" + fileName); String content = new String(resource.getContents()); assertFalse("replaced", content.contains("<sld:Name>foo</sld:Name>")); assertTrue("replaced", content.contains("not valid")); }
@Test public void testPostCSS() throws Exception { Catalog cat = getCatalog(); assertNull("foo not available", cat.getStyleByName("foo")); String content = newCSS(); MockHttpServletResponse response = postAsServletResponse("/rest/styles?name=foo", content, CssHandler.MIME_TYPE); assertEquals(201, response.getStatus()); GeoServerResourceLoader resources = catalog.getResourceLoader(); Resource resource = resources.get("/styles/foo.css"); String definition = new String(resource.getContents()); assertTrue("is css", definition.contains("stroke: red")); StyleInfo styleInfo = catalog.getStyleByName("foo"); Style s = styleInfo.getStyle(); ByteArrayOutputStream out = new ByteArrayOutputStream(); SLDHandler handler = new SLDHandler(); handler.encode(Styles.sld(s), SLDHandler.VERSION_10, false, out); content = new String(out.toByteArray()); assertTrue(content.contains("<sld:Name>foo</sld:Name>")); catalog.remove(styleInfo); }