001package org.andromda.repositories.emf;
002
003import org.andromda.core.repository.RepositoryFacadeException;
004import org.apache.commons.lang.StringUtils;
005import org.eclipse.emf.common.util.URI;
006
007/**
008 * Contains some utilities methods for dealing with the EMF repository
009 * facade functionality.
010 *
011 * @author Chad Brandon
012 */
013class EMFRepositoryFacadeUtils
014{
015    /**
016     * The URI file prefix.
017     */
018    private static final String FILE_PREFIX = "file:";
019
020    /**
021     * The URI archive file prefix.
022     */
023    private static final String ARCHIVE_FILE_PREFIX = "jar:file:";
024
025    /**
026     * Creates the EMF URI instance from the given <code>uri</code>.
027     *
028     * @param uri the path from which to create the URI.
029     * @return the URI
030     */
031    static URI createUri(String uri)
032    {
033        if (!uri.startsWith(ARCHIVE_FILE_PREFIX) && uri.startsWith(FILE_PREFIX))
034        {
035            final String filePrefixWithSlash = FILE_PREFIX + '/';
036            if (!uri.startsWith(filePrefixWithSlash))
037            {
038                uri = StringUtils.replaceOnce(
039                        uri,
040                        FILE_PREFIX,
041                        filePrefixWithSlash);
042            }
043        }
044        final URI resourceUri = URI.createURI(uri);
045        if (resourceUri == null)
046        {
047            throw new RepositoryFacadeException("The path '" + uri + "' is not a valid URI");
048        }
049        return resourceUri;
050    }
051}