Java Code Examples for org.eclipse.emf.ecore.util.EcoreUtil#getObjectByType()

The following examples show how to use org.eclipse.emf.ecore.util.EcoreUtil#getObjectByType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: neoscada   File: ServerHostImpl.java    License: Eclipse Public License 1.0 6 votes vote down vote up
public Collection<? extends ServerDescriptor> startServer ( final URI exporterFileUri, final String locationLabel ) throws CoreException
{
    final ResourceSetImpl resourceSet = new ResourceSetImpl ();

    resourceSet.getResourceFactoryRegistry ().getExtensionToFactoryMap ().put ( "*", new ExporterResourceFactoryImpl () );

    final Resource resource = resourceSet.createResource ( exporterFileUri );
    try
    {
        resource.load ( null );
    }
    catch ( final IOException e )
    {
        throw new CoreException ( StatusHelper.convertStatus ( HivesPlugin.PLUGIN_ID, "Failed to load configuration", e ) );
    }

    final DocumentRoot root = (DocumentRoot)EcoreUtil.getObjectByType ( resource.getContents (), ExporterPackage.Literals.DOCUMENT_ROOT );
    if ( root == null )
    {
        throw new CoreException ( new Status ( IStatus.ERROR, HivesPlugin.PLUGIN_ID, "Failed to locate exporter configuration in: " + exporterFileUri ) );
    }
    return startServer ( root, locationLabel );
}
 
Example 2
/**
 * @generated NOT
 */
@Override
public Profile getProfile ()
{
    if ( this.profile == null )
    {
        final ResourceSet rs = new ResourceSetImpl ();
        final Resource r = rs.createResource ( URI.createURI ( DEFAULT_URI ), "org.eclipse.scada.configuration.world.osgi.profile" );
        try
        {
            r.load ( null );
        }
        catch ( final IOException e )
        {
            throw new RuntimeException ( e );
        }
        this.profile = (Profile)EcoreUtil.getObjectByType ( r.getContents (), ProfilePackage.Literals.PROFILE );
        if ( this.profile == null )
        {
            throw new IllegalStateException ( String.format ( "Resource loaded from %s does not contain an object of type %s", DEFAULT_URI, Profile.class.getName () ) );
        }
    }

    return this.profile;
}
 
Example 3
/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated NOT
 */
public Profile getProfile ()
{
    if ( this.profile == null )
    {
        final ResourceSet rs = new ResourceSetImpl ();
        final Resource r = rs.createResource ( URI.createURI ( DEFAULT_URI ), "org.eclipse.scada.configuration.world.osgi.profile" );
        try
        {
            r.load ( null );
        }
        catch ( final IOException e )
        {
            throw new RuntimeException ( e );
        }
        this.profile = (Profile)EcoreUtil.getObjectByType ( r.getContents (), ProfilePackage.Literals.PROFILE );
        if ( this.profile == null )
        {
            throw new IllegalStateException ( String.format ( "Resource loaded from %s does not contain an object of type %s", DEFAULT_URI, Profile.class.getName () ) );
        }
    }

    return this.profile;
}
 
Example 4
@Override
public Profile getProfile ()
{
    if ( this.profile == null )
    {
        final ResourceSet rs = new ResourceSetImpl ();
        final Resource r = rs.createResource ( URI.createURI ( DEFAULT_URI ), "org.eclipse.scada.configuration.world.osgi.profile" );
        try
        {
            r.load ( null );
        }
        catch ( final IOException e )
        {
            throw new RuntimeException ( e );
        }
        this.profile = (Profile)EcoreUtil.getObjectByType ( r.getContents (), ProfilePackage.Literals.PROFILE );
        if ( this.profile == null )
        {
            throw new IllegalStateException ( String.format ( "Resource loaded from %s does not contain an object of type %s", DEFAULT_URI, Profile.class.getName () ) );
        }
    }

    return this.profile;
}
 
