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