Skip to content

Instantly share code, notes, and snippets.

@SergioLarios
Created December 9, 2015 09:04
Show Gist options
  • Save SergioLarios/f159337db14d650dca65 to your computer and use it in GitHub Desktop.
Save SergioLarios/f159337db14d650dca65 to your computer and use it in GitHub Desktop.
package com.movistar.latam.colportal.util;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.kernel.xml.Element;
import com.liferay.portal.kernel.xml.Node;
import com.liferay.portal.kernel.xml.SAXReaderUtil;
import com.liferay.portlet.journal.model.JournalArticle;
import java.util.ArrayList;
import java.util.List;
public class ContentUtils {
// ****************************
// ****** Public Metods *******
// ****************************
public static Element getRootEl(JournalArticle ja, String locale) throws Exception {
String content = ja.getContentByLocale(locale);
return SAXReaderUtil.read(content).getRootElement();
}
public static String getStrValFirst(Element root, String name) {
Node node = root.selectSingleNode(String.format(BASE_NODE, name));
if (Validator.isNotNull(node)) {
return GetterUtil.getString(node.getStringValue(), StringPool.BLANK);
}
else {
return StringPool.BLANK;
}
}
public static int getIntValFirst(Element root, String name) {
Node node = root.selectSingleNode(String.format(BASE_NODE, name));
if (Validator.isNotNull(node)) {
return GetterUtil.getInteger(node.getStringValue(), -1);
}
else {
return -1;
}
}
public static String getStrValSecond(Element root, String parent, String name) {
Node node = root.selectSingleNode(String.format(BASE_NODE_2, parent, name));
if (Validator.isNotNull(node)) {
return GetterUtil.getString(node.getStringValue(), StringPool.BLANK);
}
else {
return StringPool.BLANK;
}
}
public static int getIntValSecond(Element root, String parent, String name) {
Node node = root.selectSingleNode(String.format(BASE_NODE_2, parent, name));
if (Validator.isNotNull(node)) {
return GetterUtil.getInteger(node.getStringValue(), -1);
}
else {
return -1;
}
}
public static List<Element> getElementsFirst(Element root, String name) {
List<Element> result = new ArrayList<>();
List<Node> nodes = root.selectNodes(String.format(BASE_NODE_EL, name));
for (Node node : nodes) {
result.add((Element)node);
}
return result;
}
//****************************
// **** Private Constants *****
// ****************************
private static final String BASE_NODE = "dynamic-element[@name='%s']/dynamic-content";
private static final String BASE_NODE_EL = "dynamic-element[@name='%s']";
private static final String BASE_NODE_2 = "dynamic-element[@name='%s']/dynamic-element[@name='%s']/dynamic-content";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment