private static void Define(string[] parameters, ref PreprocessingContext context)
        {
            if (context.CanInclude)
            {
                string name;
                string[] mParam;
                name = GetMacro(parameters[0], out mParam);

                if (!context.CanBeDefined(name, mParam))
                {
                    context.messageLog.AddError(context.currentFile, context.currentLine,
                        parameters[0] + " is already defined.");
                    return;
                }

                if (parameters.Length == 1)
                {
                    context.defCol.Add(name, "", mParam);
                }
                else
                {
                    context.defCol.Add(name, parameters[1], mParam);
                }
            }
        }
        static private void Define(string[] parameters, ref PreprocessingContext context)
        {
            if (context.CanInclude)
            {
                string   name;
                string[] mParam;
                name = GetMacro(parameters[0], out mParam);

                if (!context.CanBeDefined(name, mParam))
                {
                    context.messageLog.AddError(context.currentFile, context.currentLine,
                                                parameters[0] + " is already defined.");
                    return;
                }

                if (parameters.Length == 1)
                {
                    context.defCol.Add(name, "", mParam);
                }
                else
                {
                    context.defCol.Add(name, parameters[1], mParam);
                }
            }
        }
 static private void Include(string[] parameters, ref PreprocessingContext context)
 {
     if (context.CanInclude)
     {
         //Remove comments
     }
 }
Example #4
0
        /// <summary>
        /// Parses the specified binding value.
        /// </summary>
        /// <param name="context">The preprocessing context.</param>
        /// <param name="bindingExpression">The binding value.</param>
        /// <returns></returns>
        public static DynamicBindingExpression Parse(PreprocessingContext context, string bindingExpression)
        {
            bool   isValid            = true;
            string bindingType        = null;
            int    firstEqualPosition = -1;
            string bindingValue       = bindingExpression.Trim();

            if (!(bindingValue.StartsWith(context.TokenStart) && bindingValue.EndsWith(context.TokenEnd)))
            {
                isValid = false;
            }

            if (isValid)
            {
                bindingValue = bindingValue.Substring(2, bindingValue.Length - 3).Trim();

                firstEqualPosition = bindingValue.IndexOf('=');

                if (firstEqualPosition < 0)
                {
                    isValid = false;
                }
                else
                {
                    bindingType = bindingValue.Substring(0, firstEqualPosition);
                }
            }

            if (isValid)
            {
                if (!bindingType.Equals("XPath", StringComparison.OrdinalIgnoreCase) &&
                    !bindingType.Equals("RegEx", StringComparison.OrdinalIgnoreCase))
                {
                    isValid = false;
                }
            }

            if (!isValid)
            {
                throw new ArgumentException(string.Format("Binding value '{0}' was not properly formed, must be ${{XPath=XpathExpression [IncludedFiles=*.config;*.xml]}} or ${{Regex=regexPattern [IncludedFiles=*.config;*.xml]}}", bindingExpression));
            }

            string files = null;
            string path  = null;

            ParseFilesAndPath(bindingValue, out files, out path);

            if (bindingType.Equals("XPath", StringComparison.OrdinalIgnoreCase))
            {
                XPathBinding xpathBinding = new XPathBinding(path, files);
                return(xpathBinding);
            }
            else if (bindingType.Equals("Regex", StringComparison.OrdinalIgnoreCase))
            {
                RegexBinding xpathBinding = new RegexBinding(path, files);
                return(xpathBinding);
            }

            return(null);
        }
 static private void IncludeBinary(string[] parameters, ref PreprocessingContext context)
 {
     if (context.CanInclude)
     {
         //Read binary
     }
 }
 public void Start(IDataAnalysisProblemData problemData, IContentView currentView) {
   IAlgorithm algorithm;
   IDataAnalysisProblem problem;
   GetMostOuterContent(currentView as Control, out algorithm, out problem);
   var context = new PreprocessingContext(problemData, algorithm ?? problem ?? problemData as IItem);
   MainFormManager.MainForm.ShowContent(context);
 }
        static private void IfNotDefined(string[] parameters, ref PreprocessingContext context)
        {
            bool predefined = context.predefined.Contains(parameters[0]);
            bool defined    = context.defCol.ContainsName(parameters[0]);

            context.include.Push(!predefined && !defined);
        }
