| 1 | /** |
| 2 | * <copyright> |
| 3 | * </copyright> |
| 4 | * |
| 5 | * $Id$ |
| 6 | */ |
| 7 | package de.uka.ipd.sdq.pipesandfilters.tests; |
| 8 | |
| 9 | import de.uka.ipd.sdq.pipesandfilters.RawRecorder; |
| 10 | import de.uka.ipd.sdq.pipesandfilters.pipesandfiltersFactory; |
| 11 | import de.uka.ipd.sdq.pipesandfilters.pipesandfiltersPackage; |
| 12 | |
| 13 | import de.uka.ipd.sdq.pipesandfilters.util.pipesandfiltersResourceFactoryImpl; |
| 14 | |
| 15 | import java.io.File; |
| 16 | import java.io.IOException; |
| 17 | |
| 18 | import org.eclipse.emf.common.util.Diagnostic; |
| 19 | import org.eclipse.emf.common.util.URI; |
| 20 | |
| 21 | import org.eclipse.emf.ecore.EObject; |
| 22 | |
| 23 | import org.eclipse.emf.ecore.resource.Resource; |
| 24 | import org.eclipse.emf.ecore.resource.ResourceSet; |
| 25 | |
| 26 | import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; |
| 27 | |
| 28 | import org.eclipse.emf.ecore.util.Diagnostician; |
| 29 | |
| 30 | /** |
| 31 | * <!-- begin-user-doc --> |
| 32 | * A sample utility for the '<em><b>pipesandfilters</b></em>' package. |
| 33 | * <!-- end-user-doc --> |
| 34 | * @generated |
| 35 | */ |
| 36 | public class pipesandfiltersExample { |
| 37 | /** |
| 38 | * <!-- begin-user-doc --> |
| 39 | * Load all the argument file paths or URIs as instances of the model. |
| 40 | * <!-- end-user-doc --> |
| 41 | * @param args the file paths or URIs. |
| 42 | * @generated |
| 43 | */ |
| 44 | public static void main(String[] args) { |
| 45 | // Create a resource set to hold the resources. |
| 46 | // |
| 47 | ResourceSet resourceSet = new ResourceSetImpl(); |
| 48 | |
| 49 | // Register the appropriate resource factory to handle all file extensions. |
| 50 | // |
| 51 | resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put |
| 52 | (Resource.Factory.Registry.DEFAULT_EXTENSION, |
| 53 | new pipesandfiltersResourceFactoryImpl()); |
| 54 | |
| 55 | // Register the package to ensure it is available during loading. |
| 56 | // |
| 57 | resourceSet.getPackageRegistry().put |
| 58 | (pipesandfiltersPackage.eNS_URI, |
| 59 | pipesandfiltersPackage.eINSTANCE); |
| 60 | |
| 61 | // If there are no arguments, emit an appropriate usage message. |
| 62 | // |
| 63 | if (args.length == 0) { |
| 64 | System.out.println("Enter a list of file paths or URIs that have content like this:"); |
| 65 | try { |
| 66 | Resource resource = resourceSet.createResource(URI.createURI("http:///My.pipesandfilters")); |
| 67 | RawRecorder root = pipesandfiltersFactory.eINSTANCE.createRawRecorder(); |
| 68 | resource.getContents().add(root); |
| 69 | resource.save(System.out, null); |
| 70 | } |
| 71 | catch (IOException exception) { |
| 72 | exception.printStackTrace(); |
| 73 | } |
| 74 | } |
| 75 | else { |
| 76 | // Iterate over all the arguments. |
| 77 | // |
| 78 | for (int i = 0; i < args.length; ++i) { |
| 79 | // Construct the URI for the instance file. |
| 80 | // The argument is treated as a file path only if it denotes an existing file. |
| 81 | // Otherwise, it's directly treated as a URL. |
| 82 | // |
| 83 | File file = new File(args[i]); |
| 84 | URI uri = file.isFile() ? URI.createFileURI(file.getAbsolutePath()): URI.createURI(args[0]); |
| 85 | |
| 86 | try { |
| 87 | // Demand load resource for this file. |
| 88 | // |
| 89 | Resource resource = resourceSet.getResource(uri, true); |
| 90 | System.out.println("Loaded " + uri); |
| 91 | |
| 92 | // Validate the contents of the loaded resource. |
| 93 | // |
| 94 | for (EObject eObject : resource.getContents()) { |
| 95 | Diagnostic diagnostic = Diagnostician.INSTANCE.validate(eObject); |
| 96 | if (diagnostic.getSeverity() != Diagnostic.OK) { |
| 97 | printDiagnostic(diagnostic, ""); |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | catch (RuntimeException exception) { |
| 102 | System.out.println("Problem loading " + uri); |
| 103 | exception.printStackTrace(); |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * <!-- begin-user-doc --> |
| 111 | * Prints diagnostics with indentation. |
| 112 | * <!-- end-user-doc --> |
| 113 | * @param diagnostic the diagnostic to print. |
| 114 | * @param indent the indentation for printing. |
| 115 | * @generated |
| 116 | */ |
| 117 | protected static void printDiagnostic(Diagnostic diagnostic, String indent) { |
| 118 | System.out.print(indent); |
| 119 | System.out.println(diagnostic.getMessage()); |
| 120 | for (Diagnostic child : diagnostic.getChildren()) { |
| 121 | printDiagnostic(child, indent + " "); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | } //pipesandfiltersExample |