Search in sources :

Example 41 with CMNamedNodeMap

use of org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap in project webtools.sourceediting by eclipse.

the class AbstractContentAssistProcessor method computeEntityReferenceProposals.

/**
 * return all possible EntityReferenceProposals (according to current
 * position in doc)
 */
protected ICompletionProposal[] computeEntityReferenceProposals(int documentPosition, ITextRegion completionRegion, IDOMNode treeNode) {
    // only handle XML content for now
    // ICompletionProposals
    Vector proposals = new Vector();
    IStructuredDocumentRegion sdRegion = getStructuredDocumentRegion(documentPosition);
    if ((completionRegion != null) && (completionRegion.getType() == DOMRegionContext.XML_CONTENT)) {
        int nodeOffset = documentPosition - sdRegion.getStartOffset(completionRegion);
        String regionText = sdRegion.getFullText(completionRegion);
        // the previous region...there might be a better way to do this
        if ((regionText != null) && regionText.trim().equals("") && (documentPosition > 0)) {
            // $NON-NLS-1$
            IStructuredDocumentRegion prev = treeNode.getStructuredDocument().getRegionAtCharacterOffset(documentPosition - 1);
            if ((prev != null) && prev.getText().equals("&")) {
                // $NON-NLS-1$
                // https://bugs.eclipse.org/bugs/show_bug.cgi?id=206680
                // examine previous region
                sdRegion = prev;
                completionRegion = prev.getLastRegion();
                regionText = prev.getFullText();
                nodeOffset = 1;
            }
        }
        // string must start w/ &
        if ((regionText != null) && regionText.startsWith("&")) {
            // $NON-NLS-1$
            // $NON-NLS-1$
            String key = (nodeOffset > 0) ? regionText.substring(1, nodeOffset) : "";
            // get entity proposals, passing in the appropriate start
            // string
            ModelQuery mq = ModelQueryUtil.getModelQuery(((Node) treeNode).getOwnerDocument());
            if (mq != null) {
                CMDocument xmlDoc = mq.getCorrespondingCMDocument(treeNode);
                CMNamedNodeMap cmmap = null;
                Properties entities = null;
                if (xmlDoc != null) {
                    cmmap = xmlDoc.getEntities();
                }
                if (cmmap != null) {
                    entities = mapToProperties(cmmap);
                } else // 224787 in absence of content model, just use
                // minimal 5 entities
                {
                    entities = new Properties();
                    // $NON-NLS-1$ //$NON-NLS-2$
                    entities.put("quot", "\"");
                    // $NON-NLS-1$ //$NON-NLS-2$
                    entities.put("apos", "'");
                    // $NON-NLS-1$ //$NON-NLS-2$
                    entities.put("amp", "&");
                    // $NON-NLS-1$ //$NON-NLS-2$
                    entities.put("lt", "<");
                    // $NON-NLS-1$ //$NON-NLS-2$
                    entities.put("gt", ">");
                    // $NON-NLS-1$ //$NON-NLS-2$
                    entities.put("nbsp", " ");
                }
                addEntityProposals(proposals, entities, key, nodeOffset, sdRegion, completionRegion);
            }
        }
    }
    return (ICompletionProposal[]) ((proposals.size() > 0) ? proposals.toArray(new ICompletionProposal[proposals.size()]) : null);
}
Also used : CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) ModelQuery(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap) Properties(java.util.Properties) Vector(java.util.Vector)

Example 42 with CMNamedNodeMap

use of org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap in project webtools.sourceediting by eclipse.

the class NewXMLGenerator method createXMLDocument.

