use of org.structr.web.importer.Importer in project structr by structr.
the class DeploymentTest method test34WidgetWithTemplate.
@Test
public void test34WidgetWithTemplate() {
// setup
try (final Tx tx = app.tx()) {
final Page testPage = Page.createNewPage(securityContext, "WidgetTestPage");
final Html html = createElement(testPage, testPage, "html");
final Head head = createElement(testPage, html, "head");
final Body body = createElement(testPage, html, "body");
final Div div = createElement(testPage, body, "div");
final Div div2 = createElement(testPage, body, "div");
div.setProperty(AbstractNode.name, "WidgetTestPage-Div");
div2.setProperty(AbstractNode.name, "WidgetTestPage-Div2");
Widget widgetToImport = app.create(Widget.class, new NodeAttribute<>(StructrApp.key(Widget.class, "name"), "TestWidget"), new NodeAttribute<>(StructrApp.key(Widget.class, "source"), "<!-- @structr:content(text/html) --><structr:template>${{Structr.print(\"<div>Test</div>\");}}</structr:template>"), new NodeAttribute<>(StructrApp.key(Widget.class, "configuration"), "{\"processDeploymentInfo\": true}"), new NodeAttribute<>(StructrApp.key(Widget.class, "visibleToPublicUsers"), true), new NodeAttribute<>(StructrApp.key(Widget.class, "visibleToAuthenticatedUsers"), true));
Importer importer = new Importer(securityContext, widgetToImport.getProperty(new StringProperty("source")), null, null, true, true);
importer.setIsDeployment(true);
importer.setCommentHandler(new DeploymentCommentHandler());
importer.parse(true);
DOMNode template = importer.createComponentChildNodes(div, testPage);
div.appendChild(template);
makePublic(testPage, html, head, body, div, div2, template);
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
// test
try (final Tx tx = app.tx()) {
Div div = (Div) app.nodeQuery().andName("WidgetTestPage-Div").getFirst();
assertEquals(1, div.treeGetChildCount());
Object obj = div.treeGetFirstChild();
assertTrue(Template.class.isAssignableFrom(obj.getClass()));
Template template = (Template) obj;
assertEquals("${{Structr.print(\"<div>Test</div>\");}}", template.getTextContent());
assertEquals("text/html", template.getContentType());
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
}
use of org.structr.web.importer.Importer in project structr by structr.
the class ImporterTest method testImportWidget.
private String testImportWidget(final String code, final RenderContext.EditMode editMode, final String address) {
String sourceHtml = null;
try {
// render page into HTML string
try (final Tx tx = app.tx()) {
final Importer importer = new Importer(securityContext, code, address, "widget", true, true);
importer.parse(true);
// create page from source
final Page sourcePage = importer.readPage();
sourceHtml = sourcePage.getContent(editMode);
tx.success();
}
} catch (Throwable t) {
logger.warn("", t);
}
return sourceHtml;
}
use of org.structr.web.importer.Importer in project structr by structr.
the class ImportCommand method processMessage.
@Override
public void processMessage(final WebSocketMessage webSocketData) {
final SecurityContext securityContext = getWebSocket().getSecurityContext();
final Map<String, Object> properties = webSocketData.getNodeData();
final String code = (String) properties.get("code");
final String address = (String) properties.get("address");
final String name = (String) properties.get("name");
final boolean publicVisible = (Boolean) properties.get("publicVisible");
final boolean authVisible = (Boolean) properties.get("authVisible");
final boolean processDeploymentInfo = (Boolean) properties.get("processDeploymentInfo");
try {
final Importer pageImporter = new Importer(securityContext, code, address, name, publicVisible, authVisible);
if (processDeploymentInfo) {
pageImporter.setIsDeployment(true);
pageImporter.setCommentHandler(new DeploymentCommentHandler());
}
final boolean parseOk = pageImporter.parse();
if (parseOk) {
if (address != null) {
logger.info("Successfully parsed {}", address);
getWebSocket().send(MessageBuilder.status().code(200).message("Successfully parsed address " + address).build(), true);
}
String pageId = pageImporter.readPage().getUuid();
Map<String, Object> resultData = new HashMap();
if (pageId != null) {
resultData.put("id", pageId);
getWebSocket().send(MessageBuilder.status().code(200).message("Successfully created page " + name).data(resultData).build(), true);
} else {
getWebSocket().send(MessageBuilder.status().code(400).message("Error while creating page " + name).data(resultData).build(), true);
}
}
} catch (FrameworkException fex) {
logger.warn("Error while importing content", fex);
getWebSocket().send(MessageBuilder.status().code(fex.getStatus()).message(fex.getMessage()).build(), true);
}
}
use of org.structr.web.importer.Importer in project structr by structr.
the class Widget method expandWidget.
public static void expandWidget(final SecurityContext securityContext, final Page page, final DOMNode parent, final String baseUrl, final Map<String, Object> parameters, final boolean processDeploymentInfo) throws FrameworkException {
String _source = (String) parameters.get("source");
ErrorBuffer errorBuffer = new ErrorBuffer();
if (_source == null) {
errorBuffer.add(new EmptyPropertyToken(Widget.class.getSimpleName(), source));
} else {
// check source for mandatory parameters
Matcher matcher = threadLocalTemplateMatcher.get();
// initialize with source
matcher.reset(_source);
while (matcher.find()) {
final String group = matcher.group();
final String source = group.substring(1, group.length() - 1);
final ReplacementInfo info = new ReplacementInfo(source);
String key = info.getKey();
Object value = parameters.get(key);
if (value != null) {
// replace and restart matching process
_source = _source.replace(group, value.toString());
matcher.reset(_source);
}
}
}
if (!errorBuffer.hasError()) {
Importer importer = new Importer(securityContext, _source, baseUrl, null, false, false);
if (processDeploymentInfo) {
importer.setIsDeployment(true);
importer.setCommentHandler(new DeploymentCommentHandler());
}
importer.parse(true);
importer.createChildNodes(parent, page, true);
} else {
// report error to ui
throw new FrameworkException(422, "Unable to import the given source code", errorBuffer);
}
}
use of org.structr.web.importer.Importer in project structr by structr.
the class ImporterTest method testImport.
private String testImport(final String address, final RenderContext.EditMode editMode) {
String sourceHtml = null;
try {
// render page into HTML string
try (final Tx tx = app.tx()) {
final Importer importer = new Importer(securityContext, null, address, "testpage", true, true);
importer.parse();
// create page from source
final Page sourcePage = importer.readPage();
sourceHtml = sourcePage.getContent(editMode);
tx.success();
}
} catch (Throwable t) {
logger.warn("", t);
}
return sourceHtml;
}
Aggregations