Search in sources :

Example 66 with CMDocument

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

the class BugFixesTest method testBase64BinaryDefaultValue.

/**
 * See https://bugs.eclipse.org/bugs/show_bug.cgi?id=322841
 */
public void testBase64BinaryDefaultValue() {
    Bundle bundle = Platform.getBundle(XSDCoreTestsPlugin.PLUGIN_ID);
    // $NON-NLS-1$
    URL url = bundle.getEntry("/testresources/samples/base64Binary/Test.xsd");
    CMDocument cmDocument = XSDImpl.buildCMDocument(url.toExternalForm());
    assertNotNull(cmDocument);
    CMNamedNodeMap elements = cmDocument.getElements();
    // $NON-NLS-1$
    CMElementDeclaration cmElementDeclaration = (CMElementDeclaration) elements.getNamedItem("Test");
    assertNotNull(cmElementDeclaration);
    CMDataType dataType = cmElementDeclaration.getDataType();
    assertNotNull(dataType);
    String impliedValue = dataType.generateInstanceValue();
    // $NON-NLS-1$
    assertEquals("MA==", impliedValue);
}
Also used : CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) CMDataType(org.eclipse.wst.xml.core.internal.contentmodel.CMDataType) Bundle(org.osgi.framework.Bundle) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap) URL(java.net.URL)

Example 67 with CMDocument

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

the class TestNewXMLGenerator method runOneTest.

/**
 * Runs a generic generate + comparison test.
 * @param buildPolicy Integer representing the build policy for this generation.
 * @param rootElement Desired root element.
 * @param xsdUri URI of the desired XML schema.
 */
private void runOneTest(int buildPolicy, String rootElement, String xsdUriFile) {
    try {
        // generic setup
        PLUGIN_ABSOLUTE_PATH = XMLUITestsPlugin.getInstallURL();
        // $NON-NLS-3$
        String uri = "file:///" + PLUGIN_ABSOLUTE_PATH + "testresources/" + xsdUriFile + ".xsd";
        String[] errorInfo = new String[2];
        CMDocument cmd = NewXMLGenerator.createCMDocument(uri, errorInfo);
        NewXMLGenerator generator = new NewXMLGenerator(uri, cmd);
        String id = xsdUriFile + "-" + rootElement + "-" + buildPolicy;
        // $NON-NLS-2$
        String fileNameResult = PLUGIN_ABSOLUTE_PATH + "testresources/Tested-" + id + ".xml";
        File nFile = new File(fileNameResult);
        if (!nFile.exists()) {
            nFile.createNewFile();
        }
        // $NON-NLS-2$
        String fileNameCompare = PLUGIN_ABSOLUTE_PATH + "testresources/Compare-" + id + ".xml";
        generator.setBuildPolicy(buildPolicy);
        generator.setRootElementName(rootElement);
        // $NON-NLS-1$
        generator.setDefaultSystemId(xsdUriFile + ".xsd");
        generator.createNamespaceInfoList();
        generator.createXMLDocument(fileNameResult);
        XMLDiff differ = new XMLDiff();
        // $NON-NLS-2$
        assertTrue("The XML files are not identical.", differ.diff(fileNameCompare, fileNameResult, "XML"));
        // if we've made it this far, delete the output file
        remove(nFile);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) NewXMLGenerator(org.eclipse.wst.xml.ui.internal.wizards.NewXMLGenerator) File(java.io.File)

Example 68 with CMDocument

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

the class CMDocumentFactoryTLD method createCMDocument.

/**
 * Builds a CMDocument from a taglib descriptor
 *
 * @param uri -
 *            the location of a valid taglib descriptor
 */
public CMDocument createCMDocument(String uri) {
    CMDocument result = null;
    URL url = null;
    try {
        url = new URL(uri);
    } catch (MalformedURLException e) {
        result = createCMDocumentFromFile(uri);
    }
    if (result == null && url != null) {
        if (url.getProtocol().equals("file")) {
            // $NON-NLS-1$
            result = createCMDocumentFromFile(url.getFile());
        } else {
        /**
         * Skip anything else since trying to load a TLD from a remote
         * location has unpredictable performance impact.
         */
        }
    }
    if (result == null)
        result = new CMDocumentImpl();
    return result;
}
Also used : CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) MalformedURLException(java.net.MalformedURLException) URL(java.net.URL)

Example 69 with CMDocument

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

the class CommentElementHandlerForSSI method createElement.

public Element createElement(Document document, String data, boolean isJSPTag) {
    ModelQuery modelQuery = ModelQueryUtil.getModelQuery(document);
    if (modelQuery == null) {
        return null;
    }
    CMDocument cm = modelQuery.getCorrespondingCMDocument(document);
    if (cm == null) {
        return null;
    }
    CMNamedNodeMap map = cm.getElements();
    if (map == null) {
        return null;
    }
    TagScanner scanner = new TagScanner(data, 1);
    String name = scanner.nextName();
    if (name == null) {
        return null;
    }
    StringBuffer buffer = new StringBuffer(name.length() + 4);
    buffer.append(SSI_PREFIX);
    buffer.append(':');
    buffer.append(name);
    String tagName = buffer.toString();
    // check if valid (defined) SSI tag or not
    if (map.getNamedItem(tagName) == null) {
        return null;
    }
    CommentElementFactory factory = new CommentElementFactory(document, isJSPTag, this);
    Element element = factory.create(tagName, CommentElementFactory.IS_START);
    // set attributes
    String attrName = scanner.nextName();
    while (attrName != null) {
        String attrValue = scanner.nextValue();
        Attr attr = document.createAttribute(attrName);
        if (attr != null) {
            if (attrValue != null)
                attr.setValue(attrValue);
            element.setAttributeNode(attr);
        }
        attrName = scanner.nextName();
    }
    return element;
}
Also used : CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) CommentElementFactory(org.eclipse.wst.xml.core.internal.commentelement.util.CommentElementFactory) Element(org.w3c.dom.Element) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement) ModelQuery(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery) TagScanner(org.eclipse.wst.xml.core.internal.commentelement.util.TagScanner) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap) Attr(org.w3c.dom.Attr)

