Beispiel #1
0
        static void Main(string[] args)
        {
            string Html;

            Log.Register(new ConsoleEventSink(false));
            Log.RegisterExceptionToUnnest(typeof(System.Runtime.InteropServices.ExternalException));
            Log.RegisterExceptionToUnnest(typeof(System.Security.Authentication.AuthenticationException));

            try
            {
                if (!File.Exists("table.htm") || (DateTime.Now - File.GetLastWriteTime("table.htm")).TotalHours >= 1.0)
                {
                    Log.Informational("Downloading table.");

                    WebClient Client = new WebClient();
                    Client.DownloadFile("http://unicodey.com/emoji-data/table.htm", "table.htm");

                    Log.Informational("Loading table");
                    Html = File.ReadAllText("table.htm");

                    Log.Informational("Fixing encoding errors.");
                    Html = Html.
                           Replace("<td><3</td>", "<td>&lt;3</td>").
                           Replace("<td></3</td>", "<td>&lt;/3</td>").
                           Replace("</body>\n<html>", "</body>\n</html>");

                    File.WriteAllText("table.htm", Html);
                }
                else
                {
                    Log.Informational("Loading table");
                    Html = File.ReadAllText("table.htm");
                }

                Log.Informational("Transforming to C#.");

                XslCompiledTransform Transform = XSL.LoadTransform("Waher.Utility.GetEmojiCatalog.Transforms.HtmlToCSharp.xslt");
                string CSharp = XSL.Transform(Html, Transform);

                Log.Informational("Saving C#.");
                File.WriteAllText("EmojiUtilities.cs", CSharp);
            }
            catch (Exception ex)
            {
                Log.Critical(ex);
            }
            finally
            {
                Log.Terminate();
            }
        }
