123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435 |
- #region Apache License
- #endregion
- using System;
- using System.Collections;
- using System.Globalization;
- using log4net.Core;
- using log4net.Layout;
- namespace log4net.Util
- {
-
-
-
-
-
-
-
-
-
-
-
-
- public sealed class PatternParser
- {
- #region Public Instance Constructors
-
-
-
-
-
-
-
-
-
-
- public PatternParser(string pattern)
- {
- m_pattern = pattern;
- }
- #endregion Public Instance Constructors
- #region Public Instance Methods
-
-
-
-
-
-
-
-
-
- public PatternConverter Parse()
- {
- string[] converterNamesCache = BuildCache();
- ParseInternal(m_pattern, converterNamesCache);
- return m_head;
- }
- #endregion Public Instance Methods
- #region Public Instance Properties
-
-
-
-
-
-
-
-
-
-
-
- public Hashtable PatternConverters
- {
- get { return m_patternConverters; }
- }
- #endregion Public Instance Properties
- #region Private Instance Methods
-
-
-
-
-
-
-
-
-
- private string[] BuildCache()
- {
- string[] converterNamesCache = new string[m_patternConverters.Keys.Count];
- m_patternConverters.Keys.CopyTo(converterNamesCache, 0);
-
- Array.Sort(converterNamesCache, 0, converterNamesCache.Length, StringLengthComparer.Instance);
- return converterNamesCache;
- }
- #region StringLengthComparer
-
-
-
-
-
-
-
-
-
- private sealed class StringLengthComparer : IComparer
- {
- public static readonly StringLengthComparer Instance = new StringLengthComparer();
- private StringLengthComparer()
- {
- }
- #region Implementation of IComparer
- public int Compare(object x, object y)
- {
- string s1 = x as string;
- string s2 = y as string;
- if (s1 == null && s2 == null)
- {
- return 0;
- }
- if (s1 == null)
- {
- return 1;
- }
- if (s2 == null)
- {
- return -1;
- }
- return s2.Length.CompareTo(s1.Length);
- }
-
- #endregion
- }
- #endregion // StringLengthComparer
-
-
-
-
-
-
-
-
-
-
- private void ParseInternal(string pattern, string[] matches)
- {
- int offset = 0;
- while(offset < pattern.Length)
- {
- int i = pattern.IndexOf('%', offset);
- if (i < 0 || i == pattern.Length - 1)
- {
- ProcessLiteral(pattern.Substring(offset));
- offset = pattern.Length;
- }
- else
- {
- if (pattern[i+1] == '%')
- {
-
- ProcessLiteral(pattern.Substring(offset, i - offset + 1));
- offset = i + 2;
- }
- else
- {
- ProcessLiteral(pattern.Substring(offset, i - offset));
- offset = i + 1;
- FormattingInfo formattingInfo = new FormattingInfo();
-
-
- if (offset < pattern.Length)
- {
- if (pattern[offset] == '-')
- {
-
- formattingInfo.LeftAlign = true;
- offset++;
- }
- }
-
- while (offset < pattern.Length && char.IsDigit(pattern[offset]))
- {
-
- if (formattingInfo.Min < 0)
- {
- formattingInfo.Min = 0;
- }
- formattingInfo.Min = (formattingInfo.Min * 10) + int.Parse(pattern[offset].ToString(), NumberFormatInfo.InvariantInfo);
- offset++;
- }
-
- if (offset < pattern.Length)
- {
- if (pattern[offset] == '.')
- {
-
- offset++;
- }
- }
-
- while (offset < pattern.Length && char.IsDigit(pattern[offset]))
- {
-
- if (formattingInfo.Max == int.MaxValue)
- {
- formattingInfo.Max = 0;
- }
- formattingInfo.Max = (formattingInfo.Max * 10) + int.Parse(pattern[offset].ToString(), NumberFormatInfo.InvariantInfo);
- offset++;
- }
- int remainingStringLength = pattern.Length - offset;
-
- for(int m=0; m<matches.Length; m++)
- {
- string key = matches[m];
- if (key.Length <= remainingStringLength)
- {
- if (string.Compare(pattern, offset, key, 0, key.Length) == 0)
- {
-
- offset = offset + matches[m].Length;
- string option = null;
-
- if (offset < pattern.Length)
- {
- if (pattern[offset] == '{')
- {
-
- offset++;
-
- int optEnd = pattern.IndexOf('}', offset);
- if (optEnd < 0)
- {
-
- }
- else
- {
- option = pattern.Substring(offset, optEnd - offset);
- offset = optEnd + 1;
- }
- }
- }
- ProcessConverter(matches[m], option, formattingInfo);
- break;
- }
- }
- }
- }
- }
- }
- }
-
-
-
-
- private void ProcessLiteral(string text)
- {
- if (text.Length > 0)
- {
-
- ProcessConverter("literal", text, new FormattingInfo());
- }
- }
-
-
-
-
-
-
- private void ProcessConverter(string converterName, string option, FormattingInfo formattingInfo)
- {
- LogLog.Debug(declaringType, "Converter ["+converterName+"] Option ["+option+"] Format [min="+formattingInfo.Min+",max="+formattingInfo.Max+",leftAlign="+formattingInfo.LeftAlign+"]");
-
- ConverterInfo converterInfo = (ConverterInfo)m_patternConverters[converterName];
- if (converterInfo == null)
- {
- LogLog.Error(declaringType, "Unknown converter name ["+converterName+"] in conversion pattern.");
- }
- else
- {
-
- PatternConverter pc = null;
- try
- {
- pc = (PatternConverter)Activator.CreateInstance(converterInfo.Type);
- }
- catch(Exception createInstanceEx)
- {
- LogLog.Error(declaringType, "Failed to create instance of Type [" + converterInfo.Type.FullName + "] using default constructor. Exception: " + createInstanceEx.ToString());
- }
-
-
- pc.FormattingInfo = formattingInfo;
- pc.Option = option;
- pc.Properties = converterInfo.Properties;
- IOptionHandler optionHandler = pc as IOptionHandler;
- if (optionHandler != null)
- {
- optionHandler.ActivateOptions();
- }
- AddConverter(pc);
- }
- }
-
-
-
-
-
- private void AddConverter(PatternConverter pc)
- {
-
- if (m_head == null)
- {
- m_head = m_tail = pc;
- }
- else
- {
-
-
-
-
- m_tail = m_tail.SetNext(pc);
- }
- }
- #endregion Protected Instance Methods
- #region Private Constants
- private const char ESCAPE_CHAR = '%';
-
- #endregion Private Constants
- #region Private Instance Fields
-
-
-
- private PatternConverter m_head;
-
-
-
- private PatternConverter m_tail;
-
-
-
- private string m_pattern;
-
-
-
-
-
-
-
-
- private Hashtable m_patternConverters = new Hashtable();
- #endregion Private Instance Fields
- #region Private Static Fields
-
-
-
-
-
-
-
- private readonly static Type declaringType = typeof(PatternParser);
- #endregion Private Static Fields
- }
- }
|