Search in sources :

Example 86 with DocumentType

use of org.w3c.dom.DocumentType in project webtools.sourceediting by eclipse.

the class HTMLDocumentTypeAdapter method documentTypeChanged.

/**
 */
private void documentTypeChanged() {
    IDOMDocument document = getDocument();
    if (document == null)
        // error
        return;
    IDOMModel model = document.getModel();
    if (model == null)
        // error
        return;
    IFile file = getFile(model);
    // find DOCTYPE delcaration and Public ID
    String publicId = null;
    String systemId = null;
    DocumentType newDocumentType = findDocumentType(document);
    if (newDocumentType != null) {
        publicId = newDocumentType.getPublicId();
        systemId = newDocumentType.getSystemId();
    } else {
        // lookup default set by contentsettings
        publicId = HTMLContentProperties.getProperty(HTMLContentProperties.DOCUMENT_TYPE, file, true);
    }
    // lookup DOCTYPE registry
    HTMLDocumentTypeEntry newEntry = null;
    if (publicId != null) {
        newEntry = HTMLDocumentTypeRegistry.getInstance().getEntry(publicId);
    } else if (systemId == null) {
        newEntry = HTMLDocumentTypeRegistry.getInstance().getDefaultEntry(HTMLDocumentTypeRegistry.DEFAULT_HTML5);
    }
    boolean newXMLType = (newEntry != null ? newEntry.isXMLType() : false);
    boolean newWMLType = (newEntry != null ? newEntry.isWMLType() : false);
    if (!newXMLType) {
        // find XML declaration
        if (findXMLNode(document) != null) {
            newXMLType = true;
        }
        // check file extension
        if (file != null) {
            String ext = file.getFileExtension();
            if (ext != null && ext.equalsIgnoreCase(XHTML)) {
                newXMLType = true;
            }
            if (ext != null && ext.equalsIgnoreCase(WML)) {
                newXMLType = true;
                newWMLType = true;
            }
        }
    }
    if (newEntry == null) {
        // lookup system default
        if (newXMLType && newDocumentType == null) {
            // declared
            if (newWMLType)
                newEntry = HTMLDocumentTypeRegistry.getInstance().getDefaultEntry(HTMLDocumentTypeRegistry.DEFAULT_WML);
            else
                newEntry = HTMLDocumentTypeRegistry.getInstance().getDefaultEntry(HTMLDocumentTypeRegistry.DEFAULT_XHTML);
        } else {
            newEntry = HTMLDocumentTypeRegistry.getInstance().getDefaultEntry(HTMLDocumentTypeRegistry.DEFAULT_HTML);
        }
        if (newEntry == null)
            // error
            return;
    }
    if (newDocumentType == null) {
        DocumentType oldDocumentType = getDocumentType();
        if (oldDocumentType == null || oldDocumentType.getName() != newEntry.getName()) {
            // create implicit DocumentType
            DOMImplementation impl = document.getImplementation();
            if (impl != null) {
                String name = newEntry.getName();
                publicId = newEntry.getPublicId();
                systemId = newEntry.getSystemId();
                newDocumentType = impl.createDocumentType(name, publicId, systemId);
            }
        }
    }
    boolean notify = false;
    if (this.entry != null) {
        // do not notify on initialization
        notify = (newEntry != this.entry || newXMLType != this.isXMLType);
    }
    if (newDocumentType != null)
        setDocumentType(newDocumentType);
    this.entry = newEntry;
    this.isXMLType = newXMLType;
    if (notify)
        notifyDocumentTypeChanged();
}
Also used : IFile(org.eclipse.core.resources.IFile) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IDOMDocumentType(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocumentType) DocumentType(org.w3c.dom.DocumentType) DOMImplementation(org.w3c.dom.DOMImplementation) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)

Example 87 with DocumentType

use of org.w3c.dom.DocumentType 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 88 with DocumentType

