use of org.eclipse.wst.sse.core.internal.util.URIResolver in project webtools.sourceediting by eclipse.
the class URLModelProviderCSS method resolveURI.
/**
* <code>baseModel</code>: the model containing the link
* <code>ref</code>: the link URL string
* <code>resolveCrossProjectLinks</code>: If resolveCrossProjectLinks
* is set to true, then this method will properly resolve the URI if it is
* a valid URI pointing to another (appropriate) project.
*/
public static String resolveURI(IStructuredModel baseModel, String ref, boolean resolveCrossProjectLinks) {
if (baseModel == null)
return null;
// get resolver in Model
final URIResolver resolver = baseModel.getResolver();
// resolve to absolute url
final String absurl = (resolver != null) ? resolver.getLocationByURI(ref, resolveCrossProjectLinks) : null;
if ((resolver != null) && (absurl == null) && (ref != null) && (ref.trim().length() > 0) && (ref.trim().charAt(0) == '/')) {
// so that href is a broken and should not create model
return null;
}
if ((absurl != null) && (absurl.length() > 0)) {
return absurl;
}
// maybe ref is at outside of the Project
// obtain docroot;
final IContainer container = (resolver != null) ? resolver.getRootLocation() : null;
String docroot = null;
if (container != null) {
docroot = container.getLocation().toString();
}
if (docroot == null) {
docroot = baseModel.getBaseLocation();
}
if (docroot == null) {
// should not be
return null;
}
// obtain document url
String modelBaseLocation = baseModel.getBaseLocation();
if ((modelBaseLocation == null) || (modelBaseLocation.length() == 0)) {
// fallback...
modelBaseLocation = baseModel.getId();
}
if ((modelBaseLocation == null) || (modelBaseLocation.length() == 0)) {
// i can't resolve uri !
return null;
}
// resolve url
URLHelper helper = new URLHelper(PathHelper.getContainingFolderPath(modelBaseLocation), PathHelper.getContainingFolderPath(PathHelper.appendTrailingURLSlash(docroot)));
return helper.toAbsolute(ref);
}
use of org.eclipse.wst.sse.core.internal.util.URIResolver in project webtools.sourceediting by eclipse.
the class FileBufferModelManager method createURIResolver.
URIResolver createURIResolver(ITextFileBuffer buffer) {
URIResolver resolver = null;
IPath location = buffer.getLocation();
if (location != null) {
IFile workspaceFile = FileBuffers.getWorkspaceFileAtLocation(location);
if (workspaceFile != null) {
IProject project = workspaceFile.getProject();
resolver = project.getAdapter(URIResolver.class);
if (resolver == null) {
resolver = new CommonURIResolver(workspaceFile);
}
String baseLocation = null;
if (workspaceFile.getLocation() != null) {
baseLocation = workspaceFile.getLocation().toString();
}
if (baseLocation == null && workspaceFile.getLocationURI() != null) {
baseLocation = workspaceFile.getLocationURI().toString();
}
if (baseLocation == null) {
baseLocation = workspaceFile.getFullPath().toString();
}
resolver.setFileBaseLocation(baseLocation);
} else {
resolver = new ExternalURIResolver(location);
}
} else if (buffer.getFileStore() != null) {
resolver = new BasicURIResolver(buffer.getFileStore().toURI());
}
return resolver;
}
use of org.eclipse.wst.sse.core.internal.util.URIResolver 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.URIResolver in project webtools.sourceediting by eclipse.
the class TestCleanupProcessorXML method getModelForEdit.
/**
* must release model (from edit) after
*
* @param filename
* relative to this class (TestCleanupProcessorXML)
*/
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 TestFormatProcessorXML 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;
}
Aggregations