Beispiel #1
0
        public void ShowHeaders(
            [Argument(Description = "Path to the OML file to be loaded. It is possible to read from stdin instead of a file by using UNIX pipe access syntax (e.g.: 'pipe:').")]
            string input,
            [Argument(Description = "Target platform version to use for loading the OML file. For the latest compatible version, use the value 'OL'.")]
            string version,
            [Argument(Description = "If set, returns only the value of the specified header.")]
            string headerName = null)
        {
            Oml  oml   = _GetOmlInstance(input, version);
            bool found = false;

            foreach (PropertyInfo property in typeof(OmlHeader).GetProperties())
            {
                OmlHeaderAttribute attribute = (OmlHeaderAttribute)Attribute.GetCustomAttribute(property, typeof(OmlHeaderAttribute));

                if (attribute == null || !string.IsNullOrEmpty(headerName) && !headerName.Equals(property.Name, StringComparison.InvariantCultureIgnoreCase))
                {
                    continue;
                }

                if (!string.IsNullOrEmpty(headerName))
                {
                    Console.WriteLine(property.GetValue(oml.Header, null));
                }
                else
                {
                    Console.WriteLine("{0}:{1}", property.Name, property.GetValue(oml.Header, null));
                }

                found = true;
            }

            if (!string.IsNullOrEmpty(headerName) && !found)
            {
                throw new Exception("Header name \"" + headerName + "\" was not found.");
            }
        }
Beispiel #2
0
        public void Manipulate(
            [Argument(Description = "Path to the OML file to be loaded. It is possible to read from stdin instead of a file by using UNIX pipe access syntax (e.g.: 'pipe:').")]
            string input,
            [Argument(Description = "Destination path to save the manipulated OML file. It is possible to send the data stream to stdout instead by using UNIX pipe access syntax (e.g.: 'pipe:').")]
            string output,
            [Argument(Description = "Target platform version to use for loading the OML file. For the latest compatible version, use the value 'OL'.")]
            string version,
            [Option(Description = "Destination file format. Possible formats are 'oml' and 'xml'. If not set, will be guessed according to the output file extension.",
                    LongName = "format",
                    ShortName = "f")]
            string format = null,
            [Option(Description = "Sets a header value. Name and value must be separated by colon (':').",
                    LongName = "header",
                    ShortName = "H")]
            List <string> headers = null,
            [Option(Description = "Sets the content of a fragment. Name and value must be separated by colon (':').",
                    LongName = "fragment",
                    ShortName = "F")]
            List <string> fragments = null)
        {
            Oml oml = _GetOmlInstance(input, version);

            // Set headers
            if (headers != null)
            {
                foreach (string headerLine in headers)
                {
                    int colonIndex = headerLine.IndexOf(':');

                    if (colonIndex == -1)
                    {
                        throw new Exception("Unable to parse header value \"" + headerLine + "\". Name and value must be separated by colon (':').");
                    }

                    string headerName = headerLine.Substring(0, colonIndex);

                    if (string.IsNullOrEmpty(headerName))
                    {
                        throw new Exception("The header name in the header parameter is mandatory.");
                    }

                    bool found = false;

                    foreach (PropertyInfo property in typeof(OmlHeader).GetProperties())
                    {
                        OmlHeaderAttribute attribute = (OmlHeaderAttribute)Attribute.GetCustomAttribute(property, typeof(OmlHeaderAttribute));

                        if (attribute == null || !headerName.Equals(property.Name, StringComparison.InvariantCultureIgnoreCase))
                        {
                            continue;
                        }

                        if (attribute.IsReadOnly)
                        {
                            throw new Exception("Cannot change header \"" + property.Name + "\" because it is read-only.");
                        }

                        string headerValue = headerLine.Substring(colonIndex + 1);
                        property.SetValue(oml.Header, headerValue);
                        found = true;
                    }

                    if (!found)
                    {
                        throw new Exception("Header name \"" + headerName + "\" was not found.");
                    }
                }
            }

            // Set fragments
            if (fragments != null)
            {
                foreach (string fragmentLine in fragments)
                {
                    int colonIndex = fragmentLine.IndexOf(':');

                    if (colonIndex == -1)
                    {
                        throw new Exception("Unable to parse fragment value \"" + fragmentLine + "\". Name and value must be separated by colon (':').");
                    }

                    string fragmentName = fragmentLine.Substring(0, colonIndex);

                    if (string.IsNullOrEmpty(fragmentName))
                    {
                        throw new Exception("The fragment name in the fragment parameter is mandatory.");
                    }

                    XElement fragment = XElement.Parse(fragmentLine.Substring(colonIndex + 1));
                    oml.SetFragmentXml(fragmentName, fragment);
                }
            }

            // Save manipulated OML
            Stream outputStream = _GetStream(output, false);

            if (format != null && format.Equals("xml", StringComparison.InvariantCultureIgnoreCase) || format == null && output.EndsWith(".xml", StringComparison.InvariantCultureIgnoreCase))
            {
                StreamWriter sw = new StreamWriter(outputStream);
                sw.Write(oml.GetXml().ToString(SaveOptions.DisableFormatting)); // Export XML
                sw.Flush();
                sw.Close();
            }
            else
            {
                oml.Save(outputStream); // Export OML
            }

            outputStream.Close();
        }