Search in sources :

Example 21 with ModelQueryAdapter

use of org.eclipse.wst.xml.core.internal.ssemodelquery.ModelQueryAdapter in project webtools.sourceediting by eclipse.

the class HTMLTagsCompletionProposalComputer method isXHTMLNode.

/**
 * Determine if this Document is an XHTML Document. Operates solely off of
 * the Document Type declaration
 */
private static boolean isXHTMLNode(Node node) {
    if (node == null) {
        return false;
    }
    Document doc = null;
    if (node.getNodeType() != Node.DOCUMENT_NODE)
        doc = node.getOwnerDocument();
    else
        doc = ((Document) node);
    if (doc instanceof IDOMDocument) {
        return ((IDOMDocument) doc).isXMLType();
    }
    if (doc instanceof INodeNotifier) {
        ModelQueryAdapter adapter = (ModelQueryAdapter) ((INodeNotifier) doc).getAdapterFor(ModelQueryAdapter.class);
        CMDocument cmdoc = null;
        if (adapter != null && adapter.getModelQuery() != null)
            cmdoc = adapter.getModelQuery().getCorrespondingCMDocument(doc);
        if (cmdoc != null) {
            // model
            if (cmdoc instanceof HTMLCMDocument)
                return false;
            if (cmdoc.supports(HTMLCMProperties.IS_XHTML))
                return Boolean.TRUE.equals(cmdoc.getProperty(HTMLCMProperties.IS_XHTML));
        }
    }
    // this should never be reached
    DocumentType docType = doc.getDoctype();
    // $NON-NLS-1$
    return docType != null && docType.getPublicId() != null && docType.getPublicId().indexOf("-//W3C//DTD XHTML ") == 0;
}
Also used : HTMLCMDocument(org.eclipse.wst.html.core.internal.contentmodel.HTMLCMDocument) CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) ModelQueryAdapter(org.eclipse.wst.xml.core.internal.ssemodelquery.ModelQueryAdapter) HTMLCMDocument(org.eclipse.wst.html.core.internal.contentmodel.HTMLCMDocument) DocumentType(org.w3c.dom.DocumentType) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) HTMLCMDocument(org.eclipse.wst.html.core.internal.contentmodel.HTMLCMDocument) Document(org.w3c.dom.Document) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) INodeNotifier(org.eclipse.wst.sse.core.internal.provisional.INodeNotifier)

Example 22 with ModelQueryAdapter

use of org.eclipse.wst.xml.core.internal.ssemodelquery.ModelQueryAdapter in project webtools.sourceediting by eclipse.

the class ElementNodeFormatter method formatStartTag.

/**
 * This method formats the start tag name, and formats the attributes if
 * available.
 */
