Example #1
0
 private void LoadSchemaFile(string rngFile)
 {
     using (var rngReader = new XmlTextReader(rngFile))
     {
         relaxPattern = RelaxngPattern.Read(rngReader);
         relaxPattern.Compile();
     }
 }
 /// <summary>
 /// Initializes a new instance of RngVerifier class from specified RelaxNG schema definition
 /// </summary>
 /// <param name="schema">RelaxNG schema definition</param>
 public RngVerifier(string schema)
 {
     using (var rdr = new StringReader(schema))
     {
         XmlTextReader xtrRng = new XmlTextReader(rdr);
         this.rngPattern = RelaxngPattern.Read(xtrRng);
         this.rngPattern.Compile();
     }
 }
        public override NvdlValidatorGenerator CreateGenerator(
            XmlReader reader, NvdlConfig config)
        {
            if (reader.NamespaceURI != RelaxngGrammar.NamespaceURI)
            {
                return(null);
            }

            return(new NvdlRelaxngValidatorGenerator(RelaxngPattern.Read(reader), config));
        }
Example #4
0
        public void Bug347945()
        {
            string         rng = @"
<element name='x' xmlns='http://relaxng.org/ns/structure/1.0'>
  <interleave>
    <element name='y'><text/></element>
    <element name='z'><text/></element>
  </interleave>
</element>";
            RelaxngPattern p   = RelaxngPattern.Read(new XmlTextReader(rng, XmlNodeType.Document, null));
        }
        private bool _validateGrammarRules()
        {
            if (!_validateGrammarFormat())
            {
                return(false);
            }

            try
            {
                RelaxngPattern pattern = RelaxngPattern.Read(_grammarReader);
                GrammarRuleError = null;
                return(true);
            }
            catch (RelaxngException e)
            {
                GrammarRuleError = e.Message;
                return(false);
            }
        }
Example #6
0
        private void loadSchemas()
        {
            Assembly ass = Assembly.GetExecutingAssembly();

            if (schemaModel == null)
            {
                System.IO.Stream s = ass.GetManifestResourceStream("doap.rdf");
                schemaStorage = new Storage("memory", "schema", null);
                schemaModel   = new Model(schemaStorage);
                Encoding     e   = Encoding.GetEncoding("utf-8");
                StreamReader r   = new StreamReader(s, e);
                string       txt = r.ReadToEnd();
                parser.ParseStringIntoModel(txt, DoapSchemaUri, schemaModel);
            }
            if (rngSchema == null)
            {
                System.IO.Stream s = ass.GetManifestResourceStream("doap.rng");
                rngSchema = RelaxngPattern.Read(new XmlTextReader(s));
            }
        }
Example #7
0
    static void RunTest()
    {
        foreach (DirectoryInfo di in new DirectoryInfo(@"relax-ng").GetDirectories())
        {
            XmlTextReader xtr = null;
            FileInfo      fi  = new FileInfo(di.FullName + "/i.rng");
            // Invalid grammar case:
            if (fi.Exists)
            {
                xtr = new XmlTextReader(fi.FullName);
                try {
                    RelaxngPattern.Read(xtr).Compile();
                    Console.WriteLine("Expected error: " + di.Name);
                } catch (RelaxngException ex) {
                } catch (XmlException ex) {
                } catch (ArgumentNullException ex) {
                } catch (UriFormatException ex) {
                } catch (Exception ex) {
                    Console.WriteLine("Unexpected error type : " + di.Name + " : " + ex.Message);
                } finally {
                    xtr.Close();
                }
                continue;
            }

            // Valid grammar case:
            xtr = new XmlTextReader(di.FullName + "/c.rng");
            RelaxngPattern p = null;
            try {
                p = RelaxngPattern.Read(xtr);
                p.Compile();
            } catch (Exception ex) {
                Console.WriteLine("Invalidated grammar: " + di.Name + " : " + ex.Message);
                continue;
            } finally {
                xtr.Close();
            }


            // Instance validation
            foreach (FileInfo inst in di.GetFiles("*.xml"))
            {
                try {
                    RelaxngValidatingReader vr = new RelaxngValidatingReader(new XmlTextReader(inst.FullName), p);
                    if (skip_error)
                    {
                        vr.InvalidNodeFound += RelaxngValidatingReader.IgnoreError;
                    }
                    while (!vr.EOF)
                    {
                        vr.Read();
                    }
                    if (inst.Name.IndexOf("i.") >= 0 && !skip_error)
                    {
                        Console.WriteLine("Incorrectly validated instance: " + di.Name + "/" + inst.Name);
                    }
                } catch (RelaxngException ex) {
                    string path = di.Name + "/" + inst.Name;
                    if (skip_error)
                    {
                        Console.WriteLine("Failed to skip error : " + path + ex.Message);
                    }
                    if (inst.Name.IndexOf("i.") >= 0)
                    {
                        continue;
                    }
                    Console.WriteLine("Invalidated instance: " + path + " : " + ex.Message);
                }
            }
        }
    }