public ByteArrayOutputStream createXMLDocument(String xmlFileName, String charset) throws Exception {
    if (charset == null) {
        charset = getUserPreferredCharset();
        if (charset == null) {
            // $NON-NLS-1$
            charset = "UTF-8";
        }
    }
    CMDocument cmDocument = getCMDocument();
    Assert.isNotNull(cmDocument);
    Assert.isNotNull(getRootElementName());
    // create the xml model
    CMNamedNodeMap nameNodeMap = cmDocument.getElements();
    CMElementDeclaration cmElementDeclaration = (CMElementDeclaration) nameNodeMap.getNamedItem(getRootElementName());
    Document xmlDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    DOMContentBuilderImpl contentBuilder = new DOMContentBuilderImpl(xmlDocument);
    // this 'uglyTempHack' flag is required in order to supress the
    // creation a default encoding
    // we'll handle this later in the domWriter.print() method used below
    // 
    contentBuilder.supressCreationOfDoctypeAndXMLDeclaration = true;
    contentBuilder.setBuildPolicy(buildPolicy);
    contentBuilder.setOptionalElementDepthLimit(optionalElementDepthLimit);
    contentBuilder.setExternalCMDocumentSupport(new MyExternalCMDocumentSupport(namespaceInfoList, xmlFileName));
    contentBuilder.createDefaultRootContent(cmDocument, cmElementDeclaration, namespaceInfoList);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, charset);
    DOMWriter domWriter = new DOMWriter(outputStreamWriter);
    // TODO... instead of relying on file extensions, we need to keep
    // track of the grammar type
    // better yet we should reate an SSE document so that we can format it
    // nicely before saving
    // then we won't need the DOMWriter at all
    // 
    domWriter.print(xmlDocument, charset, cmDocument.getNodeName(), getNonWhitespaceString(getPublicId()), getNonWhitespaceString(getSystemId()));
    outputStream.flush();
    outputStream.close();
    return outputStream;
}
Also used : CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) DOMContentBuilderImpl(org.eclipse.wst.xml.core.internal.contentmodel.util.DOMContentBuilderImpl) DOMWriter(org.eclipse.wst.xml.core.internal.contentmodel.util.DOMWriter) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap) Document(org.w3c.dom.Document) CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument)

Example 43 with CMNamedNodeMap

use of org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap in project webtools.sourceediting by eclipse.

the class AttributeContextInformationProvider method getInfoForElement.

/**
 * @param node
 */
private IContextInformation[] getInfoForElement(IDOMNode node) {
    IContextInformation[] results = EMPTY_CONTEXT_INFO;
    CMElementDeclaration decl = fModelUtil.getModelQuery().getCMElementDeclaration((Element) node);
    if (decl != null) {
        CMNamedNodeMap attributes = decl.getAttributes();
        CMNamedNodeMapImpl allAttributes = new CMNamedNodeMapImpl(attributes);
        List nodes = ModelQueryUtil.getModelQuery(node.getOwnerDocument()).getAvailableContent((Element) node, decl, ModelQuery.INCLUDE_ATTRIBUTES);
        for (int k = 0; k < nodes.size(); k++) {
            CMNode cmnode = (CMNode) nodes.get(k);
            if (cmnode.getNodeType() == CMNode.ATTRIBUTE_DECLARATION) {
                allAttributes.put(cmnode);
            }
        }
        attributes = allAttributes;
        String attrContextString = node.getNodeName();
        // $NON-NLS-1$
        StringBuffer attrInfo = new StringBuffer(" ");
        // $NON-NLS-1$
        String name = "";
        HashMap attrPosMap = new HashMap();
        int pos = 0;
        int length = 0;
        int numPerLine = 8;
        CMAttributeDeclaration[] sortedAttrs = getSortedAttributes(attributes);
        for (int i = 0; i < sortedAttrs.length; i++) {
            name = sortedAttrs[i].getAttrName();
            length = name.length();
            pos = attrInfo.length();
            attrInfo.append(name);
            if (sortedAttrs[i].getUsage() == CMAttributeDeclaration.REQUIRED) {
                // $NON-NLS-1$
                attrInfo.append("*");
                length++;
            }
            if (i < attributes.getLength() - 1) {
                // $NON-NLS-1$
                attrInfo.append(" ");
                if ((i != 0) && (i % numPerLine == 0)) {
                    // $NON-NLS-1$
                    attrInfo.append("\n ");
                }
            }
            attrPosMap.put(name, new Position(pos, length));
        }
        if (!attrInfo.toString().trim().equals("")) {
            return new IContextInformation[] { new AttributeContextInformation(attrContextString, attrInfo.toString(), attrPosMap) };
        }
    }
    return results;
}
Also used : CMNamedNodeMapImpl(org.eclipse.wst.xml.core.internal.contentmodel.basic.CMNamedNodeMapImpl) HashMap(java.util.HashMap) Position(org.eclipse.jface.text.Position) IContextInformation(org.eclipse.jface.text.contentassist.IContextInformation) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) ArrayList(java.util.ArrayList) CMNodeList(org.eclipse.wst.xml.core.internal.contentmodel.CMNodeList) List(java.util.List) CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode) CMAttributeDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)

