001package org.hl7.fhir.utilities;
002
003import static org.apache.commons.lang3.StringUtils.isBlank;
004
005import java.io.BufferedInputStream;
006import java.io.File;
007import java.io.FileInputStream;
008import java.io.FileNotFoundException;
009import java.io.FileOutputStream;
010import java.io.FilenameFilter;
011import java.io.IOException;
012import java.io.InputStream;
013import java.io.UnsupportedEncodingException;
014import java.math.BigDecimal;
015import java.math.RoundingMode;
016import java.net.URLDecoder;
017import java.net.URLEncoder;
018import java.nio.file.Files;
019import java.nio.file.Path;
020import java.nio.file.Paths;
021import java.nio.file.StandardCopyOption;
022import java.time.Duration;
023import java.util.ArrayList;
024import java.util.Arrays;
025import java.util.Collection;
026import java.util.Collections;
027import java.util.LinkedList;
028import java.util.List;
029import java.util.Set;
030import java.util.UUID;
031import java.util.concurrent.TimeUnit;
032import java.util.zip.ZipEntry;
033import java.util.zip.ZipInputStream;
034
035/*
036  Copyright (c) 2011+, HL7, Inc.
037  All rights reserved.
038  
039  Redistribution and use in source and binary forms, with or without modification, 
040  are permitted provided that the following conditions are met:
041    
042   * Redistributions of source code must retain the above copyright notice, this 
043     list of conditions and the following disclaimer.
044   * Redistributions in binary form must reproduce the above copyright notice, 
045     this list of conditions and the following disclaimer in the documentation 
046     and/or other materials provided with the distribution.
047   * Neither the name of HL7 nor the names of its contributors may be used to 
048     endorse or promote products derived from this software without specific 
049     prior written permission.
050  
051  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
052  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
053  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
054  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
055  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
056  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
057  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
058  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
059  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
060  POSSIBILITY OF SUCH DAMAGE.
061  
062 */
063
064
065import org.apache.commons.io.FileUtils;
066import org.hl7.fhir.exceptions.FHIRException;
067
068public class Utilities {
069
070  private static final String UUID_REGEX = "[0-9a-f]{8}\\-[0-9a-f]{4}\\-[0-9a-f]{4}\\-[0-9a-f]{4}\\-[0-9a-f]{12}";
071  private static final String OID_REGEX = "[0-2](\\.(0|[1-9][0-9]*))+";
072
073  /**
074   * Returns the plural form of the word in the string.
075   * <p>
076   * Examples:
077   *
078   * <pre>
079   *   inflector.pluralize(&quot;post&quot;)               #=&gt; &quot;posts&quot;
080   *   inflector.pluralize(&quot;octopus&quot;)            #=&gt; &quot;octopi&quot;
081   *   inflector.pluralize(&quot;sheep&quot;)              #=&gt; &quot;sheep&quot;
082   *   inflector.pluralize(&quot;words&quot;)              #=&gt; &quot;words&quot;
083   *   inflector.pluralize(&quot;the blue mailman&quot;)   #=&gt; &quot;the blue mailmen&quot;
084   *   inflector.pluralize(&quot;CamelOctopus&quot;)       #=&gt; &quot;CamelOctopi&quot;
085   * </pre>
086   * <p>
087   * <p>
088   * <p>
089   * Note that if the {@link Object#toString()} is called on the supplied object, so this method works for non-strings, too.
090   *
091   * @param word the word that is to be pluralized.
092   * @return the pluralized form of the word, or the word itself if it could not be pluralized
093   * @see #singularize(Object)
094   */
095  public static String pluralizeMe(String word) {
096    Inflector inf = new Inflector();
097    return inf.pluralize(word);
098  }
099
100  public static String pluralize(String word, int count) {
101    if (count == 1)
102      return word;
103    Inflector inf = new Inflector();
104    return inf.pluralize(word);
105  }
106
107
108  public static boolean isInteger(String string) {
109    if (isBlank(string)) {
110      return false;
111    }
112    String value = string.startsWith("-") ? string.substring(1) : string;
113    for (char next : value.toCharArray()) {
114      if (!Character.isDigit(next)) {
115        return false;
116      }
117    }
118    // check bounds -2,147,483,648..2,147,483,647
119    if (value.length() > 10)
120      return false;
121    if (string.startsWith("-")) {
122      if (value.length() == 10 && string.compareTo("2147483648") > 0)
123        return false;
124    } else {
125      if (value.length() == 10 && string.compareTo("2147483647") > 0)
126        return false;
127    }
128    return true;
129  }
130
131  public static boolean isLong(String string) {
132    if (isBlank(string)) {
133      return false;
134    }
135    String value = string.startsWith("-") ? string.substring(1) : string;
136    for (char next : value.toCharArray()) {
137      if (!Character.isDigit(next)) {
138        return false;
139      }
140    }
141    // check bounds  -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807
142    if (value.length() > 20)
143      return false;
144    if (string.startsWith("-")) {
145      if (value.length() == 20 && string.compareTo("9223372036854775808") > 0)
146        return false;
147    } else {
148      if (value.length() == 20 && string.compareTo("9223372036854775807") > 0)
149        return false;
150    }
151    return true;
152  }
153
154  public static boolean isHex(String string) {
155    try {
156      int i = Integer.parseInt(string, 16);
157      return i != i + 1;
158    } catch (Exception e) {
159      return false;
160    }
161  }
162
163  public enum DecimalStatus {
164    BLANK, SYNTAX, RANGE, OK
165  }
166
167  public static boolean isDecimal(String value, boolean allowExponent, boolean allowLeadingZero) {
168    DecimalStatus ds = checkDecimal(value, allowExponent, true);
169    return ds == DecimalStatus.OK || ds == DecimalStatus.RANGE;
170  }
171
172  public static boolean isDecimal(String value, boolean allowExponent) {
173    DecimalStatus ds = checkDecimal(value, allowExponent, false);
174    return ds == DecimalStatus.OK || ds == DecimalStatus.RANGE;
175  }
176
177  public static DecimalStatus checkDecimal(String value, boolean allowExponent, boolean allowLeadingZero) {
178    if (isBlank(value)) {
179      return DecimalStatus.BLANK;
180    }
181
182    // check for leading zeros
183    if (!allowLeadingZero) {
184      if (value.startsWith("0") && !"0".equals(value) && !value.startsWith("0."))
185        return DecimalStatus.SYNTAX;
186      if (value.startsWith("-0") && !"-0".equals(value) && !value.startsWith("-0."))
187        return DecimalStatus.SYNTAX;
188      if (value.startsWith("+0") && !"+0".equals(value) && !value.startsWith("+0."))
189        return DecimalStatus.SYNTAX;
190    }
191
192    // check for trailing dot
193    if (value.endsWith(".")) {
194      return DecimalStatus.SYNTAX;
195    }
196
197    boolean havePeriod = false;
198    boolean haveExponent = false;
199    boolean haveSign = false;
200    boolean haveDigits = false;
201    int preDecLength = 0;
202    int postDecLength = 0;
203    int exponentLength = 0;
204    int length = 0;
205    for (char next : value.toCharArray()) {
206      if (next == '.') {
207        if (!haveDigits || havePeriod || haveExponent)
208          return DecimalStatus.SYNTAX;
209        havePeriod = true;
210        preDecLength = length;
211        length = 0;
212      } else if (next == '-' || next == '+') {
213        if (haveDigits || haveSign)
214          return DecimalStatus.SYNTAX;
215        haveSign = true;
216      } else if (next == 'e' || next == 'E') {
217        if (!haveDigits || haveExponent || !allowExponent)
218          return DecimalStatus.SYNTAX;
219        haveExponent = true;
220        haveSign = false;
221        haveDigits = false;
222        if (havePeriod)
223          postDecLength = length;
224        else
225          preDecLength = length;
226        length = 0;
227      } else if (!Character.isDigit(next)) {
228        return DecimalStatus.SYNTAX;
229      } else {
230        haveDigits = true;
231        length++;
232      }
233    }
234    if (haveExponent && !haveDigits)
235      return DecimalStatus.SYNTAX;
236    if (haveExponent)
237      exponentLength = length;
238    else if (havePeriod)
239      postDecLength = length;
240    else
241      preDecLength = length;
242
243    // now, bounds checking - these are arbitrary
244    if (exponentLength > 4)
245      return DecimalStatus.RANGE;
246    if (preDecLength + postDecLength > 18)
247      return DecimalStatus.RANGE;
248
249    return DecimalStatus.OK;
250  }
251
252  public static String camelCase(String value) {
253    return new Inflector().camelCase(value.trim().replace(" ", "_"), false);
254  }
255
256  public static String escapeXml(String doco) {
257    if (doco == null)
258      return "";
259
260    StringBuilder b = new StringBuilder();
261    for (char c : doco.toCharArray()) {
262      if (c == '<')
263        b.append("&lt;");
264      else if (c == '>')
265        b.append("&gt;");
266      else if (c == '&')
267        b.append("&amp;");
268      else if (c == '"')
269        b.append("&quot;");
270      else
271        b.append(c);
272    }
273    return b.toString();
274  }
275
276  public static String titleize(String s) {
277    StringBuilder b = new StringBuilder();
278    boolean up = true;
279    for (char c : s.toCharArray()) {
280      if (up)
281        b.append(Character.toUpperCase(c));
282      else
283        b.append(c);
284      up = c == ' ';
285    }
286    return b.toString();
287  }
288
289  public static String capitalize(String s) {
290    if (s == null) return null;
291    if (s.length() == 0) return s;
292    if (s.length() == 1) return s.toUpperCase();
293
294    return s.substring(0, 1).toUpperCase() + s.substring(1);
295  }
296
297  public static void copyDirectory(String sourceFolder, String destFolder, FileNotifier notifier) throws IOException, FHIRException {
298    CSFile src = new CSFile(sourceFolder);
299    if (!src.exists())
300      throw new FHIRException("Folder " + sourceFolder + " not found");
301    createDirectory(destFolder);
302
303    String[] files = src.list();
304    for (String f : files) {
305      if (new CSFile(sourceFolder + File.separator + f).isDirectory()) {
306        if (!f.startsWith(".")) // ignore .git files...
307          copyDirectory(sourceFolder + File.separator + f, destFolder + File.separator + f, notifier);
308      } else {
309        if (notifier != null)
310          notifier.copyFile(sourceFolder + File.separator + f, destFolder + File.separator + f);
311        copyFile(new CSFile(sourceFolder + File.separator + f), new CSFile(destFolder + File.separator + f));
312      }
313    }
314  }
315
316  public static void copyFile(String source, String dest) throws IOException {
317    copyFile(new File(source), new File(dest));
318  }
319
320  public static void copyFile(File sourceFile, File destFile) throws IOException {
321    if (!destFile.exists()) {
322      if (!new CSFile(destFile.getParent()).exists()) {
323        createDirectory(destFile.getParent());
324      }
325      destFile.createNewFile();
326    }
327
328    FileInputStream source = null;
329    FileOutputStream destination = null;
330
331    try {
332      source = new FileInputStream(sourceFile);
333      destination = new FileOutputStream(destFile);
334      destination.getChannel().transferFrom(source.getChannel(), 0, source.getChannel().size());
335    } finally {
336      if (source != null) {
337        source.close();
338      }
339      if (destination != null) {
340        destination.close();
341      }
342    }
343  }
344
345  public static boolean checkFolder(String dir, List<String> errors)
346    throws IOException {
347    if (!new CSFile(dir).exists()) {
348      errors.add("Unable to find directory " + dir);
349      return false;
350    } else {
351      return true;
352    }
353  }
354
355  public static boolean checkFile(String purpose, String dir, String file, List<String> errors)
356    throws IOException {
357    if (!new CSFile(dir + file).exists()) {
358      if (errors != null)
359        errors.add("Unable to find " + purpose + " file " + file + " in " + dir);
360      return false;
361    } else {
362      return true;
363    }
364  }
365
366  public static String asCSV(List<String> strings) {
367    StringBuilder s = new StringBuilder();
368    boolean first = true;
369    for (String n : strings) {
370      if (!first)
371        s.append(",");
372      s.append(n);
373      first = false;
374    }
375    return s.toString();
376  }
377
378  public static String asHtmlBr(String prefix, List<String> strings) {
379    StringBuilder s = new StringBuilder();
380    boolean first = true;
381    for (String n : strings) {
382      if (!first)
383        s.append("<br/>");
384      s.append(prefix);
385      s.append(n);
386      first = false;
387    }
388    return s.toString();
389  }
390
391  public static void clearDirectory(String folder, String... exemptions) throws IOException {
392    File dir = new File(folder);
393    if (dir.exists()) {
394      if (exemptions.length == 0)
395        FileUtils.cleanDirectory(dir);
396      else {
397        String[] files = new CSFile(folder).list();
398        if (files != null) {
399          for (String f : files) {
400            if (!existsInList(f, exemptions)) {
401              File fh = new CSFile(folder + File.separatorChar + f);
402              if (fh.isDirectory())
403                clearDirectory(fh.getAbsolutePath());
404              fh.delete();
405            }
406            }
407          }
408      }
409    }
410  }
411
412  public static File createDirectory(String path) throws IOException {
413    new CSFile(path).mkdirs();
414    return new File(path);
415  }
416
417  public static String changeFileExt(String name, String ext) {
418    if (name.lastIndexOf('.') > -1)
419      return name.substring(0, name.lastIndexOf('.')) + ext;
420    else
421      return name + ext;
422  }
423
424  public static String cleanupTextString(String contents) {
425    if (contents == null || contents.trim().equals(""))
426      return null;
427    else
428      return contents.trim();
429  }
430
431
432  public static boolean noString(String v) {
433    return v == null || v.equals("");
434  }
435
436
437  public static void bytesToFile(byte[] content, String filename) throws IOException {
438    FileOutputStream out = new FileOutputStream(filename);
439    out.write(content);
440    out.close();
441
442  }
443
444
445  public static String appendSlash(String definitions) {
446    return definitions.endsWith(File.separator) ? definitions : definitions + File.separator;
447  }
448
449  public static String appendForwardSlash(String definitions) {
450    return definitions.endsWith("/") ? definitions : definitions + "/";
451  }
452
453
454  public static String fileTitle(String file) {
455    if (file == null)
456      return null;
457    String s = new File(file).getName();
458    return s.indexOf(".") == -1 ? s : s.substring(0, s.indexOf("."));
459  }
460
461
462  public static String systemEol() {
463    return System.getProperty("line.separator");
464  }
465
466  public static String normaliseEolns(String value) {
467    return value.replace("\r\n", "\r").replace("\n", "\r").replace("\r", "\r\n");
468  }
469
470
471  public static String unescapeXml(String xml) throws FHIRException {
472    if (xml == null)
473      return null;
474
475    StringBuilder b = new StringBuilder();
476    int i = 0;
477    while (i < xml.length()) {
478      if (xml.charAt(i) == '&') {
479        StringBuilder e = new StringBuilder();
480        i++;
481        while (xml.charAt(i) != ';') {
482          e.append(xml.charAt(i));
483          i++;
484        }
485        if (e.toString().equals("lt"))
486          b.append("<");
487        else if (e.toString().equals("gt"))
488          b.append(">");
489        else if (e.toString().equals("amp"))
490          b.append("&");
491        else if (e.toString().equals("quot"))
492          b.append("\"");
493        else if (e.toString().equals("mu"))
494          b.append((char) 956);
495        else
496          throw new FHIRException("unknown XML entity \"" + e.toString() + "\"");
497      } else
498        b.append(xml.charAt(i));
499      i++;
500    }
501    return b.toString();
502  }
503
504  public static String unescapeJson(String json) throws FHIRException {
505    if (json == null)
506      return null;
507
508    StringBuilder b = new StringBuilder();
509    int i = 0;
510    while (i < json.length()) {
511      if (json.charAt(i) == '\\') {
512        i++;
513        char ch = json.charAt(i);
514        switch (ch) {
515          case '"':
516            b.append('b');
517            break;
518          case '\\':
519            b.append('\\');
520            break;
521          case '/':
522            b.append('/');
523            break;
524          case 'b':
525            b.append('\b');
526            break;
527          case 'f':
528            b.append('\f');
529            break;
530          case 'n':
531            b.append('\n');
532            break;
533          case 'r':
534            b.append('\r');
535            break;
536          case 't':
537            b.append('\t');
538            break;
539          case 'u':
540            String hex = json.substring(i + 1, i + 5);
541            b.append((char) Integer.parseInt(hex, 16));
542            break;
543          default:
544            throw new FHIRException("Unknown JSON escape \\" + ch);
545        }
546      } else
547        b.append(json.charAt(i));
548      i++;
549    }
550    return b.toString();
551  }
552
553
554  public static boolean isPlural(String word) {
555    word = word.toLowerCase();
556    if ("restricts".equals(word) || "contains".equals(word) || "data".equals(word) || "specimen".equals(word) || "replaces".equals(word) || "addresses".equals(word)
557      || "supplementalData".equals(word) || "instantiates".equals(word) || "imports".equals(word))
558      return false;
559    Inflector inf = new Inflector();
560    return !inf.singularize(word).equals(word);
561  }
562
563
564  public static String padRight(String src, char c, int len) {
565    StringBuilder s = new StringBuilder();
566    s.append(src);
567    for (int i = 0; i < len - src.length(); i++)
568      s.append(c);
569    return s.toString();
570  }
571
572
573  public static String padLeft(String src, char c, int len) {
574    StringBuilder s = new StringBuilder();
575    for (int i = 0; i < len - src.length(); i++)
576      s.append(c);
577    s.append(src);
578    return s.toString();
579  }
580
581
582  public static String path(String... args) throws IOException {
583    StringBuilder s = new StringBuilder();
584    boolean d = false;
585    boolean first = true;
586    for (String arg : args) {
587      if (first && arg == null)
588        continue;
589      first = false;
590      if (!d)
591        d = !noString(arg);
592      else if (!s.toString().endsWith(File.separator))
593        s.append(File.separator);
594      String a = arg;
595      if (s.length() == 0) {
596        if ("[tmp]".equals(a)) {
597          if (hasCTempDir()) {
598            a = "c:\\temp";
599          } else if (ToolGlobalSettings.hasTempPath()) {            
600            a = ToolGlobalSettings.getTempPath();
601          } else {
602            a = System.getProperty("java.io.tmpdir");
603          }
604        } else if ("[user]".equals(a)) {
605          a = System.getProperty("user.home");
606        } else if (a.startsWith("[") && a.endsWith("]")) {
607          String ev = System.getenv(a.replace("[", "").replace("]", ""));
608          if (ev != null) {
609            a = ev;
610          } else {
611            a = "null";
612          }
613        }
614      }
615      a = a.replace("\\", File.separator);
616      a = a.replace("/", File.separator);
617      if (s.length() > 0 && a.startsWith(File.separator))
618        a = a.substring(File.separator.length());
619
620      while (a.startsWith(".." + File.separator)) {
621        if (s.length() == 0) {
622          s = new StringBuilder(Paths.get(".").toAbsolutePath().normalize().toString());
623        } else {
624          String p = s.toString().substring(0, s.length() - 1);
625          if (!p.contains(File.separator)) {
626            s = new StringBuilder();
627          } else {
628            s = new StringBuilder(p.substring(0, p.lastIndexOf(File.separator)) + File.separator);
629          }
630        }
631        a = a.substring(3);
632      }
633      if ("..".equals(a)) {
634        int i = s.substring(0, s.length() - 1).lastIndexOf(File.separator);
635        s = new StringBuilder(s.substring(0, i + 1));
636      } else
637        s.append(a);
638    }
639    return s.toString();
640  }
641
642  private static boolean hasCTempDir() {
643    if (!System.getProperty("os.name").toLowerCase().contains("win")) {
644      return false;
645    }
646    File tmp = new File("c:\\temp");
647    return tmp.exists() && tmp.isDirectory() && tmp.canWrite();
648  }
649
650  public static String pathURL(String... args) {
651    StringBuilder s = new StringBuilder();
652    boolean d = false;
653    for (String arg : args) {
654      if (arg != null) {
655        if (!d)
656          d = !noString(arg);
657        else if (s.toString() != null && !s.toString().endsWith("/") && !arg.startsWith("/"))
658          s.append("/");
659        s.append(arg);
660      }
661    }
662    return s.toString();
663  }
664
665  public static String nmtokenize(String cs) {
666    if (cs == null)
667      return "";
668    StringBuilder s = new StringBuilder();
669    for (int i = 0; i < cs.length(); i++) {
670      char c = cs.charAt(i);
671      if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '-' || c == '_')
672        s.append(c);
673      else if (c != ' ')
674        s.append("." + Integer.toString(c));
675    }
676    return s.toString();
677  }
678
679
680  public static boolean isToken(String tail) {
681    if (tail == null || tail.length() == 0)
682      return false;
683    boolean result = isAlphabetic(tail.charAt(0));
684    for (int i = 1; i < tail.length(); i++) {
685      result = result && (isAlphabetic(tail.charAt(i)) || isDigit(tail.charAt(i)) || (tail.charAt(i) == '_') || (tail.charAt(i) == '[') || (tail.charAt(i) == ']'));
686    }
687    return result;
688  }
689
690
691  public static boolean isDigit(char c) {
692    return (c >= '0') && (c <= '9');
693  }
694
695
696  public static boolean isAlphabetic(char c) {
697    return ((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z'));
698  }
699
700
701  public static String getDirectoryForFile(String filepath) {
702    File f = new File(filepath);
703    return f.getParent();
704  }
705
706  public static String appendPeriod(String s) {
707    if (Utilities.noString(s))
708      return s;
709    s = s.trim();
710    if (s.endsWith(".") || s.endsWith("?"))
711      return s;
712    return s + ".";
713  }
714
715
716  public static String removePeriod(String s) {
717    if (Utilities.noString(s))
718      return s;
719    if (s.endsWith("."))
720      return s.substring(0, s.length() - 1);
721    return s;
722  }
723
724
725  public static String stripBOM(String string) {
726    return string.replace("\uFEFF", "");
727  }
728
729
730  public static String oidTail(String id) {
731    if (id == null || !id.contains("."))
732      return id;
733    return id.substring(id.lastIndexOf(".") + 1);
734  }
735
736
737  public static String oidRoot(String id) {
738    if (id == null || !id.contains("."))
739      return id;
740    return id.substring(0, id.indexOf("."));
741  }
742
743  public static String escapeJava(String doco) {
744    if (doco == null)
745      return "";
746
747    StringBuilder b = new StringBuilder();
748    for (char c : doco.toCharArray()) {
749      if (c == '\r')
750        b.append("\\r");
751      else if (c == '\n')
752        b.append("\\n");
753      else if (c == '"')
754        b.append("\\\"");
755      else if (c == '\\')
756        b.append("\\\\");
757      else
758        b.append(c);
759    }
760    return b.toString();
761  }
762
763
764  public static String[] splitByCamelCase(String name) {
765    List<String> parts = new ArrayList<String>();
766    StringBuilder b = new StringBuilder();
767    for (int i = 0; i < name.length(); i++) {
768      if (i > 0 && Character.isUpperCase(name.charAt(i))) {
769        parts.add(b.toString());
770        b = new StringBuilder();
771      }
772      b.append(Character.toLowerCase(name.charAt(i)));
773    }
774    parts.add(b.toString());
775    return parts.toArray(new String[]{});
776  }
777
778
779  public static String encodeUri(String v) {
780    return v.replace(" ", "%20").replace("?", "%3F").replace("=", "%3D").replace("|", "%7C");
781  }
782
783
784  public static String normalize(String s) {
785    if (noString(s))
786      return null;
787    StringBuilder b = new StringBuilder();
788    boolean isWhitespace = false;
789    for (int i = 0; i < s.length(); i++) {
790      char c = s.charAt(i);
791      if (!Character.isWhitespace(c)) {
792        b.append(Character.toLowerCase(c));
793        isWhitespace = false;
794      } else if (!isWhitespace) {
795        b.append(' ');
796        isWhitespace = true;
797      }
798    }
799    return b.toString().trim();
800  }
801
802  public static String normalizeSameCase(String s) {
803    if (noString(s))
804      return null;
805    StringBuilder b = new StringBuilder();
806    boolean isWhitespace = false;
807    for (int i = 0; i < s.length(); i++) {
808      char c = s.charAt(i);
809      if (!Character.isWhitespace(c)) {
810        b.append(c);
811        isWhitespace = false;
812      } else if (!isWhitespace) {
813        b.append(' ');
814        isWhitespace = true;
815      }
816    }
817    return b.toString().trim();
818  }
819
820
821  public static void copyFileToDirectory(File source, File destDir) throws IOException {
822    copyFile(source, new File(path(destDir.getAbsolutePath(), source.getName())));
823  }
824
825
826  public static boolean isWhitespace(String s) {
827    boolean ok = true;
828    for (int i = 0; i < s.length(); i++)
829      ok = ok && Character.isWhitespace(s.charAt(i));
830    return ok;
831
832  }
833
834
835  public static String URLEncode(String string) {
836    try {
837      return URLEncoder.encode(string, "UTF-8");
838    } catch (UnsupportedEncodingException e) {
839      throw new Error(e.getMessage());
840    }
841  }
842
843
844  public static String URLDecode(String ref) {
845    try {
846      return URLDecoder.decode(ref, "UTF-8");
847    } catch (UnsupportedEncodingException e) {
848      throw new Error(e.getMessage());
849    }
850  }
851
852  public static boolean charInSet(char value, char... array) {
853    for (int i : array)
854      if (value == i)
855        return true;
856    return false;
857  }
858
859
860  public static boolean charInRange(char ch, char a, char z) {
861    return ch >= a && ch <= z;
862  }
863
864  public static boolean existsInList(String value, List<String> array) {
865    if (value == null)
866      return false;
867    for (String s : array)
868      if (value.equals(s))
869        return true;
870    return false;
871  }
872
873  public static boolean existsInList(String value, String... array) {
874    if (value == null)
875      return false;
876    for (String s : array)
877      if (value.equals(s))
878        return true;
879    return false;
880  }
881
882  public static boolean existsInList(int value, int... array) {
883    for (int i : array)
884      if (value == i)
885        return true;
886    return false;
887  }
888
889  public static boolean existsInListNC(String value, String... array) {
890    for (String s : array)
891      if (value.equalsIgnoreCase(s))
892        return true;
893    return false;
894  }
895
896
897  public static String getFileNameForName(String name) {
898    return name.toLowerCase();
899  }
900
901  public static void deleteTempFiles() throws IOException {
902    File file = createTempFile("test", "test");
903    String folder = getDirectoryForFile(file.getAbsolutePath());
904    String[] list = new File(folder).list(new FilenameFilter() {
905      public boolean accept(File dir, String name) {
906        return name.startsWith("ohfu-");
907      }
908    });
909    if (list != null) {
910      for (String n : list) {
911        new File(path(folder, n)).delete();
912      }
913    }
914  }
915
916  public static File createTempFile(String prefix, String suffix) throws IOException {
917    // this allows use to eaily identify all our dtemp files and delete them, since delete on Exit doesn't really work.
918    File file = File.createTempFile("ohfu-" + prefix, suffix);
919    file.deleteOnExit();
920    return file;
921  }
922
923
924  public static boolean isAsciiChar(char ch) {
925    return ch >= ' ' && ch <= '~';
926  }
927
928
929  public static String makeUuidLC() {
930    return UUID.randomUUID().toString().toLowerCase();
931  }
932
933  public static String makeUuidUrn() {
934    return "urn:uuid:" + UUID.randomUUID().toString().toLowerCase();
935  }
936
937  public static boolean isURL(String s) {
938    boolean ok = s.matches("^http(s{0,1})://[a-zA-Z0-9_/\\-\\.]+\\.([A-Za-z/]{2,5})[a-zA-Z0-9_/\\&\\?\\=\\-\\.\\~\\%]*");
939    return ok;
940  }
941
942
943  public static String escapeJson(String value) {
944    if (value == null)
945      return "";
946
947    StringBuilder b = new StringBuilder();
948    for (char c : value.toCharArray()) {
949      if (c == '\r')
950        b.append("\\r");
951      else if (c == '\n')
952        b.append("\\n");
953      else if (c == '\t')
954        b.append("\\t");
955      else if (c == '"')
956        b.append("\\\"");
957      else if (c == '\\')
958        b.append("\\\\");
959      else if (((int) c) < 32)
960        b.append("\\u" + Utilities.padLeft(String.valueOf((int) c), '0', 4));
961      else
962        b.append(c);
963    }
964    return b.toString();
965  }
966
967  public static String humanize(String code) {
968    StringBuilder b = new StringBuilder();
969    boolean lastBreak = true;
970    for (char c : code.toCharArray()) {
971      if (Character.isLetter(c)) {
972        if (lastBreak)
973          b.append(Character.toUpperCase(c));
974        else {
975          if (Character.isUpperCase(c))
976            b.append(" ");
977          b.append(c);
978        }
979        lastBreak = false;
980      } else {
981        b.append(" ");
982        lastBreak = true;
983      }
984    }
985    if (b.length() == 0)
986      return code;
987    else
988      return b.toString();
989  }
990
991
992  public static String uncapitalize(String s) {
993    if (s == null) return null;
994    if (s.length() == 0) return s;
995    if (s.length() == 1) return s.toLowerCase();
996
997    return s.substring(0, 1).toLowerCase() + s.substring(1);
998  }
999
1000  public static int charCount(String s, char c) {
1001    int res = 0;
1002    for (char ch : s.toCharArray())
1003      if (ch == c)
1004        res++;
1005    return res;
1006  }
1007
1008  public static boolean isOid(String cc) {
1009    return cc.matches(OID_REGEX);
1010  }
1011
1012  public static boolean equals(String one, String two) {
1013    if (one == null && two == null)
1014      return true;
1015    if (one == null || two == null)
1016      return false;
1017    return one.equals(two);
1018  }
1019
1020
1021  public static void deleteAllFiles(String folder, String type) {
1022    File src = new File(folder);
1023    String[] files = src.list();
1024    for (String f : files) {
1025      if (new File(folder + File.separator + f).isDirectory()) {
1026        deleteAllFiles(folder + File.separator + f, type);
1027      } else if (f.endsWith(type)) {
1028        new File(folder + File.separator + f).delete();
1029      }
1030    }
1031
1032  }
1033
1034  public static boolean compareIgnoreWhitespace(File f1, File f2) throws IOException {
1035    InputStream in1 = null;
1036    InputStream in2 = null;
1037    try {
1038      in1 = new BufferedInputStream(new FileInputStream(f1));
1039      in2 = new BufferedInputStream(new FileInputStream(f2));
1040
1041      int expectedByte = in1.read();
1042      while (expectedByte != -1) {
1043        boolean w1 = isWhitespace(expectedByte);
1044        if (w1)
1045          while (isWhitespace(expectedByte))
1046            expectedByte = in1.read();
1047        int foundByte = in2.read();
1048        if (w1) {
1049          if (!isWhitespace(foundByte))
1050            return false;
1051          while (isWhitespace(foundByte))
1052            foundByte = in2.read();
1053        }
1054        if (expectedByte != foundByte)
1055          return false;
1056        expectedByte = in1.read();
1057      }
1058      if (in2.read() != -1) {
1059        return false;
1060      }
1061      return true;
1062    } finally {
1063      if (in1 != null) {
1064        try {
1065          in1.close();
1066        } catch (IOException e) {
1067        }
1068      }
1069      if (in2 != null) {
1070        try {
1071          in2.close();
1072        } catch (IOException e) {
1073        }
1074      }
1075    }
1076  }
1077
1078  private static boolean isWhitespace(int b) {
1079    return b == 9 || b == 10 || b == 13 || b == 32;
1080  }
1081
1082
1083  public static boolean compareIgnoreWhitespace(String fn1, String fn2) throws IOException {
1084    return compareIgnoreWhitespace(new File(fn1), new File(fn2));
1085  }
1086
1087
1088  public static boolean isAbsoluteUrl(String ref) {
1089    if (ref != null && ref.contains(":")) {
1090      String scheme = ref.substring(0, ref.indexOf(":"));
1091      String details = ref.substring(ref.indexOf(":")+1);
1092      return (existsInList(scheme, "http", "https", "urn") || (isToken(scheme) && scheme.equals(scheme.toLowerCase())) || Utilities.startsWithInList(ref, "urn:iso:", "urn:iso-iec:", "urn:iso-cie:", "urn:iso-astm:", "urn:iso-ieee:", "urn:iec:"))
1093          && details != null && details.length() > 0 && !details.contains(" "); // rfc5141
1094    }
1095    return false; 
1096  }
1097  
1098  public static boolean isAbsoluteUrlLinkable(String ref) {
1099    if (ref != null && ref.contains(":")) {
1100      String scheme = ref.substring(0, ref.indexOf(":"));
1101      String details = ref.substring(ref.indexOf(":")+1);
1102      return (existsInList(scheme, "http", "https", "ftp"))
1103          && details != null && details.length() > 0 && !details.contains(" "); // rfc5141
1104    }
1105    return false; 
1106  }
1107
1108  public static boolean equivalent(String l, String r) {
1109    if (Utilities.noString(l) && Utilities.noString(r))
1110      return true;
1111    if (Utilities.noString(l) || Utilities.noString(r))
1112      return false;
1113    return l.toLowerCase().equals(r.toLowerCase());
1114  }
1115
1116
1117  public static boolean equivalentNumber(String l, String r) {
1118    if (Utilities.noString(l) && Utilities.noString(r))
1119      return true;
1120    if (Utilities.noString(l) || Utilities.noString(r))
1121      return false;
1122    if (!Utilities.isDecimal(l, true) || !Utilities.isDecimal(r, true))
1123      return false;
1124    BigDecimal dl = new BigDecimal(l);
1125    BigDecimal dr = new BigDecimal(r);
1126    if (dl.scale() < dr.scale()) {
1127      dr = dr.setScale(dl.scale(), RoundingMode.HALF_UP);
1128    } else if (dl.scale() > dr.scale()) {
1129      dl = dl.setScale(dr.scale(), RoundingMode.HALF_UP);
1130    }
1131    return dl.equals(dr);
1132  }
1133
1134  public static String getFileExtension(String fn) {
1135    return fn.contains(".") ? fn.substring(fn.lastIndexOf(".") + 1) : "";
1136  }
1137
1138
1139  public static String unCamelCase(String name) {
1140    StringBuilder b = new StringBuilder();
1141    boolean first = true;
1142    for (char c : name.toCharArray()) {
1143      if (Character.isUpperCase(c)) {
1144        if (!first)
1145          b.append(" ");
1146        b.append(Character.toLowerCase(c));
1147      } else
1148        b.append(c);
1149      first = false;
1150    }
1151    return b.toString();
1152  }
1153
1154
1155  public static boolean isAbsoluteFileName(String source) {
1156    if (isWindows())
1157      return (source.length() > 2 && source.charAt(1) == ':') || source.startsWith("\\\\");
1158    else
1159      return source.startsWith("//");
1160  }
1161
1162
1163  public static boolean isWindows() {
1164    return System.getProperty("os.name").startsWith("Windows");
1165  }
1166
1167
1168  public static String splitLineForLength(String line, int prefixLength, int indent, int allowedLength) {
1169    List<String> list = new ArrayList<String>();
1170    while (prefixLength + line.length() > allowedLength) {
1171      int i = allowedLength - (list.size() == 0 ? prefixLength : indent);
1172      while (i > 0 && line.charAt(i) != ' ')
1173        i--;
1174      if (i == 0)
1175        break;
1176      list.add(line.substring(0, i));
1177      line = line.substring(i + 1);
1178    }
1179    list.add(line);
1180    StringBuilder b = new StringBuilder();
1181    boolean first = true;
1182    for (String s : list) {
1183      if (first)
1184        first = false;
1185      else
1186        b.append("\r\n" + padLeft("", ' ', indent));
1187      b.append(s);
1188    }
1189    return b.toString();
1190  }
1191
1192
1193  public static int countFilesInDirectory(String dirName) {
1194    File dir = new File(dirName);
1195    if (dir.exists() == false) {
1196      return 0;
1197    }
1198    int i = 0;
1199    for (File f : dir.listFiles())
1200      if (!f.isDirectory())
1201        i++;
1202    return i;
1203  }
1204
1205  public static String makeId(String name) {
1206    StringBuilder b = new StringBuilder();
1207    for (char ch : name.toCharArray()) {
1208      if (ch >= 'a' && ch <= 'z')
1209        b.append(ch);
1210      else if (ch >= 'A' && ch <= 'Z')
1211        b.append(ch);
1212      else if (ch >= '0' && ch <= '9')
1213        b.append(ch);
1214      else if (ch == '-' || ch == '.')
1215        b.append(ch);
1216    }
1217    return b.toString();
1218  }
1219
1220  public interface FileVisitor {
1221    void visitFile(File file) throws FileNotFoundException, IOException;
1222  }
1223
1224  public static void visitFiles(String folder, String extension, FileVisitor visitor) throws FileNotFoundException, IOException {
1225    visitFiles(new File(folder), extension, visitor);
1226  }
1227
1228  public static void visitFiles(File folder, String extension, FileVisitor visitor) throws FileNotFoundException, IOException {
1229    for (File file : folder.listFiles()) {
1230      if (file.isDirectory())
1231        visitFiles(file, extension, visitor);
1232      else if (extension == null || file.getName().endsWith(extension))
1233        visitor.visitFile(file);
1234    }
1235  }
1236
1237  public static String extractBaseUrl(String url) {
1238    if (url == null)
1239      return null;
1240    else if (url.contains("/"))
1241      return url.substring(0, url.lastIndexOf("/"));
1242    else
1243      return url;
1244  }
1245
1246  public static String listCanonicalUrls(Set<String> keys) {
1247    return keys.toString();
1248  }
1249
1250  public static boolean isValidId(String id) {
1251    return id.matches("[A-Za-z0-9\\-\\.]{1,64}");
1252  }
1253
1254  public static List<String> sorted(Set<String> set) {
1255    List<String> list = new ArrayList<>();
1256    list.addAll(set);
1257    Collections.sort(list);
1258    return list;
1259  }
1260
1261  public static void analyseStringDiffs(Set<String> source, Set<String> target, Set<String> missed, Set<String> extra) {
1262    for (String s : source)
1263      if (!target.contains(s))
1264        missed.add(s);
1265    for (String s : target)
1266      if (!source.contains(s))
1267        extra.add(s);
1268
1269  }
1270
1271  /**
1272   * Only handles simple FHIRPath expressions of the type produced by the validator
1273   *
1274   * @param path
1275   * @return
1276   */
1277  public static String fhirPathToXPath(String path) {
1278    String[] p = path.split("\\.");
1279    CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder(".");
1280    int i = 0;
1281    while (i < p.length) {
1282      String s = p[i];
1283      if (s.contains("[")) {
1284        String si = s.substring(s.indexOf("[") + 1, s.length() - 1);
1285        if (!Utilities.isInteger(si))
1286          throw new FHIRException("The FHIRPath expression '" + path + "' is not valid");
1287        s = s.substring(0, s.indexOf("[")) + "[" + Integer.toString(Integer.parseInt(si) + 1) + "]";
1288      }
1289      if (i < p.length - 1 && p[i + 1].startsWith(".ofType(")) {
1290        i++;
1291        s = s + capitalize(p[i].substring(8, p.length - 1));
1292      }
1293      b.append(s);
1294      i++;
1295    }
1296    return b.toString();
1297  }
1298
1299  public static String describeDuration(Duration d) {
1300    if (d.toDays() > 2) {
1301      return String.format("%s days", d.toDays());
1302    } else if (d.toHours() > 2) {
1303      return String.format("%s hours", d.toHours());
1304    } else if (d.toMinutes() > 2) {
1305      return String.format("%s mins", d.toMinutes());
1306    } else {
1307      return String.format("%s ms", d.toMillis());
1308    }
1309  }
1310
1311  public static boolean startsWithInList(String s, String... list) {
1312    if (s == null) {
1313      return false;
1314    }
1315    for (String l : list) {
1316      if (s.startsWith(l)) {
1317        return true;
1318      }
1319    }
1320    return false;
1321  }
1322  
1323  public static boolean startsWithInList(String s, Collection<String> list) {
1324    if (s == null) {
1325      return false;
1326    }
1327    for (String l : list) {
1328      if (s.startsWith(l)) {
1329        return true;
1330      }
1331    }
1332    return false;
1333  }
1334
1335  public static final int ONE_MB = 1024;
1336  public static final String GB = "Gb";
1337  public static final String MB = "Mb";
1338  public static final String KB = "Kb";
1339  public static final String BT = "b";
1340
1341  public static String describeSize(int length) {
1342    if (length < 0) throw new IllegalArgumentException("File length of < 0  passed in...");
1343
1344    if (length > Math.pow(ONE_MB, 3)) {
1345      return length / ((long) Math.pow(ONE_MB, 3)) + GB;
1346    }
1347    if (length > Math.pow(ONE_MB, 2)) {
1348      return length / ((long) Math.pow(ONE_MB, 2)) + MB;
1349    }
1350    if (length > ONE_MB) {
1351      return length / (ONE_MB) + KB;
1352    }
1353    return length + BT;
1354  }
1355
1356  public static String describeSize(long length) {
1357    if (length < 0) throw new IllegalArgumentException("File length of < 0  passed in...");
1358
1359    if (length > Math.pow(ONE_MB, 3)) {
1360      return length / ((long) Math.pow(ONE_MB, 3)) + GB;
1361    }
1362    if (length > Math.pow(ONE_MB, 2)) {
1363      return length / ((long) Math.pow(ONE_MB, 2)) + MB;
1364    }
1365    if (length > ONE_MB) {
1366      return length / (ONE_MB) + KB;
1367    }
1368    return length + BT;
1369  }
1370
1371  public static List<byte[]> splitBytes(byte[] array, byte[] delimiter) {
1372    List<byte[]> byteArrays = new LinkedList<byte[]>();
1373    if (delimiter.length == 0)
1374    {
1375      return byteArrays;
1376    }
1377    int begin = 0;
1378
1379    outer: for (int i = 0; i < array.length - delimiter.length + 1; i++)
1380    {
1381      for (int j = 0; j < delimiter.length; j++)
1382      {
1383        if (array[i + j] != delimiter[j])
1384        {
1385          continue outer;
1386        }
1387      }
1388
1389      // If delimiter is at the beginning then there will not be any data.
1390      if (begin < i)
1391        byteArrays.add(Arrays.copyOfRange(array, begin, i));
1392      begin = i + delimiter.length;
1393    }
1394
1395    // delimiter at the very end with no data following?
1396    if (begin != array.length)
1397      byteArrays.add(Arrays.copyOfRange(array, begin, array.length));
1398
1399    return byteArrays;
1400  }
1401  
1402  public static String presentDuration(long duration) {
1403    duration = duration / 1000000;
1404    String res = "";    // ;
1405    long days       = TimeUnit.MILLISECONDS.toDays(duration);
1406    long hours      = TimeUnit.MILLISECONDS.toHours(duration) -
1407        TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(duration));
1408    long minutes    = TimeUnit.MILLISECONDS.toMinutes(duration) -
1409        TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(duration));
1410    long seconds    = TimeUnit.MILLISECONDS.toSeconds(duration) -
1411        TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(duration));
1412    long millis     = TimeUnit.MILLISECONDS.toMillis(duration) -
1413        TimeUnit.SECONDS.toMillis(TimeUnit.MILLISECONDS.toSeconds(duration));
1414
1415    if (days > 0)
1416      res = String.format("%dd %02d:%02d:%02d.%04d", days, hours, minutes, seconds, millis);
1417    else if (hours > 0)
1418      res = String.format("%02d:%02d:%02d.%04d", hours, minutes, seconds, millis);
1419    else //
1420      res = String.format("%02d:%02d.%04d", minutes, seconds, millis);
1421//    else
1422//      res = String.format("%02d.%04d", seconds, millis);
1423    return res;
1424  }
1425
1426  public static void unzip(InputStream zip, Path target) throws IOException {
1427    try (ZipInputStream zis = new ZipInputStream(zip)) {
1428      ZipEntry zipEntry = zis.getNextEntry();
1429      while (zipEntry != null) {
1430        boolean isDirectory = false;
1431        if (zipEntry.getName().endsWith("/") || zipEntry.getName().endsWith("\\")) {
1432          isDirectory = true;
1433        }
1434        Path newPath = zipSlipProtect(zipEntry, target);
1435        if (isDirectory) {
1436          Files.createDirectories(newPath);
1437        } else {
1438          if (newPath.getParent() != null) {
1439            if (Files.notExists(newPath.getParent())) {
1440              Files.createDirectories(newPath.getParent());
1441            }
1442          }
1443          Files.copy(zis, newPath, StandardCopyOption.REPLACE_EXISTING);
1444        }
1445        zipEntry = zis.getNextEntry();
1446      }
1447      zis.closeEntry();
1448    }
1449  }
1450
1451  private static Path zipSlipProtect(ZipEntry zipEntry, Path targetDir)
1452      throws IOException {
1453
1454    // test zip slip vulnerability
1455    // Path targetDirResolved = targetDir.resolve("../../" + zipEntry.getName());
1456
1457    Path targetDirResolved = targetDir.resolve(zipEntry.getName());
1458
1459    // make sure normalized file still has targetDir as its prefix
1460    // else throws exception
1461    Path normalizePath = targetDirResolved.normalize();
1462    if (!normalizePath.startsWith(targetDir)) {
1463      throw new IOException("Bad zip entry: " + zipEntry.getName());
1464    }
1465
1466    return normalizePath;
1467  }
1468
1469  final static int[] illegalChars = {34, 60, 62, 124, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 58, 42, 63, 92, 47};
1470  
1471  static {
1472    Arrays.sort(illegalChars);
1473  }
1474
1475  public static String cleanFileName(String badFileName) {
1476    StringBuilder cleanName = new StringBuilder();
1477    int len = badFileName.codePointCount(0, badFileName.length());
1478    for (int i=0; i<len; i++) {
1479      int c = badFileName.codePointAt(i);
1480      if (Arrays.binarySearch(illegalChars, c) < 0) {
1481        cleanName.appendCodePoint(c);
1482      }
1483    }
1484    return cleanName.toString();
1485  }
1486
1487  public static boolean isValidUUID(String uuid) {
1488    return uuid.matches(UUID_REGEX);
1489  }
1490
1491  public static boolean isValidOID(String oid) {
1492    return oid.matches(OID_REGEX);
1493  }
1494
1495  public static int findinList(String[] list, String val) {
1496    for (int i = 0; i < list.length; i++) {
1497      if (val.equals(list[i])) {
1498        return i;
1499      }
1500    }
1501    return -1;
1502  }
1503
1504  public static String toString(String[] expected) {
1505    return "['"+String.join("' | '", expected)+"']";
1506  }
1507
1508
1509}