use of org.eclipse.wst.sse.core.internal.util.URIResolver in project webtools.sourceediting by eclipse.
the class HTMLValidator method validate.
/**
* @deprecated but remains for compatibility
*/
protected HTMLValidationResult validate(IDOMModel model, IFile file) {
IProject prj = null;
if (file != null) {
prj = file.getProject();
}
if ((prj == null) && (model != null)) {
URIResolver res = model.getResolver();
if (res != null) {
prj = res.getProject();
}
}
final WorkbenchReporter reporter = new WorkbenchReporter(prj, new NullProgressMonitor());
return validate(reporter, file, model);
}
use of org.eclipse.wst.sse.core.internal.util.URIResolver in project webtools.sourceediting by eclipse.
the class URLModelProvider 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;
// for HTML, 'href' attribute value of BASE element
// should be used, if exists any
String baseHref = null;
// of HTML or XHTML
if (isHTMLFamily(baseModel)) {
final IDOMModel xmlmodel = (IDOMModel) baseModel;
final IDOMDocument doc = xmlmodel.getDocument();
// look for <BASE> w/ href
// $NON-NLS-1$
final NodeList nl = doc.getElementsByTagName("BASE");
if ((nl != null) && (nl.getLength() > 0)) {
// per each <BASE>
for (int i = 0; i < nl.getLength(); i++) {
final Node baseNode = nl.item(i);
if (baseNode != null) {
// get all attrs
final NamedNodeMap attrNodes = baseNode.getAttributes();
if (attrNodes != null) {
// $NON-NLS-1$
final Node attrNode = attrNodes.getNamedItem("HREF");
if (attrNode != null) {
// found href=""
final String attrValue = attrNode.getNodeValue();
if (attrValue != null) {
baseHref = attrValue.trim();
}
}
}
}
// what if there are multiple <BASE> tags ??
if (baseHref != null) {
break;
}
}
}
}
// get resolver in Model
final URIResolver resolver = baseModel.getResolver();
// resolve to absolute url
final String absurl = (resolver != null) ? ((baseHref != null) ? resolver.getLocationByURI(ref, baseHref, resolveCrossProjectLinks) : 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) {
IPath containerLocation = container.getLocation();
if (containerLocation != null) {
docroot = containerLocation.toString();
} else if (container.getLocationURI() != null) {
docroot = container.getLocationURI().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 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.URIResolver in project webtools.sourceediting by eclipse.
the class LinkElementAdapter method createModel.
protected ICSSModel createModel() {
// create phantom(broken link) external CSS model
if (getElement() == null)
return null;
IStructuredModel baseModel = ((IDOMNode) getElement()).getModel();
ICSSModel newModel = (ICSSModel) baseModel.getModelManager().createUnManagedStructuredModelFor(CSS_ID);
// calculate base location and set
// get resolver in Model
URIResolver resolver = baseModel.getResolver();
// resolve to absolute url : this need not exact location of css file. It is important that absurl is not null.
String ref = getElement().getAttribute(HTML40Namespace.ATTR_NAME_HREF);
String absurl = (resolver != null && ref != null && ref.length() > 0) ? resolver.getLocationByURI(ref, true) : null;
if ((absurl == null) || (absurl.length() == 0)) {
IPath basePath = new Path(baseModel.getBaseLocation());
URLHelper helper = new URLHelper(basePath.removeLastSegments(1).toString());
// $NON-NLS-1$
absurl = helper.toAbsolute(ref == null ? "" : ref);
}
if ((absurl == null) || (absurl.length() == 0)) {
absurl = ref;
}
if (absurl == null) {
// $NON-NLS-1$
absurl = "";
}
newModel.setBaseLocation(absurl);
// set style listener
newModel.addStyleListener(this);
return newModel;
}
use of org.eclipse.wst.sse.core.internal.util.URIResolver in project webtools.sourceediting by eclipse.
the class AbstractOpenOn method getFile.
/**
* Returns an IFile from the given uri if possible, null if cannot find
* file from uri.
*
* @param fileString
* file system path
* @return returns IFile if fileString exists in the workspace
*/
protected IFile getFile(String fileString) {
IStructuredModel model = null;
IFile file = null;
try {
model = StructuredModelManager.getModelManager().getExistingModelForRead(getDocument());
if (model != null) {
// use the base location to obtain the in-workspace IFile
IFile modelFile = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(model.getBaseLocation()));
if (modelFile != null) {
// find the referenced file's location on disk
URIResolver resolver = model.getResolver();
if (resolver != null) {
String filesystemLocation = resolver.getLocationByURI(fileString);
if (filesystemLocation != null) {
IFile[] workspaceFiles = ResourcesPlugin.getWorkspace().getRoot().findFilesForLocation(new Path(filesystemLocation));
// favor a workspace file in the same project
for (int i = 0; i < workspaceFiles.length && file == null; i++) {
if (workspaceFiles[i].getProject().equals(modelFile.getProject())) {
file = workspaceFiles[i];
}
}
// if none were in the same project, just pick one
if (file == null && workspaceFiles.length > 0) {
file = workspaceFiles[0];
}
}
}
}
}
} catch (Exception e) {
Logger.log(Logger.WARNING, e.getMessage());
} finally {
if (model != null) {
model.releaseFromRead();
}
}
if (file == null && fileString.startsWith("/")) {
// $NON-NLS-1$
file = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(fileString));
}
return file;
}
Aggregations