use of org.w3c.dom.DocumentType in project webtools.sourceediting by eclipse.

the class DOMImplementationTests method testCreateDocumentUsedDoctype.

public void testCreateDocumentUsedDoctype() {
    final DOMModelImpl model = (DOMModelImpl) StructuredModelManager.getModelManager().createUnManagedStructuredModelFor(ContentTypeIdForXML.ContentTypeID_XML);
    final DocumentType doctype = model.createDocumentType("bar", "publicTest", "systemTest");
    IDOMDocument document = model.getDocument();
    document.appendChild(doctype);
    try {
        model.createDocument("http://eclipse.org", "foo:bar", doctype);
    } catch (DOMException e) {
        assertEquals("Wrong DOMExcetion thrown", DOMException.WRONG_DOCUMENT_ERR, e.code);
        return;
    }
    fail("Reusing the doctype from another document should have caused an exception");
}
Also used : DOMException(org.w3c.dom.DOMException) DOMModelImpl(org.eclipse.wst.xml.core.internal.document.DOMModelImpl) DocumentType(org.w3c.dom.DocumentType) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)

Example 89 with DocumentType

use of org.w3c.dom.DocumentType in project webtools.sourceediting by eclipse.

the class DeploymentDescriptorPropertyCache method _parseDocument.

private void _parseDocument(IPath path, Float[] version, List groupList, List urlPatterns, SubProgressMonitor subMonitor, Document document) {
    Element webapp = document.getDocumentElement();
    if (webapp != null) {
        if (webapp.getTagName().equals(WEB_APP_ELEMENT_NAME) || webapp.getNodeName().endsWith(WEB_APP_ELEMENT_LOCAL_NAME)) {
            // this convention only started with 2.4?
            if (webapp.hasAttribute(WEB_APP_VERSION_NAME)) {
                String versionValue = webapp.getAttribute(WEB_APP_VERSION_NAME);
                versionValue = versionValue.trim();
                if (versionValue.length() > 0) {
                    try {
                        version[0] = Float.valueOf(versionValue);
                    } catch (NumberFormatException e) {
                    // doesn't matter
                    }
                }
            }
            if (version[0] == null) {
                // try determining the version from the doctype reference
                DocumentType doctype = document.getDoctype();
                if (doctype != null) {
                    String systemId = doctype.getSystemId();
                    String publicId = doctype.getPublicId();
                    if ((systemId != null && systemId.endsWith("web-app_2_3.dtd")) || (publicId != null && publicId.indexOf("Web Application 2.3") > 0)) {
                        // $NON-NLS-1$ //$NON-NLS-2$
                        version[0] = new Float(2.3);
                    } else if ((systemId != null && systemId.endsWith("web-app_2_2.dtd")) || (publicId != null && publicId.indexOf("Web Application 2.2") > 0)) {
                        // $NON-NLS-1$ //$NON-NLS-2$
                        version[0] = new Float(2.2);
                    } else if ((systemId != null && systemId.endsWith("web-app_2_1.dtd")) || (publicId != null && publicId.indexOf("Web Application 2.1") > 0)) {
                        // $NON-NLS-1$ //$NON-NLS-2$
                        version[0] = new Float(2.1);
                    }
                }
            }
        }
    }
    NodeList propertyGroupElements = document.getElementsByTagName(JSP_PROPERTY_GROUP);
    int length = propertyGroupElements.getLength();
    // $NON-NLS-1$
    subMonitor.beginTask("Reading Property Groups", length);
    for (int i = 0; i < length; i++) {
        PropertyGroup group = PropertyGroup.createFrom(path, propertyGroupElements.item(i), i);
        subMonitor.worked(1);
        if (group != null) {
            groupList.add(group);
        }
    }
    // 325554 : only apply to URL patterns for Servlet mappings
    NodeList urlPatternElements = document.getElementsByTagName(URL_PATTERN);
    int urlPatternElementCount = urlPatternElements.getLength();
    for (int i = 0; i < urlPatternElementCount; i++) {
        Node urlPatternElement = urlPatternElements.item(i);
        if (SERVLET_MAPPING.equals(urlPatternElement.getParentNode().getNodeName())) {
            String urlPattern = getContainedText(urlPatternElement);
            if (urlPattern != null && urlPattern.length() > 0) {
                urlPatterns.add(new StringMatcher(urlPattern));
            }
        }
    }
}
Also used : Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) DocumentType(org.w3c.dom.DocumentType)

