Search in sources :

Example 1 with CDATASection

use of org.w3c.dom.CDATASection in project intellij-community by JetBrains.

the class IntentionDump method main.

@Override
public void main(String[] args) {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    try {
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.newDocument();
        Element intentions = document.createElement("Intentions");
        document.appendChild(intentions);
        while (((IntentionManagerImpl) IntentionManager.getInstance()).hasActiveRequests()) {
            TimeoutUtil.sleep(100);
        }
        for (IntentionActionMetaData actionMetaData : IntentionManagerSettings.getInstance().getMetaData()) {
            Element intention = document.createElement("Intention");
            intention.setAttribute("categories", StringUtil.join(actionMetaData.myCategory, ","));
            intention.setAttribute("name", actionMetaData.getFamily());
            Element description = document.createElement("description");
            CDATASection descriptionSection = document.createCDATASection(escapeCDATA(actionMetaData.getDescription().getText()));
            description.appendChild(descriptionSection);
            intention.appendChild(description);
            TextDescriptor[] beforeDescriptors = actionMetaData.getExampleUsagesBefore();
            if (beforeDescriptors.length > 0) {
                Element before = document.createElement("before");
                CDATASection beforeSection = document.createCDATASection(escapeCDATA(beforeDescriptors[0].getText()));
                before.appendChild(beforeSection);
                intention.appendChild(before);
            }
            TextDescriptor[] afterDescriptors = actionMetaData.getExampleUsagesAfter();
            if (afterDescriptors.length > 0) {
                Element after = document.createElement("after");
                CDATASection afterSection = document.createCDATASection(escapeCDATA(afterDescriptors[0].getText()));
                after.appendChild(afterSection);
                intention.appendChild(after);
            }
            intentions.appendChild(intention);
        }
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        DOMSource source = new DOMSource(document);
        final String path = args.length == 2 ? args[1] : PathManager.getHomePath() + File.separator + "AllIntentions.xml";
        StreamResult console = new StreamResult(new File(path));
        transformer.transform(source, console);
        System.exit(0);
    } catch (ParserConfigurationException | IOException | TransformerException e) {
        // noinspection CallToPrintStackTrace
        e.printStackTrace();
        System.exit(1);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) Element(org.w3c.dom.Element) TextDescriptor(com.intellij.codeInsight.intention.impl.config.TextDescriptor) IOException(java.io.IOException) Document(org.w3c.dom.Document) IntentionManagerImpl(com.intellij.codeInsight.intention.impl.config.IntentionManagerImpl) CDATASection(org.w3c.dom.CDATASection) DocumentBuilder(javax.xml.parsers.DocumentBuilder) IntentionActionMetaData(com.intellij.codeInsight.intention.impl.config.IntentionActionMetaData) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) File(java.io.File) TransformerException(javax.xml.transform.TransformerException)

Example 2 with CDATASection

use of org.w3c.dom.CDATASection in project robovm by robovm.

the class DomTest method testCoalescingOff.

public void testCoalescingOff() throws Exception {
    String xml = "<foo>abc<![CDATA[def]]>ghi</foo>";
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setCoalescing(false);
    document = factory.newDocumentBuilder().parse(new InputSource(new StringReader(xml)));
    Element documentElement = document.getDocumentElement();
    Text abc = (Text) documentElement.getFirstChild();
    assertEquals("abc", abc.getTextContent());
    CDATASection def = (CDATASection) abc.getNextSibling();
    assertEquals("def", def.getTextContent());
    Text ghi = (Text) def.getNextSibling();
    assertEquals("ghi", ghi.getTextContent());
    assertNull(ghi.getNextSibling());
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) CDATASection(org.w3c.dom.CDATASection) Element(org.w3c.dom.Element) StringReader(java.io.StringReader) Text(org.w3c.dom.Text)

Example 3 with CDATASection

use of org.w3c.dom.CDATASection in project robovm by robovm.

the class NodeNormalize method testNormalize.

/**
     * Runs the test case.
     *
     * @throws Throwable
     *             Any uncaught exception causes test to fail
     */
