Search in sources :

Example 96 with DOMException

use of org.w3c.dom.DOMException in project muikku by otavanopisto.

the class FieldElementsHandler method wrapWithObjectElement.

private Element wrapWithObjectElement(org.w3c.dom.Document ownerDocument, Element[] contents, FieldMeta fieldMeta) {
    ObjectMapper objectMapper = new ObjectMapper();
    Element objectElement = ownerDocument.createElement("object");
    objectElement.setAttribute("type", fieldMeta.getType());
    Element paramTypeElement = ownerDocument.createElement("param");
    paramTypeElement.setAttribute("name", "type");
    paramTypeElement.setAttribute("value", "application/json");
    Element paramContentElement = ownerDocument.createElement("param");
    paramContentElement.setAttribute("name", "content");
    try {
        paramContentElement.setAttribute("value", objectMapper.writeValueAsString(fieldMeta));
    } catch (DOMException | IOException e) {
        e.printStackTrace();
    }
    objectElement.appendChild(paramTypeElement);
    objectElement.appendChild(paramContentElement);
    for (Element content : contents) {
        objectElement.appendChild(content);
    }
    return objectElement;
}
Also used : DOMException(org.w3c.dom.DOMException) Element(org.w3c.dom.Element) IOException(java.io.IOException) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 97 with DOMException

use of org.w3c.dom.DOMException in project muikku by otavanopisto.

the class DeusNexStructureParser method parseBinary.

private Binary parseBinary(Element resourceElement) throws DeusNexSyntaxException, XPathExpressionException, DeusNexInternalException {
    Binary binary = new Binary();
    parseBasicResourceProperties(resourceElement, binary);
    Element contentElement = (Element) DeusNexXmlUtils.findNodeByXPath(resourceElement, "content");
    Node contentChild = contentElement.getFirstChild();
    String textContent = null;
    if (contentChild instanceof CDATASection) {
        textContent = ((CDATASection) contentChild).getTextContent();
    } else if (contentChild instanceof Text) {
        textContent = ((Text) contentChild).getTextContent();
    }
    byte[] content;
    try {
        content = decodeDeusNexBinary(textContent);
    } catch (UnsupportedEncodingException | DOMException e) {
        throw new DeusNexInternalException("Could not retrieve binary content", e);
    }
    binary.setContent(content);
    binary.setContentType(DeusNexXmlUtils.getChildValue(resourceElement, "contentType"));
    return binary;
}
Also used : DOMException(org.w3c.dom.DOMException) CDATASection(org.w3c.dom.CDATASection) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) DeusNexInternalException(fi.otavanopisto.muikku.plugins.dnm.parser.DeusNexInternalException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Text(org.w3c.dom.Text) Binary(fi.otavanopisto.muikku.plugins.dnm.parser.structure.model.Binary)

Example 98 with DOMException

use of org.w3c.dom.DOMException in project structr by structr.

the class HtmlServletObjectResolvingTest method testObjectResolvingInHtmlServlet.

@Test
public void testObjectResolvingInHtmlServlet() {
    final List<String> testObjectIDs = new LinkedList<>();
    try (final Tx tx = app.tx()) {
        // setup three different test objects to be found by HtmlServlet
        testObjectIDs.add(app.create(TestOne.class, new NodeAttribute<>(TestOne.anInt, 123)).getUuid());
        testObjectIDs.add(app.create(TestOne.class, new NodeAttribute<>(TestOne.aDouble, 0.345)).getUuid());
        testObjectIDs.add(app.create(TestOne.class, new NodeAttribute<>(TestOne.aString, "abcdefg")).getUuid());
        // create a page
        final Page newPage = Page.createNewPage(securityContext, "testPage");
        if (newPage != null) {
            Element html = newPage.createElement("html");
            Element head = newPage.createElement("head");
            Element body = newPage.createElement("body");
            Text textNode = newPage.createTextNode("${current.id}");
            try {
                // add HTML element to page
                newPage.appendChild(html);
                html.appendChild(head);
                html.appendChild(body);
                body.appendChild(textNode);
            } catch (DOMException dex) {
                logger.warn("", dex);
                throw new FrameworkException(422, dex.getMessage());
            }
        }
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
    }
    RestAssured.basePath = "/structr/html";
    RestAssured.given().header("X-User", "superadmin").header("X-Password", "sehrgeheim").filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(200)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(201)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(400)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(401)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(403)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(404)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(422)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(500)).expect().response().contentType("text/html").statusCode(200).body(Matchers.containsString(testObjectIDs.get(0))).when().get("/testPage/123");
    RestAssured.given().header("X-User", "superadmin").header("X-Password", "sehrgeheim").filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(200)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(201)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(400)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(401)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(403)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(404)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(422)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(500)).expect().response().statusCode(200).body(Matchers.containsString(testObjectIDs.get(1))).when().get("/testPage/0.345");
    RestAssured.given().header("X-User", "superadmin").header("X-Password", "sehrgeheim").filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(200)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(201)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(400)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(401)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(403)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(404)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(422)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(500)).expect().response().statusCode(200).body(Matchers.containsString(testObjectIDs.get(2))).when().get("/testPage/abcdefg");
}
Also used : DOMException(org.w3c.dom.DOMException) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Element(org.w3c.dom.Element) Page(org.structr.web.entity.dom.Page) Text(org.w3c.dom.Text) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 99 with DOMException

use of org.w3c.dom.DOMException in project structr by structr.

the class SimpleTest method testPagePath.

