Search in sources :

Example 1 with HtmlImport

use of com.vaadin.flow.component.dependency.HtmlImport 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)

Example 2 with HtmlImport

use of com.vaadin.flow.component.dependency.HtmlImport in project flow by vaadin.

the class ComponentGenerator method addClassAnnotations.

private void addClassAnnotations(ComponentMetadata metadata, JavaClassSource javaClass) {
    Properties properties = getProperties("version.prop");
    String generator = String.format("Generator: %s#%s", ComponentGenerator.class.getName(), properties.getProperty("generator.version"));
    String webComponent = String.format("WebComponent: %s#%s", metadata.getName(), metadata.getVersion());
    String flow = String.format("Flow#%s", properties.getProperty("flow.version"));
    String[] generatedValue = new String[] { generator, webComponent, flow };
    javaClass.addAnnotation(Generated.class).setStringArrayValue(generatedValue);
    javaClass.addAnnotation(Tag.class).setStringValue(metadata.getTag());
    String importPath = metadata.getBaseUrl().replace("\\", "/");
    if (importPath.startsWith("/")) {
        importPath = importPath.substring(1);
    }
    String htmlImport = String.format("frontend://%s%s", frontendDirectory, importPath);
    javaClass.addAnnotation(HtmlImport.class).setStringValue(htmlImport);
}
Also used : HtmlImport(com.vaadin.flow.component.dependency.HtmlImport) Generated(javax.annotation.Generated) Tag(com.vaadin.flow.component.Tag) Properties(java.util.Properties)

Example 3 with HtmlImport

use of com.vaadin.flow.component.dependency.HtmlImport in project flow by vaadin.

the class BootstrapUtils method getThemeSettings.

static Map<TargetElement, List<JsonObject>> getThemeSettings(BootstrapHandler.BootstrapContext context) {
    Optional<Theme> themeAnnotation = context.getPageConfigurationAnnotation(Theme.class);
    if (themeAnnotation.isPresent()) {
        Map<TargetElement, List<JsonObject>> themeContents = new EnumMap<>(TargetElement.class);
        Class<? extends AbstractTheme> themeClass = themeAnnotation.get().value();
        AbstractTheme theme = ReflectTools.createInstance(themeClass);
        if (!context.isProductionMode()) {
            List<JsonObject> head = Stream.of(themeClass.getAnnotationsByType(HtmlImport.class)).map(HtmlImport::value).map(url -> createImportLink(context.getUriResolver(), url)).map(BootstrapUtils::createInlineDependencyObject).collect(Collectors.toList());
            themeContents.put(TargetElement.HEAD, head);
        }
        List<JsonObject> body = theme.getBodyInlineContents().stream().map(BootstrapUtils::createInlineDependencyObject).collect(Collectors.toList());
        themeContents.put(TargetElement.BODY, body);
        return themeContents;
    }
    return Collections.emptyMap();
}
Also used : HtmlImport(com.vaadin.flow.component.dependency.HtmlImport) AbstractTheme(com.vaadin.flow.theme.AbstractTheme) Theme(com.vaadin.flow.theme.Theme) AbstractTheme(com.vaadin.flow.theme.AbstractTheme) JsonObject(elemental.json.JsonObject) List(java.util.List) EnumMap(java.util.EnumMap) TargetElement(com.vaadin.flow.component.page.TargetElement)

Example 4 with HtmlImport

use of com.vaadin.flow.component.dependency.HtmlImport in project flow by vaadin.

the class UidlWriterTest method parentViewDependenciesAreAddedFirst.

@Test
public void parentViewDependenciesAreAddedFirst() {
    UI ui = initializeUIForDependenciesTest(new UI());
    UidlWriter uidlWriter = new UidlWriter();
    ui.add(new BaseClass());
    JsonObject response = uidlWriter.createUidl(ui, false);
    assertFalse("Did not expect to have lazy dependencies in uidl", response.hasKey(LoadMode.LAZY.name()));
    assertFalse("Did not expect to have inline dependencies in uidl", response.hasKey(LoadMode.INLINE.name()));
    assertTrue("Expected to have eager dependencies in uidl", response.hasKey(LoadMode.EAGER.name()));
    JsonArray eagerDependencies = response.getArray(LoadMode.EAGER.name());
    assertEquals("Expected to have exactly 3 eager dependencies in uidl, actual: %d", eagerDependencies.length(), 3);
    List<Class<?>> expectedClassOrder = Arrays.asList(SuperParentClass.class, ParentClass.class, BaseClass.class);
    for (int i = 0; i < expectedClassOrder.size(); i++) {
        Class<?> expectedClass = expectedClassOrder.get(i);
        HtmlImport htmlImport = expectedClass.getAnnotation(HtmlImport.class);
        JsonValue actualDependency = eagerDependencies.get(i);
        JsonObject expectedDependency = new Dependency(Dependency.Type.HTML_IMPORT, htmlImport.value(), htmlImport.loadMode()).toJson();
        assertTrue(String.format("Unexpected dependency. Expected: '%s', actual: '%s', class: '%s'", expectedDependency, actualDependency, expectedClass), expectedDependency.jsEquals(actualDependency));
    }
}
Also used : JsonArray(elemental.json.JsonArray) HtmlImport(com.vaadin.flow.component.dependency.HtmlImport) UI(com.vaadin.flow.component.UI) JsonValue(elemental.json.JsonValue) JsonObject(elemental.json.JsonObject) Dependency(com.vaadin.flow.shared.ui.Dependency) Test(org.junit.Test)

Aggregations

HtmlImport (com.vaadin.flow.component.dependency.HtmlImport)4 Dependency (com.vaadin.flow.shared.ui.Dependency)2 JsonObject (elemental.json.JsonObject)2 List (java.util.List)2 Tag (com.vaadin.flow.component.Tag)1 UI (com.vaadin.flow.component.UI)1 TargetElement (com.vaadin.flow.component.page.TargetElement)1 AnnotationReader (com.vaadin.flow.internal.AnnotationReader)1 ReflectionCache (com.vaadin.flow.internal.ReflectionCache)1 DependencyFilter (com.vaadin.flow.server.DependencyFilter)1 FilterContext (com.vaadin.flow.server.DependencyFilter.FilterContext)1 VaadinService (com.vaadin.flow.server.VaadinService)1 VaadinServlet (com.vaadin.flow.server.VaadinServlet)1 VaadinSession (com.vaadin.flow.server.VaadinSession)1 Type (com.vaadin.flow.shared.ui.Dependency.Type)1 AbstractTheme (com.vaadin.flow.theme.AbstractTheme)1 Theme (com.vaadin.flow.theme.Theme)1 JsonArray (elemental.json.JsonArray)1 JsonValue (elemental.json.JsonValue)1 IOException (java.io.IOException)1