blob: 0343bcbac8f70bd15d5b6cca73d47296018d2767 [file] [log] [blame]
/*=============================================================================#
# Copyright (c) 2009, 2020 Stephan Wahlbrink and others.
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
# https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
# which is available at https://www.apache.org/licenses/LICENSE-2.0.
#
# SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
#
# Contributors:
# Stephan Wahlbrink <sw@wahlbrink.eu> - initial API and implementation
#=============================================================================*/
package org.eclipse.statet.ltk.model.core.impl;
import java.net.URI;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.statet.ltk.core.LTK;
import org.eclipse.statet.ltk.model.core.ISourceUnitFactory;
import org.eclipse.statet.ltk.model.core.elements.ISourceUnit;
/**
* Abstract factory for {@link LTK#PERSISTENCE_CONTEXT}.
*/
public abstract class AbstractFilePersistenceSourceUnitFactory implements ISourceUnitFactory {
private static final String IFILE_PREFIX= "platform:/resource"; //$NON-NLS-1$
public static String createResourceId(final IResource file) {
if (file != null) {
final IPath path= file.getFullPath();
if (path != null) {
return IFILE_PREFIX + path.toPortableString(); // eclipse-platform-resource
}
}
return null;
}
public static String createResourceId(URI uri) {
if (uri != null) {
uri= uri.normalize();
if (uri.getScheme() == null) {
return "xxx:"+uri.toString(); //$NON-NLS-1$
}
else {
return uri.toString();
}
}
return null;
}
public AbstractFilePersistenceSourceUnitFactory() {
}
@Override
public String createId(final Object from) {
if (from instanceof IFile) {
return createResourceId((IFile) from);
}
if (from instanceof String) {
final String s= (String) from;
if (s.startsWith(IFILE_PREFIX)) {
return s;
}
}
return null;
}
@Override
public ISourceUnit createSourceUnit(final String id, final Object from) {
IFile ifile;
if (from instanceof IFile) {
ifile= (IFile) from;
}
else {
final IPath path= Path.fromPortableString(id.substring(IFILE_PREFIX.length()));
ifile= ResourcesPlugin.getWorkspace().getRoot().getFile(path);
}
return createSourceUnit(id, ifile);
}
protected abstract ISourceUnit createSourceUnit(final String id, final IFile file);
}