Search in sources :

Example 41 with IDOMModel

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

the class CleanupActionHTMLDelegate method isXHTML.

private boolean isXHTML() {
    boolean isxhtml = false;
    if (fEditor instanceof ITextEditor) {
        ITextEditor textEditor = (ITextEditor) fEditor;
        IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
        IStructuredModel model = null;
        try {
            model = StructuredModelManager.getModelManager().getExistingModelForRead(document);
            if (model instanceof IDOMModel) {
                IDOMDocument domDocument = ((IDOMModel) model).getDocument();
                if (domDocument != null)
                    isxhtml = domDocument.isXMLType();
            }
        } finally {
            if (model != null) {
                model.releaseFromRead();
            }
        }
    }
    return isxhtml;
}
Also used : ITextEditor(org.eclipse.ui.texteditor.ITextEditor) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IDocument(org.eclipse.jface.text.IDocument)

Example 42 with IDOMModel

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

the class JSPTranslator method postReadExternalSetup.

/**
 * <p>This does mandatory setup needed after a JSPTranslator is restored from
 * a persisted file</p>
 *
 * @param jspModel {@link IStructuredModel} representing the JSP file that this
 * is a translator for
 */
protected void postReadExternalSetup(IStructuredModel jspModel) {
    this.fStructuredDocument = jspModel.getStructuredDocument();
    this.fJspTextBuffer = new StringBuffer(this.fStructuredDocument.get());
    if (jspModel instanceof IDOMModel) {
        this.fStructuredModel = (IDOMModel) jspModel;
    }
    // Do Post-Initialization of created problems if any
    for (int i = 0; i < fTranslationProblems.size(); i++) {
        if (fTranslationProblems.get(i) instanceof IJSPProblem) {
            IJSPProblem problem = (IJSPProblem) fTranslationProblems.get(i);
            if (problem.getSourceLineNumber() == -1) {
                problem.setSourceLineNumber(fStructuredDocument.getLineOfOffset(problem.getSourceStart()));
            }
        }
    }
}
Also used : IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IExtensionPoint(org.eclipse.core.runtime.IExtensionPoint)

Example 43 with IDOMModel

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

the class AutoImportProposal method getInsertPosition.

/**
 * @param doc
 * @param isXml
 * @return position after <jsp:root> if xml, otherwise right before the document element
 */
private int getInsertPosition(IDocument doc, boolean isXml) {
    int pos = 0;
    IStructuredModel sModel = StructuredModelManager.getModelManager().getExistingModelForRead(doc);
    try {
        if (sModel != null) {
            if (sModel instanceof IDOMModel) {
                IDOMDocument documentNode = ((IDOMModel) sModel).getDocument();
                /*
					 * document element must be sole Element child of Document
					 * to remain valid
					 */
                Node targetElement = null;
                if (isXml) {
                    targetElement = documentNode.getDocumentElement();
                }
                if (targetElement == null)
                    targetElement = getInsertNode(documentNode);
                if (targetElement != null) {
                    IStructuredDocumentRegion sdRegion = ((IDOMNode) targetElement).getFirstStructuredDocumentRegion();
                    if (isXml) {
                        /*
							 * document Element must be sole Element child of
							 * Document to remain valid, so insert after
							 */
                        pos = sdRegion.getEndOffset();
                        try {
                            while (pos < doc.getLength() && (doc.getChar(pos) == '\r' || doc.getChar(pos) == '\n')) {
                                pos++;
                            }
                        } catch (BadLocationException e) {
                        // not important, use pos as determined earlier
                        }
                    } else {
                        // insert before target element
                        pos = sdRegion.getStartOffset();
                    }
                } else {
                    pos = 0;
                }
            }
        }
    } finally {
        if (sModel != null)
            sModel.releaseFromRead();
    }
    return pos;
}
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) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) Node(org.w3c.dom.Node) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 44 with IDOMModel

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

the class AutoImportProposal method isXmlFormat.

/**
 * @param doc
 * @return true if this document is xml-jsp syntax, otherwise false
 */