Example #8
0
        public void Start(IDataAnalysisProblemData problemData, IContentView currentView)
        {
            IAlgorithm           algorithm;
            IDataAnalysisProblem problem;

            GetMostOuterContent(currentView as Control, out algorithm, out problem);
            var context = new PreprocessingContext(problemData, algorithm ?? problem ?? problemData as IItem);

            MainFormManager.MainForm.ShowContent(context);
        }
        static private void UnDefine(string[] parameters, ref PreprocessingContext context)
        {
            if (context.CanInclude)
            {
                string   name;
                string[] mParam;
                name = GetMacro(parameters[0], out mParam);

                if (context.defCol.ContainsName(name, mParam))
                {
                    context.defCol.Remove(name, mParam);
                }
            }
        }
Example #10
0
        /// <summary>
        /// Evaluates the provided C# expression
        /// </summary>
        /// <param name="context">The preprocessing context.</param>
        /// <param name="input">The input.</param>
        /// <returns>The expression resolved to a single string</returns>
        public string EvaluateToString(PreprocessingContext context, string input)
        {
            // generate code
            string code = string.Format(STRING_CODE_TEMPLATE, context.TokenStart, context.TokenEnd, input);

            // compile it
            CompilerResults results = CompileAssembly(code);

            if (null != results && null != results.CompiledAssembly)
            {
                // execute it
                return(RunStringEvaluation(context, results));
            }
            return("");
        }
Example #11
0
        /// <summary>
        /// Read the settings from a custom source
        /// </summary>
        /// <param name="dataSource">The settings data source.</param>
        /// <param name="context">The preprocessing context.</param>
        /// <returns></returns>
        public DataTable ReadSettings(DataSource dataSource, PreprocessingContext context)
        {
            string fileName  = null;
            string arguments = null;

            ParseArguments(dataSource.Path, out fileName, out arguments);

            DataTable dt       = null;
            string    tempFile = Path.GetTempFileName();

            try
            {
                arguments = arguments.Replace("@tempFile@", tempFile);

                using (Process customProcess = new Process())
                {
                    customProcess.StartInfo.FileName               = fileName;
                    customProcess.StartInfo.Arguments              = arguments;
                    customProcess.StartInfo.UseShellExecute        = false;
                    customProcess.StartInfo.RedirectStandardOutput = true;
                    customProcess.StartInfo.RedirectStandardError  = false;
                    customProcess.StartInfo.CreateNoWindow         = true;
                    customProcess.Start();

                    Console.WriteLine(customProcess.StandardOutput.ReadToEnd());

                    customProcess.WaitForExit();
                }

                IConfigSettingsReader reader = new CsvSpreadsheetFileReader();
                DataSource            ds     = new DataSource(tempFile, DataSourceType.Spreadsheet, DataSourceSpreadsheetFormat.Csv);
                dt = reader.ReadSettings(ds, context);
            }
            finally
            {
                if (File.Exists(tempFile))
                {
                    File.Delete(tempFile);
                }
            }

            return(dt);
        }
        /// <summary>
        /// Read the settings from a binary Excel XLS file into a DataTable.
        /// </summary>
        /// <param name="dataSource">The settings data source.</param>
        /// <param name="context">The preprocessing context.</param>
        /// <returns></returns>
        public DataTable ReadSettings(DataSource dataSource, PreprocessingContext context)
        {
            string connectionString =
                string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"Excel 8.0;HDR=NO;MAXSCANROWS=1\"", dataSource.Path);

            DataSet ds = new DataSet();

            using (OleDbConnection conn = new OleDbConnection(connectionString))
            {
                OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM [Settings$]", conn);
                da.Fill(ds);
            }

            DataTable dt = ds.Tables[0];

            dt.Columns.Add("Comment", typeof(string));

            return(dt);
        }