Beispiel #2
0
        public void SaveAsButton_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog Dialog = new SaveFileDialog()
            {
                AddExtension    = true,
                CheckPathExists = true,
                CreatePrompt    = false,
                DefaultExt      = "html",
                Filter          = "XML Files (*.xml)|*.xml|HTML Files (*.html,*.htm)|*.html,*.htm|All Files (*.*)|*.*",
                Title           = "Save Sensor data readout"
            };

            bool?Result = Dialog.ShowDialog(MainWindow.FindWindow(this));

            if (Result.HasValue && Result.Value)
            {
                try
                {
                    if (Dialog.FilterIndex == 2)
                    {
                        StringBuilder Xml = new StringBuilder();
                        using (XmlWriter w = XmlWriter.Create(Xml, XML.WriterSettings(true, true)))
                        {
                            this.SaveAsXml(w);
                        }

                        string Html = XSL.Transform(Xml.ToString(), sensorDataToHtml);

                        File.WriteAllText(Dialog.FileName, Html, System.Text.Encoding.UTF8);
                    }
                    else
                    {
                        using (FileStream f = File.Create(Dialog.FileName))
                        {
                            using (XmlWriter w = XmlWriter.Create(f, XML.WriterSettings(true, false)))
                            {
                                this.SaveAsXml(w);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(MainWindow.FindWindow(this), ex.Message, "Unable to save file.", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
        }
 private IElement DoTransform(string Xml, XslCompiledTransform Xslt)
 {
     return(new StringValue(XSL.Transform(Xml, Xslt)));
 }
Beispiel #4
0
        /// <summary>
        /// Analyzes the object database
        /// </summary>
        /// <param name="FullPath">Full path of report file</param>
        /// <param name="FileName">Filename of report</param>
        /// <param name="Created">Time when report file was created</param>
        /// <param name="XmlOutput">XML Output</param>
        /// <param name="fs">File stream</param>
        /// <param name="Repair">If database should be repaired, if errors are found</param>
        public static async Task DoAnalyze(string FullPath, string FileName, DateTime Created, XmlWriter XmlOutput, FileStream fs, bool Repair)
        {
            try
            {
                Log.Informational("Starting analyzing database.", FileName);

                ExportFormats.ExportFormat.UpdateClientsFileUpdated(FileName, 0, Created);

                await Database.Analyze(XmlOutput, Path.Combine(Gateway.AppDataFolder, "Transforms", "DbStatXmlToHtml.xslt"),
                                       Gateway.AppDataFolder, false, Repair);

                XmlOutput.Flush();
                fs.Flush();

                ExportFormats.ExportFormat.UpdateClientsFileUpdated(FileName, fs.Length, Created);

                XmlOutput.Dispose();
                XmlOutput = null;

                fs.Dispose();
                fs = null;

                if (xslt is null)
                {
                    xslt = XSL.LoadTransform(typeof(Gateway).Namespace + ".Transforms.DbStatXmlToHtml.xslt");
                }

                string s = File.ReadAllText(FullPath);
                s = XSL.Transform(s, xslt);
                byte[] Bin = utf8Bom.GetBytes(s);

                string FullPath2 = FullPath.Substring(0, FullPath.Length - 4) + ".html";
                File.WriteAllBytes(FullPath2, Bin);

                ExportFormats.ExportFormat.UpdateClientsFileUpdated(FileName.Substring(0, FileName.Length - 4) + ".html", Bin.Length, Created);

                Log.Informational("Database analysis successfully completed.", FileName);
            }
            catch (Exception ex)
            {
                Log.Critical(ex);

                string[] Tabs = ClientEvents.GetTabIDsForLocation("/Settings/Backup.md");
                ClientEvents.PushEvent(Tabs, "BackupFailed", "{\"fileName\":\"" + CommonTypes.JsonStringEncode(FileName) +
                                       "\", \"message\": \"" + CommonTypes.JsonStringEncode(ex.Message) + "\"}", true, "User");
            }
            finally
            {
                try
                {
                    XmlOutput?.Dispose();
                    fs?.Dispose();
                }
                catch (Exception ex)
                {
                    Log.Critical(ex);
                }

                lock (synchObject)
                {
                    analyzing = false;
                }
            }
        }
Beispiel #5
0
        /// <summary>
        /// Transforms an XML file using an XSL Transform (XSLT) file.
        ///
        /// Command line switches:
        ///
        /// -i INPUT_FILE         File name of XML file.
        /// -t TRANSFORM_FILE     XSLT transform to use.
        /// -o OUTPUT_FILE        File name of output file.
        /// -enc ENCODING         Text encoding. Default=UTF-8
        /// -?                    Help.
        /// </summary>
        static int Main(string[] args)
        {
            try
            {
                Encoding Encoding       = Encoding.UTF8;
                string   InputFileName  = null;
                string   OutputFileName = null;
                string   XsltPath       = null;
                string   s;
                int      i    = 0;
                int      c    = args.Length;
                bool     Help = false;

                while (i < c)
                {
                    s = args[i++].ToLower();

                    switch (s)
                    {
                    case "-o":
                        if (i >= c)
                        {
                            throw new Exception("Missing output file name.");
                        }

                        if (string.IsNullOrEmpty(OutputFileName))
                        {
                            OutputFileName = args[i++];
                        }
                        else
                        {
                            throw new Exception("Only one output file name allowed.");
                        }
                        break;

                    case "-i":
                        if (i >= c)
                        {
                            throw new Exception("Missing input file name.");
                        }

                        if (string.IsNullOrEmpty(InputFileName))
                        {
                            InputFileName = args[i++];
                        }
                        else
                        {
                            throw new Exception("Only one input file name allowed.");
                        }
                        break;

                    case "-enc":
                        if (i >= c)
                        {
                            throw new Exception("Text encoding missing.");
                        }

                        Encoding = Encoding.GetEncoding(args[i++]);
                        break;

                    case "-t":
                        if (i >= c)
                        {
                            throw new Exception("XSLT transform missing.");
                        }

                        XsltPath = args[i++];
                        break;

                    case "-?":
                        Help = true;
                        break;

                    default:
                        throw new Exception("Unrecognized switch: " + s);
                    }
                }

                if (Help || c == 0)
                {
                    Console.Out.WriteLine("Transforms an XML file using an XSL Transform (XSLT) file.");
                    Console.Out.WriteLine();
                    Console.Out.WriteLine("Command line switches:");
                    Console.Out.WriteLine();
                    Console.Out.WriteLine("-i INPUT_FILE         File name of XML file.");
                    Console.Out.WriteLine("-t TRANSFORM_FILE     XSLT transform to use.");
                    Console.Out.WriteLine("-o OUTPUT_FILE        File name of output file.");
                    Console.Out.WriteLine("-enc ENCODING         Text encoding. Default=UTF-8");
                    Console.Out.WriteLine("-?                    Help.");
                    return(0);
                }

                if (string.IsNullOrEmpty(InputFileName))
                {
                    throw new Exception("No input filename specified.");
                }

                if (string.IsNullOrEmpty(XsltPath))
                {
                    throw new Exception("No transform filename specified.");
                }

                if (string.IsNullOrEmpty(OutputFileName))
                {
                    throw new Exception("No output filename specified.");
                }

                string Xml = File.ReadAllText(InputFileName);

                using (Stream f = File.OpenRead(XsltPath))
                {
                    using (XmlReader r = XmlReader.Create(f))
                    {
                        XslCompiledTransform Xslt = new XslCompiledTransform();
                        Xslt.Load(r);

                        Xml = XSL.Transform(Xml, Xslt);

                        File.WriteAllText(OutputFileName, Xml);
                    }
                }

                return(0);
            }
            catch (Exception ex)
            {
                Console.Out.WriteLine(ex.Message);
                return(-1);
            }
        }