protected void formatStartTag(IDOMNode node, IStructuredFormatContraints formatContraints) {
    StructuredFormatPreferencesXML preferences = (StructuredFormatPreferencesXML) getFormatPreferences();
    String singleIndent = preferences.getIndent();
    String lineIndent = formatContraints.getCurrentIndent();
    String attrIndent = lineIndent + singleIndent;
    boolean splitMultiAttrs = preferences.getSplitMultiAttrs();
    boolean alignEndBracket = preferences.isAlignEndBracket();
    boolean sawXmlSpace = false;
    IStructuredDocumentRegion flatNode = node.getFirstStructuredDocumentRegion();
    NamedNodeMap attributes = node.getAttributes();
    // for null just in case.
    if (attributes != null) {
        // compute current available line width
        int currentAvailableLineWidth = 0;
        try {
            // 1 is for "<"
            int nodeNameOffset = node.getStartOffset() + 1 + node.getNodeName().length();
            int lineOffset = node.getStructuredDocument().getLineInformationOfOffset(nodeNameOffset).getOffset();
            String text = node.getStructuredDocument().get(lineOffset, nodeNameOffset - lineOffset);
            int usedWidth = getIndentationLength(text);
            currentAvailableLineWidth = preferences.getLineWidth() - usedWidth;
        } catch (BadLocationException e) {
            // log for now, unless we find reason not to
            Logger.log(Logger.INFO, e.getMessage());
        }
        StringBuffer stringBuffer = new StringBuffer();
        String lineDelimiter = node.getModel().getStructuredDocument().getLineDelimiter();
        int attrLength = attributes.getLength();
        int lastUndefinedRegionOffset = 0;
        boolean startTagSpansOver1Line = false;
        for (int i = 0; i < attrLength; i++) {
            AttrImpl attr = (AttrImpl) attributes.item(i);
            ITextRegion nameRegion = attr.getNameRegion();
            ITextRegion equalRegion = attr.getEqualRegion();
            ITextRegion valueRegion = attr.getValueRegion();
            // append undefined regions
            String undefinedRegion = getUndefinedRegions(node, lastUndefinedRegionOffset, attr.getStartOffset() - lastUndefinedRegionOffset);
            stringBuffer.append(undefinedRegion);
            lastUndefinedRegionOffset = attr.getStartOffset();
            // check for xml:space attribute
            if (flatNode.getText(nameRegion).compareTo(XML_SPACE) == 0) {
                if (valueRegion == null) {
                    // [111674] If nothing has been written yet, treat as
                    // preserve, but only as hint
                    formatContraints.setInPreserveSpaceElement(true);
                // Note we don't set 'sawXmlSpace', so that default or
                // fixed DTD/XSD values may override.
                } else {
                    ISourceGenerator generator = node.getModel().getGenerator();
                    String newAttrValue = generator.generateAttrValue(attr);
                    // Workaround for now.
                    if (flatNode.getText(valueRegion).length() == 1) {
                        char firstChar = flatNode.getText(valueRegion).charAt(0);
                        if ((firstChar == DOUBLE_QUOTE) || (firstChar == SINGLE_QUOTE))
                            newAttrValue = DOUBLE_QUOTES;
                    }
                    if (newAttrValue.compareTo(PRESERVE_QUOTED) == 0)
                        formatContraints.setInPreserveSpaceElement(true);
                    else
                        formatContraints.setInPreserveSpaceElement(false);
                    sawXmlSpace = true;
                }
            }
            if (splitMultiAttrs && attrLength > 1) {
                stringBuffer.append(lineDelimiter + attrIndent);
                stringBuffer.append(flatNode.getText(nameRegion));
                startTagSpansOver1Line = true;
                if (valueRegion != null) {
                    // append undefined regions
                    undefinedRegion = getUndefinedRegions(node, lastUndefinedRegionOffset, flatNode.getStartOffset(equalRegion) - lastUndefinedRegionOffset);
                    stringBuffer.append(undefinedRegion);
                    lastUndefinedRegionOffset = flatNode.getStartOffset(equalRegion);
                    stringBuffer.append(EQUAL_CHAR);
                    // append undefined regions
                    undefinedRegion = getUndefinedRegions(node, lastUndefinedRegionOffset, flatNode.getStartOffset(valueRegion) - lastUndefinedRegionOffset);
                    stringBuffer.append(undefinedRegion);
                    lastUndefinedRegionOffset = flatNode.getStartOffset(valueRegion);
                    // Note: trim() should not be needed for
                    // valueRegion.getText(). Just a workaround for a
                    // problem found in valueRegion for now.
                    stringBuffer.append(flatNode.getText(valueRegion).trim());
                }
            } else {
                if (valueRegion != null) {
                    int textLength = 1 + flatNode.getText(nameRegion).length() + 1 + flatNode.getText(valueRegion).length();
                    if (i == attrLength - 1) {
                        if (flatNode != null) {
                            ITextRegionList regions = flatNode.getRegions();
                            ITextRegion lastRegion = regions.get(regions.size() - 1);
                            if (lastRegion.getType() != DOMRegionContext.XML_EMPTY_TAG_CLOSE)
                                // 3 is for " />"
                                textLength += 3;
                            else
                                // 1 is for ">"
                                textLength++;
                        }
                    }
                    if (currentAvailableLineWidth >= textLength) {
                        stringBuffer.append(SPACE_CHAR);
                        currentAvailableLineWidth--;
                    } else {
                        stringBuffer.append(lineDelimiter + attrIndent);
                        startTagSpansOver1Line = true;
                        currentAvailableLineWidth = preferences.getLineWidth() - attrIndent.length();
                    }
                    stringBuffer.append(flatNode.getText(nameRegion));
                    // append undefined regions
                    undefinedRegion = getUndefinedRegions(node, lastUndefinedRegionOffset, flatNode.getStartOffset(equalRegion) - lastUndefinedRegionOffset);
                    stringBuffer.append(undefinedRegion);
                    lastUndefinedRegionOffset = flatNode.getStartOffset(equalRegion);
                    stringBuffer.append(EQUAL_CHAR);
                    // append undefined regions
                    undefinedRegion = getUndefinedRegions(node, lastUndefinedRegionOffset, flatNode.getStartOffset(valueRegion) - lastUndefinedRegionOffset);
                    stringBuffer.append(undefinedRegion);
                    lastUndefinedRegionOffset = flatNode.getStartOffset(valueRegion);
                    // Note: trim() should not be needed for
                    // valueRegion.getText(). Just a workaround for a
                    // problem found in valueRegion for now.
                    stringBuffer.append(flatNode.getText(valueRegion).trim());
                    currentAvailableLineWidth -= flatNode.getText(nameRegion).length();
                    currentAvailableLineWidth--;
                    currentAvailableLineWidth -= flatNode.getText(valueRegion).trim().length();
                } else {
                    if (currentAvailableLineWidth >= 1 + flatNode.getText(nameRegion).length()) {
                        stringBuffer.append(SPACE_CHAR);
                        currentAvailableLineWidth--;
                    } else {
                        stringBuffer.append(lineDelimiter + attrIndent);
                        startTagSpansOver1Line = true;
                        currentAvailableLineWidth = preferences.getLineWidth() - attrIndent.length();
                    }
                    stringBuffer.append(flatNode.getText(nameRegion));
                    currentAvailableLineWidth -= flatNode.getText(nameRegion).length();
                }
            }
        }
        // append undefined regions
        String undefinedRegion = getUndefinedRegions(node, lastUndefinedRegionOffset, node.getEndOffset() - lastUndefinedRegionOffset);
        stringBuffer.append(undefinedRegion);
        IDOMModel structuredModel = node.getModel();
        IStructuredDocument structuredDocument = structuredModel.getStructuredDocument();
        // 1 is for "<"
        int offset = node.getStartOffset() + 1 + node.getNodeName().length();
        // 1 is for "<"
        int length = node.getFirstStructuredDocumentRegion().getTextLength() - 1 - node.getNodeName().length();
        if (flatNode != null) {
            ITextRegionList regions = flatNode.getRegions();
            ITextRegion firstRegion = regions.get(0);
            ITextRegion lastRegion = regions.get(regions.size() - 1);
            if (firstRegion.getType() == DOMRegionContext.XML_END_TAG_OPEN)
                // skip formatting for end tags in this format: </tagName>
                return;
            else {
                if (lastRegion.getType() == DOMRegionContext.XML_TAG_CLOSE || lastRegion.getType() == DOMRegionContext.XML_EMPTY_TAG_CLOSE)
                    length = length - lastRegion.getLength();
                if (lastRegion.getType() == DOMRegionContext.XML_EMPTY_TAG_CLOSE) {
                    // unless already going to move end bracket
                    if (!startTagSpansOver1Line || !alignEndBracket)
                        stringBuffer.append(SPACE_CHAR);
                }
            }
        }
        if (startTagSpansOver1Line && alignEndBracket) {
            stringBuffer.append(lineDelimiter).append(lineIndent);
        }
        replace(structuredDocument, offset, length, stringBuffer.toString());
        // BUG108074 & BUG84688 - preserve whitespace in xsl:text &
        // xsl:attribute
        String nodeNamespaceURI = node.getNamespaceURI();
        if (XSL_NAMESPACE.equals(nodeNamespaceURI)) {
            String nodeName = ((Element) node).getLocalName();
            if (XSL_ATTRIBUTE.equals(nodeName) || XSL_TEXT.equals(nodeName)) {
                sawXmlSpace = true;
                formatContraints.setInPreserveSpaceElement(true);
            }
        }
        // leave that to the validator.
        if (!sawXmlSpace) {
            ModelQueryAdapter adapter = (ModelQueryAdapter) ((IDOMDocument) node.getOwnerDocument()).getAdapterFor(ModelQueryAdapter.class);
            CMElementDeclaration elementDeclaration = (CMElementDeclaration) adapter.getModelQuery().getCMNode(node);
            if (elementDeclaration != null) {
                int contentType = elementDeclaration.getContentType();
                if (preferences.isPreservePCDATAContent() && contentType == CMElementDeclaration.PCDATA) {
                    formatContraints.setInPreserveSpaceElement(true);
                } else {
                    CMNamedNodeMap cmAttributes = elementDeclaration.getAttributes();
                    // Check implied values from the DTD way.
                    CMAttributeDeclaration attributeDeclaration = (CMAttributeDeclaration) cmAttributes.getNamedItem(XML_SPACE);
                    if (attributeDeclaration != null) {
                        // CMAttributeDeclaration found, check it out.
                        String defaultValue = attributeDeclaration.getAttrType().getImpliedValue();
                        // everything else means back to default.
                        if (PRESERVE.compareTo(defaultValue) == 0)
                            formatContraints.setInPreserveSpaceElement(true);
                        else
                            formatContraints.setInPreserveSpaceElement(false);
                    }
                }
            }
        }
    }
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap) NamedNodeMap(org.w3c.dom.NamedNodeMap) ModelQueryAdapter(org.eclipse.wst.xml.core.internal.ssemodelquery.ModelQueryAdapter) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) Element(org.w3c.dom.Element) AttrImpl(org.eclipse.wst.xml.core.internal.document.AttrImpl) ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) ISourceGenerator(org.eclipse.wst.xml.core.internal.provisional.document.ISourceGenerator) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) CMAttributeDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 23 with ModelQueryAdapter