private boolean isXmlFormat(IDocument doc) {
    boolean isXml = false;
    IStructuredModel sModel = StructuredModelManager.getModelManager().getExistingModelForRead(doc);
    try {
        if (sModel != null) {
            if (!isXml) {
                if (sModel instanceof IDOMModel) {
                    IDOMDocument documentNode = ((IDOMModel) sModel).getDocument();
                    Element docElement = documentNode.getDocumentElement();
                    // $NON-NLS-1$  //$NON-NLS-2$
                    isXml = docElement != null && ((docElement.getNodeName().equals("jsp:root")) || docElement.getAttributeNode("xmlns:jsp") != null || ((((IDOMNode) docElement).getStartStructuredDocumentRegion() == null && ((IDOMNode) docElement).getEndStructuredDocumentRegion() == null)));
                }
            }
        }
    } finally {
        if (sModel != null)
            sModel.releaseFromRead();
    }
    return isXml;
}
Also used : IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) Element(org.w3c.dom.Element) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)

Example 45 with IDOMModel

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

the class StructuredDocumentToDOMUnitTests method testNodeDeletion.

/**
 * Unit test -- tests basic parsing results of inserting a test string
 * into an initial string.
 */
void testNodeDeletion() {
    // String initialString =
    // "<par><x>\ntextx\n</x>\n<y>\ntexty\n</y></par>";
    // String initialString = "<par><x>textx</x><y>texty</y></par>";
    // test cases for two text nodes left together (remove <b/>):
    // first case works, second doesn't
    // String initialString = "<a>a<b /></a>"; // -> <a>a</a>
    // String initialString = "<a>a<b />b</a>"; // -> <a>ab</a>
    // String initialString = getTestString1();
    String initialString = getTestString2();
    // print out what we always can
    System.out.println();
    System.out.println("----------------");
    System.out.println("Test Node Deletion");
    String outString = StringUtils.escape(initialString);
    System.out.println("Initial String: " + outString);
    // always start with fresh model
    IStructuredDocument f = null;
    IModelManager mm = StructuredModelManager.getModelManager();
    try {
        f = mm.createStructuredDocumentFor("dummy.xml", (InputStream) null, null);
    } catch (IOException e) {
    // do nothing, since dummy
    }
    // 
    // we'll listen to structuredDocument events to print out diagnostics
    f.addDocumentChangedListener(this);
    // 
    IDOMModel tree = new DOMModelImpl();
    f.addDocumentChangingListener((IStructuredDocumentListener) tree);
    // set text to structuredDocument (which updates tree)
    f.setText(null, initialString);
    // dump initial structuredDocument
    Debug.dump(f);
    // dump initial dom
    DebugDocument.dump(tree.getDocument());
    // 
    // 
    // makeChange1(tree);
    makeChange2(f);
    // display resulting text
    System.out.println("resultString (from structuredDocument): ");
    System.out.println(StringUtils.escape(f.getText()));
    // 
    // 
    // dump post change structuredDocument
    Debug.dump(f);
    // dump post change DOM
    DebugDocument.dump(tree.getDocument());
// 
}
Also used : InputStream(java.io.InputStream) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IModelManager(org.eclipse.wst.sse.core.internal.provisional.IModelManager) DOMModelImpl(org.eclipse.wst.xml.core.internal.document.DOMModelImpl) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) IOException(java.io.IOException)

Aggregations

IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)433 Document (org.w3c.dom.Document)123 IDOMDocument (org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)120 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)110 Element (org.w3c.dom.Element)109 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)103 Node (org.w3c.dom.Node)57 IFile (org.eclipse.core.resources.IFile)56 NodeList (org.w3c.dom.NodeList)47 IModelManager (org.eclipse.wst.sse.core.internal.provisional.IModelManager)45 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)44 IJsTranslation (org.eclipse.wst.jsdt.web.core.javascript.IJsTranslation)41 JsTranslationAdapter (org.eclipse.wst.jsdt.web.core.javascript.JsTranslationAdapter)41 Text (org.w3c.dom.Text)39 INodeNotifier (org.eclipse.wst.sse.core.internal.provisional.INodeNotifier)35 IJSPTranslation (org.eclipse.jst.jsp.core.internal.java.IJSPTranslation)28 IOException (java.io.IOException)26 CoreException (org.eclipse.core.runtime.CoreException)26 JSPTranslationAdapter (org.eclipse.jst.jsp.core.internal.java.JSPTranslationAdapter)26 INodeAdapter (org.eclipse.wst.sse.core.internal.provisional.INodeAdapter)25