public void testNormalize() throws Throwable {
    Document doc;
    Document newDoc;
    DOMImplementation domImpl;
    DocumentType docTypeNull = null;
    Element documentElement;
    Element element1;
    Element element2;
    Element element3;
    Element element4;
    Element element5;
    Element element6;
    Element element7;
    Text text1;
    Text text2;
    Text text3;
    ProcessingInstruction pi;
    CDATASection cData;
    Comment comment;
    EntityReference entRef;
    NodeList elementList;
    doc = (Document) load("staffNS", builder);
    domImpl = doc.getImplementation();
    newDoc = domImpl.createDocument("http://www.w3.org/DOM/Test", "dom:root", docTypeNull);
    element1 = newDoc.createElement("element1");
    element2 = newDoc.createElement("element2");
    element3 = newDoc.createElement("element3");
    element4 = newDoc.createElement("element4");
    element5 = newDoc.createElement("element5");
    element6 = newDoc.createElement("element6");
    element7 = newDoc.createElement("element7");
    text1 = newDoc.createTextNode("text1");
    text2 = newDoc.createTextNode("text2");
    text3 = newDoc.createTextNode("text3");
    cData = newDoc.createCDATASection("Cdata");
    comment = newDoc.createComment("comment");
    pi = newDoc.createProcessingInstruction("PITarget", "PIData");
    entRef = newDoc.createEntityReference("EntRef");
    assertNotNull("createdEntRefNotNull", entRef);
    documentElement = newDoc.getDocumentElement();
    documentElement.appendChild(element1);
    element2.appendChild(text1);
    element2.appendChild(text2);
    element2.appendChild(text3);
    element1.appendChild(element2);
    text1 = (Text) text1.cloneNode(false);
    text2 = (Text) text2.cloneNode(false);
    element3.appendChild(entRef);
    element3.appendChild(text1);
    element3.appendChild(text2);
    element1.appendChild(element3);
    text1 = (Text) text1.cloneNode(false);
    text2 = (Text) text2.cloneNode(false);
    element4.appendChild(cData);
    element4.appendChild(text1);
    element4.appendChild(text2);
    element1.appendChild(element4);
    text2 = (Text) text2.cloneNode(false);
    text3 = (Text) text3.cloneNode(false);
    element5.appendChild(comment);
    element5.appendChild(text2);
    element5.appendChild(text3);
    element1.appendChild(element5);
    text2 = (Text) text2.cloneNode(false);
    text3 = (Text) text3.cloneNode(false);
    element6.appendChild(pi);
    element6.appendChild(text2);
    element6.appendChild(text3);
    element1.appendChild(element6);
    entRef = (EntityReference) entRef.cloneNode(false);
    text1 = (Text) text1.cloneNode(false);
    text2 = (Text) text2.cloneNode(false);
    text3 = (Text) text3.cloneNode(false);
    element7.appendChild(entRef);
    element7.appendChild(text1);
    element7.appendChild(text2);
    element7.appendChild(text3);
    element1.appendChild(element7);
    elementList = element1.getChildNodes();
    assertEquals("nodeNormalize01_1Bef", 6, elementList.getLength());
    elementList = element2.getChildNodes();
    assertEquals("nodeNormalize01_2Bef", 3, elementList.getLength());
    elementList = element3.getChildNodes();
    assertEquals("nodeNormalize01_3Bef", 3, elementList.getLength());
    elementList = element4.getChildNodes();
    assertEquals("nodeNormalize01_4Bef", 3, elementList.getLength());
    elementList = element5.getChildNodes();
    assertEquals("nodeNormalize01_5Bef", 3, elementList.getLength());
    elementList = element6.getChildNodes();
    assertEquals("nodeNormalize01_6Bef", 3, elementList.getLength());
    elementList = element7.getChildNodes();
    assertEquals("nodeNormalize01_7Bef", 4, elementList.getLength());
    newDoc.normalize();
    elementList = element1.getChildNodes();
    assertEquals("nodeNormalize01_1Aft", 6, elementList.getLength());
    elementList = element2.getChildNodes();
    assertEquals("nodeNormalize01_2Aft", 1, elementList.getLength());
    elementList = element3.getChildNodes();
    assertEquals("nodeNormalize01_3Aft", 2, elementList.getLength());
    elementList = element4.getChildNodes();
    assertEquals("nodeNormalize01_4Aft", 2, elementList.getLength());
    elementList = element5.getChildNodes();
    assertEquals("nodeNormalize01_5Aft", 2, elementList.getLength());
    elementList = element6.getChildNodes();
    assertEquals("nodeNormalize01_6Aft", 2, elementList.getLength());
    elementList = element7.getChildNodes();
    assertEquals("nodeNormalize01_7Aft", 2, elementList.getLength());
}
Also used : Comment(org.w3c.dom.Comment) CDATASection(org.w3c.dom.CDATASection) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) EntityReference(org.w3c.dom.EntityReference) DOMImplementation(org.w3c.dom.DOMImplementation) DocumentType(org.w3c.dom.DocumentType) Text(org.w3c.dom.Text) Document(org.w3c.dom.Document) ProcessingInstruction(org.w3c.dom.ProcessingInstruction)

