[16869] | 1 | /*
|
---|
| 2 | * XSLTUtil.java
|
---|
| 3 | * Copyright (C) 2008 New Zealand Digital Library, http://www.nzdl.org
|
---|
| 4 | *
|
---|
| 5 | * This program is free software; you can redistribute it and/or modify
|
---|
| 6 | * it under the terms of the GNU General Public License as published by
|
---|
| 7 | * the Free Software Foundation; either version 2 of the License, or
|
---|
| 8 | * (at your option) any later version.
|
---|
| 9 | *
|
---|
| 10 | * This program is distributed in the hope that it will be useful,
|
---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 13 | * GNU General Public License for more details.
|
---|
| 14 | *
|
---|
| 15 | * You should have received a copy of the GNU General Public License
|
---|
| 16 | * along with this program; if not, write to the Free Software
|
---|
| 17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 18 | */
|
---|
[3341] | 19 | package org.greenstone.gsdl3.util;
|
---|
| 20 |
|
---|
[26325] | 21 | import java.io.ByteArrayInputStream;
|
---|
[25858] | 22 | import java.io.File;
|
---|
[30359] | 23 | import java.io.UnsupportedEncodingException;
|
---|
| 24 | import java.net.URLEncoder;
|
---|
[25858] | 25 | import java.text.SimpleDateFormat;
|
---|
[25107] | 26 | import java.util.ArrayList;
|
---|
[8649] | 27 | import java.util.Date;
|
---|
[25347] | 28 | import java.util.Enumeration;
|
---|
[25107] | 29 | import java.util.HashMap;
|
---|
[8649] | 30 | import java.util.Locale;
|
---|
| 31 |
|
---|
[26325] | 32 | import javax.xml.parsers.DocumentBuilder;
|
---|
| 33 | import javax.xml.parsers.DocumentBuilderFactory;
|
---|
| 34 |
|
---|
[25264] | 35 | import net.tanesha.recaptcha.ReCaptcha;
|
---|
| 36 | import net.tanesha.recaptcha.ReCaptchaFactory;
|
---|
| 37 |
|
---|
[23791] | 38 | import org.apache.commons.lang3.StringUtils;
|
---|
[25858] | 39 | import org.apache.log4j.Logger;
|
---|
[25567] | 40 | import org.greenstone.util.GlobalProperties;
|
---|
[25858] | 41 | import org.w3c.dom.Node;
|
---|
[23791] | 42 |
|
---|
[24450] | 43 | /**
|
---|
| 44 | * a class to contain various static methods that are used by the xslt
|
---|
[3341] | 45 | * stylesheets
|
---|
| 46 | */
|
---|
[24450] | 47 | public class XSLTUtil
|
---|
| 48 | {
|
---|
[25107] | 49 | protected static HashMap<String, ArrayList<String>> _foundTableValues = new HashMap<String, ArrayList<String>>();
|
---|
[24450] | 50 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.XSLTUtil.class.getName());
|
---|
[26279] | 51 | protected static HashMap<String, String> _stringVariables = new HashMap<String, String>();
|
---|
[13124] | 52 |
|
---|
[26279] | 53 | public static void storeString(String name, String value)
|
---|
| 54 | {
|
---|
| 55 | _stringVariables.put(name, value);
|
---|
| 56 | }
|
---|
[26325] | 57 |
|
---|
[26279] | 58 | public static String getString(String name)
|
---|
| 59 | {
|
---|
| 60 | return _stringVariables.get(name);
|
---|
| 61 | }
|
---|
[26325] | 62 |
|
---|
[24450] | 63 | /* some tests */
|
---|
| 64 | public static boolean equals(String s1, String s2)
|
---|
| 65 | {
|
---|
| 66 | return s1.equals(s2);
|
---|
| 67 | }
|
---|
[10783] | 68 |
|
---|
[24450] | 69 | public static boolean notEquals(String s1, String s2)
|
---|
| 70 | {
|
---|
| 71 | return !s1.equals(s2);
|
---|
| 72 | }
|
---|
[3341] | 73 |
|
---|
[24450] | 74 | public static boolean exists(String s1, String s2)
|
---|
| 75 | {
|
---|
| 76 | return !s1.equals("");
|
---|
[3362] | 77 | }
|
---|
[24450] | 78 |
|
---|
| 79 | public static boolean contains(String s1, String s2)
|
---|
| 80 | {
|
---|
| 81 | return (s1.indexOf(s2) != -1);
|
---|
[3362] | 82 | }
|
---|
| 83 |
|
---|
[24450] | 84 | public static boolean startsWith(String s1, String s2)
|
---|
| 85 | {
|
---|
| 86 | return s1.startsWith(s2);
|
---|
[4244] | 87 | }
|
---|
[4271] | 88 |
|
---|
[24450] | 89 | public static boolean endsWith(String s1, String s2)
|
---|
| 90 | {
|
---|
| 91 | return s1.endsWith(s2);
|
---|
| 92 | }
|
---|
[24219] | 93 |
|
---|
[24450] | 94 | public static boolean lessThan(String s1, String s2)
|
---|
| 95 | {
|
---|
| 96 | return (s1.compareTo(s2) < 0);
|
---|
[24219] | 97 | }
|
---|
| 98 |
|
---|
[24450] | 99 | public static boolean lessThanOrEquals(String s1, String s2)
|
---|
| 100 | {
|
---|
| 101 | return (s1.compareTo(s2) <= 0);
|
---|
[4271] | 102 | }
|
---|
[4702] | 103 |
|
---|
[24450] | 104 | public static boolean greaterThan(String s1, String s2)
|
---|
| 105 | {
|
---|
| 106 | return (s1.compareTo(s2) > 0);
|
---|
| 107 | }
|
---|
[4702] | 108 |
|
---|
[24450] | 109 | public static boolean greaterThanOrEquals(String s1, String s2)
|
---|
| 110 | {
|
---|
| 111 | return (s1.compareTo(s2) >= 0);
|
---|
[4702] | 112 | }
|
---|
| 113 |
|
---|
[25353] | 114 | public static boolean oidIsMatchOrParent(String first, String second)
|
---|
| 115 | {
|
---|
| 116 | if (first.equals(second))
|
---|
| 117 | {
|
---|
| 118 | return true;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | String[] firstParts = first.split(".");
|
---|
| 122 | String[] secondParts = second.split(".");
|
---|
| 123 |
|
---|
| 124 | if (firstParts.length >= secondParts.length)
|
---|
| 125 | {
|
---|
| 126 | return false;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | for (int i = 0; i < firstParts.length; i++)
|
---|
| 130 | {
|
---|
| 131 | if (!firstParts[i].equals(secondParts[i]))
|
---|
| 132 | {
|
---|
| 133 | return false;
|
---|
| 134 | }
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | return true;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
[26471] | 140 | public static String oidDocumentRoot(String oid)
|
---|
| 141 | {
|
---|
| 142 | String[] oidParts = oid.split("\\.");
|
---|
| 143 |
|
---|
| 144 | return oidParts[0];
|
---|
| 145 | }
|
---|
| 146 |
|
---|
[25264] | 147 | public static String replace(String orig, String match, String replacement)
|
---|
| 148 | {
|
---|
| 149 | return orig.replace(match, replacement);
|
---|
| 150 | }
|
---|
[18308] | 151 |
|
---|
[24450] | 152 | public static String getNumberedItem(String list, int number)
|
---|
| 153 | {
|
---|
[29175] | 154 | String[] items = list.split(",", -1); //String[] items = StringUtils.split(list, ",", -1);
|
---|
| 155 | // Using StringUtils.split() causes an off-by-one error for the boolean operators (fqk)
|
---|
| 156 | // where boolean operators combining rows in multiforms are shifted up by 1 row.
|
---|
| 157 |
|
---|
[24450] | 158 | if (items.length > number)
|
---|
| 159 | {
|
---|
| 160 | return items[number];
|
---|
| 161 | }
|
---|
| 162 | return ""; // index out of bounds
|
---|
[18308] | 163 | }
|
---|
[8540] | 164 |
|
---|
[24450] | 165 | /**
|
---|
| 166 | * Generates links to equivalent documents for a document with a default
|
---|
| 167 | * document icon/type. Links are generated from the parameters: a list of
|
---|
| 168 | * document icons which are each in turn embedded in the matching starting
|
---|
| 169 | * link tag in the list of docStartLinks (these starting links link to the
|
---|
| 170 | * equivalent documents in another format). Each link's start tag is closed
|
---|
| 171 | * with the corresponding closing tag in the docEndLinks list. Parameter
|
---|
| 172 | * token is the list separator. Parameter divider is the string that should
|
---|
| 173 | * separate each final link generated from the next. Returns a string that
|
---|
| 174 | * represents a sequence of links to equivalent documents, where the anchor
|
---|
| 175 | * is a document icon.
|
---|
| 176 | */
|
---|
| 177 | public static String getEquivDocLinks(String token, String docIconsString, String docStartLinksString, String docEndLinksString, String divider)
|
---|
| 178 | {
|
---|
| 179 | String[] docIcons = StringUtils.split(docIconsString, token, -1);
|
---|
| 180 | String[] startLinks = StringUtils.split(docStartLinksString, token, -1);
|
---|
| 181 | String[] endLinks = StringUtils.split(docEndLinksString, token, -1);
|
---|
| 182 |
|
---|
| 183 | StringBuffer buffer = new StringBuffer();
|
---|
| 184 | for (int i = 0; i < docIcons.length; i++)
|
---|
| 185 | {
|
---|
| 186 | if (i > 0)
|
---|
| 187 | {
|
---|
| 188 | buffer.append(divider);
|
---|
| 189 | }
|
---|
| 190 | buffer.append(startLinks[i] + docIcons[i] + endLinks[i]);
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | return buffer.toString();
|
---|
[18308] | 194 | }
|
---|
| 195 |
|
---|
[24450] | 196 | public static String getInterfaceText(String interface_name, String lang, String key)
|
---|
| 197 | {
|
---|
| 198 | return getInterfaceText(interface_name, lang, key, null);
|
---|
[8657] | 199 | }
|
---|
[24450] | 200 |
|
---|
| 201 | public static String getInterfaceText(String interface_name, String lang, String key, String args_str)
|
---|
| 202 | {
|
---|
| 203 | key = key.replaceAll("__INTERFACE_NAME__", interface_name);
|
---|
[25107] | 204 |
|
---|
[24450] | 205 | String[] args = null;
|
---|
| 206 | if (args_str != null && !args_str.equals(""))
|
---|
| 207 | {
|
---|
| 208 | args = StringUtils.split(args_str, ";");
|
---|
| 209 | }
|
---|
| 210 | Dictionary dict = new Dictionary("interface_" + interface_name, lang);
|
---|
| 211 | String result = dict.get(key, args);
|
---|
| 212 | if (result == null)
|
---|
| 213 | { // not found
|
---|
| 214 | //if not found, search a separate subdirectory named by the interface name
|
---|
| 215 | String sep_interface_dir = interface_name + File.separatorChar + lang + File.separatorChar + "interface";
|
---|
| 216 | dict = new Dictionary(sep_interface_dir, lang);
|
---|
| 217 | result = dict.get(key, args);
|
---|
| 218 | if (result != null)
|
---|
| 219 | {
|
---|
| 220 | result = result.replaceAll("__INTERFACE_NAME__", interface_name);
|
---|
| 221 | return result;
|
---|
| 222 | }
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | if (result == null && !interface_name.equals("default"))
|
---|
| 226 | { // not found, try the default interface
|
---|
| 227 | dict = new Dictionary("interface_default", lang);
|
---|
| 228 | result = dict.get(key, args);
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | if (result == null)
|
---|
| 232 | { // not found
|
---|
| 233 | return "_" + key + "_";
|
---|
| 234 | }
|
---|
| 235 | result = result.replaceAll("__INTERFACE_NAME__", interface_name);
|
---|
| 236 | return result;
|
---|
[8649] | 237 | }
|
---|
| 238 |
|
---|
[26557] | 239 | public static String getInterfaceText(String interfaceName, String dictionaryName, String lang, String key, String args_str)
|
---|
| 240 | {
|
---|
| 241 | key = key.replaceAll("__INTERFACE_NAME__", interfaceName);
|
---|
| 242 |
|
---|
| 243 | String[] args = null;
|
---|
| 244 | if (args_str != null && !args_str.equals(""))
|
---|
| 245 | {
|
---|
| 246 | args = StringUtils.split(args_str, ";");
|
---|
| 247 | }
|
---|
| 248 | Dictionary dict = new Dictionary(dictionaryName, lang);
|
---|
| 249 | String result = dict.get(key, args);
|
---|
| 250 | if (result == null)
|
---|
| 251 | { // not found
|
---|
| 252 | //if not found, search a separate subdirectory named by the interface name
|
---|
| 253 | String sep_interface_dir = interfaceName + File.separatorChar + lang + File.separatorChar + "interface";
|
---|
| 254 | dict = new Dictionary(sep_interface_dir, lang);
|
---|
| 255 | result = dict.get(key, args);
|
---|
| 256 | if (result != null)
|
---|
| 257 | {
|
---|
| 258 | result = result.replaceAll("__INTERFACE_NAME__", interfaceName);
|
---|
| 259 | return result;
|
---|
| 260 | }
|
---|
| 261 | }
|
---|
| 262 |
|
---|
| 263 | if (result == null && !interfaceName.equals("default"))
|
---|
| 264 | { // not found, try the default interface
|
---|
| 265 | dict = new Dictionary("interface_default", lang);
|
---|
| 266 | result = dict.get(key, args);
|
---|
| 267 | }
|
---|
| 268 |
|
---|
| 269 | if (result == null)
|
---|
| 270 | { // not found
|
---|
| 271 | return "_" + key + "_";
|
---|
| 272 | }
|
---|
| 273 | result = result.replaceAll("__INTERFACE_NAME__", interfaceName);
|
---|
| 274 | return result;
|
---|
| 275 | }
|
---|
| 276 |
|
---|
[24450] | 277 | public static String getInterfaceTextWithDOM(String interface_name, String lang, String key, Node arg_node)
|
---|
| 278 | {
|
---|
| 279 | String[] args = new String[1];
|
---|
[8649] | 280 |
|
---|
[24450] | 281 | String node_str = XMLConverter.getString(arg_node);
|
---|
| 282 | args[0] = node_str;
|
---|
| 283 | Dictionary dict = new Dictionary("interface_" + interface_name, lang);
|
---|
| 284 | String result = dict.get(key, args);
|
---|
| 285 | if (result == null)
|
---|
| 286 | { // not found
|
---|
| 287 | //if not found, search a separate subdirectory named by the interface name
|
---|
| 288 | String sep_interface_dir = interface_name + File.separatorChar + lang + File.separatorChar + "interface";
|
---|
| 289 | dict = new Dictionary(sep_interface_dir, lang);
|
---|
| 290 | result = dict.get(key, args);
|
---|
| 291 | if (result != null)
|
---|
| 292 | {
|
---|
| 293 | return result;
|
---|
| 294 | }
|
---|
| 295 | }
|
---|
[8824] | 296 |
|
---|
[24450] | 297 | if (result == null && !interface_name.equals("default"))
|
---|
| 298 | { // not found, try the default interface
|
---|
| 299 | dict = new Dictionary("interface_default", lang);
|
---|
| 300 | result = dict.get(key, args);
|
---|
| 301 | }
|
---|
[8824] | 302 |
|
---|
[24450] | 303 | if (result == null)
|
---|
| 304 | { // not found
|
---|
| 305 | return "_" + key + "_";
|
---|
| 306 | }
|
---|
[15977] | 307 |
|
---|
[24450] | 308 | return result;
|
---|
| 309 | }
|
---|
[15977] | 310 |
|
---|
[24450] | 311 | public static String getInterfaceTextWithDOM(String interface_name, String lang, String key, Node arg1_node, Node arg2_node)
|
---|
| 312 | {
|
---|
| 313 | String[] args = new String[2];
|
---|
[15977] | 314 |
|
---|
[24450] | 315 | String node_str = XMLConverter.getString(arg1_node);
|
---|
| 316 | args[0] = node_str;
|
---|
| 317 | node_str = XMLConverter.getString(arg2_node);
|
---|
| 318 | args[1] = node_str;
|
---|
| 319 | Dictionary dict = new Dictionary("interface_" + interface_name, lang);
|
---|
| 320 | String result = dict.get(key, args);
|
---|
| 321 | if (result == null)
|
---|
| 322 | { // not found
|
---|
| 323 | //if not found, search a separate subdirectory named by the interface name
|
---|
| 324 | String sep_interface_dir = interface_name + File.separatorChar + lang + File.separatorChar + "interface";
|
---|
| 325 | dict = new Dictionary(sep_interface_dir, lang);
|
---|
| 326 | result = dict.get(key, args);
|
---|
| 327 | if (result != null)
|
---|
| 328 | {
|
---|
| 329 | return result;
|
---|
| 330 | }
|
---|
| 331 | }
|
---|
[15977] | 332 |
|
---|
[24450] | 333 | if (result == null && !interface_name.equals("default"))
|
---|
| 334 | { // not found, try the default interface
|
---|
| 335 | dict = new Dictionary("interface_default", lang);
|
---|
| 336 | result = dict.get(key, args);
|
---|
[15977] | 337 | }
|
---|
| 338 |
|
---|
[24450] | 339 | if (result == null)
|
---|
| 340 | { // not found
|
---|
| 341 | return "_" + key + "_";
|
---|
| 342 | }
|
---|
[15977] | 343 |
|
---|
[24450] | 344 | return result;
|
---|
| 345 | }
|
---|
| 346 |
|
---|
[30477] | 347 |
|
---|
| 348 | public static String getInterfaceTextSubstituteArgs(String value, String args_str)
|
---|
| 349 | {
|
---|
| 350 | String[] args = null;
|
---|
| 351 | if (args_str != null && !args_str.equals("")) {
|
---|
| 352 | args = StringUtils.split(args_str, ";");
|
---|
| 353 | }
|
---|
| 354 |
|
---|
| 355 | return Dictionary.processArgs(value,args);
|
---|
| 356 | }
|
---|
| 357 |
|
---|
[26325] | 358 | public static Node getCollectionText(String collection, String site_name, String lang, String key)
|
---|
[26253] | 359 | {
|
---|
[26325] | 360 | return getCollectionTextWithArgs(collection, site_name, lang, key, null);
|
---|
[26253] | 361 | }
|
---|
[26243] | 362 |
|
---|
[26325] | 363 | public static Node getCollectionText(String collection, String site_name, String lang, String key, String args_str)
|
---|
| 364 | {
|
---|
[26315] | 365 |
|
---|
[26325] | 366 | String[] args = null;
|
---|
| 367 | if (args_str != null && !args_str.equals(""))
|
---|
| 368 | {
|
---|
| 369 | args = StringUtils.split(args_str, ";");
|
---|
| 370 | }
|
---|
[26315] | 371 |
|
---|
[26325] | 372 | return getCollectionTextWithArgs(collection, site_name, lang, key, args);
|
---|
| 373 | }
|
---|
[26315] | 374 |
|
---|
[26253] | 375 | // xslt didn't like calling the function with Node varargs, so have this hack for now
|
---|
[26325] | 376 | public static Node getCollectionTextWithDOM(String collection, String site_name, String lang, String key, Node n1)
|
---|
[26253] | 377 | {
|
---|
| 378 | return getCollectionTextWithDOMMulti(collection, site_name, lang, key, n1);
|
---|
| 379 | }
|
---|
[26243] | 380 |
|
---|
[26325] | 381 | public static Node getCollectionTextWithDOM(String collection, String site_name, String lang, String key, Node n1, Node n2)
|
---|
[26253] | 382 | {
|
---|
| 383 | return getCollectionTextWithDOMMulti(collection, site_name, lang, key, n1, n2);
|
---|
| 384 | }
|
---|
[26243] | 385 |
|
---|
[26325] | 386 | public static Node getCollectionTextWithDOM(String collection, String site_name, String lang, String key, Node n1, Node n2, Node n3)
|
---|
[26253] | 387 | {
|
---|
| 388 | return getCollectionTextWithDOMMulti(collection, site_name, lang, key, n1, n2, n3);
|
---|
| 389 | }
|
---|
[26243] | 390 |
|
---|
[26325] | 391 | public static Node getCollectionTextWithDOM(String collection, String site_name, String lang, String key, Node n1, Node n2, Node n3, Node n4)
|
---|
[26253] | 392 | {
|
---|
| 393 | return getCollectionTextWithDOMMulti(collection, site_name, lang, key, n1, n2, n3, n4);
|
---|
| 394 | }
|
---|
| 395 |
|
---|
[26325] | 396 | public static Node getCollectionTextWithDOMMulti(String collection, String site_name, String lang, String key, Node... nodes)
|
---|
[26253] | 397 | {
|
---|
| 398 | int num_nodes = nodes.length;
|
---|
| 399 | String[] args = null;
|
---|
| 400 | if (num_nodes != 0)
|
---|
| 401 | {
|
---|
| 402 | args = new String[num_nodes];
|
---|
| 403 |
|
---|
| 404 | for (int i = 0; i < num_nodes; i++)
|
---|
| 405 | {
|
---|
| 406 | String node_str = XMLConverter.getString(nodes[i]);
|
---|
| 407 | args[i] = node_str;
|
---|
| 408 | }
|
---|
| 409 | }
|
---|
[26315] | 410 | return getCollectionTextWithArgs(collection, site_name, lang, key, args);
|
---|
| 411 | }
|
---|
[26253] | 412 |
|
---|
[26325] | 413 | public static Node getCollectionTextWithArgs(String collection, String site_name, String lang, String key, String[] args)
|
---|
| 414 | {
|
---|
| 415 | try
|
---|
| 416 | {
|
---|
| 417 | DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
|
---|
[26315] | 418 |
|
---|
[26325] | 419 | CollectionClassLoader class_loader = new CollectionClassLoader(XSLTUtil.class.getClassLoader(), GSFile.siteHome(GlobalProperties.getGSDL3Home(), site_name), collection);
|
---|
| 420 | Dictionary dict = new Dictionary(collection, lang, class_loader);
|
---|
| 421 | String result = dict.get(key, args);
|
---|
| 422 | if (result != null)
|
---|
| 423 | {
|
---|
[26334] | 424 | return docBuilder.parse(new ByteArrayInputStream(("<fragment>" + result + "</fragment>").getBytes("UTF-8"))).getDocumentElement();
|
---|
[26325] | 425 | }
|
---|
| 426 | return docBuilder.parse(new ByteArrayInputStream(("<fragment>" + "text:" + collection + ":" + key + "</fragment>").getBytes())).getDocumentElement();
|
---|
| 427 | }
|
---|
| 428 | catch (Exception ex)
|
---|
[26253] | 429 | {
|
---|
[26325] | 430 | return null;
|
---|
[26253] | 431 | }
|
---|
| 432 | }
|
---|
| 433 |
|
---|
[24450] | 434 | public static boolean isImage(String mimetype)
|
---|
| 435 | {
|
---|
| 436 | if (mimetype.startsWith("image/"))
|
---|
| 437 | {
|
---|
| 438 | return true;
|
---|
[15977] | 439 | }
|
---|
[24450] | 440 | return false;
|
---|
| 441 | }
|
---|
| 442 |
|
---|
[26253] | 443 | // formatting /preprocessing functions
|
---|
| 444 | // some require a language, so we'll have a language param for all
|
---|
| 445 | public static String toLower(String orig, String lang)
|
---|
[26086] | 446 | {
|
---|
| 447 | return orig.toLowerCase();
|
---|
| 448 | }
|
---|
| 449 |
|
---|
| 450 | public static String toUpper(String orig, String lang)
|
---|
| 451 | {
|
---|
| 452 | return orig.toUpperCase();
|
---|
| 453 | }
|
---|
| 454 |
|
---|
[26253] | 455 | public static String tidyWhitespace(String original, String lang)
|
---|
[26104] | 456 | {
|
---|
| 457 |
|
---|
| 458 | if (original == null || original.equals(""))
|
---|
| 459 | {
|
---|
| 460 | return original;
|
---|
| 461 | }
|
---|
| 462 | String new_s = original.replaceAll("\\s+", " ");
|
---|
| 463 | return new_s;
|
---|
| 464 | }
|
---|
| 465 |
|
---|
[26253] | 466 | public static String stripWhitespace(String original, String lang)
|
---|
[26104] | 467 | {
|
---|
| 468 |
|
---|
| 469 | if (original == null || original.equals(""))
|
---|
| 470 | {
|
---|
| 471 | return original;
|
---|
| 472 | }
|
---|
| 473 | String new_s = original.replaceAll("\\s+", "");
|
---|
| 474 | return new_s;
|
---|
| 475 | }
|
---|
| 476 |
|
---|
[26086] | 477 | public static byte[] toUTF8(String orig, String lang)
|
---|
| 478 | {
|
---|
| 479 | try
|
---|
| 480 | {
|
---|
| 481 | byte[] utf8 = orig.getBytes("UTF-8");
|
---|
| 482 | return utf8;
|
---|
| 483 | }
|
---|
| 484 | catch (Exception e)
|
---|
| 485 | {
|
---|
| 486 | logger.error("unsupported encoding");
|
---|
| 487 | return orig.getBytes();
|
---|
| 488 | }
|
---|
| 489 | }
|
---|
| 490 |
|
---|
[24450] | 491 | public static String formatDate(String date, String lang)
|
---|
| 492 | {
|
---|
| 493 | String in_pattern = "yyyyMMdd";
|
---|
| 494 | String out_pattern = "dd MMMM yyyy";
|
---|
| 495 | if (date.length() == 6)
|
---|
| 496 | {
|
---|
| 497 | in_pattern = "yyyyMM";
|
---|
[15977] | 498 | }
|
---|
[24450] | 499 |
|
---|
| 500 | SimpleDateFormat formatter = new SimpleDateFormat(in_pattern, new Locale(lang));
|
---|
| 501 | try
|
---|
| 502 | {
|
---|
| 503 | Date d = formatter.parse(date);
|
---|
| 504 | formatter.applyPattern(out_pattern);
|
---|
| 505 | String new_date = formatter.format(d);
|
---|
| 506 | return new_date;
|
---|
| 507 | }
|
---|
| 508 | catch (Exception e)
|
---|
| 509 | {
|
---|
| 510 | return date;
|
---|
| 511 | }
|
---|
| 512 |
|
---|
[15977] | 513 | }
|
---|
[24450] | 514 |
|
---|
[26279] | 515 | public static String getDetailFromDate(String date, String detail, String lang)
|
---|
| 516 | {
|
---|
| 517 | String in_pattern = "yyyyMMdd";
|
---|
| 518 | if (date.length() == 6)
|
---|
| 519 | {
|
---|
| 520 | in_pattern = "yyyyMM";
|
---|
| 521 | }
|
---|
| 522 |
|
---|
| 523 | SimpleDateFormat formatter = new SimpleDateFormat(in_pattern, new Locale(lang));
|
---|
| 524 | try
|
---|
| 525 | {
|
---|
| 526 | Date d = formatter.parse(date);
|
---|
| 527 | if (detail.toLowerCase().equals("day"))
|
---|
| 528 | {
|
---|
| 529 | formatter.applyPattern("dd");
|
---|
| 530 | }
|
---|
| 531 | else if (detail.toLowerCase().equals("month"))
|
---|
| 532 | {
|
---|
| 533 | formatter.applyPattern("MMMM");
|
---|
| 534 | }
|
---|
| 535 | else if (detail.toLowerCase().equals("year"))
|
---|
| 536 | {
|
---|
| 537 | formatter.applyPattern("yyyy");
|
---|
| 538 | }
|
---|
| 539 | else
|
---|
| 540 | {
|
---|
| 541 | return "";
|
---|
| 542 | }
|
---|
| 543 | return formatter.format(d);
|
---|
| 544 | }
|
---|
| 545 | catch (Exception ex)
|
---|
| 546 | {
|
---|
| 547 | return "";
|
---|
| 548 | }
|
---|
| 549 | }
|
---|
| 550 |
|
---|
[24450] | 551 | public static String formatLanguage(String display_lang, String lang)
|
---|
| 552 | {
|
---|
| 553 |
|
---|
| 554 | return new Locale(display_lang).getDisplayLanguage(new Locale(lang));
|
---|
| 555 | }
|
---|
[30542] | 556 |
|
---|
| 557 | public static boolean checkFileExistence(String site_name, String filePath){
|
---|
| 558 |
|
---|
| 559 | String gsdl3_home = GlobalProperties.getGSDL3Home();
|
---|
| 560 | //Remove leading file separator
|
---|
| 561 | filePath = filePath.replaceAll("^/+", "");
|
---|
| 562 | //Remove duplicates and replace by separator for current platform
|
---|
| 563 | filePath = filePath.replaceAll("/+", File.separator);
|
---|
| 564 | //Create full path to check
|
---|
| 565 | String fullPath = GSFile.siteHome(gsdl3_home, site_name) + File.separator + filePath;
|
---|
| 566 | File file = new File(fullPath);
|
---|
| 567 | if (file.exists() && file.isFile()) {
|
---|
| 568 | return true;
|
---|
| 569 | }
|
---|
| 570 | return false;
|
---|
| 571 | }
|
---|
| 572 |
|
---|
[30359] | 573 | public static String uriEncode(String input)
|
---|
| 574 | {
|
---|
| 575 | String result = "";
|
---|
[30360] | 576 | try {
|
---|
| 577 | result = URLEncoder.encode(input, "UTF-8");
|
---|
| 578 | } catch (UnsupportedEncodingException e) {
|
---|
| 579 | e.printStackTrace();
|
---|
| 580 | }
|
---|
[30359] | 581 |
|
---|
| 582 | return result;
|
---|
| 583 |
|
---|
| 584 | }
|
---|
[24450] | 585 |
|
---|
| 586 | public static String cgiSafe(String original, String lang)
|
---|
| 587 | {
|
---|
| 588 |
|
---|
| 589 | original = original.replace('&', ' ');
|
---|
| 590 | original = original.replaceAll(" ", "%20");
|
---|
| 591 | return original;
|
---|
| 592 | }
|
---|
| 593 |
|
---|
[26086] | 594 | public static String formatBigNumber(String num, String lang)
|
---|
[24450] | 595 | {
|
---|
| 596 |
|
---|
| 597 | String num_str = num;
|
---|
| 598 | char[] num_chars = num_str.toCharArray();
|
---|
| 599 | String zero_str = "";
|
---|
| 600 | String formatted_str = "";
|
---|
| 601 |
|
---|
| 602 | for (int i = num_chars.length - 4; i >= 0; i--)
|
---|
| 603 | {
|
---|
| 604 | zero_str += '0';
|
---|
| 605 | }
|
---|
| 606 |
|
---|
| 607 | String sig_str = "";
|
---|
| 608 | for (int i = 0; i < 3 && i < num_chars.length; i++)
|
---|
| 609 | {
|
---|
| 610 | sig_str = sig_str + num_chars[i];
|
---|
| 611 | if (i == 1 && i + 1 < num_chars.length)
|
---|
| 612 | {
|
---|
| 613 | sig_str = sig_str + ".";
|
---|
| 614 | }
|
---|
| 615 | }
|
---|
| 616 |
|
---|
| 617 | int sig_int = Math.round(Float.parseFloat(sig_str));
|
---|
| 618 | String new_sig_str = sig_int + "";
|
---|
| 619 | if (sig_str.length() > 2)
|
---|
| 620 | {
|
---|
| 621 | new_sig_str = sig_int + "0";
|
---|
| 622 | }
|
---|
| 623 |
|
---|
| 624 | char[] final_chars = (new_sig_str + zero_str).toCharArray();
|
---|
| 625 | int count = 1;
|
---|
| 626 | for (int i = final_chars.length - 1; i >= 0; i--)
|
---|
| 627 | {
|
---|
| 628 | formatted_str = final_chars[i] + formatted_str;
|
---|
| 629 | if (count == 3 && i != 0)
|
---|
| 630 | {
|
---|
| 631 | formatted_str = "," + formatted_str;
|
---|
| 632 | count = 1;
|
---|
| 633 | }
|
---|
| 634 | else
|
---|
| 635 | {
|
---|
| 636 | count++;
|
---|
| 637 | }
|
---|
| 638 | }
|
---|
| 639 | return formatted_str;
|
---|
| 640 | }
|
---|
| 641 |
|
---|
[23968] | 642 | public static String hashToSectionId(String hashString)
|
---|
| 643 | {
|
---|
[24450] | 644 | if (hashString == null || hashString.length() == 0)
|
---|
| 645 | {
|
---|
| 646 | return "";
|
---|
| 647 | }
|
---|
| 648 |
|
---|
[23968] | 649 | int firstDotIndex = hashString.indexOf(".");
|
---|
[24450] | 650 | if (firstDotIndex == -1)
|
---|
| 651 | {
|
---|
[23968] | 652 | return "";
|
---|
| 653 | }
|
---|
[24450] | 654 |
|
---|
[23968] | 655 | String sectionString = hashString.substring(firstDotIndex + 1);
|
---|
[24450] | 656 |
|
---|
[23968] | 657 | return sectionString;
|
---|
| 658 | }
|
---|
[24450] | 659 |
|
---|
[23968] | 660 | public static String hashToDepthClass(String hashString)
|
---|
| 661 | {
|
---|
[24450] | 662 | if (hashString == null || hashString.length() == 0)
|
---|
| 663 | {
|
---|
| 664 | return "";
|
---|
| 665 | }
|
---|
| 666 |
|
---|
[23968] | 667 | String sectionString = hashToSectionId(hashString);
|
---|
[24450] | 668 |
|
---|
[23968] | 669 | int count = sectionString.split("\\.").length;
|
---|
[24450] | 670 |
|
---|
[23968] | 671 | if (sectionString.equals(""))
|
---|
| 672 | {
|
---|
| 673 | return "sectionHeaderDepthTitle";
|
---|
| 674 | }
|
---|
[24450] | 675 | else
|
---|
[23968] | 676 | {
|
---|
| 677 | return "sectionHeaderDepth" + count;
|
---|
| 678 | }
|
---|
| 679 | }
|
---|
[25107] | 680 |
|
---|
[24928] | 681 | public static String escapeNewLines(String str)
|
---|
| 682 | {
|
---|
[25107] | 683 | if (str == null || str.length() < 1)
|
---|
[24928] | 684 | {
|
---|
| 685 | return null;
|
---|
| 686 | }
|
---|
| 687 | return str.replace("\n", "\\\n");
|
---|
| 688 | }
|
---|
[25107] | 689 |
|
---|
[24972] | 690 | public static String escapeQuotes(String str)
|
---|
| 691 | {
|
---|
[25107] | 692 | if (str == null || str.length() < 1)
|
---|
[24972] | 693 | {
|
---|
| 694 | return null;
|
---|
| 695 | }
|
---|
| 696 | return str.replace("\"", "\\\"");
|
---|
| 697 | }
|
---|
[25107] | 698 |
|
---|
[24972] | 699 | public static String escapeNewLinesAndQuotes(String str)
|
---|
| 700 | {
|
---|
[25107] | 701 | if (str == null || str.length() < 1)
|
---|
[24972] | 702 | {
|
---|
| 703 | return null;
|
---|
| 704 | }
|
---|
| 705 | return escapeNewLines(escapeQuotes(str));
|
---|
| 706 | }
|
---|
[25654] | 707 |
|
---|
[25567] | 708 | public static String getGlobalProperty(String name)
|
---|
| 709 | {
|
---|
| 710 | return GlobalProperties.getProperty(name);
|
---|
| 711 | }
|
---|
[25107] | 712 |
|
---|
| 713 | public static void clearMetadataStorage()
|
---|
| 714 | {
|
---|
| 715 | _foundTableValues.clear();
|
---|
| 716 | }
|
---|
| 717 |
|
---|
| 718 | public static boolean checkMetadataNotDuplicate(String name, String value)
|
---|
| 719 | {
|
---|
[25353] | 720 | if (_foundTableValues.containsKey(name))
|
---|
[25107] | 721 | {
|
---|
[25353] | 722 | for (String mapValue : _foundTableValues.get(name))
|
---|
[25107] | 723 | {
|
---|
[25353] | 724 | if (mapValue.equals(value))
|
---|
[25107] | 725 | {
|
---|
| 726 | return false;
|
---|
| 727 | }
|
---|
| 728 | }
|
---|
| 729 | _foundTableValues.get(name).add(value);
|
---|
| 730 | return true;
|
---|
| 731 | }
|
---|
[25353] | 732 |
|
---|
[25107] | 733 | ArrayList<String> newList = new ArrayList<String>();
|
---|
| 734 | newList.add(value);
|
---|
[25353] | 735 |
|
---|
[25107] | 736 | _foundTableValues.put(name, newList);
|
---|
[25353] | 737 |
|
---|
[25107] | 738 | return true;
|
---|
| 739 | }
|
---|
[25353] | 740 |
|
---|
[25312] | 741 | public static String reCAPTCHAimage(String publicKey, String privateKey)
|
---|
[25264] | 742 | {
|
---|
[25312] | 743 | ReCaptcha c = ReCaptchaFactory.newReCaptcha(publicKey, privateKey, false);
|
---|
[25264] | 744 | return c.createRecaptchaHtml(null, null);
|
---|
| 745 | }
|
---|
[25347] | 746 |
|
---|
[25567] | 747 | public static String getInterfaceStringsAsJavascript(String interface_name, String lang, String prefix)
|
---|
| 748 | {
|
---|
| 749 | String prependToPrefix = "gs.text";
|
---|
| 750 | return XSLTUtil.getInterfaceStringsAsJavascript(interface_name, lang, prefix, prependToPrefix);
|
---|
| 751 | }
|
---|
[25378] | 752 |
|
---|
[25567] | 753 | // generates javascript: 2 arrays are declared and populated with strings that declare variables and assign their values
|
---|
| 754 | // to be strings loaded from the interface_name.properties file for the language.
|
---|
| 755 | public static String getInterfaceStringsAsJavascript(String interface_name, String lang, String prefix, String prependToPrefix)
|
---|
| 756 | {
|
---|
[30770] | 757 | String prefixwithdot = prefix+".";
|
---|
[25567] | 758 | // 1. Generating Javascript of the form:
|
---|
| 759 | // if(!gs.text) { gs.text = new Array(); }
|
---|
| 760 | // if(!gs.text.dse) { gs.text.dse = new Array(); }
|
---|
| 761 | StringBuffer outputStr = new StringBuffer();
|
---|
| 762 | outputStr.append("if(!gs.text) { ");
|
---|
| 763 | outputStr.append(prependToPrefix + " = new Array(); ");
|
---|
| 764 | outputStr.append("}\n");
|
---|
| 765 | outputStr.append("if(!gs.text." + prefix + ") { ");
|
---|
| 766 | outputStr.append(prependToPrefix + "." + prefix + " = new Array(); ");
|
---|
| 767 | outputStr.append("}\n");
|
---|
| 768 |
|
---|
[26557] | 769 | int foundCount = 0;
|
---|
[25567] | 770 |
|
---|
[26557] | 771 | for (String dictName : new String[] { "interface_" + interface_name, "interface_default", "interface_default2" })
|
---|
[25567] | 772 | {
|
---|
[26557] | 773 | Dictionary dict = new Dictionary(dictName, lang);
|
---|
| 774 | Enumeration keys = dict.getKeys();
|
---|
| 775 | if (keys == null)
|
---|
[25567] | 776 | {
|
---|
[26557] | 777 | continue;
|
---|
| 778 | }
|
---|
[25567] | 779 |
|
---|
[26557] | 780 | // Get all properties in the language-specific dictionary with the given key prefix
|
---|
| 781 | // Create Javascript strings of the form:
|
---|
| 782 | // prependToPrefix.key= "value";\n
|
---|
| 783 | while (keys.hasMoreElements())
|
---|
| 784 | {
|
---|
| 785 | String key = (String) keys.nextElement();
|
---|
[30770] | 786 | if (key.startsWith(prefixwithdot))
|
---|
[26557] | 787 | {
|
---|
| 788 | String value = getInterfaceText(interface_name, dictName, lang, key, null);
|
---|
| 789 |
|
---|
| 790 | outputStr.append(prependToPrefix);
|
---|
| 791 | outputStr.append(".");
|
---|
| 792 | outputStr.append(key);
|
---|
| 793 | outputStr.append("=\"");
|
---|
| 794 | outputStr.append(value);
|
---|
| 795 | outputStr.append("\";\n");
|
---|
| 796 | }
|
---|
[25567] | 797 | }
|
---|
[26557] | 798 |
|
---|
| 799 | if (foundCount > 0)
|
---|
| 800 | {
|
---|
| 801 | break;
|
---|
| 802 | }
|
---|
[25567] | 803 | }
|
---|
| 804 |
|
---|
| 805 | return outputStr.toString();
|
---|
[25347] | 806 | }
|
---|
[25858] | 807 |
|
---|
[25654] | 808 | public static String xmlNodeToString(Node node)
|
---|
| 809 | {
|
---|
| 810 | return GSXML.xmlNodeToString(node);
|
---|
| 811 | }
|
---|
[25347] | 812 |
|
---|
[25567] | 813 | // Test from cmdline with:
|
---|
| 814 | // java -classpath /research/ak19/gs3-svn/web/WEB-INF/lib/gsdl3.jar:/research/ak19/gs3-svn/web/WEB-INF/lib/log4j-1.2.8.jar:/research/ak19/gs3-svn/web/WEB-INF/classes/ org.greenstone.gsdl3.util.XSLTUtil
|
---|
| 815 | public static void main(String args[])
|
---|
| 816 | {
|
---|
| 817 | System.out.println("\n@@@@@\n" + XSLTUtil.getInterfaceStringsAsJavascript("default", "en", "dse", "gs.text") + "@@@@@\n");
|
---|
| 818 | }
|
---|
[3341] | 819 | }
|
---|