Search in sources :

Example 31 with IDOMNode

use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode in project webtools.sourceediting by eclipse.

the class TLDValidator method checkClass.

private Map checkClass(IJavaProject javaProject, Node classSpecifier, IScopeContext[] preferenceScopes, String preferenceKey, String errorMessage) {
    String className = getTextContents(classSpecifier);
    if (className != null && className.length() > 2) {
        IType type = null;
        try {
            type = javaProject.findType(className);
        } catch (JavaModelException e) {
            return null;
        }
        if (type == null || !type.exists()) {
            Object severity = getMessageSeverity(preferenceScopes, preferenceKey);
            if (severity == null)
                return null;
            IDOMNode classElement = (IDOMNode) classSpecifier;
            Map markerValues = new HashMap();
            markerValues.put(IMarker.SEVERITY, severity);
            int start = classElement.getStartOffset();
            if (classElement.getStartStructuredDocumentRegion() != null && classElement.getEndStructuredDocumentRegion() != null)
                start = classElement.getStartStructuredDocumentRegion().getEndOffset();
            markerValues.put(IMarker.CHAR_START, new Integer(start));
            int end = classElement.getEndOffset();
            if (classElement.getStartStructuredDocumentRegion() != null && classElement.getEndStructuredDocumentRegion() != null)
                end = classElement.getEndStructuredDocumentRegion().getStartOffset();
            markerValues.put(IMarker.CHAR_END, new Integer(end));
            int line = classElement.getStructuredDocument().getLineOfOffset(start);
            markerValues.put(IMarker.LINE_NUMBER, new Integer(line + 1));
            // $NON-NLS-1$
            markerValues.put(IMarker.MESSAGE, NLS.bind(errorMessage, (errorMessage.indexOf("{1}") >= 0) ? new String[] { getTagName(classSpecifier), className } : new String[] { className }));
            return markerValues;
        }
    }
    return null;
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) IType(org.eclipse.jdt.core.IType)

Example 32 with IDOMNode

use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode in project webtools.sourceediting by eclipse.

the class JSPModelQueryExtension method getAvailableElementContent.

/**
 * Originally taken from JSPContentAssistProcessor
 *
 * @see org.eclipse.wst.xml.core.internal.contentmodel.modelquery.extension.ModelQueryExtension#getAvailableElementContent(org.w3c.dom.Element, java.lang.String, int)
 */
