Search in sources :

Example 1 with ITaglibDescriptor

use of org.eclipse.jst.jsp.core.taglib.ITaglibDescriptor in project webtools.sourceediting by eclipse.

the class JSPTaglibCompletionProposalComputer method isTaglibForURI.

private boolean isTaglibForURI(String uri, IPath basePath, ITaglibRecord record) {
    final ITaglibDescriptor descriptor = record.getDescriptor();
    boolean matches = false;
    if (descriptor != null) {
        if (record.getRecordType() == ITaglibRecord.TLD && (descriptor.getURI() == null || "".equals(descriptor.getURI().trim()))) {
            matches = ((ITLDRecord) record).getPath().equals(FacetModuleCoreSupport.resolve(basePath, uri));
        } else {
            matches = descriptor.getURI().toLowerCase(Locale.US).equals(uri.toLowerCase(Locale.US));
        }
    }
    return matches;
}
Also used : ITaglibDescriptor(org.eclipse.jst.jsp.core.taglib.ITaglibDescriptor) ITLDRecord(org.eclipse.jst.jsp.core.taglib.ITLDRecord)

Example 2 with ITaglibDescriptor

use of org.eclipse.jst.jsp.core.taglib.ITaglibDescriptor in project webtools.sourceediting by eclipse.

the class JSPTaglibCompletionProposalComputer method addAttributeValueProposals.

/**
 * @see org.eclipse.wst.xml.ui.internal.contentassist.DefaultXMLCompletionProposalComputer#addAttributeValueProposals(org.eclipse.wst.xml.ui.internal.contentassist.ContentAssistRequest, org.eclipse.wst.sse.ui.contentassist.CompletionProposalInvocationContext)
 */
