Fix some of the warnings
[EMFCompare2.git] / plugins / org.eclipse.emf.compare.ide / src / org / eclipse / emf / compare / ide / internal / utils / URIStorage.java
blob6aa507b777d9c5fe1d34bde93527dcd3288d8b2f
1 /*******************************************************************************
2 * Copyright (c) 2014 Obeo.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * Obeo - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.emf.compare.ide.internal.utils;
13 import java.io.IOException;
14 import java.io.InputStream;
15 import java.util.Collections;
16 import java.util.Map;
18 import org.eclipse.core.resources.IStorage;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IPath;
21 import org.eclipse.core.runtime.IStatus;
22 import org.eclipse.core.runtime.Path;
23 import org.eclipse.core.runtime.Status;
24 import org.eclipse.emf.common.util.URI;
25 import org.eclipse.emf.compare.ide.EMFCompareIDEPlugin;
26 import org.eclipse.emf.ecore.resource.URIConverter;
27 import org.eclipse.emf.ecore.resource.URIHandler;
29 /**
30 * This implementation of an {@link IStorage} will allow us to keep track of the {@link URIHandler} that's
31 * been used to load a given URI from this uri converter.
33 * @author <a href="mailto:laurent.goubet@obeo.fr">Laurent Goubet</a>
35 public class URIStorage implements IStorage {
36 /** The target URI of this storage. */
37 private final URI uri;
39 /** The URI Handler that's been used to retrieve this URI's contents. */
40 private final URIHandler handler;
42 /** The URI converter from which this storage was created. */
43 private URIConverter converter;
45 /**
46 * Creates an URIStorage for the given URI an its associated handler.
48 * @param uri
49 * The target uri of this storage.
50 * @param handler
51 * The URI handler that can be used to retrieve this URI's contents.
52 * @param converter
53 * The URI converter which created this storage.
55 public URIStorage(URI uri, URIHandler handler, URIConverter converter) {
56 this.uri = uri;
57 this.handler = handler;
58 this.converter = converter;
61 /**
62 * {@inheritDoc}
64 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
66 @SuppressWarnings("unchecked")
67 public Object getAdapter(@SuppressWarnings("rawtypes") Class adapter) {
68 return null;
71 /**
72 * {@inheritDoc}
74 * @see org.eclipse.core.resources.IStorage#getContents()
76 public InputStream getContents() throws CoreException {
77 final Map<?, ?> options = Collections.singletonMap(URIConverter.OPTION_URI_CONVERTER, converter);
78 try {
79 return handler.createInputStream(uri, options);
80 } catch (IOException e) {
81 throw new CoreException(new Status(IStatus.ERROR, EMFCompareIDEPlugin.PLUGIN_ID, e.getMessage(),
82 e));
86 /**
87 * {@inheritDoc}
89 * @see org.eclipse.core.resources.IStorage#getFullPath()
91 public IPath getFullPath() {
92 final String path;
93 if (uri.isRelative()) {
94 path = uri.toString();
95 } else if (uri.isPlatformResource()) {
96 path = uri.toPlatformString(true);
97 } else {
98 path = uri.toString();
100 return new Path(path);
104 * {@inheritDoc}
106 * @see org.eclipse.core.resources.IStorage#getName()
108 public String getName() {
109 return URI.decode(uri.lastSegment());
113 * {@inheritDoc}
115 * @see org.eclipse.core.resources.IStorage#isReadOnly()
117 public boolean isReadOnly() {
118 final Map<?, ?> options = Collections.singletonMap(URIConverter.OPTION_REQUESTED_ATTRIBUTES,
119 Collections.singleton(URIConverter.ATTRIBUTE_READ_ONLY));
120 final Map<String, ?> attributes = handler.getAttributes(uri, options);
121 return Boolean.TRUE.equals(attributes.get(URIConverter.ATTRIBUTE_READ_ONLY));