Example 5
@Override
public Profile getProfile ()
{
    if ( this.profile == null )
    {
        final ResourceSet rs = new ResourceSetImpl ();
        final Resource r = rs.createResource ( URI.createURI ( DEFAULT_URI ), "org.eclipse.scada.configuration.world.osgi.profile" );
        try
        {
            r.load ( null );
        }
        catch ( final IOException e )
        {
            throw new RuntimeException ( e );
        }
        this.profile = (Profile)EcoreUtil.getObjectByType ( r.getContents (), ProfilePackage.Literals.PROFILE );
        if ( this.profile == null )
        {
            throw new IllegalStateException ( String.format ( "Resource loaded from %s does not contain an object of type %s", DEFAULT_URI, Profile.class.getName () ) );
        }
    }

    return this.profile;
}
 
Example 6
Source Project: neoscada   File: FactoryImpl.java    License: Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected Entry<ComponentFactory> createService ( final UserInformation userInformation, final String configurationId, final BundleContext context, final Map<String, String> parameters ) throws Exception
{
    final ConfigurationDataHelper cfg = new ConfigurationDataHelper ( parameters );
    final String xml = cfg.getStringNonEmpty ( "configuration" );

    final XMIResource xmi = new XMIResourceImpl ();
    final Map<?, ?> options = new HashMap<Object, Object> ();
    final InputSource is = new InputSource ( new StringReader ( xml ) );
    xmi.load ( is, options );

    final Object c = EcoreUtil.getObjectByType ( xmi.getContents (), ParserPackage.Literals.COMPONENT );
    if ( ! ( c instanceof Component ) )
    {
        throw new RuntimeException ( String.format ( "Configuration did not contain an object of type %s", Component.class.getName () ) );
    }

    final ComponentFactoryWrapper wrapper = new ComponentFactoryWrapper ( this.executor, (Component)c );

    final Dictionary<String, ?> properties = new Hashtable<> ();
    final ServiceRegistration<ComponentFactory> handle = context.registerService ( ComponentFactory.class, wrapper, properties );

    return new Entry<ComponentFactory> ( configurationId, wrapper, handle );
}
 
Example 7
Source Project: neoscada   File: Hive.java    License: Eclipse Public License 1.0 6 votes vote down vote up
private static RootType parse ( final URI uri ) throws IOException
{
    final ResourceSet rs = new ResourceSetImpl ();
    rs.getResourceFactoryRegistry ().getExtensionToFactoryMap ().put ( "*", new ConfigurationResourceFactoryImpl () );

    final Resource r = rs.createResource ( uri );
    r.load ( null );

    final DocumentRoot doc = (DocumentRoot)EcoreUtil.getObjectByType ( r.getContents (), ConfigurationPackage.Literals.DOCUMENT_ROOT );
    if ( doc == null )
    {
        return null;
    }
    else
    {
        return doc.getRoot ();
    }
}
 
Example 8
protected Protocol loadProtocol ( final URI modelURI )
{
    final ResourceSet rs = new ResourceSetImpl ();
    rs.getResourceFactoryRegistry ().getExtensionToFactoryMap ().put ( "protocol", new XMIResourceFactoryImpl () );

    final Resource resource = rs.getResource ( modelURI, true );

    return (Protocol)EcoreUtil.getObjectByType ( resource.getContents (), ProtocolPackage.Literals.PROTOCOL );
}
 
Example 9
protected static Chart load ( final InputStream input ) throws IOException
{
    final Resource resource = new XMIResourceFactoryImpl ().createResource ( URI.createURI ( "urn:memento" ) );

    final Map<?, ?> options = new HashMap<Object, Object> ();
    resource.load ( input, options );

    return (Chart)EcoreUtil.getObjectByType ( resource.getContents (), ChartPackage.Literals.CHART );
}
 
Example 10
@Test
public void testSetUpUML() {
	IProject sourceproject = ProjectUtils.createProject("sourceProject");
	
	String modelFilename = "dummy";
	
	String tmpfolder = sourceproject.getLocation().toString();

       URI uri = URI.createURI(tmpfolder).appendSegment(modelFilename).appendFileExtension(UMLResource.FILE_EXTENSION);
       URI urifile = URI.createFileURI(uri.toString());
       String modelname = "TestModel";
	createUMLFile(urifile, modelname);
	
	creator.setUpUML(urifile.toString());
	
	IFile file = project.getFile("test."+UMLResource.FILE_EXTENSION);
	
	assertTrue(file.exists());
	assertTrue(file.getFileExtension().equals(UMLResource.FILE_EXTENSION));
	/* uml model asserts */
	

	ResourceSetImpl RESOURCE_SET = new ResourceSetImpl();
	Resource resource = RESOURCE_SET.getResource(urifile, true);
	
	org.eclipse.uml2.uml.Package package_ = (org.eclipse.uml2.uml.Package) EcoreUtil
											.getObjectByType(resource.getContents(), UMLPackage.Literals.PACKAGE);
	assertTrue(package_ instanceof Model);
	assertTrue(package_.getName().equals(modelname));
}
 
