use of org.structr.web.importer.Importer in project structr by structr.
the class ComponentImportVisitor method createComponent.
private void createComponent(final Path file, final String fileName) throws IOException, FrameworkException {
final String name = StringUtils.substringBeforeLast(fileName, ".html");
final DOMNode existingComponent = getExistingComponent(name);
final boolean byId = DeployCommand.isUuid(name);
try (final Tx tx = app.tx(true, false, false)) {
final PropertyMap properties = getPropertiesForComponent(name);
if (properties == null) {
logger.info("Ignoring {} (not in components.json)", fileName);
} else {
if (existingComponent != null) {
final PropertyKey<String> contentKey = StructrApp.key(Template.class, "content");
properties.put(contentKey, existingComponent.getProperty(contentKey));
existingComponent.setOwnerDocument(null);
if (existingComponent instanceof Template) {
properties.put(contentKey, existingComponent.getProperty(contentKey));
existingComponent.setOwnerDocument(null);
} else {
deleteComponent(app, name);
}
}
final String src = new String(Files.readAllBytes(file), Charset.forName("UTF-8"));
boolean visibleToPublic = get(properties, GraphObject.visibleToPublicUsers, false);
boolean visibleToAuth = get(properties, GraphObject.visibleToAuthenticatedUsers, false);
final Importer importer = new Importer(securityContext, src, null, name, visibleToPublic, visibleToAuth);
// enable literal import of href attributes
importer.setIsDeployment(true);
final boolean parseOk = importer.parse(false);
if (parseOk) {
logger.info("Importing component {} from {}..", new Object[] { name, fileName });
// set comment handler that can parse and apply special Structr comments in HTML source files
importer.setCommentHandler(new DeploymentCommentHandler());
// parse page
final ShadowDocument shadowDocument = CreateComponentCommand.getOrCreateHiddenDocument();
final DOMNode rootElement = importer.createComponentChildNodes(shadowDocument);
if (rootElement != null) {
if (byId) {
// set UUID
rootElement.unlockSystemPropertiesOnce();
rootElement.setProperty(GraphObject.id, name);
} else {
// set name
rootElement.setProperty(AbstractNode.name, name);
}
// store properties from components.json if present
rootElement.setProperties(securityContext, properties);
}
}
}
tx.success();
}
}
use of org.structr.web.importer.Importer in project structr by structr.
the class PageImportVisitor method createPage.
private void createPage(final Path file, final String fileName) throws IOException, FrameworkException {
final String name = StringUtils.substringBeforeLast(fileName, ".html");
try (final Tx tx = app.tx(true, false, false)) {
final PropertyMap properties = getPropertiesForPage(name);
if (properties == null) {
logger.info("Ignoring {} (not in pages.json)", fileName);
} else {
final Page existingPage = getExistingPage(name);
if (existingPage != null) {
deletePage(app, name);
}
final String src = new String(Files.readAllBytes(file), Charset.forName("UTF-8"));
final String contentType = get(properties, StructrApp.key(Page.class, "contentType"), "text/html");
boolean visibleToPublic = get(properties, GraphObject.visibleToPublicUsers, false);
boolean visibleToAuth = get(properties, GraphObject.visibleToAuthenticatedUsers, false);
final Importer importer = new Importer(securityContext, src, null, name, visibleToPublic, visibleToAuth);
// enable literal import of href attributes
importer.setIsDeployment(true);
if (StringUtils.startsWithIgnoreCase(src, DoctypeString) && "text/html".equals(contentType)) {
// Import document starts with <!DOCTYPE> definition, so we treat it as an HTML
// document and use the Structr HTML importer.
final boolean parseOk = importer.parse();
if (parseOk) {
logger.info("Importing page {} from {}..", new Object[] { name, fileName });
// set comment handler that can parse and apply special Structr comments in HTML source files
importer.setCommentHandler(new DeploymentCommentHandler());
// parse page
final Page newPage = importer.readPage();
// remove duplicate elements
fixDocumentElements(newPage);
// store properties from pages.json if present
if (properties != null) {
newPage.setProperties(securityContext, properties);
}
}
} else {
// Import document does NOT start with a <!DOCTYPE> definition, so we assume a
// template or shared component that we need to parse.
final boolean parseOk = importer.parse(true);
if (parseOk) {
logger.info("Importing page {} from {}..", new Object[] { name, fileName });
// set comment handler that can parse and apply special Structr comments in HTML source files
importer.setCommentHandler(new DeploymentCommentHandler());
// parse page
final Page newPage = app.create(Page.class, name);
// store properties from pages.json if present
if (properties != null) {
newPage.setProperties(securityContext, properties);
}
// add children
importer.createChildNodes(newPage, newPage);
}
}
}
tx.success();
}
}
Aggregations