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;
}
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;
}
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");
}
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");
}
}
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");
}
}
Aggregations