diff options
Diffstat (limited to 'src/StyleReader.cs')
| -rw-r--r-- | src/StyleReader.cs | 88 |
1 files changed, 86 insertions, 2 deletions
diff --git a/src/StyleReader.cs b/src/StyleReader.cs index d99a1377..c1cc28bf 100644 --- a/src/StyleReader.cs +++ b/src/StyleReader.cs @@ -19,12 +19,96 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see <http://www.gnu.org/licenses/>. using System; +using System.Collections.Generic; +using System.IO; +using System.Reflection; + namespace Crow { - public class StyleReader + public class StyleReader : StreamReader { - public StyleReader () + enum readerState { classNames, propertyName, expression } + readerState state = readerState.classNames; + + + public StyleReader (Assembly assembly, string resId) + : base(assembly.GetManifestResourceStream (resId)) { + Interface iface = Interface.CurrentInterface; + string classNameFromResId = resId.Substring (0, resId.Length - 6); + string token = ""; + List<string> targetsClasses = new List<string> (); + string currentProperty = ""; + + while (!EndOfStream) { + char c = (Char)Read (); + + switch (state) { + case readerState.classNames: + if (c.IsWhiteSpaceOrNewLine () || c == ',' || c == '{') { + if (!string.IsNullOrEmpty (token)) + targetsClasses.Add (token); + if (c == '{') + state = readerState.propertyName; + token = ""; + }else if (c=='='){ + //this file contains only properties, + //resource Id (minus .style extention) will determine the single target class + if (targetsClasses.Count > 1) + throw new Exception ("Unexpected token '='"); + else if (targetsClasses.Count == 1) { + if (!string.IsNullOrEmpty (token)) + throw new Exception ("Unexpected token '='"); + currentProperty = targetsClasses [0]; + targetsClasses [0] = classNameFromResId; + }else{ + if (string.IsNullOrEmpty (token)) + throw new Exception ("Unexpected token '='"); + targetsClasses.Add (classNameFromResId); + currentProperty = token; + token = ""; + } + state = readerState.expression; + }else + token += c; + break; + case readerState.propertyName: + if (c.IsWhiteSpaceOrNewLine () || c == '=') { + if (!string.IsNullOrEmpty (token)) + currentProperty = token; + if (c == '=') + state = readerState.expression; + + token = ""; + }else if (c == '}'){ + if (!string.IsNullOrEmpty (token)) + throw new Exception ("Unexpected token '" + c + "'"); + targetsClasses = new List<string> (); + currentProperty = ""; + state = readerState.classNames; + } else + token += c; + break; + case readerState.expression: + if (c == ';') { + if (!string.IsNullOrEmpty (token)) { + string expression = token.Trim (); + + foreach (string tc in targetsClasses) { + if (!iface.Styling.ContainsKey (tc)) + iface.Styling [tc] = new Dictionary<string, string> (); + else if (iface.Styling [tc].ContainsKey (currentProperty)) + continue; + iface.Styling [tc] [currentProperty] = expression; + } + token = ""; + } + state = readerState.propertyName; + } else + token += c; + break; + } + } } } } |