Search in sources :

Example 81 with DocumentType

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

the class XMLAssociationProvider method getDoctypeInfo.

public static String[] getDoctypeInfo(Document document) {
    String[] result = null;
    DocumentType doctype = document.getDoctype();
    // so that the implict DTD (if any) can be used
    if (doctype != null && (doctype.getPublicId() != null || doctype.getSystemId() != null)) {
        result = new String[2];
        result[0] = doctype.getPublicId();
        result[1] = doctype.getSystemId();
    } else if (getImplictDoctype(document) != null) {
        result = getImplictDoctype(document);
    }
    return result;
}
Also used : DocumentType(org.w3c.dom.DocumentType)

Example 82 with DocumentType

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

the class XMLAssociationProvider method getCMDocumentReferences.

/**
 * This method returns a list of CMDocumentReferences associated with a particular node or subtree
 */
public List getCMDocumentReferences(Node node, boolean deep) {
    List result = new ArrayList();
    Document document = (node.getNodeType() == Node.DOCUMENT_NODE) ? (Document) node : node.getOwnerDocument();
    DocumentType doctype = document.getDoctype();
    // so that the implict DTD (if any) can be used
    if (doctype != null && (doctype.getPublicId() != null || doctype.getSystemId() != null)) {
        String uri = resolveGrammarURI(document, doctype.getPublicId(), doctype.getSystemId());
        result.add(new CMDocumentReferenceImpl(doctype.getPublicId(), uri));
    } else if (getImplictDoctype(document) != null) {
        String[] implicitDoctype = getImplictDoctype(document);
        String uri = resolveGrammarURI(document, implicitDoctype[0], implicitDoctype[1]);
        result.add(new CMDocumentReferenceImpl(implicitDoctype[0], uri));
    } else {
        NamespaceTable namespaceTable = new NamespaceTable(document);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            namespaceTable.addElement((Element) node);
        }
        if (deep) {
            addChildElementsToNamespaceTable(node, namespaceTable);
        }
        List list = namespaceTable.getNamespaceInfoList();
        for (Iterator i = list.iterator(); i.hasNext(); ) {
            NamespaceInfo info = (NamespaceInfo) i.next();
            String uri = resolveGrammarURI(document, info.uri, info.locationHint);
            result.add(new CMDocumentReferenceImpl(info.uri, uri));
        }
    }
    return result;
}
Also used : NamespaceTable(org.eclipse.wst.xml.core.internal.contentmodel.util.NamespaceTable) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) DocumentType(org.w3c.dom.DocumentType) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) List(java.util.List) Document(org.w3c.dom.Document) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) NamespaceInfo(org.eclipse.wst.xml.core.internal.contentmodel.util.NamespaceInfo)

Example 83 with DocumentType

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

the class ModelQueryActionHelper method getInsertActions.

public void getInsertActions(Document parent, CMDocument cmDocument, int index, int includeOptions, int validityChecking, List actionList) {
    // get the root element and doctype index (if any)
    // 
    int doctypeIndex = -1;
    DocumentType doctype = null;
    Element rootElement = null;
    NodeList nodeList = parent.getChildNodes();
    int nodeListLength = nodeList.getLength();
    for (int i = 0; i < nodeListLength; i++) {
        Node childNode = nodeList.item(i);
        if (childNode.getNodeType() == Node.ELEMENT_NODE) {
            rootElement = (Element) childNode;
            break;
        } else if (childNode.getNodeType() == Node.DOCUMENT_TYPE_NODE) {
            doctype = (DocumentType) childNode;
            doctypeIndex = i;
        }
    }
    // make sure that root elements are only added after the doctype (if any)
    if (rootElement == null && index > doctypeIndex) {
        CMNamedNodeMap map = cmDocument.getElements();
        int mapLength = map.getLength();
        for (int i = 0; i < mapLength; i++) {
            CMNode cmNode = map.item(i);
            boolean canAdd = true;
            if (validityChecking == ModelQuery.VALIDITY_STRICT) {
                canAdd = doctype == null || doctype.getName().equals(cmNode.getNodeName());
            }
            if (canAdd) {
                Action action = new Action(ModelQueryAction.INSERT, parent, cmNode, index, index);
                actionList.add(action);
            }
        }
    }
}
Also used : ModelQueryAction(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQueryAction) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) CMNodeList(org.eclipse.wst.xml.core.internal.contentmodel.CMNodeList) Node(org.w3c.dom.Node) CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode) DocumentType(org.w3c.dom.DocumentType) CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)

Example 84 with DocumentType

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

the class DOMContentBuilderImpl method createDefaultRootContent.

public void createDefaultRootContent(CMDocument cmDocument, CMElementDeclaration rootCMElementDeclaration) throws Exception {
    String grammarFileName = cmDocument.getNodeName();
    if (!supressCreationOfDoctypeAndXMLDeclaration) {
        // TODO cs... investigate to see if this code path is ever used,
        // doesn't seem to be
        // for now I'm setting the encoding to UTF-8 just incase this code
        // path is used somewhere
        // 
        // $NON-NLS-1$
        String piValue = "version=\"1.0\"";
        // $NON-NLS-1$
        String encoding = "UTF-8";
        // $NON-NLS-1$ //$NON-NLS-2$
        piValue += " encoding=\"" + encoding + "\"";
        // $NON-NLS-1$
        ProcessingInstruction pi = document.createProcessingInstruction("xml", piValue);
        document.appendChild(pi);
        // 
        if (// $NON-NLS-1$
        grammarFileName != null && grammarFileName.endsWith("dtd")) {
            DOMImplementation domImpl = document.getImplementation();
            DocumentType documentType = domImpl.createDocumentType(rootCMElementDeclaration.getElementName(), grammarFileName, grammarFileName);
            document.appendChild(documentType);
        }
    }
    // 
    if (// $NON-NLS-1$
    grammarFileName != null && grammarFileName.endsWith("xsd") && namespaceInfoList != null) {
        DOMNamespaceInfoManager manager = new DOMNamespaceInfoManager();
        String name = rootCMElementDeclaration.getNodeName();
        if (namespaceInfoList.size() > 0) {
            NamespaceInfo info = (NamespaceInfo) namespaceInfoList.get(0);
            if (info.prefix != null && info.prefix.length() > 0) {
                // $NON-NLS-1$
                name = info.prefix + ":" + name;
            }
        }
        rootElement = createElement(rootCMElementDeclaration, name, document);
        manager.addNamespaceInfo(rootElement, namespaceInfoList, true);
    }
    createDefaultContent(document, rootCMElementDeclaration);
}
Also used : DOMImplementation(org.w3c.dom.DOMImplementation) DocumentType(org.w3c.dom.DocumentType) ProcessingInstruction(org.w3c.dom.ProcessingInstruction)

Example 85 with DocumentType

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

the class HTMLTagsCompletionProposalComputer method isXHTMLNode.

/**
 * Determine if this Document is an XHTML Document. Operates solely off of
 * the Document Type declaration
 */
private static boolean isXHTMLNode(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) HTMLCMDocument(org.eclipse.wst.html.core.internal.contentmodel.HTMLCMDocument) Document(org.w3c.dom.Document) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) 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