Example 90 with DocumentType

use of org.w3c.dom.DocumentType in project webtools.sourceediting by eclipse.

the class HTMLContentAssistProcessor method getXHTML.

/**
 * Determine if this Document is an XHTML Document. Oprates solely off of
 * the Document Type declaration
 */
protected boolean getXHTML(Node node) {
    if (node == null)
        return false;
    Document doc = null;
    if (node.getNodeType() != Node.DOCUMENT_NODE)
        doc = node.getOwnerDocument();
    else
        doc = ((Document) node);
    if (doc instanceof IDOMDocument)
        return ((IDOMDocument) doc).isXMLType();
    if (doc instanceof INodeNotifier) {
        ModelQueryAdapter adapter = (ModelQueryAdapter) ((INodeNotifier) doc).getAdapterFor(ModelQueryAdapter.class);
        CMDocument cmdoc = null;
        if (adapter != null && adapter.getModelQuery() != null)
            cmdoc = adapter.getModelQuery().getCorrespondingCMDocument(doc);
        if (cmdoc != null) {
            // model
            if (cmdoc instanceof HTMLCMDocument)
                return false;
            if (cmdoc.supports(HTMLCMProperties.IS_XHTML))
                return Boolean.TRUE.equals(cmdoc.getProperty(HTMLCMProperties.IS_XHTML));
        }
    }
    // this should never be reached
    DocumentType docType = doc.getDoctype();
    // $NON-NLS-1$
    return docType != null && docType.getPublicId() != null && docType.getPublicId().indexOf("-//W3C//DTD XHTML ") == 0;
}
Also used : HTMLCMDocument(org.eclipse.wst.html.core.internal.contentmodel.HTMLCMDocument) CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) ModelQueryAdapter(org.eclipse.wst.xml.core.internal.ssemodelquery.ModelQueryAdapter) HTMLCMDocument(org.eclipse.wst.html.core.internal.contentmodel.HTMLCMDocument) DocumentType(org.w3c.dom.DocumentType) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) Document(org.w3c.dom.Document) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) HTMLCMDocument(org.eclipse.wst.html.core.internal.contentmodel.HTMLCMDocument) CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) INodeNotifier(org.eclipse.wst.sse.core.internal.provisional.INodeNotifier)

Aggregations

DocumentType (org.w3c.dom.DocumentType)107 Document (org.w3c.dom.Document)68 DOMImplementation (org.w3c.dom.DOMImplementation)34 Node (org.w3c.dom.Node)21 DOMException (org.w3c.dom.DOMException)19 Element (org.w3c.dom.Element)16 NamedNodeMap (org.w3c.dom.NamedNodeMap)14 NodeList (org.w3c.dom.NodeList)12 ArrayList (java.util.ArrayList)10 IDOMDocument (org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)8 Entity (org.w3c.dom.Entity)8 IOException (java.io.IOException)7 CMDocument (org.eclipse.wst.xml.core.internal.contentmodel.CMDocument)6 DocumentBuilder (javax.xml.parsers.DocumentBuilder)5 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)4 Transformer (javax.xml.transform.Transformer)4 TransformerFactory (javax.xml.transform.TransformerFactory)4 DOMSource (javax.xml.transform.dom.DOMSource)4 StreamResult (javax.xml.transform.stream.StreamResult)4 Attr (org.w3c.dom.Attr)4