use of org.eclipse.wst.xml.core.internal.ssemodelquery.ModelQueryAdapter in project webtools.sourceediting by eclipse.

the class TestModelClone method getEmbeddedModelQuery.

private ModelQuery getEmbeddedModelQuery(Node node) {
    ModelQuery embeddedModelQuery = null;
    if (node instanceof INodeNotifier) {
        Node ownerNode = node.getOwnerDocument();
        if (ownerNode == null) {
            // then must be the document itself
            ownerNode = node;
        }
        PageDirectiveAdapter pageDirectiveAdapter = (PageDirectiveAdapter) ((INodeNotifier) ownerNode).getAdapterFor(PageDirectiveAdapter.class);
        if (pageDirectiveAdapter != null) {
            String effectiveContentType = null;
            ModelQuery potentialModelQueryObject = null;
            String familyId = pageDirectiveAdapter.getEmbeddedType().getFamilyId();
            // if (ContentTypeFamilyForHTML.HTML_FAMILY.equals(familyId)) {
            if ("org.eclipse.wst.html.core.contentfamily.html".equals(familyId)) {
                effectiveContentType = "text/html";
            } else {
                effectiveContentType = pageDirectiveAdapter.getContentType();
            }
            potentialModelQueryObject = (ModelQuery) embeddedModelQueries.get(effectiveContentType);
            if (potentialModelQueryObject == null) {
                ModelQueryAdapter embeddedAdapter = (ModelQueryAdapter) pageDirectiveAdapter.adapt((INodeNotifier) node, ModelQueryAdapter.class);
                if (embeddedAdapter != null) {
                    // we will cache one model query per content type
                    embeddedModelQuery = embeddedAdapter.getModelQuery();
                    embeddedModelQueries.put(effectiveContentType, embeddedModelQuery);
                }
            } else {
                embeddedModelQuery = potentialModelQueryObject;
            }
        }
    }
    return embeddedModelQuery;
}
Also used : ModelQueryAdapter(org.eclipse.wst.xml.core.internal.ssemodelquery.ModelQueryAdapter) Node(org.w3c.dom.Node) ModelQuery(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery) PageDirectiveAdapter(org.eclipse.jst.jsp.core.internal.document.PageDirectiveAdapter) INodeNotifier(org.eclipse.wst.sse.core.internal.provisional.INodeNotifier)

