Search in sources :

Example 91 with IStructuredModel

use of org.eclipse.wst.sse.core.internal.provisional.IStructuredModel in project webtools.sourceediting by eclipse.

the class TaglibHelper method getModelQuery.

/**
 * @return Returns the fModelQuery.
 */
public ModelQuery getModelQuery(IDocument doc) {
    IStructuredModel model = null;
    ModelQuery mq = null;
    try {
        model = StructuredModelManager.getModelManager().getExistingModelForRead(doc);
        mq = ModelQueryUtil.getModelQuery(model);
    } finally {
        if (model != null)
            model.releaseFromRead();
    }
    return mq;
}
Also used : ModelQuery(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)

Example 92 with IStructuredModel

use of org.eclipse.wst.sse.core.internal.provisional.IStructuredModel in project webtools.sourceediting by eclipse.

the class TaglibHyperlinkDetector method detectHyperlinks.

public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) {
    IHyperlink hyperlink = null;
    if (textViewer != null && region != null) {
        IDocument doc = textViewer.getDocument();
        if (doc != null) {
            try {
                // check if jsp tag/directive first
                ITypedRegion partition = TextUtilities.getPartition(doc, IStructuredPartitioning.DEFAULT_STRUCTURED_PARTITIONING, region.getOffset(), false);
                if (partition != null && partition.getType() == IJSPPartitions.JSP_DIRECTIVE) {
                    IStructuredModel sModel = null;
                    try {
                        sModel = StructuredModelManager.getModelManager().getExistingModelForRead(doc);
                        // check if jsp taglib directive
                        Node currentNode = getCurrentNode(sModel, region.getOffset());
                        if (currentNode != null && currentNode.getNodeType() == Node.ELEMENT_NODE) {
                            String baseLocationForTaglib = getBaseLocationForTaglib(doc);
                            if (baseLocationForTaglib != null && JSP11Namespace.ElementName.DIRECTIVE_TAGLIB.equalsIgnoreCase(currentNode.getNodeName())) {
                                /**
                                 * The taglib directive itself
                                 */
                                // get the uri attribute
                                Attr taglibURINode = ((Element) currentNode).getAttributeNode(JSP11Namespace.ATTR_NAME_URI);
                                if (taglibURINode != null) {
                                    ITaglibRecord reference = TaglibIndex.resolve(baseLocationForTaglib, taglibURINode.getValue(), false);
                                    // there's nothing to link to
                                    if (reference != null) {
                                        // handle taglibs
                                        switch(reference.getRecordType()) {
                                            case (ITaglibRecord.TLD):
                                                {
                                                    ITLDRecord record = (ITLDRecord) reference;
                                                    String uriString = record.getPath().toString();
                                                    IRegion hyperlinkRegion = getHyperlinkRegion(taglibURINode, region);
                                                    if (hyperlinkRegion != null) {
                                                        hyperlink = createHyperlink(uriString, hyperlinkRegion, doc, null);
                                                    }
                                                }
                                                break;
                                            case (ITaglibRecord.JAR):
                                            case (ITaglibRecord.URL):
                                                {
                                                    IRegion hyperlinkRegion = getHyperlinkRegion(taglibURINode, region);
                                                    if (hyperlinkRegion != null) {
                                                        hyperlink = new TaglibJarUriHyperlink(hyperlinkRegion, reference);
                                                    }
                                                }
                                        }
                                    }
                                }
                            } else if (baseLocationForTaglib != null && JSP12Namespace.ElementName.ROOT.equalsIgnoreCase(currentNode.getNodeName())) {
                                /**
                                 * The jsp:root element
                                 */
                                NamedNodeMap attrs = currentNode.getAttributes();
                                for (int i = 0; i < attrs.getLength(); i++) {
                                    Attr attr = (Attr) attrs.item(i);
                                    if (attr.getNodeName().startsWith(XMLNS)) {
                                        String uri = StringUtils.strip(attr.getNodeValue());
                                        if (uri.startsWith(URN_TLD)) {
                                            uri = uri.substring(URN_TLD.length());
                                        }
                                        ITaglibRecord reference = TaglibIndex.resolve(baseLocationForTaglib, uri, false);
                                        // there's nothing to link to
                                        if (reference != null) {
                                            // handle taglibs
                                            switch(reference.getRecordType()) {
                                                case (ITaglibRecord.TLD):
                                                    {
                                                        ITLDRecord record = (ITLDRecord) reference;
                                                        String uriString = record.getPath().toString();
                                                        IRegion hyperlinkRegion = getHyperlinkRegion(attr, region);
                                                        if (hyperlinkRegion != null) {
                                                            hyperlink = createHyperlink(uriString, hyperlinkRegion, doc, null);
                                                        }
                                                    }
                                                    break;
                                                case (ITaglibRecord.JAR):
                                                case (ITaglibRecord.URL):
                                                    {
                                                        IRegion hyperlinkRegion = getHyperlinkRegion(attr, region);
                                                        if (hyperlinkRegion != null) {
                                                            hyperlink = new TaglibJarUriHyperlink(hyperlinkRegion, reference);
                                                        }
                                                    }
                                            }
                                        }
                                    }
                                }
                            } else {
                                /**
                                 * Hyperlink custom tag to its TLD or tag file
                                 */
                                TLDCMDocumentManager documentManager = TaglibController.getTLDCMDocumentManager(doc);
                                if (documentManager != null) {
                                    List documentTrackers = documentManager.getCMDocumentTrackers(currentNode.getPrefix(), region.getOffset());
                                    for (int i = 0; i < documentTrackers.size(); i++) {
                                        TaglibTracker tracker = (TaglibTracker) documentTrackers.get(i);
                                        CMElementDeclaration decl = (CMElementDeclaration) tracker.getElements().getNamedItem(currentNode.getNodeName());
                                        if (decl != null) {
                                            decl = (CMElementDeclaration) ((CMNodeWrapper) decl).getOriginNode();
                                            if (decl instanceof CMElementDeclarationImpl) {
                                                String base = ((CMElementDeclarationImpl) decl).getLocationString();
                                                IRegion hyperlinkRegion = getHyperlinkRegion(currentNode, region);
                                                if (hyperlinkRegion != null) {
                                                    hyperlink = createHyperlink(base, hyperlinkRegion, doc, currentNode);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    } finally {
                        if (sModel != null)
                            sModel.releaseFromRead();
                    }
                }
            } catch (BadLocationException e) {
                Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e);
            }
        }
    }
    if (hyperlink != null)
        return new IHyperlink[] { hyperlink };
    return null;
}
Also used : TLDCMDocumentManager(org.eclipse.jst.jsp.core.internal.contentmodel.tld.TLDCMDocumentManager) NamedNodeMap(org.w3c.dom.NamedNodeMap) ITaglibRecord(org.eclipse.jst.jsp.core.taglib.ITaglibRecord) ITLDRecord(org.eclipse.jst.jsp.core.taglib.ITLDRecord) TaglibTracker(org.eclipse.jst.jsp.core.internal.contentmodel.tld.TaglibTracker) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) Attr(org.w3c.dom.Attr) IDOMAttr(org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr) IRegion(org.eclipse.jface.text.IRegion) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) IHyperlink(org.eclipse.jface.text.hyperlink.IHyperlink) ITypedRegion(org.eclipse.jface.text.ITypedRegion) CMElementDeclarationImpl(org.eclipse.jst.jsp.core.internal.contentmodel.tld.CMElementDeclarationImpl) List(java.util.List) ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) NodeList(org.w3c.dom.NodeList) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 93 with IStructuredModel

use of org.eclipse.wst.sse.core.internal.provisional.IStructuredModel in project webtools.sourceediting by eclipse.

the class StructuredAutoEditStrategyJSPJava method customizeDocumentCommand.

public void customizeDocumentCommand(IDocument document, DocumentCommand command) {
    if (!supportsSmartInsert(document)) {
        return;
    }
    IStructuredModel model = null;
    try {
        // Auto edit for JSP Comments
        if ("-".equals(command.text) && isPreferenceEnabled(JSPUIPreferenceNames.TYPING_COMPLETE_COMMENTS)) {
            // $NON-NLS-1$
            if (prefixedWith(document, command.offset, "<%-")) {
                // $NON-NLS-1$
                model = StructuredModelManager.getModelManager().getExistingModelForRead(document);
                if (model != null) {
                    IDOMNode node = (IDOMNode) model.getIndexedRegion(command.offset);
                    IDOMNode parent = (node != null) ? (IDOMNode) node.getParentNode() : null;
                    // Parent is the scriptlet tag
                    if (parent != null && JSP11Namespace.ElementName.SCRIPTLET.equals(parent.getNodeName()) && !parent.getSource().endsWith("--%>")) {
                        // $NON-NLS-1$
                        IStructuredDocumentRegion end = parent.getEndStructuredDocumentRegion();
                        if (end != null) {
                            try {
                                // $NON-NLS-1$
                                document.replace(end.getStartOffset(), 0, "--");
                            } catch (BadLocationException e) {
                                Logger.logException(e);
                            }
                        }
                    }
                }
            }
        }
    } finally {
        if (model != null)
            model.releaseFromRead();
    }
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 94 with IStructuredModel

use of org.eclipse.wst.sse.core.internal.provisional.IStructuredModel in project webtools.sourceediting by eclipse.

the class JSPSearchDocument method getJSPTranslation.

/**
 * It's not recommended for clients to hold on to this JSPTranslation
 * since it's kind of large. If possible, hold on to the
 * JSPSearchDocument, which is more of a lightweight proxy.
 *
 * @return the JSPTranslation for the jsp file, or null if it's an
 *         unsupported file.
 */
public final JSPTranslationExtension getJSPTranslation() {
    JSPTranslationExtension translation = null;
    IFile jspFile = getFile();
    if (!JSPSearchSupport.isJsp(jspFile))
        return translation;
    IStructuredModel model = null;
    try {
        // get existing model for read, then get document from it
        IModelManager modelManager = getModelManager();
        if (modelManager != null) {
            jspFile.refreshLocal(IResource.DEPTH_ZERO, new NullProgressMonitor());
            model = modelManager.getModelForRead(jspFile);
        }
        // handle unsupported
        if (model instanceof IDOMModel) {
            IDOMModel xmlModel = (IDOMModel) model;
            setupAdapterFactory(xmlModel);
            IDOMDocument doc = xmlModel.getDocument();
            JSPTranslationAdapter adapter = (JSPTranslationAdapter) doc.getAdapterFor(IJSPTranslation.class);
            translation = adapter.getJSPTranslation();
        }
    } catch (IOException e) {
        Logger.logException(e);
    } catch (CoreException e) {
        Logger.logException(e);
    } catch (UnsupportedCharsetExceptionWithDetail e) {
    // no need to log this. Just consider it an invalid file for our
    // purposes.
    // Logger.logException(e);
    } finally {
        if (model != null)
            model.releaseFromRead();
    }
    return translation;
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IFile(org.eclipse.core.resources.IFile) CoreException(org.eclipse.core.runtime.CoreException) JSPTranslationExtension(org.eclipse.jst.jsp.core.internal.java.JSPTranslationExtension) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IModelManager(org.eclipse.wst.sse.core.internal.provisional.IModelManager) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) UnsupportedCharsetExceptionWithDetail(org.eclipse.wst.sse.core.internal.exceptions.UnsupportedCharsetExceptionWithDetail) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IOException(java.io.IOException) IJSPTranslation(org.eclipse.jst.jsp.core.internal.java.IJSPTranslation) JSPTranslationAdapter(org.eclipse.jst.jsp.core.internal.java.JSPTranslationAdapter)

Example 95 with IStructuredModel

use of org.eclipse.wst.sse.core.internal.provisional.IStructuredModel in project webtools.sourceediting by eclipse.

the class JSDTHyperlinkDetector method createHyperlink.

private IHyperlink createHyperlink(IJsTranslation jsTranslation, IJavaScriptElement element, IRegion region, IDocument document) {
    IHyperlink link = null;
    if (region != null) {
        // open local variable in the JSP file...
        if (element instanceof ISourceReference) {
            IFile file = null;
            IPath outsidePath = null;
            int jspOffset = 0;
            IStructuredModel sModel = null;
            // try to locate the file in the workspace
            try {
                sModel = StructuredModelManager.getModelManager().getExistingModelForRead(document);
                if (sModel != null) {
                    // URIResolver resolver = sModel.getResolver();
                    // if (resolver != null) {
                    // String uriString = resolver.getFileBaseLocation();
                    String uriString = sModel.getBaseLocation();
                    file = getFile(uriString);
                // }
                }
            } finally {
                if (sModel != null) {
                    sModel.releaseFromRead();
                }
            }
            // get Java range, translate coordinate to JSP
            try {
                ISourceRange range = null;
                if (jsTranslation != null) {
                    // link to local variable definitions
                    if (element instanceof ILocalVariable) {
                        range = ((ILocalVariable) element).getNameRange();
                        IJavaScriptElement unit = ((ILocalVariable) element).getParent();
                        IJavaScriptUnit myUnit = jsTranslation.getCompilationUnit();
                        while (!(unit instanceof IJavaScriptUnit || unit instanceof IClassFile || unit == null)) {
                            unit = ((JavaElement) unit).getParent();
                        }
                        if (unit instanceof IJavaScriptUnit) {
                            IJavaScriptUnit cu = (IJavaScriptUnit) unit;
                            if (cu != myUnit) {
                                file = getFile(cu.getPath().toString());
                                if (file == null) {
                                    outsidePath = cu.getPath();
                                }
                            }
                        } else if (unit instanceof IClassFile) {
                            IClassFile cu = (IClassFile) unit;
                            if (cu != myUnit) {
                                file = getFile(cu.getPath().toString());
                                if (file == null) {
                                    outsidePath = cu.getPath();
                                }
                            }
                        }
                    } else // linking to fields of the same compilation unit
                    if (element.getElementType() == IJavaScriptElement.FIELD) {
                        Object cu = ((IField) element).getJavaScriptUnit();
                        if (cu != null && cu.equals(jsTranslation.getCompilationUnit())) {
                            range = ((ISourceReference) element).getSourceRange();
                        }
                    } else // linking to methods of the same compilation unit
                    if (element.getElementType() == IJavaScriptElement.METHOD) {
                        Object cu = ((IFunction) element).getJavaScriptUnit();
                        if (cu != null && cu.equals(jsTranslation.getCompilationUnit())) {
                            range = ((ISourceReference) element).getSourceRange();
                        }
                    }
                }
                if (range != null && file != null) {
                    jspOffset = range.getOffset();
                    if (jspOffset >= 0) {
                        link = new WorkspaceFileHyperlink(region, file, new Region(jspOffset, range.getLength()));
                    }
                } else if (range != null && outsidePath != null) {
                    jspOffset = range.getOffset();
                    if (jspOffset >= 0) {
                        link = new ExternalFileHyperlink(region, outsidePath.toFile());
                    }
                }
            } catch (JavaScriptModelException jme) {
                Logger.log(Logger.WARNING_DEBUG, jme.getMessage(), jme);
            }
        }
        if (link == null) {
            link = new JSDTHyperlink(region, element);
        }
    }
    return link;
}
Also used : IFile(org.eclipse.core.resources.IFile) IClassFile(org.eclipse.wst.jsdt.core.IClassFile) IPath(org.eclipse.core.runtime.IPath) JavaScriptModelException(org.eclipse.wst.jsdt.core.JavaScriptModelException) IFunction(org.eclipse.wst.jsdt.core.IFunction) ILocalVariable(org.eclipse.wst.jsdt.core.ILocalVariable) IHyperlink(org.eclipse.jface.text.hyperlink.IHyperlink) IJavaScriptElement(org.eclipse.wst.jsdt.core.IJavaScriptElement) Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) ITypedRegion(org.eclipse.jface.text.ITypedRegion) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IJavaScriptUnit(org.eclipse.wst.jsdt.core.IJavaScriptUnit) ISourceReference(org.eclipse.wst.jsdt.core.ISourceReference) ISourceRange(org.eclipse.wst.jsdt.core.ISourceRange)

Aggregations

IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)482 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)110 IModelManager (org.eclipse.wst.sse.core.internal.provisional.IModelManager)102 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)81 IFile (org.eclipse.core.resources.IFile)75 IOException (java.io.IOException)69 CoreException (org.eclipse.core.runtime.CoreException)49 IDocument (org.eclipse.jface.text.IDocument)46 IDOMDocument (org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)42 InputStream (java.io.InputStream)40 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)36 Path (org.eclipse.core.runtime.Path)35 BadLocationException (org.eclipse.jface.text.BadLocationException)34 IJsTranslation (org.eclipse.wst.jsdt.web.core.javascript.IJsTranslation)32 JsTranslationAdapter (org.eclipse.wst.jsdt.web.core.javascript.JsTranslationAdapter)32 IProject (org.eclipse.core.resources.IProject)31 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)31 IPath (org.eclipse.core.runtime.IPath)27 ByteArrayInputStream (java.io.ByteArrayInputStream)22 Document (org.w3c.dom.Document)19