Index: java/org/chromium/distiller/DomUtil.java |
diff --git a/java/org/chromium/distiller/DomUtil.java b/java/org/chromium/distiller/DomUtil.java |
index 5d3ace5179e4350fb1b70bb19cd738a2616c68a0..e9c54df8740508fe2215f5d4dd032abee0a222fa 100644 |
--- a/java/org/chromium/distiller/DomUtil.java |
+++ b/java/org/chromium/distiller/DomUtil.java |
@@ -15,6 +15,8 @@ import com.google.gwt.dom.client.NodeList; |
import com.google.gwt.dom.client.Style; |
import com.google.gwt.dom.client.VideoElement; |
import com.google.gwt.http.client.URL; |
+import com.google.gwt.regexp.shared.MatchResult; |
+import com.google.gwt.regexp.shared.RegExp; |
import java.util.ArrayList; |
import java.util.HashMap; |
@@ -436,6 +438,64 @@ public class DomUtil { |
return nodes; |
} |
+ public static String formatDuration(String duration) { |
+ RegExp pattern = RegExp.compile("^P(?:([0-9]+)Y)?" + |
+ "(?:([0-9]+)M)?(?:([0-9]+)W)?(?:([0-9]+)D)?" + |
+ "(T(?:([0-9]+)H)?(?:([0-9]+)M)?(?:([0-9]+)S)?)?$", "i"); |
+ MatchResult matchResult = pattern.exec(duration); |
+ List<String> result = new ArrayList<>(); |
+ if (matchResult != null) { |
+ if (matchResult.getGroup(1) != null) { |
+ result.add(matchResult.getGroup(1) + " year(s)"); |
+ } |
+ if (matchResult.getGroup(2) != null) { |
+ result.add(matchResult.getGroup(2) + " month(s)"); |
+ } |
+ if (matchResult.getGroup(3) != null) { |
+ result.add(matchResult.getGroup(3) + " week(s)"); |
+ } |
+ if (matchResult.getGroup(4) != null) { |
+ result.add(matchResult.getGroup(4) + " day(s)"); |
+ } |
+ if (matchResult.getGroup(6) != null) { |
+ result.add(matchResult.getGroup(6) + " hour(s)"); |
+ } |
+ if (matchResult.getGroup(7) != null) { |
+ result.add(matchResult.getGroup(7) + " minute(s)"); |
+ } |
+ if (matchResult.getGroup(8) != null) { |
+ result.add(matchResult.getGroup(8) + " second(s)"); |
+ } |
+ } |
+ return join(result.toArray(), " "); |
+ } |
+ |
+ /** |
+ * Tries to get the language of the web page. It looks for |
+ * the 'lang' attribute in the HTML tag, if it doesn't find it looks |
+ * the meta tags for Content-Language or Language properties. |
+ * |
+ * @param root The root element. |
+ * @return A string containing the language(s) or empty. |
+ */ |
+ public static String getLanguage(Element root) { |
wychen
2016/05/31 21:56:34
If the language is specified in http header, inste
dalmirsilva
2016/07/06 17:53:34
Unfortunately it doesn't get it. We couldn't find
wychen
2016/07/24 23:06:33
Got it. I guess this is our technical limitation.
|
+ String language = root.getLang(); |
+ if (language.isEmpty()) { |
+ NodeList<Element> metas = root.getElementsByTagName("META"); |
wychen
2016/05/31 21:56:34
Using "META[HTTP-EQUIV="content-language" i][CONTE
dalmirsilva
2016/07/06 17:53:34
Done.
|
+ for (int i = 0; i < metas.getLength(); i++) { |
+ Element meta = metas.getItem(i); |
+ if (meta.getAttribute("HTTP-EQUIV").toUpperCase() |
+ .equals("CONTENT-LANGUAGE") || |
+ meta.getAttribute("NAME").toUpperCase() |
+ .equals("LANGUAGE")) { |
+ language = meta.getAttribute("CONTENT"); |
+ break; |
+ } |
+ } |
+ } |
+ return language; |
+ } |
+ |
public static int getArea(Element e) { |
if (e != null) { |
return e.getOffsetHeight() * e.getOffsetWidth(); |
@@ -471,4 +531,8 @@ public class DomUtil { |
public static native Element getFirstElementChild(Document document) /*-{ |
return document.firstElementChild; |
}-*/; |
+ |
+ public static native String join(Object[] list, String conjunction) /*-{ |
+ return list.join(conjunction); |
+ }-*/; |
} |