public static void revisit(Element element, DomRevisitor visitor) { if (visitor.visit(element)) { NodeList childNodes = element.getChildNodes(); for (int idx = 0; idx < childNodes.getLength(); idx++) { Node childNode = childNodes.item(idx); if (childNode.getNodeType() == Node.ELEMENT_NODE) { revisit((Element) childNode, visitor); } } } visitor.afterVisit(element); }
/** * Called to traverse and visit the tree of {@link org.w3c.dom.Element}s. * @param element the root of the tree to traverse and visit * @param visitor the visitor to be called on each of the nodes. */ public static void visit(Element element, DomVisitor visitor) { if (!visitor.visit(element)) return; NodeList childNodes = element.getChildNodes(); for (int idx = 0; idx < childNodes.getLength(); idx++) { Node childNode = childNodes.item(idx); if (childNode.getNodeType() == Node.ELEMENT_NODE) { visit((Element) childNode, visitor); } } }
public static void revisit(Element element, DomRevisitor visitor) { if (visitor.visit(element)) { NodeList childNodes = element.getChildNodes(); for (int idx = 0; idx < childNodes.getLength(); idx++) { Node childNode = childNodes.item(idx); if (childNode.getNodeType() == Node.ELEMENT_NODE) { revisit((Element) childNode, visitor); } } } visitor.afterVisit(element); }
/** * Called to traverse and visit the tree of {@link org.w3c.dom.Element}s. * @param element the root of the tree to traverse and visit * @param visitor the visitor to be called on each of the nodes. */ public static void visit(Element element, DomVisitor visitor) { if (!visitor.visit(element)) return; NodeList childNodes = element.getChildNodes(); for (int idx = 0; idx < childNodes.getLength(); idx++) { Node childNode = childNodes.item(idx); if (childNode.getNodeType() == Node.ELEMENT_NODE) { visit((Element) childNode, visitor); } } }
@Override public void callback(final Composite beanInstance) { /* * Only translate parent-less widgets to avoid re-translating a single widget multiple * times (the call to revisit will traverse the whole subtree rooted at this widget). */ if (beanInstance.getParent() == null) DomVisit.revisit(new ElementWrapper(beanInstance.getElement()), new TranslationDomRevisitor()); } });
public Document visitTemplate(URL template) { final Document document = parseTemplate(template); for (int i = 0; i < document.getChildNodes().getLength(); i++) { final Node node = document.getChildNodes().item(i); if (node instanceof Element) { DomVisit.visit((Element) node, element -> { chain.execute(element); return true; }); } } return document; }
@Override public void callback(final Composite beanInstance) { /* * Only translate parent-less widgets to avoid re-translating a single widget multiple * times (the call to revisit will traverse the whole subtree rooted at this widget). */ if (beanInstance.getParent() == null) DomVisit.revisit(new ElementWrapper(beanInstance.getElement()), new TranslationDomRevisitor()); } });
public Document visitTemplate(URL template) { final Document document = parseTemplate(template); for (int i = 0; i < document.getChildNodes().getLength(); i++) { final Node node = document.getChildNodes().item(i); if (node instanceof Element) { DomVisit.visit((Element) node, element -> { chain.execute(element); return true; }); } } return document; }
@Override public void callback(final Composite beanInstance) { /* * Only translate parent-less widgets to avoid re-translating a single widget multiple * times (the call to revisit will traverse the whole subtree rooted at this widget). */ if (beanInstance.getParent() == null) DomVisit.revisit(new ElementWrapper(beanInstance.getElement()), new TranslationDomRevisitor()); } });
/** * Gets all of the i18n key/value pairs from the given template root. In other words, returns * everything that needs to be translated. * * @param templateRoot * @param i18nPrefix */ private static Map<String, String> getTemplateI18nValues(final Element templateRoot, final String i18nPrefix) { final TemplateVisitor visitor = new TemplateVisitor(i18nPrefix); DomVisit.visit(templateRoot, visitor); return visitor.getI18nValues(); }
/** * Re-translate displayed {@link Templated} beans to the current locale. */ public static void retranslateTemplatedBeans() { // Translate DOM-attached templates DomVisit.revisit(new ElementWrapper(Document.get().getBody()), new TranslationDomRevisitor()); // Translate DOM-detached Singleton templates for (final AsyncBeanDef<Composite> beanDef : IOC.getAsyncBeanManager().lookupBeans(Composite.class)) { final Class<? extends Annotation> scope = beanDef.getScope(); if (scope != null && (scope.equals(ApplicationScoped.class))) beanDef.getInstance(new CreationalCallback<Composite>() { @Override public void callback(final Composite beanInstance) { /* * Only translate parent-less widgets to avoid re-translating a single widget multiple * times (the call to revisit will traverse the whole subtree rooted at this widget). */ if (beanInstance.getParent() == null) DomVisit.revisit(new ElementWrapper(beanInstance.getElement()), new TranslationDomRevisitor()); } }); } } }
/** * Gets all of the i18n key/value pairs from the given template root. In other words, returns * everything that needs to be translated. * * @param templateRoot * @param i18nPrefix */ private static Map<String, String> getTemplateI18nValues(final Element templateRoot, final String i18nPrefix) { final TemplateVisitor visitor = new TemplateVisitor(i18nPrefix); DomVisit.visit(templateRoot, visitor); return visitor.getI18nValues(); }
/** * Re-translate displayed {@link Templated} beans to the current locale. */ public static void retranslateTemplatedBeans() { // Translate DOM-attached templates DomVisit.revisit(new ElementWrapper(Document.get().getBody()), new TranslationDomRevisitor()); // Translate DOM-detached Singleton templates for (final AsyncBeanDef<Composite> beanDef : IOC.getAsyncBeanManager().lookupBeans(Composite.class)) { final Class<? extends Annotation> scope = beanDef.getScope(); if (scope != null && (scope.equals(ApplicationScoped.class))) beanDef.getInstance(new CreationalCallback<Composite>() { @Override public void callback(final Composite beanInstance) { /* * Only translate parent-less widgets to avoid re-translating a single widget multiple * times (the call to revisit will traverse the whole subtree rooted at this widget). */ if (beanInstance.getParent() == null) DomVisit.revisit(new ElementWrapper(beanInstance.getElement()), new TranslationDomRevisitor()); } }); } } }
/** * Called to perform i18n translation on the given template. Add i18n-prefix attribute to root of * template to allow translation after bean creation. * * @param templateRoot */ public static void translateTemplate(final String templateFile, final Element templateRoot) { if (!getTranslationService().isEnabled()) return; logger.finer("Translating template: " + templateFile); final String i18nKeyPrefix = getI18nPrefix(templateFile); // Add i18n prefix attribute for post-creation translation templateRoot.setAttribute("data-i18n-prefix", i18nKeyPrefix); DomVisit.visit(new ElementWrapper(templateRoot), new TemplateTranslationVisitor(i18nKeyPrefix)); }
/** * Re-translate displayed {@link Templated} beans to the current locale. */ public static void retranslateTemplatedBeans() { // Translate DOM-attached templates DomVisit.revisit(new ElementWrapper(Document.get().getBody()), new TranslationDomRevisitor()); // Translate DOM-detached Singleton templates for (final AsyncBeanDef<Composite> beanDef : IOC.getAsyncBeanManager().lookupBeans(Composite.class)) { final Class<? extends Annotation> scope = beanDef.getScope(); if (scope != null && (scope.equals(ApplicationScoped.class))) beanDef.getInstance(new CreationalCallback<Composite>() { @Override public void callback(final Composite beanInstance) { /* * Only translate parent-less widgets to avoid re-translating a single widget multiple * times (the call to revisit will traverse the whole subtree rooted at this widget). */ if (beanInstance.getParent() == null) DomVisit.revisit(new ElementWrapper(beanInstance.getElement()), new TranslationDomRevisitor()); } }); } } }
/** * Called to perform i18n translation on the given template. Add i18n-prefix attribute to root of * template to allow translation after bean creation. * * @param templateRoot */ public static void translateTemplate(final String templateFile, final Element templateRoot) { if (!getTranslationService().isEnabled()) return; logger.finer("Translating template: " + templateFile); final String i18nKeyPrefix = getI18nPrefix(templateFile); // Add i18n prefix attribute for post-creation translation templateRoot.setAttribute("data-i18n-prefix", i18nKeyPrefix); DomVisit.visit(new ElementWrapper(templateRoot), new TemplateTranslationVisitor(i18nKeyPrefix)); }