Search in sources :

Example 51 with Element

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));
}
Also used : Inline(com.vaadin.flow.component.page.Inline) TargetElement(com.vaadin.flow.component.page.TargetElement) DocumentType(org.jsoup.nodes.DocumentType) LoggerFactory(org.slf4j.LoggerFactory) Json(elemental.json.Json) JsonArray(elemental.json.JsonArray) Parser(org.jsoup.parser.Parser) JsonValue(elemental.json.JsonValue) WebComponents(com.vaadin.flow.component.WebComponents) PushMode(com.vaadin.flow.shared.communication.PushMode) JsonUtil(elemental.json.impl.JsonUtil) Locale(java.util.Locale) Map(java.util.Map) Element(org.jsoup.nodes.Element) UI(com.vaadin.flow.component.UI) EnumMap(java.util.EnumMap) AnnotationReader(com.vaadin.flow.internal.AnnotationReader) PushConfiguration(com.vaadin.flow.component.PushConfiguration) Tag(org.jsoup.parser.Tag) StandardCharsets(java.nio.charset.StandardCharsets) List(java.util.List) Stream(java.util.stream.Stream) Document(org.jsoup.nodes.Document) LoadMode(com.vaadin.flow.shared.ui.LoadMode) Annotation(java.lang.annotation.Annotation) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) Jsoup(org.jsoup.Jsoup) DataNode(org.jsoup.nodes.DataNode) Elements(org.jsoup.select.Elements) ApplicationConstants(com.vaadin.flow.shared.ApplicationConstants) VaadinUriResolver(com.vaadin.flow.shared.VaadinUriResolver) Dependency(com.vaadin.flow.shared.ui.Dependency) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration) ArrayList(java.util.ArrayList) OutputStreamWriter(java.io.OutputStreamWriter) Properties(java.util.Properties) Logger(org.slf4j.Logger) UTF_8(java.nio.charset.StandardCharsets.UTF_8) BufferedWriter(java.io.BufferedWriter) ReflectTools(com.vaadin.flow.internal.ReflectTools) IOException(java.io.IOException) InputStreamReader(java.io.InputStreamReader) Consumer(java.util.function.Consumer) AtmospherePushConnection(com.vaadin.flow.server.communication.AtmospherePushConnection) JsonObject(elemental.json.JsonObject) BufferedReader(java.io.BufferedReader) UidlWriter(com.vaadin.flow.server.communication.UidlWriter) Collections(java.util.Collections) InputStream(java.io.InputStream) Push(com.vaadin.flow.component.page.Push) Elements(org.jsoup.select.Elements)

Example 52 with Element

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;
}
Also used : DataNode(org.jsoup.nodes.DataNode) TargetElement(com.vaadin.flow.component.page.TargetElement) Element(org.jsoup.nodes.Element)

Example 53 with Element

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;
}
Also used : TemplateParseException(com.vaadin.flow.template.angular.TemplateParseException) Element(org.jsoup.nodes.Element) Elements(org.jsoup.select.Elements)

Example 54 with Element

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));
}
Also used : Element(org.jsoup.nodes.Element) ByteBuffer(java.nio.ByteBuffer)

Example 55 with Element

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()));
}
Also used : LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Comment(org.jsoup.nodes.Comment) Dependency(com.vaadin.flow.shared.ui.Dependency) ArrayList(java.util.ArrayList) Type(com.vaadin.flow.shared.ui.Dependency.Type) Element(org.jsoup.nodes.Element) FilterContext(com.vaadin.flow.server.DependencyFilter.FilterContext) DependencyFilter(com.vaadin.flow.server.DependencyFilter) ReflectionCache(com.vaadin.flow.internal.ReflectionCache) VaadinSession(com.vaadin.flow.server.VaadinSession) Logger(org.slf4j.Logger) AnnotationReader(com.vaadin.flow.internal.AnnotationReader) VaadinServlet(com.vaadin.flow.server.VaadinServlet) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) HtmlImport(com.vaadin.flow.component.dependency.HtmlImport) Node(org.jsoup.nodes.Node) List(java.util.List) Document(org.jsoup.nodes.Document) VaadinService(com.vaadin.flow.server.VaadinService) Optional(java.util.Optional) Jsoup(org.jsoup.Jsoup) InputStream(java.io.InputStream) HtmlImport(com.vaadin.flow.component.dependency.HtmlImport) InputStream(java.io.InputStream) Element(org.jsoup.nodes.Element) VaadinServlet(com.vaadin.flow.server.VaadinServlet) ArrayList(java.util.ArrayList) DependencyFilter(com.vaadin.flow.server.DependencyFilter) Dependency(com.vaadin.flow.shared.ui.Dependency) IOException(java.io.IOException) FilterContext(com.vaadin.flow.server.DependencyFilter.FilterContext)

Aggregations

Element (org.jsoup.nodes.Element)1237 Document (org.jsoup.nodes.Document)559 Elements (org.jsoup.select.Elements)529 ArrayList (java.util.ArrayList)316 IOException (java.io.IOException)220 Test (org.junit.Test)144 ElementHandlerImpl (org.asqatasun.ruleimplementation.ElementHandlerImpl)90 File (java.io.File)87 URL (java.net.URL)82 Matcher (java.util.regex.Matcher)73 List (java.util.List)60 HashMap (java.util.HashMap)57 Pattern (java.util.regex.Pattern)54 Node (org.jsoup.nodes.Node)50 TextNode (org.jsoup.nodes.TextNode)48 InputStream (java.io.InputStream)38 JSONException (org.json.JSONException)36 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)35 Map (java.util.Map)34 JSONObject (org.json.JSONObject)34