public CMNode[] getAvailableElementContent(Element parentElement, String namespace, int includeOptions) {
    CMNode[] nodes = EMPTY_CMNODE_ARRAY;
    ArrayList nodeList = new ArrayList();
    // only returns anything if looking for child nodes
    if (((includeOptions & ModelQuery.INCLUDE_CHILD_NODES) != 0) && parentElement instanceof IDOMNode) {
        IDOMNode node = (IDOMNode) parentElement;
        // get position dependent CMDocuments and insert their tags as
        // proposals
        ModelQueryAdapter mqAdapter = null;
        if (node.getNodeType() == Node.DOCUMENT_NODE) {
            mqAdapter = (ModelQueryAdapter) node.getAdapterFor(ModelQueryAdapter.class);
        } else {
            mqAdapter = (ModelQueryAdapter) ((IDOMNode) node.getOwnerDocument()).getAdapterFor(ModelQueryAdapter.class);
        }
        if (mqAdapter != null) {
            CMDocument doc = mqAdapter.getModelQuery().getCorrespondingCMDocument(node);
            if (doc != null) {
                CMDocument jcmdoc = getDefaultJSPCMDocument(node);
                CMNamedNodeMap jspelements = jcmdoc.getElements();
                /* For a built-in JSP action the content model is properly
					 * set up, so don't just blindly add the rest--unless this
					 * will be a direct child of the document
					 */
                if (jspelements != null && (!(doc instanceof JSPCMDocument) || node.getNodeType() == Node.DOCUMENT_NODE)) {
                    List rejectElements = new ArrayList();
                    // determine if the document is in XML form
                    Document domDoc = null;
                    if (node.getNodeType() == Node.DOCUMENT_NODE) {
                        domDoc = (Document) node;
                    } else {
                        domDoc = node.getOwnerDocument();
                    }
                    // Show XML tag forms of JSP markers if jsp:root is
                    // the document element OR it's HTML but
                    // isn't really in the text.
                    // If the document isn't strictly XML, pull out the
                    // XML tag forms it is xml format
                    rejectElements.add(JSP12Namespace.ElementName.SCRIPTLET);
                    rejectElements.add(JSP12Namespace.ElementName.EXPRESSION);
                    rejectElements.add(JSP12Namespace.ElementName.DECLARATION);
                    rejectElements.add(JSP12Namespace.ElementName.DIRECTIVE_INCLUDE);
                    rejectElements.add(JSP12Namespace.ElementName.DIRECTIVE_PAGE);
                    rejectElements.add(JSP12Namespace.ElementName.TEXT);
                    rejectElements.add(JSP12Namespace.ElementName.DIRECTIVE_TAGLIB);
                    rejectElements.add(JSP20Namespace.ElementName.DIRECTIVE_TAG);
                    rejectElements.add(JSP20Namespace.ElementName.DIRECTIVE_ATTRIBUTE);
                    rejectElements.add(JSP20Namespace.ElementName.DIRECTIVE_VARIABLE);
                    if (isXMLFormat(domDoc)) {
                        // jsp actions
                        rejectElements.add(JSP12Namespace.ElementName.FALLBACK);
                        rejectElements.add(JSP12Namespace.ElementName.USEBEAN);
                        rejectElements.add(JSP12Namespace.ElementName.GETPROPERTY);
                        rejectElements.add(JSP12Namespace.ElementName.SETPROPERTY);
                        rejectElements.add(JSP12Namespace.ElementName.INCLUDE);
                        rejectElements.add(JSP12Namespace.ElementName.FORWARD);
                        rejectElements.add(JSP12Namespace.ElementName.PLUGIN);
                        rejectElements.add(JSP12Namespace.ElementName.FALLBACK);
                        rejectElements.add(JSP12Namespace.ElementName.PARAM);
                        rejectElements.add(JSP12Namespace.ElementName.PARAMS);
                    }
                    // don't show jsp:root if a document element already
                    // exists
                    Element docElement = domDoc.getDocumentElement();
                    if (docElement != null && ((docElement.getNodeName().equals(TAG_JSP_ROOT)) || ((((IDOMNode) docElement).getStartStructuredDocumentRegion() != null || ((IDOMNode) docElement).getEndStructuredDocumentRegion() != null)))) {
                        rejectElements.add(JSP12Namespace.ElementName.ROOT);
                    }
                    for (int j = 0; j < jspelements.getLength(); j++) {
                        CMElementDeclaration ed = (CMElementDeclaration) jspelements.item(j);
                        if (!rejectElements.contains(ed.getNodeName())) {
                            nodeList.add(ed);
                        }
                    }
                }
            } else // No cm document (such as for the Document (a non-Element) node itself)
            {
                CMNamedNodeMap jspElements = getDefaultJSPCMDocument(node).getElements();
                int length = jspElements.getLength();
                for (int i = 0; i < length; i++) {
                    nodeList.add(jspElements.item(i));
                }
            }
        }
        nodes = (CMNode[]) nodeList.toArray(new CMNode[nodeList.size()]);
    }
    return nodes;
}
Also used : CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) JSPCMDocument(org.eclipse.wst.html.core.internal.contentmodel.JSPCMDocument) ModelQueryAdapter(org.eclipse.wst.xml.core.internal.ssemodelquery.ModelQueryAdapter) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) JSPCMDocument(org.eclipse.wst.html.core.internal.contentmodel.JSPCMDocument) Document(org.w3c.dom.Document) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) JSPCMDocument(org.eclipse.wst.html.core.internal.contentmodel.JSPCMDocument) CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode) ArrayList(java.util.ArrayList) List(java.util.List) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)

Example 33 with IDOMNode

use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode in project webtools.sourceediting by eclipse.

the class TagModelQuery method getEmbeddedMimeType.

