Class ConnectStringParser


  • public class ConnectStringParser
    extends java.lang.Object
    ConnectStringParser is a utility class that parses or creates a JDBC connect string according to the OLE DB Connection String Syntax.

    This code was adapted from Mondrian's mondrian.olap.Util class. The primary differences between this and its Mondrian progenitor are:

    • use of regular Properties for compatibility with the JDBC API (replaces Mondrian's use of its own order-preserving and case-insensitive PropertyList)
    • ability to pass to parse(java.lang.String) a pre-existing Properties object into which properties are to be parsed, possibly overriding prior values
    • use of SQLExceptions rather than unchecked RuntimeExceptions
    • static members for parsing and creating connect strings

    ConnectStringParser has a private constructor. Callers use the static members:

    parse(String)
    Parses the connect string into a new Properties object.
    parse(String, Properties)
    Parses the connect string into an existing Properties object.
    getParamString(Properties)
    Returns a param string, quoted and escaped as needed, to represent the supplied name-value pairs.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private int i  
      private int n  
      private java.lang.StringBuilder nameBuf  
      private java.lang.String s  
      private java.lang.StringBuilder valueBuf  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private ConnectStringParser​(java.lang.String s)
      Creates a new connect string parser.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String getParamString​(java.util.Properties props)
      Returns a param string, quoted and escaped as needed, to represent the supplied name-value pairs.
      static java.util.Properties parse​(java.lang.String s)
      Parses the connect string into a new Properties object.
      static java.util.Properties parse​(java.lang.String s, java.util.Properties props)
      Parses the connect string into an existing Properties object.
      (package private) java.util.Properties parseInternal​(java.util.Properties props)
      Parses the connect string into a Properties object.
      (package private) java.lang.String parseName()
      Reads "name=".
      (package private) void parsePair​(java.util.Properties props)
      Reads "name=value;" or "name=value<EOF>".
      (package private) java.lang.String parseQuoted​(char q)
      Reads a string quoted by a given character.
      (package private) java.lang.String parseValue()
      Reads "value;" or "value<EOF>"
      static java.util.Map<java.lang.String,​java.lang.String> toMap​(java.util.Properties properties)
      Converts a Properties object to a Map<String, String>.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • s

        private final java.lang.String s
      • n

        private final int n
      • i

        private int i
      • nameBuf

        private final java.lang.StringBuilder nameBuf
      • valueBuf

        private final java.lang.StringBuilder valueBuf
    • Constructor Detail

      • ConnectStringParser

        private ConnectStringParser​(java.lang.String s)
        Creates a new connect string parser.
        Parameters:
        s - connect string to parse
        See Also:
        parse(String), parse(String, Properties)
    • Method Detail

      • parse

        public static java.util.Properties parse​(java.lang.String s)
                                          throws java.sql.SQLException
        Parses the connect string into a new Properties object.
        Parameters:
        s - connect string to parse
        Returns:
        properties object with parsed params
        Throws:
        java.sql.SQLException - error parsing name-value pairs
      • parse

        public static java.util.Properties parse​(java.lang.String s,
                                                 java.util.Properties props)
                                          throws java.sql.SQLException
        Parses the connect string into an existing Properties object.
        Parameters:
        s - connect string to parse
        props - optional properties object, may be null
        Returns:
        properties object with parsed params; if an input props was supplied, any duplicate properties will have been replaced by those from the connect string.
        Throws:
        java.sql.SQLException - error parsing name-value pairs
      • parseInternal

        java.util.Properties parseInternal​(java.util.Properties props)
                                    throws java.sql.SQLException
        Parses the connect string into a Properties object. Note that the string can only be parsed once. Subsequent calls return empty/unchanged Properties. The original props argument is not altered.
        Parameters:
        props - optional properties object, may be null
        Returns:
        properties object with parsed params; if an input props was supplied, any duplicate properties will have been replaced by those from the connect string.
        Throws:
        java.sql.SQLException - error parsing name-value pairs
      • parsePair

        void parsePair​(java.util.Properties props)
                throws java.sql.SQLException
        Reads "name=value;" or "name=value<EOF>".
        Throws:
        java.sql.SQLException - error parsing value
      • parseName

        java.lang.String parseName()
        Reads "name=". Name can contain equals sign if equals sign is doubled.
      • parseValue

        java.lang.String parseValue()
                             throws java.sql.SQLException
        Reads "value;" or "value<EOF>"
        Throws:
        java.sql.SQLException - if find an unterminated quoted value
      • parseQuoted

        java.lang.String parseQuoted​(char q)
                              throws java.sql.SQLException
        Reads a string quoted by a given character. Occurrences of the quoting character must be doubled. For example, parseQuoted('"') reads "a ""new"" string" and returns a "new" string.
        Throws:
        java.sql.SQLException - if find an unterminated quoted value
      • getParamString

        public static java.lang.String getParamString​(java.util.Properties props)
        Returns a param string, quoted and escaped as needed, to represent the supplied name-value pairs.
        Parameters:
        props - name-value pairs
        Returns:
        param string, never null
      • toMap

        public static java.util.Map<java.lang.String,​java.lang.String> toMap​(java.util.Properties properties)
        Converts a Properties object to a Map<String, String>.

        This is necessary because Properties is a dinosaur class. It ought to extend Map<String,String>, but instead extends Hashtable<Object,Object>.

        Typical usage, to iterate over a Properties:

        Properties properties;
        for (Map.Entry<String, String> entry = Util.toMap(properties).entrySet()) {
        println("key=" + entry.getKey() + ", value=" + entry.getValue());
        }