@Test
public void testPagePath() {
    final String pageName = "page-01";
    try (final Tx tx = app.tx()) {
        Page page = Page.createNewPage(securityContext, pageName);
        assertTrue(page != null);
        assertTrue(page instanceof Page);
        DOMElement html = (DOMElement) page.createElement("html");
        DOMElement head = (DOMElement) page.createElement("head");
        DOMElement body = (DOMElement) page.createElement("body");
        DOMElement title = (DOMElement) page.createElement("title");
        DOMElement div = (DOMElement) page.createElement("div");
        DOMElement div_2 = (DOMElement) page.createElement("div");
        DOMElement div_3 = (DOMElement) page.createElement("div");
        DOMElement h1 = (DOMElement) page.createElement("h1");
        DOMElement h1_2 = (DOMElement) page.createElement("h1");
        try {
            // add HTML element to page
            page.appendChild(html);
            // add HEAD and BODY elements to HTML
            html.appendChild(head);
            html.appendChild(body);
            // add TITLE element to HEAD
            head.appendChild(title);
            title.appendChild(page.createTextNode("Test Page"));
            // add DIVs to BODY
            body.appendChild(div);
            body.appendChild(div_2);
            body.appendChild(div_3);
            // add H1 elements to DIV
            div_3.appendChild(h1);
            div_3.appendChild(h1_2);
            h1.appendChild(page.createTextNode("Page Title"));
        } catch (DOMException dex) {
            throw new FrameworkException(422, dex.getMessage());
        }
        assertEquals(html.getPositionPath(), "/0");
        assertEquals(head.getPositionPath(), "/0/0");
        assertEquals(title.getPositionPath(), "/0/0/0");
        assertEquals(body.getPositionPath(), "/0/1");
        assertEquals(div.getPositionPath(), "/0/1/0");
        assertEquals(div_2.getPositionPath(), "/0/1/1");
        assertEquals(div_3.getPositionPath(), "/0/1/2");
        assertEquals(h1.getPositionPath(), "/0/1/2/0");
        assertEquals(h1_2.getPositionPath(), "/0/1/2/1");
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception");
    }
}
Also used : DOMException(org.w3c.dom.DOMException) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Page(org.structr.web.entity.dom.Page) DOMElement(org.structr.web.entity.dom.DOMElement) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 100 with DOMException

use of org.w3c.dom.DOMException in project structr by structr.

the class XPathTest method testSimpleXPath.

@Test
public void testSimpleXPath() {
    final String pageName = "page-01";
    try (final Tx tx = app.tx()) {
        Page page = Page.createNewPage(securityContext, pageName);
        assertTrue(page != null);
        assertTrue(page instanceof Page);
        DOMElement html = (DOMElement) page.createElement("html");
        DOMElement head = (DOMElement) page.createElement("head");
        DOMElement body = (DOMElement) page.createElement("body");
        DOMElement title = (DOMElement) page.createElement("title");
        DOMElement h1 = (DOMElement) page.createElement("h1");
        try {
            // add HTML element to page
            page.appendChild(html);
            // add HEAD and BODY elements to HTML
            html.appendChild(head);
            html.appendChild(body);
            // add TITLE element to HEAD
            head.appendChild(title);
            title.appendChild(page.createTextNode("Test Page"));
            // add H1 element to BODY
            body.appendChild(h1);
            h1.appendChild(page.createTextNode("Page Title"));
        } catch (DOMException dex) {
            throw new FrameworkException(422, dex.getMessage());
        }
        assertEquals(html, page.getChildNodes().item(1));
        assertEquals(head, html.getChildNodes().item(0));
        assertEquals(body, html.getChildNodes().item(1));
        // test XPath support of structr nodes..
        XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();
        // let xpath cache first..
        assertEquals("Page Title", xpath.evaluate("/html/body/h1/text()", page, XPathConstants.STRING));
        assertEquals(h1, xpath.evaluate("/html/body/h1", page, XPathConstants.NODE));
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception");
    } catch (XPathExpressionException xpeex) {
        logger.warn("", xpeex);
        fail("Unexpected exception");
    }
}
Also used : XPath(javax.xml.xpath.XPath) DOMException(org.w3c.dom.DOMException) XPathFactory(javax.xml.xpath.XPathFactory) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) XPathExpressionException(javax.xml.xpath.XPathExpressionException) Page(org.structr.web.entity.dom.Page) DOMElement(org.structr.web.entity.dom.DOMElement) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Aggregations

DOMException (org.w3c.dom.DOMException)323 Document (org.w3c.dom.Document)165 Element (org.w3c.dom.Element)131 Node (org.w3c.dom.Node)73 NodeList (org.w3c.dom.NodeList)57 DocumentBuilder (javax.xml.parsers.DocumentBuilder)42 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)42 Attr (org.w3c.dom.Attr)40 DOMImplementation (org.w3c.dom.DOMImplementation)37 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)28 FrameworkException (org.structr.common.error.FrameworkException)27 IOException (java.io.IOException)26 NamedNodeMap (org.w3c.dom.NamedNodeMap)25 DocumentType (org.w3c.dom.DocumentType)19 ArrayList (java.util.ArrayList)17 Text (org.w3c.dom.Text)17 DOMNode (org.structr.web.entity.dom.DOMNode)16 XPathExpressionException (javax.xml.xpath.XPathExpressionException)15 SAXException (org.xml.sax.SAXException)13 TransformerException (javax.xml.transform.TransformerException)12