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
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
;
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
;
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
;
46 * Creates an URIStorage for the given URI an its associated handler.
49 * The target uri of this storage.
51 * The URI handler that can be used to retrieve this URI's contents.
53 * The URI converter which created this storage.
55 public URIStorage(URI uri
, URIHandler handler
, URIConverter converter
) {
57 this.handler
= handler
;
58 this.converter
= converter
;
64 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
66 @SuppressWarnings("unchecked")
67 public Object
getAdapter(@SuppressWarnings("rawtypes") Class adapter
) {
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
);
79 return handler
.createInputStream(uri
, options
);
80 } catch (IOException e
) {
81 throw new CoreException(new Status(IStatus
.ERROR
, EMFCompareIDEPlugin
.PLUGIN_ID
, e
.getMessage(),
89 * @see org.eclipse.core.resources.IStorage#getFullPath()
91 public IPath
getFullPath() {
93 if (uri
.isRelative()) {
94 path
= uri
.toString();
95 } else if (uri
.isPlatformResource()) {
96 path
= uri
.toPlatformString(true);
98 path
= uri
.toString();
100 return new Path(path
);
106 * @see org.eclipse.core.resources.IStorage#getName()
108 public String
getName() {
109 return URI
.decode(uri
.lastSegment());
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
));