Search in sources :

Example 1 with URIResolver

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);
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) URIResolver(org.eclipse.wst.sse.core.internal.util.URIResolver) IProject(org.eclipse.core.resources.IProject) WorkbenchReporter(org.eclipse.wst.validation.internal.operations.WorkbenchReporter)

Example 2 with URIResolver

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);
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) IPath(org.eclipse.core.runtime.IPath) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) URIResolver(org.eclipse.wst.sse.core.internal.util.URIResolver) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) IContainer(org.eclipse.core.resources.IContainer)

Example 3 with URIResolver

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;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ProjectResolver(org.eclipse.wst.sse.core.internal.util.ProjectResolver) FileNotFoundException(java.io.FileNotFoundException) URIResolver(org.eclipse.wst.sse.core.internal.util.URIResolver) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) IProject(org.eclipse.core.resources.IProject) FileInputStream(java.io.FileInputStream) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IContainer(org.eclipse.core.resources.IContainer)

Example 4 with URIResolver

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;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IPath(org.eclipse.core.runtime.IPath) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) ICSSModel(org.eclipse.wst.css.core.internal.provisional.document.ICSSModel) URIResolver(org.eclipse.wst.sse.core.internal.util.URIResolver) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)

Example 5 with URIResolver

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;
}
Also used : Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) URIResolver(org.eclipse.wst.sse.core.internal.util.URIResolver) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) PartInitException(org.eclipse.ui.PartInitException)

Aggregations

URIResolver (org.eclipse.wst.sse.core.internal.util.URIResolver)17 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)12 IProject (org.eclipse.core.resources.IProject)10 FileNotFoundException (java.io.FileNotFoundException)7 IOException (java.io.IOException)7 InputStream (java.io.InputStream)7 IPath (org.eclipse.core.runtime.IPath)5 IModelManager (org.eclipse.wst.sse.core.internal.provisional.IModelManager)5 IContainer (org.eclipse.core.resources.IContainer)4 IFile (org.eclipse.core.resources.IFile)4 Path (org.eclipse.core.runtime.Path)4 IModelHandler (org.eclipse.wst.sse.core.internal.ltk.modelhandler.IModelHandler)3 ProjectResolver (org.eclipse.wst.sse.core.internal.util.ProjectResolver)3 FileInputStream (java.io.FileInputStream)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 IWorkspaceRoot (org.eclipse.core.resources.IWorkspaceRoot)2 ResourceInUse (org.eclipse.wst.sse.core.internal.provisional.exceptions.ResourceInUse)2 File (java.io.File)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1