Search in sources :

Example 36 with IDOMElement

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

the class CreatingJSPExpression method testCreateJSPExpression.

public void testCreateJSPExpression() throws IOException {
    // First make (empty) structuredDocument
    IModelManager modelManager = StructuredModelManager.getModelManager();
    IStructuredModel model = modelManager.createUnManagedStructuredModelFor(ContentTypeIdForJSP.ContentTypeID_JSP);
    assertTrue("model could not be created!", model != null);
    // Now, assigning use a page directive, but leaving embedded type the same as default
    model.getStructuredDocument().setText(this, "<%@ page contentType=\"text/html\" language=\"java\" %>");
    Document doc = ((IDOMModel) model).getDocument();
    Element jspexpression = doc.createElement("jsp:expression");
    ((IDOMElement) jspexpression).setJSPTag(true);
    doc.appendChild(jspexpression);
    Text javacode = doc.createTextNode(" // some java code here; if (x <0) return new String(\"0\") else return new String (\"1\"); ");
    jspexpression.appendChild(javacode);
    Text cdatasection = doc.createCDATASection(" // some cdata java code here; if (x <0) return new String(\"0\") else return new String (\"1\"); ");
    doc.appendChild(cdatasection);
    // format's not needed, just prettier ... not sure why
    // it won't work right on whole document.
    new NodeFormatter().format(jspexpression);
    new NodeFormatter().format(cdatasection);
    System.out.println("document text: ");
    System.out.println(model.getStructuredDocument().get());
}
Also used : IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) NodeFormatter(org.eclipse.wst.xml.core.internal.provisional.format.NodeFormatter) Element(org.w3c.dom.Element) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement) IModelManager(org.eclipse.wst.sse.core.internal.provisional.IModelManager) Text(org.w3c.dom.Text) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) Document(org.w3c.dom.Document) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)

Example 37 with IDOMElement

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

the class AddDocumentationCommand method execute.

public void execute() {
    if (input instanceof XSDSchema) {
        ensureSchemaElement((XSDSchema) input);
    }
    try {
        beginRecording(input.getElement());
        xsdAnnotation = XSDCommonUIUtils.getInputXSDAnnotation(input, true);
        Element element = xsdAnnotation.getElement();
        List documentationList = xsdAnnotation.getUserInformation();
        documentationElement = null;
        documentationExists = false;
        if (documentationList.size() > 0) {
            documentationExists = true;
            documentationElement = (Element) documentationList.get(0);
        }
        if (documentationElement == null) {
            documentationElement = xsdAnnotation.createUserInformation(null);
            element.appendChild(documentationElement);
            formatChild(documentationElement);
            // Defect in model....I create it but the model object doesn't appear
            // to be updated
            xsdAnnotation.updateElement();
            xsdAnnotation.setElement(element);
        }
        if (documentationElement.hasChildNodes()) {
            if (documentationElement instanceof IDOMElement) {
                IDOMElement domElement = (IDOMElement) documentationElement;
                Node firstChild = documentationElement.getFirstChild();
                Node lastChild = documentationElement.getLastChild();
                int start = 0;
                int end = 0;
                // IDOMModel model = domElement.getModel();
                // IDOMDocument doc = model.getDocument();
                IDOMNode first = null;
                if (firstChild instanceof IDOMNode) {
                    first = (IDOMNode) firstChild;
                    start = first.getStartOffset();
                }
                if (lastChild instanceof IDOMNode) {
                    IDOMNode last = (IDOMNode) lastChild;
                    end = last.getEndOffset();
                }
                if (domElement != null) {
                    oldValue = domElement.getModel().getStructuredDocument().get(start, end - start);
                    domElement.getModel().getStructuredDocument().replaceText(documentationElement, start, end - start, newValue);
                }
            }
        } else {
            if (newValue.length() > 0) {
                // $NON-NLS-1$
                oldValue = "";
                Node childNode = documentationElement.getOwnerDocument().createTextNode(newValue);
                documentationElement.appendChild(childNode);
            }
        }
    } catch (Exception e) {
    } finally {
        endRecording();
    }
}
Also used : IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) Element(org.w3c.dom.Element) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement) Node(org.w3c.dom.Node) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) List(java.util.List) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement) XSDSchema(org.eclipse.xsd.XSDSchema)

Example 38 with IDOMElement

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

the class ElementNodeCleanupHandler method insertEndTag.

