Esempio n. 1
0
        public RelaxngPattern ReadExternalResource(RelaxngGrammar grammar, Uri uri, string nsContext)
        {
            XmlReader      xtr = null;
            RelaxngGrammar g   = null;
            RelaxngPattern p;

            try {
                if (uri.AbsolutePath.EndsWith(".rnc", StringComparison.OrdinalIgnoreCase))
                {
                    p = RncParser.ParseRnc(new StreamReader((Stream)grammar.Resolver.GetEntity(uri, null, typeof(Stream))), null, uri.AbsoluteUri, nsContext);
                }
                else
                {
#if PORTABLE
                    xtr = XmlReader.Create((Stream)grammar.Resolver.GetEntity(uri, null, typeof(Stream)));
#else
                    xtr = new XmlTextReader(uri.AbsoluteUri, (Stream)grammar.Resolver.GetEntity(uri, null, typeof(Stream)));
#endif
                    RelaxngReader r = new RelaxngReader(xtr, nsContext, grammar.Resolver);
                    r.MoveToContent();
                    p = r.ReadPattern();
                }
            } catch (Exception ex) {             // umm, bad catch though :-(
                throw new RelaxngException(this, String.Format("Could not include grammar {0}: {1}", uri.AbsoluteUri, ex.Message), ex);
            } finally {
                if (xtr != null)
                {
                    xtr.Dispose();
                }
            }
            p.XmlResolver = grammar.Resolver;
            return(p);
        }
	public static void Main (string [] args)
	{
		bool outAll = false;
		bool details = false;
		bool stopOnError = false;
		string filter = null;
		foreach (string arg in args) {
			switch (arg) {
			case "--outall":
				outAll = true; break;
			case "--details":
				details = true; break;
			case "--stoponerror":
				stopOnError = true; break;
			default:
				filter = arg; break;
			}
		}

		try {
			XmlDocument doc = new XmlDocument ();
			doc.Load ("test/RNCTest.xml");
			int success = 0;
			int failure = 0;
			foreach (XmlElement el in doc.SelectNodes ("/RNCTestCases/TestCase")) {
				string id = el.GetAttribute ("id");
				if (filter != null && id.IndexOf (filter) < 0)
					continue;
				if (outAll)
					Console.WriteLine ("testing " + id);
				bool isValid = el.GetAttribute ("legal") == "true";
				RncParser p = new RncParser (new NameTable ());
				try {
					string s = new StreamReader ("test" + Path.DirectorySeparatorChar + el.GetAttribute ("path")).ReadToEnd ();
					p.Parse (new StringReader (s));
					if (isValid) {
						success++;
//						Console.Error.WriteLine ("valid " + id);
					} else {
						failure++;
						Console.Error.WriteLine ("INCORRECTLY VALID   " + id);
					}
				} catch (Exception ex) {
					if (isValid) {
						if (stopOnError)
							throw;
						failure++;
						Console.Error.WriteLine ("INCORRECTLY INVALID " + id + " --> " + (details ? ex.ToString () : ex.Message));
					} else {
						success++;
//						Console.Error.WriteLine ("invalid " + id);
					}
				}
			}
			Console.Error.WriteLine ("Total success: " + success);
			Console.Error.WriteLine ("Total failure: " + failure);
		} catch (Exception ex) {
			Console.Error.WriteLine ("Unexpected Exception: " + ex);
		}
	}
Esempio n. 3
0
		RelaxngPattern Compile (TextReader reader, string baseUri)
		{
			RncParser parser = new RncParser (new NameTable ());
			RelaxngPattern g = parser.Parse (reader, baseUri);
			g.Compile ();
			return g;
		}
Esempio n. 4
0
        public static int sMain(string [] args)
        {
            if (args.Length < 1)
            {
                Console.WriteLine("Usage: monodoc2wiki monodoc_xmlfile");
                return(1);
            }

            XmlDocument doc = new XmlDocument();

#if VALIDATION
            XmlTextReader  xr = new XmlTextReader(args [0]);
            RelaxngPattern rp = RncParser.ParseRnc(new StreamReader("CLILibraryTypes.rnc"));
            Console.Error.WriteLine("**** READY ****");
            rp.Compile();
            Console.Error.WriteLine("**** DONE ****");
            RelaxngValidatingReader rvr = new RelaxngValidatingReader(xr, rp);
            doc.Load(rvr);
            rvr.Close();
#else
            doc.Load(args [0]);
#endif
            Monodoc2Wiki instance = new Monodoc2Wiki();
            string       ret      = instance.ProcessNode(doc.DocumentElement);

            Console.WriteLine(ret);

            return(0);
        }
Esempio n. 5
0
        public RelaxngPattern ReadExternalResource(RelaxngGrammar grammar, Uri uri, string nsContext)
        {
            XmlTextReader  xtr = null;
            RelaxngGrammar g   = null;
            RelaxngPattern p;

            try {
                if (grammar.IsSourceCompactSyntax)
                {
                    p = RncParser.ParseRnc(new StreamReader((Stream)grammar.Resolver.GetEntity(uri, null, typeof(Stream))), null, BaseUri, nsContext);
                }
                else
                {
                    xtr = new XmlTextReader(uri.AbsoluteUri, (Stream)grammar.Resolver.GetEntity(uri, null, typeof(Stream)));
                    RelaxngReader r = new RelaxngReader(xtr, nsContext, grammar.Resolver);
                    r.MoveToContent();
                    p = r.ReadPattern();
                }
            } catch (Exception ex) {             // umm, bad catch though :-(
                throw new RelaxngException(this, String.Format("Could not include grammar {0}: {1}", uri.AbsoluteUri, ex.Message), ex);
            } finally {
                if (xtr != null)
                {
                    xtr.Close();
                }
            }
            p.XmlResolver = grammar.Resolver;
            return(p);
        }
