List of usage examples for com.liferay.portal.kernel.templateparser TemplateNode appendChildren
public void appendChildren(List<TemplateNode> templateNodes)
From source file:com.liferay.journal.transformer.JournalTransformer.java
License:Open Source License
protected List<TemplateNode> getTemplateNodes(ThemeDisplay themeDisplay, Element element, long ddmStructureId) throws Exception { DDMStructure ddmStructure = DDMStructureLocalServiceUtil.getStructure(ddmStructureId); DDMForm ddmForm = ddmStructure.getDDMForm(); Map<String, DDMFormField> ddmFormFieldsMap = ddmForm.getDDMFormFieldsMap(true); List<TemplateNode> templateNodes = new ArrayList<>(); Map<String, TemplateNode> prototypeTemplateNodes = new HashMap<>(); List<Element> dynamicElementElements = element.elements("dynamic-element"); for (Element dynamicElementElement : dynamicElementElements) { Element dynamicContentElement = dynamicElementElement.element("dynamic-content"); String data = StringPool.BLANK; if (dynamicContentElement != null) { data = dynamicContentElement.getText(); }//from w w w . j a v a 2 s .c om String name = dynamicElementElement.attributeValue("name", StringPool.BLANK); if (name.length() == 0) { throw new TransformException("Element missing \"name\" attribute"); } String type = dynamicElementElement.attributeValue("type", StringPool.BLANK); Map<String, String> attributes = new HashMap<>(); if (type.equals("image")) { JSONObject dataJSONObject = JSONFactoryUtil.createJSONObject(data); Iterator<String> itr = dataJSONObject.keys(); while (itr.hasNext()) { String key = itr.next(); String value = dataJSONObject.getString(key); attributes.put(key, value); } } if (dynamicContentElement != null) { for (Attribute attribute : dynamicContentElement.attributes()) { attributes.put(attribute.getName(), attribute.getValue()); } } TemplateNode templateNode = new TemplateNode(themeDisplay, name, StringUtil.stripCDATA(data), type, attributes); if (dynamicElementElement.element("dynamic-element") != null) { templateNode.appendChildren(getTemplateNodes(themeDisplay, dynamicElementElement, ddmStructureId)); } else if ((dynamicContentElement != null) && (dynamicContentElement.element("option") != null)) { List<Element> optionElements = dynamicContentElement.elements("option"); for (Element optionElement : optionElements) { templateNode.appendOption(StringUtil.stripCDATA(optionElement.getText())); } } DDMFormField ddmFormField = ddmFormFieldsMap.get(name); if (ddmFormField != null) { DDMFormFieldOptions ddmFormFieldOptions = ddmFormField.getDDMFormFieldOptions(); Map<String, LocalizedValue> options = ddmFormFieldOptions.getOptions(); for (Entry<String, LocalizedValue> entry : options.entrySet()) { String optionValue = StringUtil.stripCDATA(entry.getKey()); LocalizedValue localizedLabel = entry.getValue(); String optionLabel = localizedLabel.getString(themeDisplay.getLocale()); templateNode.appendOptionMap(optionValue, optionLabel); } } TemplateNode prototypeTemplateNode = prototypeTemplateNodes.get(name); if (prototypeTemplateNode == null) { prototypeTemplateNode = templateNode; prototypeTemplateNodes.put(name, prototypeTemplateNode); templateNodes.add(templateNode); } prototypeTemplateNode.appendSibling(templateNode); } return templateNodes; }
From source file:com.liferay.portlet.journal.util.VelocityTemplateParser.java
License:Open Source License
@Override protected List<TemplateNode> getTemplateNodes(Element element) throws Exception { List<TemplateNode> templateNodes = new ArrayList<TemplateNode>(); Map<String, TemplateNode> prototypeTemplateNodes = new HashMap<String, TemplateNode>(); List<Element> dynamicElementElements = element.elements("dynamic-element"); for (Element dynamicElementElement : dynamicElementElements) { Element dynamicContentElement = dynamicElementElement.element("dynamic-content"); String data = StringPool.BLANK; if (dynamicContentElement != null) { data = dynamicContentElement.getText(); }//from w ww . java2 s. co m String name = dynamicElementElement.attributeValue("name", ""); if (name.length() == 0) { throw new TransformException("Element missing \"name\" attribute"); } String type = dynamicElementElement.attributeValue("type", ""); TemplateNode templateNode = new TemplateNode(getThemeDisplay(), name, stripCDATA(data), type); if (dynamicElementElement.element("dynamic-element") != null) { templateNode.appendChildren(getTemplateNodes(dynamicElementElement)); } else if ((dynamicContentElement != null) && (dynamicContentElement.element("option") != null)) { List<Element> optionElements = dynamicContentElement.elements("option"); for (Element optionElement : optionElements) { templateNode.appendOption(stripCDATA(optionElement.getText())); } } TemplateNode prototypeTemplateNode = prototypeTemplateNodes.get(name); if (prototypeTemplateNode == null) { prototypeTemplateNode = templateNode; prototypeTemplateNodes.put(name, prototypeTemplateNode); templateNodes.add(templateNode); } prototypeTemplateNode.appendSibling(templateNode); } return templateNodes; }