Example 4 with CDATASection

use of org.w3c.dom.CDATASection in project robovm by robovm.

the class NormalizeTest method testInvalidCharactersCdata.

public void testInvalidCharactersCdata() throws Exception {
    ErrorRecorder errorRecorder = new ErrorRecorder();
    domConfiguration.setParameter("cdata-sections", true);
    domConfiguration.setParameter("error-handler", errorRecorder);
    domConfiguration.setParameter("namespaces", false);
    Element root = document.createElement("foo");
    document.appendChild(root);
    CDATASection cdata = document.createCDATASection("");
    root.appendChild(cdata);
    for (int c = 0; c <= Character.MAX_VALUE; c++) {
        cdata.setData(new String(new char[] { 'A', 'B', (char) c }));
        document.normalizeDocument();
        if (isValid((char) c)) {
            assertEquals(Collections.<DOMError>emptyList(), errorRecorder.errors);
        } else {
            errorRecorder.assertAllErrors("For character " + c, DOMError.SEVERITY_ERROR, "wf-invalid-character");
        }
    }
}
Also used : CDATASection(org.w3c.dom.CDATASection) Element(org.w3c.dom.Element)

Example 5 with CDATASection

use of org.w3c.dom.CDATASection in project robovm by robovm.

the class ImportNode method testImportNode2.

public void testImportNode2() throws Throwable {
    Document doc;
    Document aNewDoc;
    CDATASection cDataSec;
    Node aNode;
    Document ownerDocument;
    DocumentType docType;
    String system;
    String value;
    doc = (Document) load("staffNS", builder);
    aNewDoc = (Document) load("staffNS", builder);
    cDataSec = aNewDoc.createCDATASection("this is CDATASection data");
    aNode = doc.importNode(cDataSec, false);
    ownerDocument = aNode.getOwnerDocument();
    assertNotNull("ownerDocumentNotNull", ownerDocument);
    docType = ownerDocument.getDoctype();
    system = docType.getSystemId();
    assertURIEquals("dtdSystemId", null, null, null, "staffNS.dtd", null, null, null, null, system);
    value = aNode.getNodeValue();
    assertEquals("nodeValue", "this is CDATASection data", value);
}
Also used : CDATASection(org.w3c.dom.CDATASection) Node(org.w3c.dom.Node) DocumentType(org.w3c.dom.DocumentType) Document(org.w3c.dom.Document)

Aggregations

CDATASection (org.w3c.dom.CDATASection)38 Element (org.w3c.dom.Element)19 Node (org.w3c.dom.Node)15 Document (org.w3c.dom.Document)14 NodeList (org.w3c.dom.NodeList)9 Text (org.w3c.dom.Text)9 Comment (org.w3c.dom.Comment)6 File (java.io.File)4 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)4 DocumentBuilder (javax.xml.parsers.DocumentBuilder)3 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)3 Transformer (javax.xml.transform.Transformer)3 NamedNodeMap (org.w3c.dom.NamedNodeMap)3 IOException (java.io.IOException)2 StringReader (java.io.StringReader)2 ArrayList (java.util.ArrayList)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 TransformerException (javax.xml.transform.TransformerException)2 DOMSource (javax.xml.transform.dom.DOMSource)2 StreamResult (javax.xml.transform.stream.StreamResult)2