Example #8
0
        public static string RelaxNGValidate(string xml, string rngFile)
        {
            string r = "\r\n";

            // Files must exist.
            if (!File.Exists(rngFile))
            {
                return("Schema file not found.");
            }

            // Grammar.
            RelaxngPattern p = null;

            if (Path.GetExtension(rngFile).ToUpper() == ".RNG")
            {
                XmlTextReader xtrRng = new XmlTextReader(rngFile);
                try
                {
                    p = RelaxngPattern.Read(xtrRng);
                    p.Compile();
                }
                catch (Exception ex1)
                {
                    return("Schema file has invalid grammar:" + r
                           + rngFile + r + ex1.Message);
                }
                finally
                {
                    xtrRng.Close();
                }
            }
            else
            if (Path.GetExtension(rngFile).ToUpper() == ".RNC")
            {
                var trRnc = new StreamReader(rngFile);
                try
                {
                    p = RncParser.ParseRnc(trRnc);
                    p.Compile();
                }
                catch (Exception ex1)
                {
                    return("Schema file has invalid grammar:" + r
                           + rngFile + r + ex1.Message);
                }
                finally
                {
                    trRnc.Close();
                }
            }
            else
            {
                return("Unknown schema file extension: " + Path.GetExtension(rngFile));
            }

            byte[]       byteArray = Encoding.Default.GetBytes(xml);
            MemoryStream stream    = new MemoryStream(byteArray);

            // Validate instance.
            XmlTextReader           xtrXml = new XmlTextReader(stream);
            RelaxngValidatingReader vr     = new RelaxngValidatingReader(xtrXml, p);

            try
            {
                while (!vr.EOF)
                {
                    vr.Read();
                }
                // XML file is valid.
                return("");
            }
            catch (RelaxngException ex2)
            {
                // XML file not valid.
                return(ex2.Message);
            }
            catch (Exception ex3)
            {
                // XML file not well-formed.
                return(ex3.Message);
            }
            finally
            {
                vr.Close();
                xtrXml.Close();
            }
        }
Example #9
0
        void Run(string [] args)
        {
            if (args.Length < 2)
            {
                Console.WriteLine("specify arguments: path_to_ldml_files config_file");
                return;
            }
            string dirname        = args [0];
            string configFileName = args [1];
            string config         = null;

            using (StreamReader sr = new StreamReader(configFileName)) {
                config = sr.ReadToEnd();
            }
            foreach (string configLine in config.Split('\n'))
            {
                int    idx  = configLine.IndexOf('#');
                string line = idx < 0 ? configLine : configLine.Substring(0, idx);
                if (line.StartsWith("ignore: "))
                {
                    ignoredFiles.Add(line.Substring(8).Trim());
                }
            }

            XmlTextReader  rng = new XmlTextReader("ldml-limited.rng");
            RelaxngPattern p   = RelaxngPattern.Read(rng);

            rng.Close();

            foreach (FileInfo fi in new DirectoryInfo(dirname).GetFiles("*.xml"))
            {
                if (ignoredFiles.Contains(fi.Name))
                {
                    continue;                     // skip
                }
                XmlTextReader inst = null;
                try {
                    inst             = new XmlTextReader(fi.FullName);
                    inst.XmlResolver = null;
                    RelaxngValidatingReader rvr = new
                                                  RelaxngValidatingReader(inst, p);
                    rvr.ReportDetails = true;
                    XmlDocument doc = new XmlDocument();
                    doc.XmlResolver = null;
                    doc.Load(rvr);
                    TailoringStore ts = ProcessLdml(doc);
                    if (ts != null)
                    {
                        tailorings.Add(ts);
                    }
                } finally {
                    if (inst != null)
                    {
                        inst.Close();
                    }
                }
            }

            tailorings.Sort(TailoringStoreComparer.Instance);

            using (TextWriter tw = new StreamWriter("create-tailoring.out", false, System.Text.Encoding.UTF8)) {
                Serialize(tw);
            }
        }