Example 24 with ModelQueryAdapter

use of org.eclipse.wst.xml.core.internal.ssemodelquery.ModelQueryAdapter in project webtools.sourceediting by eclipse.

the class HTMLContentAssistProcessor method getXHTML.

/**
 * Determine if this Document is an XHTML Document. Oprates solely off of
 * the Document Type declaration
 */
protected boolean getXHTML(Node node) {
    if (node == null)
        return false;
    Document doc = null;
    if (node.getNodeType() != Node.DOCUMENT_NODE)
        doc = node.getOwnerDocument();
    else
        doc = ((Document) node);
    if (doc instanceof IDOMDocument)
        return ((IDOMDocument) doc).isXMLType();
    if (doc instanceof INodeNotifier) {
        ModelQueryAdapter adapter = (ModelQueryAdapter) ((INodeNotifier) doc).getAdapterFor(ModelQueryAdapter.class);
        CMDocument cmdoc = null;
        if (adapter != null && adapter.getModelQuery() != null)
            cmdoc = adapter.getModelQuery().getCorrespondingCMDocument(doc);
        if (cmdoc != null) {
            // model
            if (cmdoc instanceof HTMLCMDocument)
                return false;
            if (cmdoc.supports(HTMLCMProperties.IS_XHTML))
                return Boolean.TRUE.equals(cmdoc.getProperty(HTMLCMProperties.IS_XHTML));
        }
    }
    // this should never be reached
    DocumentType docType = doc.getDoctype();
    // $NON-NLS-1$
    return docType != null && docType.getPublicId() != null && docType.getPublicId().indexOf("-//W3C//DTD XHTML ") == 0;
}
Also used : HTMLCMDocument(org.eclipse.wst.html.core.internal.contentmodel.HTMLCMDocument) CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) ModelQueryAdapter(org.eclipse.wst.xml.core.internal.ssemodelquery.ModelQueryAdapter) HTMLCMDocument(org.eclipse.wst.html.core.internal.contentmodel.HTMLCMDocument) DocumentType(org.w3c.dom.DocumentType) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) Document(org.w3c.dom.Document) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) HTMLCMDocument(org.eclipse.wst.html.core.internal.contentmodel.HTMLCMDocument) CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) INodeNotifier(org.eclipse.wst.sse.core.internal.provisional.INodeNotifier)

