Class CMLUtils

java.lang.Object
org.episteme.natural.chemistry.loaders.cml.util.CMLUtils

public class CMLUtils extends Object
A number of miscellaneous tools. Originally devised for jumbo.sgml, now rewritten for jumbo.xml. Use these at your peril - some will be phased out
Since:
1.0
Author:
Silvere Martin-Michiellot, Gemini AI (Google DeepMind)
  • Field Details

  • Constructor Details

    • CMLUtils

      public CMLUtils()
  • Method Details

    • deleteFile

      public static boolean deleteFile(File file, boolean deleteDirectory)
      delete a file If directory==true then file will be recursively deleted
      Parameters:
      file - Description of the Parameter
      deleteDirectory - Description of the Parameter
      Returns:
      Description of the Return Value
    • copyFile

      public static void copyFile(File inFile, File outFile) throws FileNotFoundException, IOException
      copy one file to another (I suspect there is a better way
      Parameters:
      inFile - Description of the Parameter
      outFile - Description of the Parameter
      Throws:
      FileNotFoundException - Description of the Exception
      IOException - Description of the Exception
    • dump

      public static String dump(URL url) throws Exception
      reads a stream from url and outputs it as integer values of the characters and as strings. Emulates UNIX od().
      Parameters:
      url - Description of the Parameter
      Returns:
      String tabular version of input (in 10-column chunks)
      Throws:
      Exception - Description of the Exception
    • flush

      public static void flush(String s)
      output String and flush()
      Parameters:
      s - Description of the Parameter
    • spaces

      public static String spaces(int nspace)
      make a String of a given number of spaces
      Parameters:
      nspace - Description of the Parameter
      Returns:
      Description of the Return Value
    • getSuffix

      public static String getSuffix(String filename)
      gets suffix from filename
      Parameters:
      filename - Description of the Parameter
      Returns:
      The suffix value
    • skipWhite

      public static int skipWhite(BufferedReader bReader) throws Exception
      skip white lines and end with first non-white line Leaves bReader ready to read first non-white line
      Parameters:
      bReader - Description of the Parameter
      Returns:
      int number of lines skipped
      Throws:
      Exception - Description of the Exception
    • truncate

      public static String truncate(String s, int maxlength)
      return the first n characters of a string and add ellipses if truncated
      Parameters:
      s - Description of the Parameter
      maxlength - Description of the Parameter
      Returns:
      String the (possibly) truncated string
    • getIntegerFromRoman

      public static int getIntegerFromRoman(String roman)
      translate Roman Numerals up to 50 Some normalisation is performed Failure returns -1
      Parameters:
      roman - Description of the Parameter
      Returns:
      The integerFromRoman value
    • setSystemProperty

      public static void setSystemProperty(String property, String value)
      add a property to the System ones Don't know if this is a good idea...
      Parameters:
      property - The new systemProperty value
      value - The new systemProperty value
    • addToSystemProperties

      public static void addToSystemProperties(String urlString) throws IOException
      load a file/url into the system properties,
      Parameters:
      urlString - The feature to be added to the ToSystemProperties attribute
      Throws:
      IOException - Description of the Exception
    • getNewInstance

      public static Object getNewInstance(String className) throws Exception
      gets a new instance of a class from a hashtable because normal methods are very slow
      Parameters:
      className - Description of the Parameter
      Returns:
      The newInstance value
      Throws:
      Exception - Description of the Exception
    • deQuote

      public static String deQuote(String s)
      remove balanced quotes from ends of (trimmed) string, else no action
      Parameters:
      s - Description of the Parameter
      Returns:
      Description of the Return Value
    • rightTrim

      public static String rightTrim(String s)
      remove trailing blanks
      Parameters:
      s - Description of the Parameter
      Returns:
      Description of the Return Value
    • leftTrim

      public static String leftTrim(String s)
      remove leading blanks
      Parameters:
      s - Description of the Parameter
      Returns:
      Description of the Return Value
    • indexOfBalancedBracket

      public static int indexOfBalancedBracket(char lbrack, String s)
      return index of balanced bracket -1 for none. String MUST start with '('
      Parameters:
      lbrack - Description of the Parameter
      s - Description of the Parameter
      Returns:
      Description of the Return Value
    • getCommaSeparatedStrings

      public static Vector<String> getCommaSeparatedStrings(String s) throws Exception
      parse comma-separated Strings Note fields can be "" (as in ,,,) and fields can be quoted "...". If so, embedded quotes are represented as "", for example A," this is a ""B"" character",C. An unbalanced quote returns a mess
      Parameters:
      s - Description of the Parameter
      Returns:
      Vector the vector of Strings - any error returns null
      Throws:
      Exception - Description of the Exception
    • createCommaSeparatedStrings

      public static String createCommaSeparatedStrings(Vector<?> v) throws Exception
      Description of the Method
      Parameters:
      v - Description of the Parameter
      Returns:
      Description of the Return Value
      Throws:
      Exception - Description of the Exception
    • alternativeStringTokenizer

      public static Vector<String> alternativeStringTokenizer(String s, char delim)
      tokenize the string including adjacent delimiters (for example "foo$$bar$", "$" would contain the tokens "foo", "", "bar" and "")
      Parameters:
      s - Description of the Parameter
      delim - Description of the Parameter
      Returns:
      Description of the Return Value
    • parseWhitespaceQuotedFields

      public static Vector<String> parseWhitespaceQuotedFields(String s)
      parse whitespace-separated tokens interspersed with quoted strings, for example
      this is "a quoted string" and 'another token' as well
      parses to:
      this/is/a quoted string/and/another token/as well
      Parameters:
      s - Description of the Parameter
      Returns:
      Vector of strings (size = 0 if s is whitespace);
    • quoteConcatenate

      public static String quoteConcatenate(String[] s)
      concatenate strings into quote-separated string
      Parameters:
      s - Description of the Parameter
      Returns:
      String concatenated string
    • split

      public static String[] split(String s)
      splits a whitespace-separated set of tokens into a String[]
      Parameters:
      s - Description of the Parameter
      Returns:
      String[] result of splitting (null if s==null)
    • indexOf

      public static int indexOf(String string, String[] strings, int caseSensitivity)
      get the index of a String in an array
      Parameters:
      string - Description of the Parameter
      strings - Description of the Parameter
      caseSensitivity - Description of the Parameter
      Returns:
      index of string else -1 if not found
    • equals

      public static boolean equals(String string1, String string2, int sensitivity)
      convenience function for comparing strings using CMLUtils.CASE/IGNORECASE
      Parameters:
      string1 - Description of the Parameter
      string2 - Description of the Parameter
      sensitivity - Description of the Parameter
      Returns:
      boolean true if IGNORECASE ans string1.equalsIgnoreCase(string2) or string1.equals(string2)
    • removeHTML

      public static String removeHTML(String s)
      remove balanced (well-formed) markup from a string. Crude (that is not fully XML-compliant);
      Example: "This is <A HREF="foo">bar</A> and </BR> a break" goes to "This is bar and a break"
      Parameters:
      s - Description of the Parameter
      Returns:
      Description of the Return Value
    • warning

      public static void warning(String s)
      Warning message - nothing fancy at present
      Parameters:
      s - Description of the Parameter
    • message

      public static void message(String s)
      message - nothing fancy at present
      Parameters:
      s - Description of the Parameter
    • error

      public static void error(String s)
      Error message - nothing fancy at present. Display in Text frame
      Parameters:
      s - Description of the Parameter
    • bug

      public static void bug(String s)
      record that we have hit a program bug!!!
      Parameters:
      s - Description of the Parameter
    • bug

      public static void bug(Exception e)
      Description of the Method
      Parameters:
      e - Description of the Parameter
    • createNewFile

      public static File createNewFile(String fileName) throws IOException
      create new file, including making directory if required This seems to be a mess - f.createNewFile() doesn't seem to work A directory should have a trailing file.separator
      Parameters:
      fileName - Description of the Parameter
      Returns:
      Description of the Return Value
      Throws:
      IOException - Description of the Exception
    • getPWDName

      public static String getPWDName()
      get current directory
      Returns:
      The pWDName value
    • substituteString

      public static String substituteString(String s, String oldSubstring, String newSubstring, int count)
      make substitutions in a string. If oldSubtrings = "A" and newSubstrings = "aa" then count occurrences of "A" in s are replaced with "aa", etc. "AAA" count=2 would be replaced by "aaaaA"
      Parameters:
      s - Description of the Parameter
      oldSubstring - Description of the Parameter
      newSubstring - Description of the Parameter
      count - Description of the Parameter
      Returns:
      Description of the Return Value
    • substituteStrings

      public static String substituteStrings(String s, String[] oldSubstrings, String[] newSubstrings)
      make substitutions in a string. If oldSubtrings = {"A", "BB", "C"} and newSubstrings = {"aa", "b", "zz"} then every occurrence of "A" in s is replaced with "aa", etc. "BBB" would be replaced by "bB"
      Parameters:
      s - Description of the Parameter
      oldSubstrings - Description of the Parameter
      newSubstrings - Description of the Parameter
      Returns:
      Description of the Return Value
    • substituteDOSbyAscii

      public static String substituteDOSbyAscii(String s)
      substitute certain DOS-compatible diacriticals by the Unicode value. Not guaranteed to be correct. Example 130 is e-acute (==
      Parameters:
      s - Description of the Parameter
      Returns:
      Description of the Return Value
    • substituteEquals

      public static String substituteEquals(String s)
      substitute hex representation of character, for example =2E by char(46). If line ends with =, ignore that character.
      Parameters:
      s - Description of the Parameter
      Returns:
      String result
    • isAllowedFormat

      public static boolean isAllowedFormat(String format)
      Gets the allowedFormat attribute of the CMLUtils class
      Parameters:
      format - Description of the Parameter
      Returns:
      The allowedFormat value
    • convertFormat

      public static Vector<String> convertFormat(Vector<String> vector, String format)
      converts character format within a Vector of Strings. Some formats such as '=' escaping may require lines to be joined. Original Vector is unaltered.
      Parameters:
      vector - Description of the Parameter
      format - Description of the Parameter
      Returns:
      Description of the Return Value
    • capitalise

      public static String capitalise(String s)
      capitalise a String (whatever the starting case)
      Parameters:
      s - Description of the Parameter
      Returns:
      Description of the Return Value
    • toCamelCase

      public static String toCamelCase(String s)
      Description of the Method
      Parameters:
      s - Description of the Parameter
      Returns:
      Description of the Return Value
    • escape

      public static String escape(String s, char[] escapes, String[] escape1)
      escape characters in an XML string; also escape non-XML characters (for example eacute => é). If escapes==null only escape non-XML
      Parameters:
      s - Description of the Parameter
      escapes - Description of the Parameter
      escape1 - Description of the Parameter
      Returns:
      Description of the Return Value
    • escape

      public static String escape(String s)
      default escape characters in an XML string (' -> ' , etc); also escape non-XML characters (for example eacute => é)
      Parameters:
      s - Description of the Parameter
      Returns:
      Description of the Return Value
    • equals

      public static boolean equals(Object obj1, Object obj2)
      compares two objects using equals() allows for null objects. if either object is null returns false
      Parameters:
      obj1 - Description of the Parameter
      obj2 - Description of the Parameter
      Returns:
      Description of the Return Value
    • freeMemory

      public static void freeMemory(long mem)
      runs the garbage collector if memory drops below mem. (I use a value of 300000 - your mileage may vary). Potentially used in loops for processing input and creation of objects
      Parameters:
      mem - Description of the Parameter
    • addToClasspath

      public String addToClasspath(String extraPath)
      adds to the classpath and resets the system property
      Parameters:
      extraPath - The feature to be added to the ToClasspath attribute
      Returns:
      String the new classpath
    • getIntFromHex

      public static int getIntFromHex(String hex)
      Translates a Hex number to its int equivalent. Thus "FE" translates to 254. Horrid, but I couldn't find if Java reads hex. All results are >= 0. Errors return -1
      Parameters:
      hex - Description of the Parameter
      Returns:
      The intFromHex value
    • readByteArray

      public static byte[] readByteArray(String filename) throws FileNotFoundException, IOException
      reads a byte array from file, *including* line feeds
      Parameters:
      filename - Description of the Parameter
      Returns:
      Description of the Return Value
      Throws:
      FileNotFoundException - Description of the Exception
      IOException - Description of the Exception
    • readByteArray

      public static byte[] readByteArray(DataInputStream d) throws IOException
      reads a byte array from DataInputStream, *including* line feeds
      Parameters:
      d - Description of the Parameter
      Returns:
      Description of the Return Value
      Throws:
      IOException - Description of the Exception
    • stripISOControls

      public static String stripISOControls(String s)
      remove all control (non-printing) characters
      Parameters:
      s - Description of the Parameter
      Returns:
      Description of the Return Value
    • normaliseWhitespace

      public static String normaliseWhitespace(String s)
      normalise whitespace in a String (all whitespace is transformed to single spaces and the string is NOT trimmed
      Parameters:
      s - Description of the Parameter
      Returns:
      Description of the Return Value
    • stripNewlines

      public static byte[] stripNewlines(byte[] b)
      strip linefeeds from a byte array
      Parameters:
      b - Description of the Parameter
      Returns:
      Description of the Return Value
    • isRightMouseClick

      public static boolean isRightMouseClick(MouseEvent event)
      a crude way of identifying a right mouse click (because I left the Java book behind)
      Parameters:
      event - Description of the Parameter
      Returns:
      The rightMouseClick value
    • makeDirectory

      public static String makeDirectory(String urlString)
      truncate filename suffix to make a directory name (without file.separator)
      Parameters:
      urlString - Description of the Parameter
      Returns:
      Description of the Return Value
    • makeAbsoluteURL

      public static String makeAbsoluteURL(String url) throws MalformedURLException
      If a URL is relative, make it absolute against the current directory. If url already has a protocol, return unchanged
      Parameters:
      url - Description of the Parameter
      Returns:
      Description of the Return Value
      Throws:
      MalformedURLException - Description of the Exception
    • getFileOutputStream

      public static FileOutputStream getFileOutputStream(String fileName) throws FileNotFoundException
      get an OutputStream from a file or URL. Required (I think) because strings of the sort "file:/C:\foo\bat.txt" crash FileOutputStream, so this strips off the file:/ stuff for Windows-like stuff
      Parameters:
      fileName - Description of the Parameter
      Returns:
      FileOutputStream a new (opened) FileOutputStream
      Throws:
      FileNotFoundException - Description of the Exception
    • readZip

      public static void readZip(String fileName) throws IOException
      read a Zipfile
      Parameters:
      fileName - Description of the Parameter
      Throws:
      IOException - Description of the Exception
    • outputInteger

      public static String outputInteger(int nPlaces, int value) throws IllegalArgumentException
      this is a mess
      Parameters:
      nPlaces - Description of the Parameter
      value - Description of the Parameter
      Returns:
      Description of the Return Value
      Throws:
      IllegalArgumentException - Description of the Exception
    • outputFloat

      public static String outputFloat(int nPlaces, int nDec, double value) throws IllegalArgumentException
      format for example f8.3 this is a mess; if cannot fit, then either right-truncates or when that doesn't work, returns ****
      Parameters:
      nPlaces - Description of the Parameter
      nDec - Description of the Parameter
      value - Description of the Parameter
      Returns:
      Description of the Return Value
      Throws:
      IllegalArgumentException - Description of the Exception
    • outputNumber

      public static String outputNumber(int nPlaces, int nDec, double c)
      as above, but trims trailing zeros
      Parameters:
      nPlaces - Description of the Parameter
      nDec - Description of the Parameter
      c - Description of the Parameter
      Returns:
      Description of the Return Value
    • invert

      public static Hashtable<Object,Object> invert(Hashtable<Object,Object> table)
      invert a Hashtable by interchanging keys and values. This assumes a 1;1 mapping - if not the result is probably garbage.
      Parameters:
      table - Description of the Parameter
      Returns:
      Description of the Return Value
    • andTables

      public static Hashtable<Object,Object> andTables(Hashtable<Object,Object> a, Hashtable<Object,Object> b)
      AND 2 Hashtables - inefficient except for small tables. Finds entries with the same key and value.
      Parameters:
      a - Description of the Parameter
      b - Description of the Parameter
      Returns:
      Hashtable contains only common entries. null if none
    • orTables

      public static Hashtable<Object,Object> orTables(Hashtable<Object,Object> a, Hashtable<Object,Object> b)
      OR 2 Hashtables - inefficient except for small tables. Merges entries. if entry with the same key and different value is found, take value from first table.
      Parameters:
      a - Description of the Parameter
      b - Description of the Parameter
      Returns:
      Hashtable contains all entries. null if none
    • xorTables

      public static Hashtable<Object,Object> xorTables(Hashtable<?,?> a, Hashtable<?,?> b)
      XOR 2 Hashtables inefficient except for small tables. omit
      Parameters:
      a - Description of the Parameter
      b - Description of the Parameter
      Returns:
      Hashtable contains no common entries.
    • getRepeatedValues

      public static Vector<Object> getRepeatedValues(Vector<?> v)
      returns a vector of all repeated values in v.
      Parameters:
      v - Description of the Parameter
      Returns:
      Vector containing duplicate values. If none, returns null
    • andVectors

      public static Vector<Object> andVectors(Vector<?> a, Vector<?> b)
      finds elements common to 2 vectors. Very crude (O(n^2));
      Parameters:
      a - Description of the Parameter
      b - Description of the Parameter
      Returns:
      Description of the Return Value
    • addEnumerationToVector

      public static void addEnumerationToVector(Vector<Object> v, Enumeration<?> e)
      add the elements of an Enumeration to a Vector.
      Parameters:
      v - The feature to be added to the EnumerationToVector attribute
      e - The feature to be added to the EnumerationToVector attribute
    • sort

      public static void sort(Object[] objs)
      sort an object array - very inefficient
      Parameters:
      objs - Description of the Parameter
    • printChar

      public static void printChar()
      Description of the Method
    • sortVector

      public static void sortVector(Vector<Object> v)
      sort a Vector - VERY crude and inefficient
      Parameters:
      v - Description of the Parameter
    • and

      public static Vector<Object> and(Vector<Object> v1, Vector<Object> v2)
      create Vector with elements common to v1 and v2. SLOW. Comparison is done with equals()
      Parameters:
      v1 - Description of the Parameter
      v2 - Description of the Parameter
      Returns:
      Description of the Return Value
    • not

      public static Vector<Object> not(Vector<Object> v1, Vector<Object> v2)
      create Vector with elements in v1 but not v2. SLOW. Comparison is done with equals()
      Parameters:
      v1 - Description of the Parameter
      v2 - Description of the Parameter
      Returns:
      Description of the Return Value
    • or

      public static Vector<Object> or(Vector<Object> v1, Vector<Object> v2)
      create Vector with elements in v1 but not v2. SLOW. Comparison is done with equals()
      Parameters:
      v1 - Description of the Parameter
      v2 - Description of the Parameter
      Returns:
      Description of the Return Value
    • and

      create Hashtable with elements common to h1 and h2. The keys are taken from h1. SLOW. Comparison is done with equals()
      Parameters:
      h1 - Description of the Parameter
      h2 - Description of the Parameter
      Returns:
      Description of the Return Value
    • not

      create Hashtable with elements in to h1 but not h2. The keys are taken from h1 SLOW. Comparison is done with equals()
      Parameters:
      h1 - Description of the Parameter
      h2 - Description of the Parameter
      Returns:
      Description of the Return Value
    • or

      create Hashtable with elements in to h1 but not h2. The keys are taken from h1 SLOW. Comparison is done with equals()
      Parameters:
      h1 - Description of the Parameter
      h2 - Description of the Parameter
      Returns:
      Description of the Return Value
    • sin

      public static double sin(String fString)
      Description of the Method
      Parameters:
      fString - Description of the Parameter
      Returns:
      Description of the Return Value
    • cos

      public static double cos(String fString)
      Description of the Method
      Parameters:
      fString - Description of the Parameter
      Returns:
      Description of the Return Value
    • log

      public static double log(String fString)
      Description of the Method
      Parameters:
      fString - Description of the Parameter
      Returns:
      Description of the Return Value
    • substring

      public static String substring(String string, String startPos, String lenString)
      supports XSL substring
      Parameters:
      string - Description of the Parameter
      startPos - Description of the Parameter
      lenString - Description of the Parameter
      Returns:
      Description of the Return Value
    • main

      public static void main(String[] args)
      Description of the Method
      Parameters:
      args - Description of the Parameter