    001    package com.hammurapi.eventbus.snapshot.io;
    002
    003    import java.io.File;
    004    import java.io.IOException;
    005
    006    import org.eclipse.emf.common.util.EList;
    007    import org.eclipse.emf.common.util.URI;
    008    import org.eclipse.emf.ecore.EObject;
    009    import org.eclipse.emf.ecore.resource.Resource;
    010    import org.eclipse.emf.ecore.resource.ResourceSet;
    011    import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
    012    import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
    013
    014    import com.hammurapi.convert.ConvertingService;
    015    import com.hammurapi.eventbus.EventBusException;
    016    import com.hammurapi.extract.Predicate;
    017
    018    /**
    019     * Class to dump predicates into snapshot model for troubleshooting.
    020     * @author Pavel Vlasov
    021     */
    022    public class PredicateOutput {
    023
    024
    025            public static void output(Predicate<?, ?> predicate, File out) {
    026
    027                    com.hammurapi.eventbus.snapshot.Predicate sp = ConvertingService.convert(predicate, com.hammurapi.eventbus.snapshot.Predicate.class);
    028
    029                    ResourceSet resourceSet = new ResourceSetImpl();
    030                            // Register the appropriate resource factory to handle all file extensions.
    031                            //
    032                    resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(Resource.Factory.Registry.DEFAULT_EXTENSION, new XMIResourceFactoryImpl());
    033
    034                    URI uri = URI.createFileURI(out.getAbsolutePath());
    035                    Resource predicateResource = resourceSet.createResource(uri);
    036                    EList<EObject> contents = predicateResource.getContents();
    037                    contents.add(sp);
    038                    try {
    039                            predicateResource.save(null);
    040                    } catch (IOException e) {
    041                            throw new EventBusException("Could not save snapshot: "+e, e);
    042                    }
    043            }
    044
    045    }