use of org.eclipse.wst.sse.core.internal.util.URIResolver 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;
}
use of org.eclipse.wst.sse.core.internal.util.URIResolver in project webtools.sourceediting by eclipse.
the class TestFormatProcessorHTML method getModelForEdit.
/**
* must release model (from edit) after
*
* @param filename
* relative to this class (TestStructuredPartitioner)
*/
private IStructuredModel getModelForEdit(final String filename) {
IStructuredModel model = null;
try {
IModelManager modelManager = StructuredModelManager.getModelManager();
InputStream inStream = getClass().getResourceAsStream(filename);
if (inStream == null)
throw new FileNotFoundException("Can't file resource stream " + filename);
final String baseFile = getClass().getResource(filename).toString();
model = modelManager.getModelForEdit(baseFile, inStream, new URIResolver() {
String fBase = baseFile;
public String getFileBaseLocation() {
return fBase;
}
public String getLocationByURI(String uri) {
return getLocationByURI(uri, fBase);
}
public String getLocationByURI(String uri, boolean resolveCrossProjectLinks) {
return getLocationByURI(uri);
}
public String getLocationByURI(String uri, String baseReference) {
int lastSlash = baseReference.lastIndexOf("/");
if (lastSlash > 0)
return baseReference.substring(0, lastSlash + 1) + uri;
return baseReference;
}
public String getLocationByURI(String uri, String baseReference, boolean resolveCrossProjectLinks) {
return getLocationByURI(uri, baseReference);
}
public IProject getProject() {
return null;
}
public IContainer getRootLocation() {
return null;
}
public InputStream getURIStream(String uri) {
return getClass().getResourceAsStream(getLocationByURI(uri));
}
public void setFileBaseLocation(String newLocation) {
this.fBase = newLocation;
}
public void setProject(IProject newProject) {
}
});
} catch (IOException ex) {
ex.printStackTrace();
}
return model;
}
use of org.eclipse.wst.sse.core.internal.util.URIResolver in project webtools.sourceediting by eclipse.
the class ModelManagerImpl method createUnManagedEmptyModelFor.
/**
* @param iFile
* @param result
* @return
* @throws CoreException
*/
private IStructuredModel createUnManagedEmptyModelFor(IFile iFile) throws CoreException {
IStructuredModel result = null;
IModelHandler handler = calculateType(iFile);
String id = calculateId(iFile);
URIResolver resolver = calculateURIResolver(iFile);
try {
result = _commonCreateModel(id, handler, resolver);
} catch (ResourceInUse e) {
// this may need to be re-examined.
if (Logger.DEBUG_MODELMANAGER)
// $NON-NLS-1$ //$NON-NLS-2$
Logger.log(Logger.INFO, "ModelMangerImpl::createUnManagedStructuredModelFor. Model unexpectedly in use.");
}
return result;
}
use of org.eclipse.wst.sse.core.internal.util.URIResolver in project webtools.sourceediting by eclipse.
the class ModelManagerImpl method _commonGetModel.
private IStructuredModel _commonGetModel(IFile iFile, ReadEditType rwType, EncodingRule encodingRule) throws UnsupportedEncodingException, IOException, CoreException {
IStructuredModel model = null;
if (iFile != null && iFile.exists()) {
String id = calculateId(iFile);
IModelHandler handler = calculateType(iFile);
URIResolver resolver = calculateURIResolver(iFile);
model = _commonCreateModel(iFile, id, handler, resolver, rwType, encodingRule);
}
return model;
}
use of org.eclipse.wst.sse.core.internal.util.URIResolver in project webtools.sourceediting by eclipse.
the class ModelManagerImpl method createNewInstance.
/**
* Similar to clone, except the new instance has no content. Note: this
* produces an unmanaged model, for temporary use. If a true shared model
* is desired, use "copy".
*/
public IStructuredModel createNewInstance(IStructuredModel oldModel) throws IOException {
IModelHandler handler = oldModel.getModelHandler();
IModelLoader loader = handler.getModelLoader();
IStructuredModel newModel = loader.createModel(oldModel);
newModel.setModelHandler(handler);
if (newModel instanceof AbstractStructuredModel) {
((AbstractStructuredModel) newModel).setContentTypeIdentifier(oldModel.getContentTypeIdentifier());
}
URIResolver oldResolver = oldModel.getResolver();
if (oldResolver instanceof URIResolverExtension) {
oldResolver = ((URIResolverExtension) oldResolver).newInstance();
}
newModel.setResolver(oldResolver);
try {
newModel.setId(DUPLICATED_MODEL);
} catch (ResourceInUse e) {
// impossible, since this is an unmanaged model
}
// base location should be null, but we'll set to
// null to be sure.
newModel.setBaseLocation(null);
return newModel;
}
Aggregations