public byte[] toByteArray(final Configuration configuration) throws ActorMappingExportException { if (containsActorMapping(configuration)) { final StringBuffer result = new StringBuffer(""); for (final ActorMapping mapping : configuration.getActorMappings().getActorMapping()) { if (!checkActorMappingGroup(mapping)) { result.append("- " + mapping.getName() + "\n"); } } if (result.length() > 0) { throw new ActorMappingExportException( Messages.errorActorMappingGroup + " : \n" + result.toString()); } final File tmpFile = new File(getTempFolder(), EcoreUtil.generateUUID() + ".xml"); final org.eclipse.emf.common.util.URI uri = org.eclipse.emf.common.util.URI.createFileURI(tmpFile.getAbsolutePath()); final Resource resource = new ActorMappingResourceFactoryImpl().createResource(uri); final DocumentRoot root = ActorMappingFactory.eINSTANCE.createDocumentRoot(); final ActorMappingsType mapping = EcoreUtil.copy(configuration.getActorMappings()); cleanMapping(mapping); root.setActorMappings(mapping); resource.getContents().add(root); final Map<String, String> options = new HashMap<String, String>(); options.put(XMLResource.OPTION_ENCODING, "UTF-8"); options.put(XMLResource.OPTION_XML_VERSION, "1.0"); ByteArrayOutputStream byteArrayOutputStream = null; try { byteArrayOutputStream = new ByteArrayOutputStream(); resource.save(options); new XMLProcessor().save(byteArrayOutputStream, resource, options); resource.delete(Collections.EMPTY_MAP); return byteArrayOutputStream.toByteArray(); } catch (final IOException e) { throw new ActorMappingExportException("Failed to save actor mapping", e); } finally { try { byteArrayOutputStream.close(); } catch (final IOException e) { } } } return null; }
protected boolean containsActorMapping(final Configuration configuration) { return configuration.getActorMappings() != null && configuration.getActorMappings().getActorMapping() != null && !configuration.getActorMappings().getActorMapping().isEmpty(); }