Example 44 with CMNamedNodeMap

use of org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap in project webtools.sourceediting by eclipse.

the class HTML5ContentModelTest method checkAttrValues.

private void checkAttrValues(String documentKey, String elementName, String attrName, String[] attrValues) {
    CMDocument document = HTMLCMDocumentFactory.getCMDocument(documentKey);
    CMNode elementDeclaration = document.getElements().getNamedItem(elementName);
    assertEquals("not an element declaration:" + elementDeclaration, CMNode.ELEMENT_DECLARATION, elementDeclaration.getNodeType());
    assertNotNull("missing element declaration:" + elementName, elementDeclaration);
    CMNamedNodeMap attributes = ((CMElementDeclaration) elementDeclaration).getAttributes();
    final CMNode node = attributes.getNamedItem(attrName);
    assertNotNull("No attribute [" + attrName + "]", node);
    final String[] actualValues = ((HTMLAttributeDeclaration) node).getAttrType().getEnumeratedValues();
    assertEquals(attrValues.length, actualValues.length);
    Set valueSet = new HashSet(actualValues.length);
    for (int i = 0; i < actualValues.length; i++) {
        valueSet.add(actualValues[i]);
    }
    for (int i = 0; i < attrValues.length; i++) {
        if (!valueSet.remove(attrValues[i]))
            fail("Type did not contain attribute value [" + attrValues[i] + "]");
    }
    assertTrue("Type had unexpected attribute values", valueSet.isEmpty());
}
Also used : CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) Set(java.util.Set) HashSet(java.util.HashSet) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap) HashSet(java.util.HashSet)

Example 45 with CMNamedNodeMap

use of org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap in project webtools.sourceediting by eclipse.

the class HTML5ContentModelTest method checkAttrNames.

/**
 * @param cm_doc_type
 * @param elementName
 * @param attrNameImport
 */
private void checkAttrNames(String documentKey, String elementName, String[] attrNames) {
    CMDocument document = HTMLCMDocumentFactory.getCMDocument(documentKey);
    CMNode elementDeclaration = document.getElements().getNamedItem(elementName);
    assertEquals("not an element declaration:" + elementDeclaration, CMNode.ELEMENT_DECLARATION, elementDeclaration.getNodeType());
    assertNotNull("missing element declaration:" + elementName, elementDeclaration);
    CMNamedNodeMap attributes = ((CMElementDeclaration) elementDeclaration).getAttributes();
    for (int i = 0; i < attrNames.length; i++) {
        assertNotNull("missing attribute declaration:" + attrNames[i] + " for element: " + elementName, attributes.getNamedItem(attrNames[i]));
    }
    assertEquals("Attributes defined in content model that are not expected by the test for element: " + elementName, attributes.getLength(), attrNames.length);
}
Also used : CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)

Aggregations

CMNamedNodeMap (org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)68 CMElementDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration)39 CMNode (org.eclipse.wst.xml.core.internal.contentmodel.CMNode)36 CMDocument (org.eclipse.wst.xml.core.internal.contentmodel.CMDocument)26 CMAttributeDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration)23 List (java.util.List)21 ArrayList (java.util.ArrayList)18 ModelQuery (org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery)16 ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)14 Element (org.w3c.dom.Element)14 CMNamedNodeMapImpl (org.eclipse.wst.xml.core.internal.contentmodel.basic.CMNamedNodeMapImpl)13 NamedNodeMap (org.w3c.dom.NamedNodeMap)10 CMDataType (org.eclipse.wst.xml.core.internal.contentmodel.CMDataType)9 NodeList (org.w3c.dom.NodeList)9 Iterator (java.util.Iterator)8 Attr (org.w3c.dom.Attr)8 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)7 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)6 CMNodeList (org.eclipse.wst.xml.core.internal.contentmodel.CMNodeList)6 IDOMElement (org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)6