use of org.jsoup.nodes.Element in project flow by vaadin.
the class BootstrapHandler method handleInitialPageSettings.
private static void handleInitialPageSettings(BootstrapContext context, Element head, InitialPageSettings initialPageSettings) {
if (initialPageSettings.getViewport() != null) {
Elements viewport = head.getElementsByAttributeValue("name", VIEWPORT);
if (!viewport.isEmpty() && viewport.size() == 1) {
viewport.get(0).attr(CONTENT_ATTRIBUTE, initialPageSettings.getViewport());
} else {
head.appendElement(META_TAG).attr("name", VIEWPORT).attr(CONTENT_ATTRIBUTE, initialPageSettings.getViewport());
}
}
initialPageSettings.getInline(InitialPageSettings.Position.PREPEND).stream().map(dependency -> createDependencyElement(context, dependency)).forEach(element -> insertElements(element, head::prependChild));
initialPageSettings.getInline(InitialPageSettings.Position.APPEND).stream().map(dependency -> createDependencyElement(context, dependency)).forEach(element -> insertElements(element, head::appendChild));
initialPageSettings.getElement(InitialPageSettings.Position.PREPEND).forEach(element -> insertElements(element, head::prependChild));
initialPageSettings.getElement(InitialPageSettings.Position.APPEND).forEach(element -> insertElements(element, head::appendChild));
}
use of org.jsoup.nodes.Element in project flow by vaadin.
the class BootstrapHandler method createDependencyElement.
private static Element createDependencyElement(VaadinUriResolver resolver, LoadMode loadMode, JsonObject dependency, Dependency.Type type) {
boolean inlineElement = loadMode == LoadMode.INLINE;
String url = dependency.hasKey(Dependency.KEY_URL) ? resolver.resolveVaadinUri(dependency.getString(Dependency.KEY_URL)) : null;
final Element dependencyElement;
switch(type) {
case STYLESHEET:
dependencyElement = createStylesheetElement(url);
break;
case JAVASCRIPT:
dependencyElement = createJavaScriptElement(url, !inlineElement);
break;
case HTML_IMPORT:
dependencyElement = createHtmlImportElement(url);
break;
default:
throw new IllegalStateException("Unsupported dependency type: " + type);
}
if (inlineElement) {
dependencyElement.appendChild(new DataNode(dependency.getString(Dependency.KEY_CONTENTS), dependencyElement.baseUri()));
}
return dependencyElement;
}
use of org.jsoup.nodes.Element in project flow by vaadin.
the class TemplateParser method getRootElement.
private static Element getRootElement(Document bodyFragment, TemplateResolver templateResolver) {
Elements children = bodyFragment.body().children();
int childNodeSize = children.size();
if (childNodeSize != 1) {
if (childNodeSize == 0) {
throw new TemplateParseException("AngularTemplate must not be empty. " + ROOT_CLARIFICATION);
} else {
throw new TemplateParseException("AngularTemplate must not have multiple root elements. " + ROOT_CLARIFICATION);
}
}
Element rootElement = children.get(0);
populateIncludes(rootElement, templateResolver);
return rootElement;
}
use of org.jsoup.nodes.Element in project flow by vaadin.
the class TemplateParser method parse.
private static TemplateNode parse(Document bodyFragment, TemplateResolver templateResolver) {
Element rootElement = getRootElement(bodyFragment, templateResolver);
byte[] hash = MessageDigestUtil.sha256(rootElement.outerHtml());
// Identity is based on contents for ByteBuffer, but not for byte[]
ByteBuffer key = ByteBuffer.wrap(hash);
return templateCache.computeIfAbsent(key, ignore -> parse(rootElement));
}
use of org.jsoup.nodes.Element in project flow by vaadin.
the class DefaultTemplateParser method getTemplateContent.
@Override
public TemplateData getTemplateContent(Class<? extends PolymerTemplate<?>> clazz, String tag) {
VaadinServlet servlet = VaadinServlet.getCurrent();
boolean logEnabled = LOG_CACHE.get(clazz).compareAndSet(false, true);
List<Dependency> dependencies = AnnotationReader.getAnnotationsFor(clazz, HtmlImport.class).stream().map(htmlImport -> new Dependency(Type.HTML_IMPORT, htmlImport.value(), htmlImport.loadMode())).collect(Collectors.toList());
FilterContext filterContext = new FilterContext(VaadinSession.getCurrent());
for (DependencyFilter filter : VaadinService.getCurrent().getDependencyFilters()) {
dependencies = filter.filter(new ArrayList<>(dependencies), filterContext);
}
for (Dependency dependency : dependencies) {
if (dependency.getType() != Type.HTML_IMPORT) {
continue;
}
String url = dependency.getUrl();
String path = servlet.resolveResource(url);
if (logEnabled) {
getLogger().debug("Html import path '{}' is resolved to '{}'", url, path);
}
try (InputStream content = servlet.getResourceAsStream(path)) {
if (content == null) {
throw new IllegalStateException(String.format("Can't find resource '%s' " + "via the servlet context", url));
}
Element templateElement = parseHtmlImport(content, url, tag);
if (logEnabled && templateElement != null) {
getLogger().debug("Found a template file containing template " + "definition for the tag '{}' by the path '{}'", tag, url);
}
if (templateElement != null) {
return new TemplateData(url, templateElement);
}
} catch (IOException exception) {
// ignore exception on close()
if (logEnabled) {
getLogger().warn("Couldn't close template input stream", exception);
}
}
}
throw new IllegalStateException(String.format("Couldn't find the " + "definition of the element with tag '%s' " + "in any template file declared using @'%s' annotations. " + "Check the availability of the template files in your WAR " + "file or provide alternative implementation of the " + "method getTemplateContent() which should return an element " + "representing the content of the template file", tag, HtmlImport.class.getSimpleName()));
}
Aggregations