private String getEmbeddedMimeType(Node node) {
    String type = DEFAULT_MIMETYPE;
    if (node instanceof IDOMNode) {
        IStructuredModel model = ((IDOMNode) node).getModel();
        String baseLocation = model.getBaseLocation();
        if (!baseLocation.equals(IModelManager.UNMANAGED_MODEL)) {
            IPath path = new Path(baseLocation);
            if (path.segmentCount() > 1) {
                IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(path.segment(0));
                if (project.isAccessible()) {
                    String key = createPreferenceKey(path);
                    IEclipsePreferences preferences = new ProjectScope(project).getNode(JSPCorePlugin.getDefault().getBundle().getSymbolicName());
                    type = preferences.get(key, DEFAULT_MIMETYPE);
                }
            }
        }
    }
    return type;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) ProjectScope(org.eclipse.core.resources.ProjectScope) IPath(org.eclipse.core.runtime.IPath) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) IEclipsePreferences(org.eclipse.core.runtime.preferences.IEclipsePreferences) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IProject(org.eclipse.core.resources.IProject)

Example 34 with IDOMNode

use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode in project webtools.sourceediting by eclipse.

the class TaglibHyperlinkDetector method getHyperlinkRegion.

// the below methods were copied from URIHyperlinkDetector
private IRegion getHyperlinkRegion(Node node, IRegion boundingRegion) {
    IRegion hyperRegion = null;
    if (node != null) {
        short nodeType = node.getNodeType();
        if (nodeType == Node.DOCUMENT_TYPE_NODE) {
            // handle doc type node
            IDOMNode docNode = (IDOMNode) node;
            hyperRegion = new Region(docNode.getStartOffset(), docNode.getEndOffset() - docNode.getStartOffset());
        } else if (nodeType == Node.ATTRIBUTE_NODE) {
            // handle attribute nodes
            IDOMAttr att = (IDOMAttr) node;
            // do not include quotes in attribute value region
            int regOffset = att.getValueRegionStartOffset();
            ITextRegion valueRegion = att.getValueRegion();
            if (valueRegion != null) {
                int regLength = valueRegion.getTextLength();
                String attValue = att.getValueRegionText();
                if (StringUtils.isQuoted(attValue)) {
                    ++regOffset;
                    regLength = regLength - 2;
                }
                hyperRegion = new Region(regOffset, regLength);
            }
        }
        if (nodeType == Node.ELEMENT_NODE) {
            // Handle doc type node
            IDOMNode docNode = (IDOMNode) node;
            hyperRegion = getNameRegion(docNode.getFirstStructuredDocumentRegion());
            if (hyperRegion == null) {
                hyperRegion = new Region(docNode.getStartOffset(), docNode.getFirstStructuredDocumentRegion().getTextLength());
            }
        }
    }
    /**
     * Only return a hyperlink region that overlaps the search region.
     * This will help us to not underline areas not under the cursor.
     */
    if (hyperRegion != null && intersects(hyperRegion, boundingRegion))
        return hyperRegion;
    return null;
}
Also used : IDOMAttr(org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) IRegion(org.eclipse.jface.text.IRegion) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) Region(org.eclipse.jface.text.Region) ITypedRegion(org.eclipse.jface.text.ITypedRegion) IRegion(org.eclipse.jface.text.IRegion)

Example 35 with IDOMNode

use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode 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)

Aggregations

IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)250 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)91 Node (org.w3c.dom.Node)63 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)57 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)44 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)43 ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)38 List (java.util.List)35 ArrayList (java.util.ArrayList)34 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)30 CMElementDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration)30 CMNode (org.eclipse.wst.xml.core.internal.contentmodel.CMNode)27 Element (org.w3c.dom.Element)27 NodeList (org.w3c.dom.NodeList)23 BadLocationException (org.eclipse.jface.text.BadLocationException)22 CustomCompletionProposal (org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal)22 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)20 IDOMElement (org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)19 CMDocument (org.eclipse.wst.xml.core.internal.contentmodel.CMDocument)18 IDOMDocument (org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)18