Example 70 with CMDocument

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

the class LibraryTagsCompletionProposalComputer 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) {
    if (!this.isXHTML) {
        IDOMNode node = (IDOMNode) contentAssistRequest.getNode();
        ModelQuery mq = ModelQueryUtil.getModelQuery(node.getOwnerDocument());
        if (mq != null) {
            CMDocument doc = mq.getCorrespondingCMDocument(node);
            // this shouldn't have to have the prefix coded in
            if (doc instanceof JSPCMDocument || doc instanceof CMNodeWrapper || node.getNodeName().startsWith("jsp:")) {
                // $NON-NLS-1$
                return;
            }
        }
        // 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;
            }
        }
        // on an empty value, add all the JSP and taglib tags
        CMElementDeclaration elementDecl = AbstractXMLModelQueryCompletionProposalComputer.getCMElementDeclaration(node);
        if (nameRegion != null && elementDecl != null) {
            String attributeName = open.getText(nameRegion);
            if (attributeName != null) {
                Node parent = contentAssistRequest.getParent();
                // ignore start quote in match string
                String matchString = contentAssistRequest.getMatchString().trim();
                if (matchString.startsWith("'") || matchString.startsWith("\"")) {
                    // $NON-NLS-1$ //$NON-NLS-2$
                    matchString = matchString.substring(1);
                }
                // get all the proposals
                List additionalElements = ModelQueryUtil.getModelQuery(node.getOwnerDocument()).getAvailableContent((Element) node, elementDecl, ModelQuery.INCLUDE_ALL);
                Iterator nodeIterator = additionalElements.iterator();
                // check each suggestion
                while (nodeIterator.hasNext()) {
                    CMNode additionalElementDecl = (CMNode) nodeIterator.next();
                    if (additionalElementDecl != null && additionalElementDecl instanceof CMElementDeclaration && validModelQueryNode(additionalElementDecl)) {
                        CMElementDeclaration ed = (CMElementDeclaration) additionalElementDecl;
                        // https://bugs.eclipse.org/bugs/show_bug.cgi?id=89811
                        StringBuffer sb = new StringBuffer();
                        getContentGenerator().generateTag(parent, ed, sb);
                        String proposedText = sb.toString();
                        // filter out any proposals that dont match matchString
                        if (beginsWith(proposedText, matchString)) {
                            // wrap with ' because JSP attributes are warped with "
                            // $NON-NLS-1$
                            proposedText = "'" + proposedText;
                            // don't want to risk injecting an extra
                            if (!(contentAssistRequest.getRegion() instanceof ITextRegionContainer)) {
                                // $NON-NLS-1$
                                proposedText += "'";
                            }
                            // get the image
                            Image image = CMImageUtil.getImage(elementDecl);
                            if (image == null) {
                                image = this.getGenericTagImage();
                            }
                            // create the proposal
                            int cursorAdjustment = getCursorPositionForProposedText(proposedText);
                            String proposedInfo = AbstractXMLModelQueryCompletionProposalComputer.getAdditionalInfo(AbstractXMLModelQueryCompletionProposalComputer.getCMElementDeclaration(parent), elementDecl);
                            String tagname = getContentGenerator().getRequiredName(node, ed);
                            CustomCompletionProposal proposal = new CustomCompletionProposal(proposedText, contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), cursorAdjustment, image, tagname, null, proposedInfo, XMLRelevanceConstants.R_XML_ATTRIBUTE_VALUE);
                            contentAssistRequest.addProposal(proposal);
                        }
                    }
                }
            }
        }
    }
}
Also used : JSPCMDocument(org.eclipse.wst.html.core.internal.contentmodel.JSPCMDocument) CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) CMNodeWrapper(org.eclipse.wst.xml.core.internal.provisional.contentmodel.CMNodeWrapper) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode) 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) ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) Iterator(java.util.Iterator) JSPCMDocument(org.eclipse.wst.html.core.internal.contentmodel.JSPCMDocument) ModelQuery(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery) List(java.util.List) ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode)

Aggregations

CMDocument (org.eclipse.wst.xml.core.internal.contentmodel.CMDocument)83 CMElementDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration)33 CMNamedNodeMap (org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)26 CMNode (org.eclipse.wst.xml.core.internal.contentmodel.CMNode)20 List (java.util.List)15 ModelQuery (org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery)14 Document (org.w3c.dom.Document)12 Element (org.w3c.dom.Element)11 ArrayList (java.util.ArrayList)10 Path (org.eclipse.core.runtime.Path)8 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)8 JSPCMDocument (org.eclipse.wst.html.core.internal.contentmodel.JSPCMDocument)7 ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)7 IDOMDocument (org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)7 URL (java.net.URL)6 TLDCMDocumentManager (org.eclipse.jst.jsp.core.internal.contentmodel.tld.TLDCMDocumentManager)6 Node (org.w3c.dom.Node)6 NodeList (org.w3c.dom.NodeList)6 Iterator (java.util.Iterator)5 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)5