Example 25 with ModelQueryAdapter

use of org.eclipse.wst.xml.core.internal.ssemodelquery.ModelQueryAdapter in project webtools.sourceediting by eclipse.

the class JFaceNodeAdapterFactory method initAdapter.

protected synchronized void initAdapter(INodeAdapter adapter, INodeNotifier node) {
    Assert.isTrue(cmDocumentManager == null);
    Assert.isTrue(fCMDocumentManagerListener == null);
    // register for CMDocumentManager events
    ModelQueryAdapter mqadapter = (ModelQueryAdapter) node.getAdapterFor(ModelQueryAdapter.class);
    if (mqadapter != null) {
        ModelQuery mquery = mqadapter.getModelQuery();
        if ((mquery != null) && (mquery.getCMDocumentManager() != null)) {
            cmDocumentManager = mquery.getCMDocumentManager();
            fCMDocumentManagerListener = new CMDocumentManagerListenerImpl();
            cmDocumentManager.addListener(fCMDocumentManagerListener);
        }
    }
}
Also used : ModelQueryAdapter(org.eclipse.wst.xml.core.internal.ssemodelquery.ModelQueryAdapter) ModelQuery(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery)

Aggregations

ModelQueryAdapter (org.eclipse.wst.xml.core.internal.ssemodelquery.ModelQueryAdapter)25 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)14 CMElementDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration)11 INodeNotifier (org.eclipse.wst.sse.core.internal.provisional.INodeNotifier)8 IDOMDocument (org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)7 ModelQuery (org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery)6 Document (org.w3c.dom.Document)6 IFile (org.eclipse.core.resources.IFile)5 Element (org.w3c.dom.Element)5 Node (org.w3c.dom.Node)5 NodeList (org.w3c.dom.NodeList)5 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)4 CMDocument (org.eclipse.wst.xml.core.internal.contentmodel.CMDocument)4 CMNamedNodeMap (org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)4 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)4 IProject (org.eclipse.core.resources.IProject)3 PageDirectiveAdapter (org.eclipse.jst.jsp.core.internal.document.PageDirectiveAdapter)3 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)3 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)3 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)3