protected void addAttributeValueProposals(ContentAssistRequest contentAssistRequest, CompletionProposalInvocationContext context) {
    IPath basePath = getBasePath(contentAssistRequest);
    if (basePath != null) {
        IDOMNode node = (IDOMNode) contentAssistRequest.getNode();
        // only add attribute value proposals for specific elements
        if (node.getNodeName().equals(JSP11Namespace.ElementName.DIRECTIVE_TAGLIB)) {
            // Find the attribute name for which this position should have a value
            IStructuredDocumentRegion open = node.getFirstStructuredDocumentRegion();
            ITextRegionList openRegions = open.getRegions();
            int i = openRegions.indexOf(contentAssistRequest.getRegion());
            if (i < 0)
                return;
            ITextRegion nameRegion = null;
            while (i >= 0) {
                nameRegion = openRegions.get(i--);
                if (nameRegion.getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_NAME)
                    break;
            }
            String attributeName = null;
            if (nameRegion != null)
                attributeName = open.getText(nameRegion);
            String currentValue = null;
            if (contentAssistRequest.getRegion().getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_VALUE)
                currentValue = contentAssistRequest.getText();
            else
                // $NON-NLS-1$
                currentValue = "";
            String matchString = null;
            // fixups
            int start = contentAssistRequest.getReplacementBeginPosition();
            int length = contentAssistRequest.getReplacementLength();
            if (currentValue.length() > StringUtils.strip(currentValue).length() && // $NON-NLS-1$ //$NON-NLS-2$
            (currentValue.startsWith("\"") || currentValue.startsWith("'")) && contentAssistRequest.getMatchString().length() > 0) {
                matchString = currentValue.substring(1, contentAssistRequest.getMatchString().length());
            } else {
                matchString = currentValue.substring(0, contentAssistRequest.getMatchString().length());
            }
            boolean existingComplicatedValue = contentAssistRequest.getRegion() != null && contentAssistRequest.getRegion() instanceof ITextRegionContainer;
            if (existingComplicatedValue) {
                contentAssistRequest.getProposals().clear();
                contentAssistRequest.getMacros().clear();
            } else {
                String lowerCaseMatch = matchString.toLowerCase(Locale.US);
                if (attributeName.equals(JSP11Namespace.ATTR_NAME_URI)) {
                    ITaglibRecord[] availableTaglibRecords = TaglibIndex.getAvailableTaglibRecords(basePath);
                    /*
						 * a simple enough way to remove duplicates (resolution at
						 * runtime would be nondeterministic anyway)
						 */
                    Map uriToRecords = new HashMap();
                    for (int taglibRecordNumber = 0; taglibRecordNumber < availableTaglibRecords.length; taglibRecordNumber++) {
                        ITaglibRecord taglibRecord = availableTaglibRecords[taglibRecordNumber];
                        ITaglibDescriptor descriptor = taglibRecord.getDescriptor();
                        String uri = null;
                        switch(taglibRecord.getRecordType()) {
                            case ITaglibRecord.URL:
                                uri = descriptor.getURI();
                                uriToRecords.put(uri, taglibRecord);
                                break;
                            case ITaglibRecord.JAR:
                                {
                                    IPath location = ((IJarRecord) taglibRecord).getLocation();
                                    IFile[] files = ResourcesPlugin.getWorkspace().getRoot().findFilesForLocation(location);
                                    IPath localContextRoot = FacetModuleCoreSupport.computeWebContentRootPath(basePath);
                                    for (int fileNumber = 0; fileNumber < files.length; fileNumber++) {
                                        if (localContextRoot.isPrefixOf(files[fileNumber].getFullPath())) {
                                            uri = IPath.SEPARATOR + files[fileNumber].getFullPath().removeFirstSegments(localContextRoot.segmentCount()).toString();
                                            uriToRecords.put(uri, taglibRecord);
                                        } else {
                                            uri = FacetModuleCoreSupport.getRuntimePath(files[fileNumber].getFullPath()).toString();
                                            uriToRecords.put(uri, taglibRecord);
                                        }
                                    }
                                    break;
                                }
                            case ITaglibRecord.TLD:
                                {
                                    uri = descriptor.getURI();
                                    if (uri == null || uri.trim().length() == 0) {
                                        IPath path = ((ITLDRecord) taglibRecord).getPath();
                                        IPath localContextRoot = FacetModuleCoreSupport.computeWebContentRootPath(basePath);
                                        if (localContextRoot.isPrefixOf(path)) {
                                            uri = IPath.SEPARATOR + path.removeFirstSegments(localContextRoot.segmentCount()).toString();
                                        } else {
                                            uri = FacetModuleCoreSupport.getRuntimePath(path).toString();
                                        }
                                    }
                                    uriToRecords.put(uri, taglibRecord);
                                    break;
                                }
                        }
                    }
                    /*
						 * use the records and their descriptors to construct
						 * proposals
						 */
                    Object[] uris = uriToRecords.keySet().toArray();
                    for (int uriNumber = 0; uriNumber < uris.length; uriNumber++) {
                        String uri = uris[uriNumber].toString();
                        ITaglibRecord taglibRecord = (ITaglibRecord) uriToRecords.get(uri);
                        ITaglibDescriptor descriptor = (taglibRecord).getDescriptor();
                        if (uri != null && uri.length() > 0 && (matchString.length() == 0 || uri.toLowerCase(Locale.US).startsWith(lowerCaseMatch))) {
                            String url = getSmallImageURL(taglibRecord);
                            ImageDescriptor imageDescriptor = JSPUIPlugin.getInstance().getImageRegistry().getDescriptor(url);
                            if (imageDescriptor == null && url != null) {
                                URL imageURL;
                                try {
                                    imageURL = new URL(url);
                                    imageDescriptor = ImageDescriptor.createFromURL(imageURL);
                                    JSPUIPlugin.getInstance().getImageRegistry().put(url, imageDescriptor);
                                } catch (MalformedURLException e) {
                                    Logger.logException(e);
                                }
                            }
                            String additionalInfo = // $NON-NLS-1$
                            descriptor.getDisplayName() + "<br/>" + descriptor.getDescription() + "<br/>" + // $NON-NLS-1$
                            descriptor.getTlibVersion();
                            Image image = null;
                            try {
                                image = JSPUIPlugin.getInstance().getImageRegistry().get(url);
                            } catch (Exception e) {
                                Logger.logException(e);
                            }
                            if (image == null) {
                                image = XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_ATTRIBUTE);
                            }
                            CustomCompletionProposal proposal = new CustomCompletionProposal(// $NON-NLS-1$ //$NON-NLS-2$
                            "\"" + uri + "\"", // $NON-NLS-1$ //$NON-NLS-2$
                            start, // $NON-NLS-1$ //$NON-NLS-2$
                            length, // $NON-NLS-1$ //$NON-NLS-2$
                            uri.length() + 2, image, uri, null, additionalInfo, IRelevanceConstants.R_NONE);
                            contentAssistRequest.addProposal(proposal);
                        }
                    }
                } else if (attributeName.equals(JSP20Namespace.ATTR_NAME_TAGDIR)) {
                    ITaglibRecord[] availableTaglibRecords = TaglibIndex.getAvailableTaglibRecords(basePath);
                    /*
						 * a simple enough way to remove duplicates (resolution at
						 * runtime would be nondeterministic anyway)
						 */
                    Map uriToRecords = new HashMap();
                    IPath localContextRoot = FacetModuleCoreSupport.computeWebContentRootPath(basePath);
                    for (int taglibRecordNumber = 0; taglibRecordNumber < availableTaglibRecords.length; taglibRecordNumber++) {
                        ITaglibRecord taglibRecord = availableTaglibRecords[taglibRecordNumber];
                        String uri = null;
                        if (taglibRecord.getRecordType() == ITaglibRecord.TAGDIR) {
                            IPath path = ((ITagDirRecord) taglibRecord).getPath();
                            if (localContextRoot.isPrefixOf(path)) {
                                uri = IPath.SEPARATOR + path.removeFirstSegments(localContextRoot.segmentCount()).toString();
                                uriToRecords.put(uri, taglibRecord);
                            }
                        }
                    }
                    /*
						 * use the records and their descriptors to construct
						 * proposals
						 */
                    Object[] uris = uriToRecords.keySet().toArray();
                    for (int uriNumber = 0; uriNumber < uris.length; uriNumber++) {
                        String uri = uris[uriNumber].toString();
                        ITaglibRecord taglibRecord = (ITaglibRecord) uriToRecords.get(uri);
                        ITaglibDescriptor descriptor = (taglibRecord).getDescriptor();
                        if (uri != null && uri.length() > 0 && (matchString.length() == 0 || uri.toLowerCase(Locale.US).startsWith(lowerCaseMatch))) {
                            String url = getSmallImageURL(taglibRecord);
                            ImageDescriptor imageDescriptor = null;
                            if (url != null) {
                                imageDescriptor = JSPUIPlugin.getInstance().getImageRegistry().getDescriptor(url);
                            }
                            if (imageDescriptor == null && url != null) {
                                URL imageURL;
                                try {
                                    imageURL = new URL(url);
                                    imageDescriptor = ImageDescriptor.createFromURL(imageURL);
                                    JSPUIPlugin.getInstance().getImageRegistry().put(url, imageDescriptor);
                                } catch (MalformedURLException e) {
                                    Logger.logException(e);
                                }
                            }
                            // $NON-NLS-1$
                            String additionalInfo = descriptor.getDescription() + "<br/>" + descriptor.getTlibVersion();
                            Image image = null;
                            try {
                                image = JSPUIPlugin.getInstance().getImageRegistry().get(url);
                            } catch (Exception e) {
                                Logger.logException(e);
                            }
                            if (image == null) {
                                image = XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_ATTRIBUTE);
                            }
                            CustomCompletionProposal proposal = new CustomCompletionProposal(// $NON-NLS-1$ //$NON-NLS-2$
                            "\"" + uri + "\"", // $NON-NLS-1$ //$NON-NLS-2$
                            start, // $NON-NLS-1$ //$NON-NLS-2$
                            length, // $NON-NLS-1$ //$NON-NLS-2$
                            uri.length() + 2, // $NON-NLS-1$ //$NON-NLS-2$
                            image, // $NON-NLS-1$ //$NON-NLS-2$
                            uri, null, additionalInfo, IRelevanceConstants.R_NONE);
                            contentAssistRequest.addProposal(proposal);
                        }
                    }
                } else if (attributeName.equals(JSP11Namespace.ATTR_NAME_PREFIX)) {
                    Node uriAttr = node.getAttributes().getNamedItem(JSP11Namespace.ATTR_NAME_URI);
                    String uri = null;
                    if (uriAttr != null) {
                        uri = uriAttr.getNodeValue();
                        ITaglibRecord[] availableTaglibRecords = TaglibIndex.getAvailableTaglibRecords(basePath);
                        Map prefixMap = new HashMap();
                        for (int taglibrecordNumber = 0; taglibrecordNumber < availableTaglibRecords.length; taglibrecordNumber++) {
                            ITaglibDescriptor descriptor = availableTaglibRecords[taglibrecordNumber].getDescriptor();
                            if (isTaglibForURI(uri, basePath, availableTaglibRecords[taglibrecordNumber])) {
                                String shortName = descriptor.getShortName().trim();
                                if (shortName.length() > 0) {
                                    boolean valid = true;
                                    for (int character = 0; character < shortName.length(); character++) {
                                        valid = valid && !Character.isWhitespace(shortName.charAt(character));
                                    }
                                    if (valid) {
                                        prefixMap.put(shortName, descriptor);
                                    }
                                }
                            }
                        }
                        Object[] prefixes = prefixMap.keySet().toArray();
                        for (int j = 0; j < prefixes.length; j++) {
                            String prefix = (String) prefixes[j];
                            ITaglibDescriptor descriptor = (ITaglibDescriptor) prefixMap.get(prefix);
                            Image image = XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_ATTRIBUTE);
                            CustomCompletionProposal proposal = new CustomCompletionProposal(// $NON-NLS-1$ //$NON-NLS-2$
                            "\"" + prefix + "\"", // $NON-NLS-1$ //$NON-NLS-2$
                            start, // $NON-NLS-1$ //$NON-NLS-2$
                            length, // $NON-NLS-1$ //$NON-NLS-2$
                            prefix.length() + 2, // $NON-NLS-1$ //$NON-NLS-2$
                            image, prefix, null, descriptor.getDescription(), IRelevanceConstants.R_NONE);
                            contentAssistRequest.addProposal(proposal);
                        }
                    } else {
                        Node dirAttr = node.getAttributes().getNamedItem(JSP20Namespace.ATTR_NAME_TAGDIR);
                        if (dirAttr != null) {
                            String dir = dirAttr.getNodeValue();
                            if (dir != null) {
                                ITaglibRecord record = TaglibIndex.resolve(basePath.toString(), dir, false);
                                if (record != null) {
                                    ITaglibDescriptor descriptor = record.getDescriptor();
                                    if (descriptor != null) {
                                        String shortName = descriptor.getShortName();
                                        Image image = XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_ATTRIBUTE);
                                        CustomCompletionProposal proposal = new CustomCompletionProposal(// $NON-NLS-1$ //$NON-NLS-2$
                                        "\"" + shortName + "\"", // $NON-NLS-1$ //$NON-NLS-2$
                                        start, // $NON-NLS-1$ //$NON-NLS-2$
                                        length, // $NON-NLS-1$ //$NON-NLS-2$
                                        shortName.length() + 2, image, shortName, null, descriptor.getDescription(), IRelevanceConstants.R_NONE);
                                        contentAssistRequest.addProposal(proposal);
                                    } else {
                                        if (dir.startsWith("/WEB-INF/")) {
                                            // $NON-NLS-1$
                                            dir = dir.substring(9);
                                        }
                                        // $NON-NLS-1$ //$NON-NLS-2$
                                        String prefix = StringUtils.replace(dir, "/", "-");
                                        Image image = XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_ATTRIBUTE);
                                        CustomCompletionProposal proposal = new CustomCompletionProposal(// $NON-NLS-1$ //$NON-NLS-2$
                                        "\"" + prefix + "\"", // $NON-NLS-1$ //$NON-NLS-2$
                                        start, // $NON-NLS-1$ //$NON-NLS-2$
                                        length, // $NON-NLS-1$ //$NON-NLS-2$
                                        prefix.length() + 2, image, prefix, null, null, IRelevanceConstants.R_NONE);
                                        contentAssistRequest.addProposal(proposal);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) MalformedURLException(java.net.MalformedURLException) IPath(org.eclipse.core.runtime.IPath) HashMap(java.util.HashMap) ITaglibRecord(org.eclipse.jst.jsp.core.taglib.ITaglibRecord) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) Node(org.w3c.dom.Node) CustomCompletionProposal(org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal) ITextRegionContainer(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionContainer) Image(org.eclipse.swt.graphics.Image) URL(java.net.URL) MalformedURLException(java.net.MalformedURLException) ITaglibDescriptor(org.eclipse.jst.jsp.core.taglib.ITaglibDescriptor) ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) ImageDescriptor(org.eclipse.jface.resource.ImageDescriptor) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

ITaglibDescriptor (org.eclipse.jst.jsp.core.taglib.ITaglibDescriptor)2 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 IPath (org.eclipse.core.runtime.IPath)1 ImageDescriptor (org.eclipse.jface.resource.ImageDescriptor)1 ITLDRecord (org.eclipse.jst.jsp.core.taglib.ITLDRecord)1 ITaglibRecord (org.eclipse.jst.jsp.core.taglib.ITaglibRecord)1 Image (org.eclipse.swt.graphics.Image)1 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)1 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)1 ITextRegionContainer (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionContainer)1 ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)1 CustomCompletionProposal (org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal)1 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)1 Node (org.w3c.dom.Node)1