Search in sources :

Example 11 with DOMModelImpl

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

the class StructuredDocumentToDOMUnitTests method testNodeDeletion3.

/**
 * Unit test -- tests basic parsing results of inserting a test string
 * into an initial string.
 */
void testNodeDeletion3() {
    String initialString = getTestString3();
    // 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);
    makeChange3(tree);
    // 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)

Example 12 with DOMModelImpl

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

the class TestXPathComputer method createSimpleDocument.

public Document createSimpleDocument() throws Exception {
    IDOMModel domImpl = new DOMModelImpl();
    domImpl.setId("dummy");
    Document document = domImpl.getDocument();
    document.appendChild(document.createElement("test"));
    Element rootelem = document.getDocumentElement();
    Element elem1 = document.createElement("testNode1");
    Element elem2 = document.createElement("testNode1");
    rootelem.appendChild(elem1);
    rootelem.appendChild(elem2);
    return document;
}
Also used : IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) Element(org.w3c.dom.Element) DOMModelImpl(org.eclipse.wst.xml.core.internal.document.DOMModelImpl) Document(org.w3c.dom.Document)

Example 13 with DOMModelImpl

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

the class JSPModelLoader method reInitializeEmbeddedType.

/**
 * This is "reinitialize" since there should always be at least the
 * default one assigned, before we start checking the stream
 */
private void reInitializeEmbeddedType(IStructuredModel model, EmbeddedTypeHandler oldEmbeddedContentType, EmbeddedTypeHandler newEmbeddedContentType) {
    // check program logic
    // $NON-NLS-1$
    Assert.isNotNull(oldEmbeddedContentType, "Program error: invalid call during model initialization");
    // once we know the embedded content type, we need to set it in the
    // PageDirectiveAdapter ... the order of initialization is
    // critical here, the doc must have been created, but its contents not
    // set yet,
    // and all factories must have been set up also.
    IDOMModel domModel = (IDOMModel) model;
    IStructuredDocument structuredDocument = model.getStructuredDocument();
    IDOMDocument document = domModel.getDocument();
    PageDirectiveAdapter pageDirectiveAdapter = (PageDirectiveAdapter) document.getExistingAdapter(PageDirectiveAdapter.class);
    // ==> // PropagatingAdapter propagatingAdapter = (PropagatingAdapter)
    // ((INodeNotifier)
    // document).getExistingAdapter(PropagatingAdapter.class);
    // ==> // ModelQueryAdapter modelQueryAdapter = (ModelQueryAdapter)
    // ((INodeNotifier)
    // document).getExistingAdapter(ModelQueryAdapter.class);
    oldEmbeddedContentType.uninitializeFactoryRegistry(model.getFactoryRegistry());
    oldEmbeddedContentType.uninitializeParser(structuredDocument.getParser());
    // since 'document' is not recreated in this
    // reinit path, we need to remove all adapters,
    // except for the propagated adapters (including page
    // directive adapter, and model query adapter).
    // to accomplish this, we'll just remove all, then
    // add back with a call to pre-load adapt.
    // let clients decide to unload adapters from document
    // Collection oldAdapters = document.getAdapters();
    // Iterator oldAdaptersIterator = oldAdapters.iterator();
    // while (oldAdaptersIterator.hasNext()) {
    // INodeAdapter oldAdapter = (INodeAdapter)
    // oldAdaptersIterator.next();
    // if (oldAdapter != pageDirectiveAdapter && oldAdapter !=
    // propagatingAdapter && oldAdapter != modelQueryAdapter) {
    // // DO NOT remove directly!
    // // can change contents while in notifity loop!
    // //oldAdaptersIterator.remove();
    // document.removeAdapter(oldAdapter);
    // }
    // }
    // DMW: I believe something like the following is needed,
    // since releases cached adapters
    // if (document instanceof DocumentImpl) {
    // ((DocumentImpl) document).releaseDocumentType();
    // ((DocumentImpl) document).releaseStyleSheets();
    // }
    // remember, embedded type factories are automatically cleared when
    // embededType changed
    pageDirectiveAdapter.setEmbeddedType(newEmbeddedContentType);
    // pageDirectiveAdapter.clearPageWatchers();
    if (newEmbeddedContentType != null) {
        // need to null out or else ModelParserAdapter
        // won't get reinitialized
        ((DOMModelImpl) model).setModelParser(null);
        newEmbeddedContentType.initializeFactoryRegistry(model.getFactoryRegistry());
        newEmbeddedContentType.initializeParser(structuredDocument.getParser());
        // partitioner setup is the responsibility of this loader
        IDocumentPartitioner documentPartitioner = structuredDocument.getDocumentPartitioner();
        // added null/type checks for safety.
        if (documentPartitioner != null && documentPartitioner instanceof StructuredTextPartitionerForJSP) {
            if (newEmbeddedContentType.getFamilyId().equals(ContentTypeIdForXML.ContentTypeID_XML)) {
                ((StructuredTextPartitionerForJSP) documentPartitioner).setEmbeddedPartitioner(new StructuredTextPartitionerForXML());
            } else if (newEmbeddedContentType.getFamilyId().equals(ContentTypeIdForHTML.ContentTypeID_HTML)) {
                ((StructuredTextPartitionerForJSP) documentPartitioner).setEmbeddedPartitioner(new StructuredTextPartitionerForHTML());
            }
        }
    }
    // adding language here, in this convienent central
    // location, but some obvious renaming or refactoring
    // wouldn't hurt, in future.
    // I needed to add this language setting for JSP Fragment support
    // Note: this is the one that counts, since at this point,
    // the model has an ID, so we can look up IFile, etc.
    String language = getLanguage(model);
    if (language != null && language.length() > 0) {
        pageDirectiveAdapter.setLanguage(language);
    }
}
Also used : StructuredTextPartitionerForJSP(org.eclipse.jst.jsp.core.internal.text.StructuredTextPartitionerForJSP) StructuredTextPartitionerForXML(org.eclipse.wst.xml.core.internal.text.rules.StructuredTextPartitionerForXML) IDocumentPartitioner(org.eclipse.jface.text.IDocumentPartitioner) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) DOMModelImpl(org.eclipse.wst.xml.core.internal.document.DOMModelImpl) StructuredTextPartitionerForHTML(org.eclipse.wst.html.core.internal.text.StructuredTextPartitionerForHTML) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) PageDirectiveAdapter(org.eclipse.jst.jsp.core.internal.document.PageDirectiveAdapter)