Example 11
/**
 * Helper method - loads a testmodel from the given {@link URI}
 * 
 * @param uri
 *            the {@link URI} of the model file
 * @return the {@link Statechart}
 */
public static final Statechart loadStatechart(URI uri) {
	ResourceSet resSet = new ResourceSetImpl();
	Resource resource = resSet.getResource(uri, true);
	Statechart statechart = (Statechart) EcoreUtil.getObjectByType(
			resource.getContents(), SGraphPackage.Literals.STATECHART);
	return statechart;
}
 
Example 12
/**
 * Inject the CA bootstrap property to the profile
 *
 * @param file
 *            the profile.xml file in the package target
 * @throws IOException
 */
protected void patchProfile ( final String appName, final File file ) throws IOException
{
    final ResourceSet rs = new ResourceSetImpl ();
    final Resource r = rs.createResource ( URI.createFileURI ( file.toString () ) );
    r.load ( null );

    final Profile profile = (Profile)EcoreUtil.getObjectByType ( r.getContents (), ProfilePackage.Literals.PROFILE );
    Profiles.addSystemProperty ( profile, "org.eclipse.scada.ca.file.provisionJsonUrl", "file:///usr/share/" + Constants.NEOSCADA_USER + "/ca.bootstrap/bootstrap." + appName + ".json" );
    r.save ( null );
}
 
Example 13
Source Project: neoscada   File: LaunchShortcut.java    License: Eclipse Public License 1.0 5 votes vote down vote up
protected ILaunchConfiguration createConfiguration ( final IResource resource ) throws Exception
{
    final ResourceSet rs = new ResourceSetImpl ();
    final Resource r = rs.createResource ( URI.createURI ( resource.getLocationURI ().toURL ().toString () ) );
    r.load ( null );
    final Profile profile = (Profile)EcoreUtil.getObjectByType ( r.getContents (), ProfilePackage.Literals.PROFILE );
    if ( profile == null )
    {
        return null;
    }

    String name = profile.getName ();
    if ( name == null || name.isEmpty () )
    {
        name = String.format ( "Application profile: %s", resource.getFullPath () ); //$NON-NLS-1$
    }

    final ILaunchConfigurationWorkingCopy cfg = getConfigurationType ().newInstance ( resource.getParent (), name );

    final Map<String, String> envs = new HashMap<> ();
    envs.put ( "SCADA_PROFILE", String.format ( "${project_loc:/%s}/%s", resource.getProject ().getName (), resource.getProjectRelativePath () ) ); //$NON-NLS-1$ //$NON-NLS-2$
    cfg.setAttribute ( ATTR_ENV_VARS, envs );

    cfg.setAttribute ( IPDELauncherConstants.INCLUDE_OPTIONAL, false );
    cfg.setAttribute ( IPDELauncherConstants.AUTOMATIC_ADD, false );
    cfg.setAttribute ( IPDELauncherConstants.AUTOMATIC_VALIDATE, true );
    cfg.setAttribute ( IPDELauncherConstants.DEFAULT_AUTO_START, false );
    cfg.setAttribute ( IPDELauncherConstants.CONFIG_USE_DEFAULT_AREA, false );
    cfg.setAttribute ( IPDELauncherConstants.CONFIG_LOCATION, getConfigurationArea ( profile ) );

    addAllBundels ( cfg, profile );
    addJvmOptions ( cfg, profile, resource.getParent () );

    cfg.setAttribute ( IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS, "-os ${target.os} -ws ${target.ws} -arch ${target.arch} -nl ${target.nl}  -consoleLog -console" ); //$NON-NLS-1$

    return cfg.doSave ();
}
 
