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