use of org.eclipse.wst.sse.core.internal.util.ProjectResolver in project webtools.sourceediting by eclipse.
the class URLModelProvider method getCommonModelFor.
/**
* <code>baseModel</code>: the model containing the link
* <code>ref</code>: the link URL string
*/
private IStructuredModel getCommonModelFor(final IStructuredModel baseModel, final String ref, final int which) throws IOException {
// first, create absolute url
String absURL = resolveURI(baseModel, ref, true);
if ((absURL == null) || (absURL.length() == 0)) {
return null;
}
// need to remove file:// scheme if necessary
try {
final java.net.URL aURL = new java.net.URL(absURL);
// resolve it by finding the file it points to
if (!aURL.getProtocol().equals("platform")) {
// $NON-NLS-1$
if (aURL.getProtocol().equals("file") && (aURL.getHost().equals("localhost") || aURL.getHost().length() == 0)) {
// $NON-NLS-2$//$NON-NLS-1$
absURL = aURL.getFile();
final IPath ipath = new Path(absURL);
// if path has a device, and if it begins with
// IPath.SEPARATOR, remove it
final String device = ipath.getDevice();
if ((device != null) && (device.length() > 0)) {
if (device.charAt(0) == IPath.SEPARATOR) {
final String newDevice = device.substring(1);
absURL = ipath.setDevice(newDevice).toString();
}
}
}
}
} catch (java.net.MalformedURLException mfuExc) {
}
// next, decide project
IProject project = null;
final IPath fullIPath = new Path(absURL);
IWorkspaceRoot workspace = ResourcesPlugin.getWorkspace().getRoot();
IContainer container = workspace.getContainerForLocation(fullIPath);
if (container != null) {
// fullIPath doesn't exist in workspace
project = container.getProject();
}
// now, get absURL's IFile
if ((project != null) && (project.getLocation().isPrefixOf(fullIPath) == false)) {
// it's at outside of Project
return null;
}
IStructuredModel model = null;
if (project != null) {
IPath filePath = fullIPath.removeFirstSegments(project.getLocation().segmentCount());
IFile file = (filePath != null && !filePath.isEmpty()) ? project.getFile(filePath) : null;
if (file == null) {
return null;
}
// obtain model
if (which == GET_MODEL_FOR_EDIT) {
model = getModelForEdit(file);
} else if (which == GET_MODEL_FOR_READ) {
model = getModelForRead(file);
}
// responsibility
if (model != null && model.getSynchronizationStamp() == IResource.NULL_STAMP)
model.resetSynchronizationStamp(file);
} else {
String id = null;
InputStream inStream = null;
// obtain resolver
URIResolver resolver = (project != null) ? (URIResolver) project.getAdapter(URIResolver.class) : null;
if (resolver == null) {
// ProjectResolver can take care of the case if project is
// null.
resolver = new ProjectResolver(project);
}
if (resolver == null) {
return null;
}
// there is no project. we can't expect IProject help to create
// id/inputStream
java.io.File file = fullIPath.toFile();
// obatin id
id = calculateId(fullIPath);
// obtain InputStream
try {
inStream = new FileInputStream(file);
} catch (FileNotFoundException fnfe) {
// the file does not exist, or we don't have read permission
return null;
}
// obtain model
try {
if (which == GET_MODEL_FOR_EDIT) {
model = getModelManager().getModelForEdit(id, inStream, resolver);
} else if (which == GET_MODEL_FOR_READ) {
model = getModelManager().getModelForRead(id, inStream, resolver);
}
} catch (UnsupportedEncodingException ue) {
} catch (IOException ioe) {
} finally {
// close now !
if (inStream != null) {
inStream.close();
}
}
}
// set locationid
if (model != null && model.getBaseLocation() == null) {
model.setBaseLocation(fullIPath.toString());
}
return model;
}
use of org.eclipse.wst.sse.core.internal.util.ProjectResolver in project webtools.sourceediting by eclipse.
the class ModelManagerImpl method calculateURIResolver.
/**
*/
private URIResolver calculateURIResolver(IFile file) {
// Note: see comment in plugin.xml for potentially
// breaking change in behavior.
IProject project = file.getProject();
URIResolver resolver = project.getAdapter(URIResolver.class);
if (resolver == null)
resolver = new ProjectResolver(project);
Object location = file.getLocation();
if (location == null)
location = file.getLocationURI();
if (location != null)
resolver.setFileBaseLocation(location.toString());
return resolver;
}
use of org.eclipse.wst.sse.core.internal.util.ProjectResolver in project webtools.sourceediting by eclipse.
the class URLModelProviderCSS method getCommonModelFor.
/**
* <code>baseModel</code>: the model containing the link
* <code>ref</code>: the link URL string
*/
private IStructuredModel getCommonModelFor(final IStructuredModel baseModel, final String ref, final int which) throws IOException {
// first, create absolute url
String absURL = resolveURI(baseModel, ref, true);
if ((absURL == null) || (absURL.length() == 0)) {
return null;
}
// need to remove file:// scheme if necessary
try {
final URL aURL = new URL(absURL);
// resolve it by finding the file it points to
if (!aURL.getProtocol().equals("platform")) {
// $NON-NLS-1$
if (aURL.getProtocol().equals("file") && (aURL.getHost().equals("localhost") || aURL.getHost().length() == 0)) {
// $NON-NLS-2$//$NON-NLS-1$
absURL = aURL.getFile();
final IPath ipath = new Path(absURL);
// if path has a device, and if it begins with
// IPath.SEPARATOR, remove it
final String device = ipath.getDevice();
if ((device != null) && (device.length() > 0)) {
if (device.charAt(0) == IPath.SEPARATOR) {
final String newDevice = device.substring(1);
absURL = ipath.setDevice(newDevice).toString();
}
}
}
}
} catch (MalformedURLException mfuExc) {
}
// next, decide project
IProject project = null;
final IPath fullIPath = new Path(absURL);
IWorkspaceRoot workspace = ResourcesPlugin.getWorkspace().getRoot();
IContainer container = workspace.getContainerForLocation(fullIPath);
if (container != null) {
// fullIPath doesn't exist in workspace
project = container.getProject();
}
// now, get absURL's IFile
if ((project != null) && (project.getLocation().isPrefixOf(fullIPath) == false)) {
// it's at outside of Project
return null;
}
IStructuredModel model = null;
if (project != null) {
IPath filePath = fullIPath.removeFirstSegments(project.getLocation().segmentCount());
IFile file = (filePath != null && !filePath.isEmpty()) ? project.getFile(filePath) : null;
if (file == null) {
return null;
}
// obtain model
if (which == GET_MODEL_FOR_EDIT) {
model = getModelForEdit(file);
} else if (which == GET_MODEL_FOR_READ) {
model = getModelForRead(file);
}
// responsibility
if (model != null && model.getSynchronizationStamp() == IResource.NULL_STAMP)
model.resetSynchronizationStamp(file);
} else {
String id = null;
InputStream inStream = null;
// obtain resolver
URIResolver resolver = (project != null) ? (URIResolver) project.getAdapter(URIResolver.class) : null;
if (resolver == null) {
// ProjectResolver can take care of the case if project is
// null.
resolver = new ProjectResolver(project);
}
if (resolver == null) {
return null;
}
// there is no project. we can't expect IProject help to create
// id/inputStream
File file = fullIPath.toFile();
// obatin id
id = calculateId(fullIPath);
// obtain InputStream
try {
inStream = new FileInputStream(file);
} catch (FileNotFoundException fnfe) {
// the file does not exist, or we don't have read permission
return null;
}
// obtain model
try {
if (which == GET_MODEL_FOR_EDIT) {
model = getModelManager().getModelForEdit(id, inStream, resolver);
} else if (which == GET_MODEL_FOR_READ) {
model = getModelManager().getModelForRead(id, inStream, resolver);
}
} catch (UnsupportedEncodingException ue) {
} catch (IOException ioe) {
} finally {
// close now !
if (inStream != null) {
inStream.close();
}
}
}
// set locationid
if (model != null && model.getBaseLocation() == null) {
model.setBaseLocation(fullIPath.toString());
}
return model;
}
Aggregations