Exemple #1
0
        /// <summary>
        /// Parse a level name string into a Level.
        /// <para>
        /// The argument string may consist of either a level name
        /// or an integer value.
        /// </para>
        /// <para>
        /// For example:
        /// <ul>
        /// <li>     "SEVERE"
        /// <li>     "1000"
        /// </ul>
        ///
        /// </para>
        /// </summary>
        /// <param name="name">   string to be parsed </param>
        /// <exception cref="NullPointerException"> if the name is null </exception>
        /// <exception cref="IllegalArgumentException"> if the value is not valid.
        /// Valid values are integers between <CODE>Integer.MIN_VALUE</CODE>
        /// and <CODE>Integer.MAX_VALUE</CODE>, and all known level names.
        /// Known names are the levels defined by this class (e.g., <CODE>FINE</CODE>,
        /// <CODE>FINER</CODE>, <CODE>FINEST</CODE>), or created by this class with
        /// appropriate package access, or new levels defined or created
        /// by subclasses.
        /// </exception>
        /// <returns> The parsed value. Passing an integer that corresponds to a known name
        /// (e.g., 700) will return the associated name (e.g., <CODE>CONFIG</CODE>).
        /// Passing an integer that does not (e.g., 1) will return a new level name
        /// initialized to that value. </returns>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public static synchronized Level parse(String name) throws IllegalArgumentException
        public static Level Parse(String name)
        {
            lock (typeof(Level))
            {
                // Check that name is not null.
                name.Length();

                KnownLevel level;

                // Look for a known Level with the given non-localized name.
                level = KnownLevel.FindByName(name);
                if (level != null)
                {
                    return(level.LevelObject);
                }

                // Now, check if the given name is an integer.  If so,
                // first look for a Level with the given value and then
                // if necessary create one.
                try
                {
                    int x = Convert.ToInt32(name);
                    level = KnownLevel.FindByValue(x);
                    if (level == null)
                    {
                        // add new Level
                        Level levelObject = new Level(name, x);
                        level = KnownLevel.FindByValue(x);
                    }
                    return(level.LevelObject);
                }
                catch (NumberFormatException)
                {
                    // Not an integer.
                    // Drop through.
                }

                // Finally, look for a known level with the given localized name,
                // in the current default locale.
                // This is relatively expensive, but not excessively so.
                level = KnownLevel.FindByLocalizedLevelName(name);
                if (level != null)
                {
                    return(level.LevelObject);
                }

                // OK, we've tried everything and failed
                throw new IllegalArgumentException("Bad level \"" + name + "\"");
            }
        }
Exemple #2
0
        // Returns a mirrored Level object that matches the given name as
        // specified in the Level.parse method.  Returns null if not found.
        //
        // It returns the same Level object as the one returned by Level.parse
        // method if the given name is a non-localized name or integer.
        //
        // If the name is a localized name, findLevel and parse method may
        // return a different level value if there is a custom Level subclass
        // that overrides Level.getLocalizedName() to return a different string
        // than what's returned by the default implementation.
        //
        internal static Level FindLevel(String name)
        {
            if (name == null)
            {
                throw new NullPointerException();
            }

            KnownLevel level;

            // Look for a known Level with the given non-localized name.
            level = KnownLevel.FindByName(name);
            if (level != null)
            {
                return(level.MirroredLevel);
            }

            // Now, check if the given name is an integer.  If so,
            // first look for a Level with the given value and then
            // if necessary create one.
            try
            {
                int x = Convert.ToInt32(name);
                level = KnownLevel.FindByValue(x);
                if (level == null)
                {
                    // add new Level
                    Level levelObject = new Level(name, x);
                    level = KnownLevel.FindByValue(x);
                }
                return(level.MirroredLevel);
            }
            catch (NumberFormatException)
            {
                // Not an integer.
                // Drop through.
            }

            level = KnownLevel.FindByLocalizedLevelName(name);
            if (level != null)
            {
                return(level.MirroredLevel);
            }

            return(null);
        }