protected IDOMNode insertEndTag(IDOMNode node) {
    IDOMElement element = (IDOMElement) node;
    int startTagStartOffset = node.getStartOffset();
    IDOMModel structuredModel = node.getModel();
    IDOMNode newNode = null;
    if (element.isCommentTag()) {
    // do nothing
    } else if (isEmptyElement(element)) {
        IStructuredDocument structuredDocument = structuredModel.getStructuredDocument();
        IStructuredDocumentRegion startStructuredDocumentRegion = node.getStartStructuredDocumentRegion();
        ITextRegionList regions = startStructuredDocumentRegion.getRegions();
        ITextRegion lastRegion = regions.get(regions.size() - 1);
        replaceSource(structuredModel, structuredDocument, startStructuredDocumentRegion.getStartOffset(lastRegion), lastRegion.getLength(), EMPTY_TAG_CLOSE);
        if (regions.size() > 1) {
            ITextRegion regionBeforeTagClose = regions.get(regions.size() - 1 - 1);
            // region does not have extra spaces
            if (regionBeforeTagClose.getTextLength() == regionBeforeTagClose.getLength())
                // $NON-NLS-1$
                replaceSource(structuredModel, structuredDocument, startStructuredDocumentRegion.getStartOffset(lastRegion), 0, " ");
        }
    } else {
        String tagName = node.getNodeName();
        String endTag = END_TAG_OPEN.concat(tagName).concat(TAG_CLOSE);
        IDOMNode lastChild = (IDOMNode) node.getLastChild();
        int endTagStartOffset = 0;
        if (lastChild != null)
            // if this node has children, insert the end tag after the
            // last child
            endTagStartOffset = lastChild.getEndOffset();
        else
            // if this node does not has children, insert the end tag
            // after the start tag
            endTagStartOffset = node.getEndOffset();
        IStructuredDocument structuredDocument = structuredModel.getStructuredDocument();
        replaceSource(structuredModel, structuredDocument, endTagStartOffset, 0, endTag);
    }
    // save
    newNode = (IDOMNode) structuredModel.getIndexedRegion(startTagStartOffset);
    return newNode;
}
Also used : ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)

Example 39 with IDOMElement

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

the class ElementNodeCleanupHandler method cleanup.

public Node cleanup(Node node) {
    IDOMNode renamedNode = (IDOMNode) cleanupChildren(node);
    // call quoteAttrValue() first so it will close any unclosed attr
    // quoteAttrValue() will return the new start tag if there is a
    // structure change
    renamedNode = quoteAttrValue(renamedNode);
    // and not implicit tag
    if (!((IDOMElement) renamedNode).isCommentTag() && (renamedNode.getStartStructuredDocumentRegion() != null)) {
        IDOMModel structuredModel = renamedNode.getModel();
        // save start offset before insertTagClose()
        // or else renamedNode.getStartOffset() will be zero if
        // renamedNode replaced by insertTagClose()
        int startTagStartOffset = renamedNode.getStartOffset();
        // for start tag
        IStructuredDocumentRegion startTagStructuredDocumentRegion = renamedNode.getStartStructuredDocumentRegion();
        insertTagClose(structuredModel, startTagStructuredDocumentRegion);
        // update renamedNode and startTagStructuredDocumentRegion after
        // insertTagClose()
        renamedNode = (IDOMNode) structuredModel.getIndexedRegion(startTagStartOffset);
        startTagStructuredDocumentRegion = renamedNode.getStartStructuredDocumentRegion();
        // for end tag
        IStructuredDocumentRegion endTagStructuredDocumentRegion = renamedNode.getEndStructuredDocumentRegion();
        if (endTagStructuredDocumentRegion != startTagStructuredDocumentRegion)
            insertTagClose(structuredModel, endTagStructuredDocumentRegion);
    }
    // call insertMissingTags() next, it will generate implicit tags if
    // there are any
    // insertMissingTags() will return the new missing start tag if one is
    // missing
    // then compress any empty element tags
    // applyTagNameCase() will return the renamed node.
    // The renamed/new node will be saved and returned to caller when all
    // cleanup is done.
    renamedNode = insertMissingTags(renamedNode);
    renamedNode = compressEmptyElementTag(renamedNode);
    renamedNode = insertRequiredAttrs(renamedNode);
    renamedNode = applyTagNameCase(renamedNode);
    applyAttrNameCase(renamedNode);
    cleanupCSSAttrValue(renamedNode);
    return renamedNode;
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)

Example 40 with IDOMElement

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

the class ElementNodeCleanupHandler method insertStartTag.

protected IDOMNode insertStartTag(IDOMNode node) {
    IDOMElement element = (IDOMElement) node;
    if (element.isCommentTag())
        // do nothing
        return node;
    IDOMNode newNode = null;
    String tagName = node.getNodeName();
    String startTag = START_TAG_OPEN.concat(tagName).concat(TAG_CLOSE);
    int startTagStartOffset = node.getStartOffset();
    IDOMModel structuredModel = node.getModel();
    IStructuredDocument structuredDocument = structuredModel.getStructuredDocument();
    replaceSource(structuredModel, structuredDocument, startTagStartOffset, 0, startTag);
    // save
    newNode = (IDOMNode) structuredModel.getIndexedRegion(startTagStartOffset);
    return newNode;
}
Also used : IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)

Aggregations

IDOMElement (org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)59 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)20 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)19 Node (org.w3c.dom.Node)18 NodeList (org.w3c.dom.NodeList)13 Element (org.w3c.dom.Element)11 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)9 IDOMAttr (org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr)9 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)8 CMElementDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration)8 NamedNodeMap (org.w3c.dom.NamedNodeMap)8 ArrayList (java.util.ArrayList)7 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)7 IDOMDocument (org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)7 IFile (org.eclipse.core.resources.IFile)6 ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)6 List (java.util.List)5 CMNamedNodeMap (org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)5 Text (org.w3c.dom.Text)5 ModelQuery (org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery)4