Esempio n. 6
0
        RelaxngPattern Compile(TextReader reader, string baseUri)
        {
            RncParser      parser = new RncParser(new NameTable());
            RelaxngPattern g      = parser.Parse(reader, baseUri);

            g.Compile();
            return(g);
        }
Esempio n. 7
0
        static void ValidateRelaxngCompact(string [] args)
        {
            StreamReader   sr = new StreamReader(args [1]);
            RelaxngPattern p  = RncParser.ParseRnc(sr, null, Path.GetFullPath(args [1]));

            sr.Close();
            ValidateRelaxng(p, args);
        }
Esempio n. 8
0
        public void SimpleDefaultNamespace()
        {
            var g = RncParser.ParseRnc(new StringReader("element e { empty }"));
            var x = XmlReader.Create(new StringReader("<e/>"));
            var r = new RelaxngValidatingReader(x, g);

            try {
                while (!r.EOF)
                {
                    r.Read();
                }
            } finally {
                r.Close();
            }
        }
        private NvdlValidatorGenerator CreateRncGenerator(NvdlValidate validate, NvdlConfig config)
        {
            XmlResolver resolver  = config.XmlResolverInternal;
            string      schemaUri = GetSchemaUri(validate);

            if (schemaUri == null)
            {
                return(null);
            }
            Uri            baseUri = resolver.ResolveUri(null, validate.SourceUri);
            Uri            absUri  = resolver.ResolveUri(baseUri, schemaUri);
            RelaxngPattern p       = RncParser.ParseRnc(
                new StreamReader((Stream)resolver.GetEntity(
                                     absUri,
                                     null,
                                     typeof(Stream))), null, absUri.ToString());

            return(new NvdlRelaxngValidatorGenerator(p, config));
        }
Esempio n. 10
0
        // --- Methods ---
        public ScriptManifestValidationResult Validate(string path) {
            try {
                XmlTextReader xmlReader = new XmlTextReader(path);
                RelaxngValidatingReader validator;

                using(Stream resourceStream
                    = Plug.New("resource://mindtouch.deki.script.check/MindTouch.Deki.Script.ExtensionManifest.rnc").Get().ToStream()) {
                    using(TextReader source = new StreamReader(resourceStream)) {
                        RncParser parser = new RncParser(new NameTable());
                        RelaxngPattern pattern = parser.Parse(source);
                        validator = new RelaxngValidatingReader(xmlReader, pattern);
                    }
                }
                while(validator.Read()) {
                    // do nothing, errors will be reported through exceptions
                }
            } catch(Exception e) {
                return new ScriptManifestValidationResult(true, e.Message);
            }

            return new ScriptManifestValidationResult();
        }
Esempio n. 11
0
        // --- Methods ---
        public ScriptManifestValidationResult Validate(string path)
        {
            try {
                XmlTextReader           xmlReader = new XmlTextReader(path);
                RelaxngValidatingReader validator;

                using (Stream resourceStream
                           = Plug.New("resource://mindtouch.deki.script.check/MindTouch.Deki.Script.ExtensionManifest.rnc").Get().ToStream()) {
                    using (TextReader source = new StreamReader(resourceStream)) {
                        RncParser      parser  = new RncParser(new NameTable());
                        RelaxngPattern pattern = parser.Parse(source);
                        validator = new RelaxngValidatingReader(xmlReader, pattern);
                    }
                }
                while (validator.Read())
                {
                    // do nothing, errors will be reported through exceptions
                }
            } catch (Exception e) {
                return(new ScriptManifestValidationResult(true, e.Message));
            }

            return(new ScriptManifestValidationResult());
        }
Esempio n. 12
0
    public static void Main(string [] args)
    {
        bool   outAll      = false;
        bool   details     = false;
        bool   stopOnError = false;
        string filter      = null;

        foreach (string arg in args)
        {
            switch (arg)
            {
            case "--outall":
                outAll = true; break;

            case "--details":
                details = true; break;

            case "--stoponerror":
                stopOnError = true; break;

            default:
                filter = arg; break;
            }
        }

        try {
            XmlDocument doc = new XmlDocument();
            doc.Load("test/RNCTest.xml");
            int success = 0;
            int failure = 0;
            foreach (XmlElement el in doc.SelectNodes("/RNCTestCases/TestCase"))
            {
                string id = el.GetAttribute("id");
                if (filter != null && id.IndexOf(filter) < 0)
                {
                    continue;
                }
                if (outAll)
                {
                    Console.WriteLine("testing " + id);
                }
                bool      isValid = el.GetAttribute("legal") == "true";
                RncParser p       = new RncParser(new NameTable());
                try {
                    string s = new StreamReader("test" + Path.DirectorySeparatorChar + el.GetAttribute("path")).ReadToEnd();
                    p.Parse(new StringReader(s));
                    if (isValid)
                    {
                        success++;
//						Console.Error.WriteLine ("valid " + id);
                    }
                    else
                    {
                        failure++;
                        Console.Error.WriteLine("INCORRECTLY VALID   " + id);
                    }
                } catch (Exception ex) {
                    if (isValid)
                    {
                        if (stopOnError)
                        {
                            throw;
                        }
                        failure++;
                        Console.Error.WriteLine("INCORRECTLY INVALID " + id + " --> " + (details ? ex.ToString() : ex.Message));
                    }
                    else
                    {
                        success++;
//						Console.Error.WriteLine ("invalid " + id);
                    }
                }
            }
            Console.Error.WriteLine("Total success: " + success);
            Console.Error.WriteLine("Total failure: " + failure);
        } catch (Exception ex) {
            Console.Error.WriteLine("Unexpected Exception: " + ex);
        }
    }
Esempio n. 13
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();
            }
        }