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()));
}
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);
}
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();
}
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));
}
}
Aggregations