Example #13
0
        /// <summary>
        /// Runs the code.
        /// </summary>
        /// <param name="context">The preprocessing context.</param>
        /// <param name="results">The results.</param>
        /// <returns>The expression resolved to a single string.</returns>
        private bool RunBoolEvaluation(PreprocessingContext context, CompilerResults results)
        {
            bool result = false;

            Assembly executingAssembly = results.CompiledAssembly;

            if (executingAssembly != null)
            {
                object assemblyInstance = executingAssembly.CreateInstance(
                    "XmlPreprocess.BoolEvaluator", false, BindingFlags.CreateInstance, null,
                    new object[] { context }, null, null);

                Module[]   modules = executingAssembly.GetModules(false);
                Type[]     types   = modules[0].GetTypes();
                Type       type    = types[0];
                MethodInfo mi      = type.GetMethod("Evaluate");

                result = (bool)mi.Invoke(assemblyInstance, null);
            }

            return(result);
        }
        static private void Else(string[] parameters, ref PreprocessingContext context)
        {
            bool flag = context.include.Pop();

            context.include.Push(!flag);
        }
Example #15
0
        /// <summary>
        /// Finds the index of the row containing the desired setting.
        /// </summary>
        /// <param name="settingsTable">The settings table.</param>
        /// <param name="settingNameToFind">Name of the setting to find.</param>
        /// <param name="context">Preprocessing context.</param>
        /// <returns>row index of setting, or -1 if not found</returns>
        private int FindSettingRowIndex(DataTable settingsTable, string settingNameToFind, PreprocessingContext context)
        {
            int propertyRowIndex = -1;

            // Loop through the rows that contain settings and export each one to the XML file.
            for (int rowIndex = context.FirstValueRowIndex - 1; rowIndex < settingsTable.Rows.Count; rowIndex++)
            {
                // Determine the setting name, or skip the row if there is no setting name value.
                if (settingsTable.Rows[rowIndex].IsNull(context.SettingNameColumnIndex - 1))
                {
                    continue;
                }

                string settingName = (string)settingsTable.Rows[rowIndex][context.SettingNameColumnIndex - 1];
                if (settingName.Trim().Length == 0)
                {
                    continue;
                }

                if (settingName.Equals(settingNameToFind, StringComparison.OrdinalIgnoreCase))
                {
                    propertyRowIndex = rowIndex;
                    break;
                }
            }
            return(propertyRowIndex);
        }
        /// <summary>
        /// Read the settings from a CSV file into a DataTable.
        /// </summary>
        /// <param name="dataSource">The settings data source.</param>
        /// <param name="context">The preprocessing context.</param>
        /// <returns></returns>
        public DataTable ReadSettings(DataSource dataSource, PreprocessingContext context)
        {
            DataTable dt = new DataTable("Settings");

            TextReader textReader = null;
            FileStream fileStream = null;

            try
            {
                if (FileUtils.IsHttpUrl(dataSource.Path))
                {
                    string contents = FileUtils.DownloadFile(dataSource.Path);
                    textReader = new StringReader(contents);
                }
                else
                {
                    fileStream = File.Open(dataSource.Path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                    textReader = new StreamReader(fileStream);
                }

                CsvFileReader csvReader = new CsvFileReader(textReader, "#");

                bool          hasHeaderRowBeenEncountered = false;
                List <string> row = csvReader.ReadRow();
                while (null != row)
                {
                    if (!hasHeaderRowBeenEncountered)
                    {
                        // skip blank rows
                        if (row.Count > 0)
                        {
                            // Add a column for each cell in header row
                            for (int index = 0; index < row.Count; index++)
                            {
                                dt.Columns.Add(index.ToString(), typeof(string));
                            }
                            hasHeaderRowBeenEncountered = true;
                        }
                    }

                    if (hasHeaderRowBeenEncountered)
                    {
                        DataRow newRow = dt.NewRow();
                        for (int columnIndex = 0; columnIndex < row.Count; columnIndex++)
                        {
                            // this will throw out values in columns beyond the length of the header row
                            if (columnIndex < dt.Columns.Count)
                            {
                                newRow[columnIndex] = row[columnIndex];
                            }
                        }
                        dt.Rows.Add(newRow);
                    }

                    row = csvReader.ReadRow();
                }
            }
            finally
            {
                if (null != textReader)
                {
                    textReader.Close();
                }

                if (null != fileStream)
                {
                    fileStream.Close();
                }
            }

            return(dt);
        }
        /// <summary>
        /// Read the settings from Office 2003 SpreadsheetML XML file into a DataTable.
        /// </summary>
        /// <param name="dataSource">The settings data source.</param>
        /// <param name="context">The preprocessing context.</param>
        /// <returns></returns>
        public DataTable ReadSettings(DataSource dataSource, PreprocessingContext context)
        {
            // Create a new, empty DataTable to hold the data. The resulting structure of the DataTable will
            // be identical to that produced with a binary XLS file.
            DataTable dt = new DataTable("Settings");

            FileStream fileStream = null;

            try
            {
                // Read the SpreadsheetML XML file into a new XPathDocument.
                XPathDocument doc = null;
                if (FileUtils.IsHttpUrl(dataSource.Path))
                {
                    doc = new XPathDocument(dataSource.Path);
                }
                else
                {
                    fileStream = File.Open(dataSource.Path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                    doc        = new XPathDocument(fileStream);
                }

                XPathNavigator nav = doc.CreateNavigator();

                // Create a namespace manager and register the SpreadsheetML namespace and prefix
                XmlNamespaceManager nm = new XmlNamespaceManager(nav.NameTable);
                nm.AddNamespace("ss", "urn:schemas-microsoft-com:office:spreadsheet");
                nm.AddNamespace("x", "urn:schemas-microsoft-com:office:excel");

                // Locate the Settings worksheet
                XPathNavigator worksheetNav = nav.SelectSingleNode("//ss:Worksheet[1]/ss:Table", nm);

                if (worksheetNav == null)
                {
                    throw new ArgumentException("The input file does not contain a valid worksheet.");
                }

                // If the first value row hasn't been overridden from command line,
                // then look in the XML Spreadsheet for a frozen row, indicating the
                // end of the header, and the beginning of the data
                if (CommandLine.FIRST_VALUE_ROW_DEFAULT == context.FirstValueRowIndex)
                {
                    XPathNavigator worksheetOptionsNav = nav.SelectSingleNode("//ss:Worksheet[1]/x:WorksheetOptions", nm);
                    if (worksheetOptionsNav != null)
                    {
                        XPathNavigator splitterNode = worksheetOptionsNav.SelectSingleNode("x:SplitHorizontal", nm);
                        if (splitterNode != null)
                        {
                            int splitHorizontal = 0;
                            if (Int32.TryParse(splitterNode.Value, out splitHorizontal))
                            {
                                context.FirstValueRowIndex = splitHorizontal + 1;
                            }
                        }
                    }
                }

                int    expandedColumnCount       = 0;
                string expandedColumnCountString = worksheetNav.GetAttribute("ExpandedColumnCount", "urn:schemas-microsoft-com:office:spreadsheet");
                int.TryParse(expandedColumnCountString, out expandedColumnCount);

                // Add a column for each cell
                for (int index = 0; index < expandedColumnCount; index++)
                {
                    dt.Columns.Add(index.ToString(), typeof(string));
                }

                // Select all of the rows in the worksheet
                XPathNodeIterator rowsIterator = worksheetNav.Select(".//ss:Row", nm);

                // Loop through the rows
                while (rowsIterator.MoveNext())
                {
                    // This is a workaround for a characteristic of the XML Spreadsheet 2003 format
                    // where rows can be sparsely populated, and an ss:Index attribute is used (ex: ss:Index="7")
                    // to indicate how many intervening empty rows were omitted
                    string rowIndexString = rowsIterator.Current.GetAttribute("Index", nm.LookupNamespace("ss"));
                    if (!string.IsNullOrEmpty(rowIndexString))
                    {
                        int rowIndex = -1;
                        if (Int32.TryParse(rowIndexString, out rowIndex))
                        {
                            int currentRowCount = dt.Rows.Count;
                            for (int i = currentRowCount; i < rowIndex - 1; i++)
                            {
                                dt.Rows.Add(dt.NewRow());
                            }
                        }
                    }

                    // Select all of the cells in the row
                    XPathNodeIterator cellsIterator = rowsIterator.Current.Select("ss:Cell", nm);

                    // Create a new DataRow to hold the incoming values
                    DataRow newRow = dt.NewRow();

                    // Loop through the cells
                    int columnIndex = 0;
                    while (cellsIterator.MoveNext())
                    {
                        // This is a workaround for a characteristic of the XML Spreadsheet 2003 format
                        // where cells can be sparsely populated. In the following example the cell at
                        // index 2 is skipped, and the cell at index 3 is in it's place with an Index
                        // attribute telling where it should go.
                        //
                        //   <Row ss:Height="25.5">
                        //    <Cell ss:StyleID="s73"><Data ss:Type="String">SomeSetting</Data></Cell>
                        //    <Cell ss:Index="3" ss:StyleID="s75"/>
                        //    <Cell ss:StyleID="s75"/>
                        //    <Cell ss:StyleID="s75"><Data ss:Type="String">data1</Data></Cell>
                        //    <Cell ss:StyleID="s74"><Data ss:Type="String">data2</Data></Cell>
                        //   </Row>

                        string indexString = cellsIterator.Current.GetAttribute("Index", nm.LookupNamespace("ss"));
                        if (!string.IsNullOrEmpty(indexString))
                        {
                            int index = -1;
                            if (Int32.TryParse(indexString, out index))
                            {
                                columnIndex = index - 1;
                            }
                        }


                        // Select the data value in the cell, if present
                        XPathNavigator dataNav = cellsIterator.Current.SelectSingleNode("ss:Data", nm);

                        if (null != dataNav)
                        {
                            string dataType = dataNav.GetAttribute("Type", nm.LookupNamespace("ss"));
                            if ("Boolean".Equals(dataType, StringComparison.OrdinalIgnoreCase))
                            {
                                if ("0".Equals(dataNav.Value))
                                {
                                    newRow[columnIndex] = "False";
                                }
                                else if ("1".Equals(dataNav.Value))
                                {
                                    newRow[columnIndex] = "True";
                                }
                                else
                                {
                                    newRow[columnIndex] = dataNav.Value;
                                }
                            }
                            else
                            {
                                newRow[columnIndex] = dataNav.Value;
                            }
                        }

                        columnIndex++;
                    }

                    // Add the newly populated DataRow to the DataTable
                    dt.Rows.Add(newRow);
                }
            }
            finally
            {
                if (null != fileStream)
                {
                    fileStream.Close();
                }
            }

            return(dt);
        }
Example #18
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SettingsLoader"/> class.
 /// </summary>
 public SettingsLoader(PreprocessingContext context)
 {
     Context = context;
 }
 public LingoProcessingContext( ReducedVectorSpaceModelContext reducedVsmContext )
 {
     this.reducedVsmContext = reducedVsmContext;
     this.vsmContext = reducedVsmContext.vsmContext;
     this.preprocessingContext = vsmContext.preprocessingContext;
 }
Example #20
0
        /// <summary>
        /// Read the settings from a database into a DataTable.
        /// </summary>
        /// <param name="dataSource">The settings data source.</param>
        /// <param name="context">The preprocessing context.</param>
        /// <returns></returns>
        public DataTable ReadSettings(DataSource dataSource, PreprocessingContext context)
        {
            var environments = new List <string>();
            var rows         = new List <SqlSettings>();

            string sql = @"select e.EnvironmentName, e.IsDefault, n.SettingName, n.SettingType, v.SettingValue from ConfigSettingNames n
                    left outer join ConfigSettingValues v on n.SettingID = v.SettingID
                    left join ConfigEnvironments e on e.EnvironmentID = v.EnvironmentID
                order by n.ViewOrder, e.ViewOrder";

            using (SqlConnection connection = new SqlConnection(dataSource.Path))
            {
                using (SqlCommand command = new SqlCommand(sql, connection))
                {
                    connection.Open();
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            string envName      = (!reader.IsDBNull(0) ? reader.GetString(0) : null);
                            bool   isDefault    = (!reader.IsDBNull(1) ? reader.GetBoolean(1) : false);
                            string settingName  = (!reader.IsDBNull(2) ? reader.GetString(2) : null);
                            int    settingType  = (!reader.IsDBNull(3) ? reader.GetInt32(3) : 0);
                            string settingValue = (!reader.IsDBNull(4) ? reader.GetString(4) : null);

                            if (!string.IsNullOrEmpty(envName) && !environments.Contains(envName))
                            {
                                if (isDefault)
                                {
                                    environments.Insert(0, envName);
                                }
                                else
                                {
                                    environments.Add(envName);
                                }
                            }

                            SqlSettings row = rows.Find(delegate(SqlSettings s) { return(s.SettingName.Equals(settingName, StringComparison.OrdinalIgnoreCase)); });
                            if (null == row)
                            {
                                row = new SqlSettings(settingName);
                                rows.Add(row);
                            }

                            if (!string.IsNullOrEmpty(envName))
                            {
                                row.Values[envName] = settingValue;
                            }
                        }
                    }
                }
            }


            DataTable dt = new DataTable("Settings");

            // Add columns
            dt.Columns.Add("0", typeof(string));         // for setting name
            for (int i = 0; i < environments.Count; i++) // one for each environment
            {
                dt.Columns.Add((i + 1).ToString(), typeof(string));
            }

            // Add first row
            DataRow newRow = dt.NewRow();

            newRow[0] = "";
            for (int i = 0; i < environments.Count; i++)
            {
                newRow[i + 1] = environments[i];
            }
            dt.Rows.Add(newRow);

            // Add values
            foreach (SqlSettings row in rows)
            {
                newRow    = dt.NewRow();
                newRow[0] = row.SettingName;
                for (int i = 0; i < environments.Count; i++)
                {
                    string envName = environments[i];
                    if (row.Values.ContainsKey(envName))
                    {
                        newRow[i + 1] = row.Values[envName];
                    }
                }
                dt.Rows.Add(newRow);
            }

            return(dt);
        }
 private static void EndIf(string[] parameters, ref PreprocessingContext context)
 {
     context.include.Pop();
 }
 private static void Else(string[] parameters, ref PreprocessingContext context)
 {
     bool flag = context.include.Pop();
     context.include.Push(!flag);
 }
        private static void IfNotDefined(string[] parameters, ref PreprocessingContext context)
        {
            bool predefined = context.predefined.Contains(parameters[0]);
            bool defined = context.defCol.ContainsName(parameters[0]);

            context.include.Push(!predefined && !defined);
        }
        private static void Include(string[] parameters, ref PreprocessingContext context)
        {
            if (context.CanInclude)
            {

                //Remove comments
            }
        }
 private static void IncludeBinary(string[] parameters, ref PreprocessingContext context)
 {
     if (context.CanInclude)
     {
         //Read binary
     }
 }
 static private void EndIf(string[] parameters, ref PreprocessingContext context)
 {
     context.include.Pop();
 }
        private static void UnDefine(string[] parameters, ref PreprocessingContext context)
        {
            if (context.CanInclude)
            {
                string name;
                string[] mParam;
                name = GetMacro(parameters[0], out mParam);

                if (context.defCol.ContainsName(name, mParam))
                {
                    context.defCol.Remove(name, mParam);
                }
            }
        }