Example 14 with DOMModelImpl

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

the class DOMImplementationTests method testCreateDocument.

public void testCreateDocument() {
    final DOMModelImpl model = (DOMModelImpl) StructuredModelManager.getModelManager().createUnManagedStructuredModelFor(ContentTypeIdForXML.ContentTypeID_XML);
    final DocumentType doctype = model.createDocumentType("bar", "publicTest", "systemTest");
    final Document document = model.createDocument("http://eclipse.org", "foo:bar", doctype);
    assertEquals("Document's doctype was not properly set", doctype, document.getDoctype());
    assertEquals("Document owner node is not set properly", document, doctype.getOwnerDocument());
    final Node node = document.getDocumentElement();
    assertNotNull("Document should not be empty", node);
    assertEquals("Element qualified name is not equal", "foo:bar", node.getNodeName());
    assertEquals("Element namespace URI is not equal", "http://eclipse.org", node.getNamespaceURI());
}
Also used : Node(org.w3c.dom.Node) DOMModelImpl(org.eclipse.wst.xml.core.internal.document.DOMModelImpl) DocumentType(org.w3c.dom.DocumentType) Document(org.w3c.dom.Document) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)

Example 15 with DOMModelImpl

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

the class DOMImplementationTests method testCreateDocumentIllegalChar.

public void testCreateDocumentIllegalChar() {
    final DOMModelImpl model = (DOMModelImpl) StructuredModelManager.getModelManager().createUnManagedStructuredModelFor(ContentTypeIdForXML.ContentTypeID_XML);
    final char[] illegalTest = new char[] { '`', '~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '=', '+', ';', '\'', '\"', ',', '\\', '/', '<', '>', '[', ']', '{', '}', ' ' };
    for (int i = 0; i < illegalTest.length; i++) {
        try {
            model.createDocument("http://eclipse.org", "foo:" + illegalTest[i], null);
        } catch (DOMException e) {
            assertEquals("Did not receive the correct DOMException", DOMException.INVALID_CHARACTER_ERR, e.code);
            continue;
        }
        fail("Illegal character [" + illegalTest[i] + "] allowed in document element qualified name");
    }
    final String[] illegalNames = { " ", "f oo:bar", "foo: bar", "foo:bar ", "" };
    for (int i = 0; i < illegalNames.length; i++) {
        try {
            model.createDocument("http://eclipse.org", illegalNames[i], null);
        } catch (DOMException e) {
            assertEquals("Did not receive the correct DOMException for [" + illegalNames[i] + "]", DOMException.INVALID_CHARACTER_ERR, e.code);
            continue;
        }
        fail("Illegal character [" + illegalNames[i] + "] allowed in document element qualified name");
    }
}
Also used : DOMException(org.w3c.dom.DOMException) DOMModelImpl(org.eclipse.wst.xml.core.internal.document.DOMModelImpl)

Aggregations

DOMModelImpl (org.eclipse.wst.xml.core.internal.document.DOMModelImpl)19 DOMException (org.w3c.dom.DOMException)7 IDOMDocument (org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)6 IOException (java.io.IOException)5 InputStream (java.io.InputStream)5 IModelManager (org.eclipse.wst.sse.core.internal.provisional.IModelManager)5 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)5 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)4 Document (org.w3c.dom.Document)4 Node (org.w3c.dom.Node)3 DocumentType (org.w3c.dom.DocumentType)2 Preferences (org.eclipse.core.runtime.Preferences)1 IDocumentPartitioner (org.eclipse.jface.text.IDocumentPartitioner)1 PageDirectiveAdapter (org.eclipse.jst.jsp.core.internal.document.PageDirectiveAdapter)1 JSPSourceParser (org.eclipse.jst.jsp.core.internal.parser.JSPSourceParser)1 StructuredTextPartitionerForJSP (org.eclipse.jst.jsp.core.internal.text.StructuredTextPartitionerForJSP)1 StructuredTextPartitionerForHTML (org.eclipse.wst.html.core.internal.text.StructuredTextPartitionerForHTML)1 IStructuredCleanupPreferences (org.eclipse.wst.sse.core.internal.cleanup.IStructuredCleanupPreferences)1 StructuredCleanupPreferences (org.eclipse.wst.sse.core.internal.cleanup.StructuredCleanupPreferences)1 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)1