Example 14
Source Project: neoscada   File: Controller.java    License: Eclipse Public License 1.0 5 votes vote down vote up
private static DocumentRoot parse ( final URI uri ) throws ConfigurationException
{
    ExporterPackage.eINSTANCE.eClass ();

    /*
     * we do need to provide the current context classloader, otherwise there
     * seem to be problems finding services when run with jsvc. 
     */
    ServiceLoaderProcessor.initialize ( "emf", Thread.currentThread ().getContextClassLoader () );

    try
    {
        final ResourceSet rs = new ResourceSetImpl ();
        rs.getResourceFactoryRegistry ().getExtensionToFactoryMap ().put ( "*", new ExporterResourceFactoryImpl () );
        final Resource resource = rs.createResource ( uri );
        resource.load ( null );

        final DocumentRoot result = (DocumentRoot)EcoreUtil.getObjectByType ( resource.getContents (), ExporterPackage.Literals.DOCUMENT_ROOT );
        if ( result == null )
        {
            throw new IllegalStateException ( "Document does not contain a configuration" );
        }
        return result;
    }
    catch ( final Exception e )
    {
        throw new ConfigurationException ( "Failed to parse document", e );
    }
}
 
Example 15
Source Project: neoscada   File: Hive.java    License: Eclipse Public License 1.0 5 votes vote down vote up
private static RootType parse ( final URI uri ) throws IOException
{
    final ResourceSet rs = new ResourceSetImpl ();

    rs.getResourceFactoryRegistry ().getExtensionToFactoryMap ().put ( "*", new ConfigurationResourceFactoryImpl () );
    final Resource r = rs.createResource ( uri );
    r.load ( null );

    return (RootType)EcoreUtil.getObjectByType ( r.getContents (), ConfigurationPackage.Literals.ROOT_TYPE );
}
 
Example 16
protected Collection<ImportScope> getImportScopes(final Resource resource) {
	StatechartSpecification specification = (StatechartSpecification) EcoreUtil
			.getObjectByType(resource.getContents(), StextPackage.Literals.STATECHART_SPECIFICATION);
	if (specification != null) {
		return EcoreUtil.getObjectsByType(specification.getScopes(), StextPackage.Literals.IMPORT_SCOPE);
	} else {
		Statechart statechart = utils.getStatechart(resource);
		if (statechart == null) {
			return new LinkedHashSet<>();
		}
		return EcoreUtil.getObjectsByType(statechart.getScopes(), StextPackage.Literals.IMPORT_SCOPE);
	}
}
 
Example 17
protected Statechart loadStatechart(String path) {
	ResourceSet resSet = new ResourceSetImpl();
	Resource resource = resSet.getResource(URI.createPlatformPluginURI(VALIDATION_TESTMODEL_DIR + path, true),
			true);
	Statechart statechart = (Statechart) EcoreUtil.getObjectByType(resource.getContents(),
			SGraphPackage.Literals.STATECHART);
	return statechart;
}
 
Example 18
protected void load () throws Exception
{
    // register model
    VisualInterfacePackage.eINSTANCE.eClass ();

    this.symbol = null;

    final ResourceSet resourceSet = new ResourceSetImpl ();

    // set resource factory to XMI on extension map
    resourceSet.getResourceFactoryRegistry ().getExtensionToFactoryMap ().put ( "vi", FACTORY_INSTANCE ); //$NON-NLS-1$

    final Resource resource = resourceSet.getResource ( this.uri, true );

    this.symbol = (Symbol)EcoreUtil.getObjectByType ( resource.getContents (), VisualInterfacePackage.Literals.SYMBOL );
}
 
Example 19
protected View getViewForState(Statechart initial, String stateName) {
	Diagram diagram = (Diagram) EcoreUtil.getObjectByType(initial.eResource().getContents(), NotationPackage.Literals.DIAGRAM);
	RefactoringHelper helper = new RefactoringHelper();
	return helper.getViewForSemanticElement(getStateByName(initial, stateName), diagram);
}
 
Example 20
public static Statechart loadStatechart(String filename) {
	Resource resource = loadResource(filename);
	Statechart statechart = (Statechart) EcoreUtil.getObjectByType(
			resource.getContents(), SGraphPackage.Literals.STATECHART);
	return statechart;
}