예제 #1
0
        static void ValidateRelaxngXml(string [] args)
        {
            XmlReader      xr = new XmlTextReader(args [1]);
            RelaxngPattern p  = RelaxngPattern.Read(xr);

            xr.Close();
            ValidateRelaxng(p, args);
        }
예제 #2
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);
        }
예제 #3
0
 /// <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();
     }
 }
예제 #4
0
		public static RelaxngPattern Read (XmlReader xmlReader, RelaxngDatatypeProvider provider, XmlResolver xmlResolver)
		{
			RelaxngReader r = new RelaxngReader (xmlReader, null, xmlResolver);
			if (r.ReadState == ReadState.Initial)
				r.Read ();
			r.MoveToContent ();
			RelaxngPattern p = r.ReadPattern ();
			p.DataProvider = provider;
			p.XmlResolver = xmlResolver;
			return p;
		}
예제 #5
0
		public RelaxngValidatingReader (XmlReader reader, RelaxngPattern pattern)
			: base (reader)
		{
			if (pattern == null)
				throw new ArgumentNullException ("pattern");

			if (reader.NodeType == XmlNodeType.Attribute)
				throw new RelaxngException ("RELAX NG does not support standalone attribute validation (it is prohibited due to the specification section 7.1.5");
			this.reader = reader;
			this.pattern = pattern;
		}
예제 #6
0
 private void ResetCompileState()
 {
     startPattern = null;
     assembledDefs.Clear();
     assembledStart = null;
     compiledStart  = null;
     elementReplacedDefs.Clear();
     includedUris.Clear();
     parentGrammar = null;
     refPatterns.Clear();
     checkedDefs.Clear();
     unresolvedPatterns.Clear();
     ElementDefMap.Clear();
 }
예제 #7
0
        public RelaxngValidatingReader(XmlReader reader, RelaxngPattern pattern)
            : base(reader)
        {
            if (pattern == null)
            {
                throw new ArgumentNullException("pattern");
            }

            if (reader.NodeType == XmlNodeType.Attribute)
            {
                throw new RelaxngException("RELAX NG does not support standalone attribute validation (it is prohibited due to the specification section 7.1.5");
            }
            this.reader  = reader;
            this.pattern = pattern;
        }
예제 #8
0
		internal override RdpPattern Compile (RelaxngGrammar grammar)
		{
			grammar.CheckIncludeRecursion (Href);
			grammar.IncludedUris.Add (Href, Href);
			if (grammar.Resolver == null)
				throw new RelaxngException (this, "To compile 'include' element, XmlResolver is required.");
			Uri uri = grammar.Resolver.ResolveUri (BaseUri != String.Empty ? new Uri (BaseUri) : null, Href);
			RelaxngPattern p = ReadExternalResource (grammar, uri, ns);

			p.DataProvider = grammar.Provider;
			RdpPattern ret = p.Compile (grammar);

			grammar.IncludedUris.Remove (Href);

			return ret;

		}
예제 #9
0
        static void ValidateRelaxng(RelaxngPattern p, string [] args)
        {
            p.Compile();

            if (args.Length < 2)
            {
                return;
            }

            for (int i = 2; i < args.Length; i++)
            {
                XmlTextReader           xtr = new XmlTextReader(args [i]);
                RelaxngValidatingReader vr  =
                    new RelaxngValidatingReader(xtr, p);
                if (Environment.GetEnvironmentVariable("MONO_XMLTOOL_ERROR_DETAILS") == "yes")
                {
                    vr.ReportDetails = true;
                }
                else
                {
                    vr.InvalidNodeFound += delegate(XmlReader source, string message)
                    {
                        IXmlLineInfo li = source as IXmlLineInfo;
                        Console.WriteLine("ERROR: {0} (at {1} line {2} column {3})",
                                          message,
                                          source.BaseURI,
                                          li != null && li.HasLineInfo() ? li.LineNumber : 0,
                                          li != null && li.HasLineInfo() ? li.LinePosition : 0);
                        return(true);
                    }
                };

                while (!vr.EOF)
                {
                    vr.Read();
                }
            }
        }
예제 #10
0
		// validate string value agains attr and 
		// if invalid, then relax the type.
		private void InferMergedAttribute (RelaxngPattern ap)
		{
			switch (ap.PatternType) {
			case RelaxngPatternType.Ref:
				string refName = ((RelaxngRef) ap).Name;
				RelaxngDefine def = GetDefine (refName);
				InferMergedAttribute (def.Patterns [0]);
				return;
			case RelaxngPatternType.Optional:
				InferMergedAttribute (
					((RelaxngOptional) ap).Patterns [0]);
				return;
			}

			RelaxngAttribute attr = (RelaxngAttribute) ap;

			RelaxngPattern p = attr.Pattern;
			if (p is RelaxngText)
				return; // We could do nothing anymore.
			if (p is RelaxngEmpty) {
				if (source.Value.Length == 0)
					return; // We can keep empty.
				// We still could infer a choice of empty and
				// data, but it's being too complicated. So
				// here we just set text.
				attr.Pattern = new RelaxngText ();
				return;
			}
			RelaxngData data = p as RelaxngData;
			if (data == null)
				throw Error (p, "This inference implementation only allows text, empty and data for an attribute.");
			attr.Pattern = CreateSimplePattern (
				InferMergedType (source.Value,
				new QName (data.Type, data.DatatypeLibrary)));
		}
예제 #11
0
		public void Remove (RelaxngPattern p)
		{
			List.Remove (p);
		}
예제 #12
0
		public void Insert (int pos, RelaxngPattern p)
		{
			List.Insert (pos, p);
		}
예제 #13
0
		public void Add (RelaxngPattern p)
		{
			List.Add (p);
		}
예제 #14
0
		// When we found define, use it.
		public RelaxngRefPattern (RelaxngPattern patternRef)
		{
			this.patternRef = patternRef;
		}
예제 #15
0
 static RelaxngReader()
 {
     relaxngXmlReader  = XmlReader.Create(typeof(RelaxngReader).GetTypeInfo().Assembly.GetManifestResourceStream("relaxng.rng"));
     grammarForRelaxng =
         RelaxngPattern.Read(relaxngXmlReader);
 }
예제 #16
0
 public void Remove(RelaxngPattern p)
 {
     List.Remove(p);
 }
예제 #17
0
파일: Validator.cs 프로젝트: B-Rich/doap
		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));
			}
		}
예제 #18
0
		static RelaxngReader ()
		{
			relaxngXmlReader = new XmlTextReader (typeof (RelaxngReader).Assembly.GetManifestResourceStream ("relaxng.rng"));
			grammarForRelaxng =
				RelaxngPattern.Read (relaxngXmlReader);
		}
예제 #19
0
파일: xmltool.cs 프로젝트: nobled/mono
		static void ValidateRelaxng (RelaxngPattern p, string [] args)
		{
			p.Compile ();

			if (args.Length < 2)
				return;

			for (int i = 2; i < args.Length; i++) {
				XmlTextReader xtr = new XmlTextReader (args [i]);
				RelaxngValidatingReader vr = 
					new RelaxngValidatingReader (xtr, p);
				if (Environment.GetEnvironmentVariable ("MONO_XMLTOOL_ERROR_DETAILS") == "yes")
					vr.ReportDetails = true;
				else
					vr.InvalidNodeFound += delegate (XmlReader source, string message) {
						IXmlLineInfo li = source as IXmlLineInfo;
						Console.WriteLine ("ERROR: {0} (at {1} line {2} column {3})",
							message,
							source.BaseURI,
							li != null && li.HasLineInfo () ? li.LineNumber : 0,
							li != null && li.HasLineInfo () ? li.LinePosition : 0);
						return true;
					};

				while (!vr.EOF)
					vr.Read ();
			}
		}
예제 #20
0
        // Compile from this simplified syntax to derivatives.
        internal override RdpPattern Compile(RelaxngGrammar grammar)
        {
            ResetCompileState();

            parentGrammar = grammar;

            // First, process includes and divs. RELAX NG 4.1 - 4.15.
            ArrayList compiledDivs = new ArrayList();

            foreach (RelaxngInclude inc in includes)
            {
                compiledDivs.Add(inc.Compile(this));
            }
            compiledDivs.AddRange(divs);
            foreach (RelaxngDiv div in compiledDivs)
            {
                div.Compile(this);
            }

            // Check constraints. RELAX NG 4.16
            foreach (RelaxngStart start in starts)
            {
                start.Pattern.CheckConstraints();
            }
            foreach (RelaxngDefine define in defs)
            {
                foreach (RelaxngPattern p in define.Patterns)
                {
                    p.CheckConstraints();
                }
            }

            // Assemble combine into the same name defines/start.
            // see RELAX NG 4.17.
            AssembleCombine();

            // 4.18 : <grammar> must have at least one <start>.
            if (assembledStart == null)
            {
                throw new RelaxngException("A grammar elements must contain at least one start element.");
            }
            compiledStart = assembledStart.Compile(this);

            // Assemble all define components into top grammar and
            // return start patterns for descendant grammars.
            // see RELAX NG 4.18.
            CollectGrammars();
            if (parentGrammar != null)
            {
                return(compiledStart);
            }
            assembledStart = null; // no use anymore

            // 4.19 (a) remove non-reachable defines

            /*
             *                      compiledStart.MarkReachableDefs ();
             *                      ArrayList tmp = new ArrayList ();
             *                      foreach (DictionaryEntry entry in this.assembledDefs)
             *                              if (!reachableDefines.ContainsKey (entry.Key))
             *                                      tmp.Add (entry.Key);
             *                      foreach (string key in tmp)
             *                              assembledDefs.Remove (key);
             */
            // 4.19 (b) check illegal recursion
            CheckRecursion(compiledStart, 0);
            // here we collected element-replaced definitions
            foreach (DictionaryEntry entry in elementReplacedDefs)
            {
                assembledDefs.Add(entry.Key, entry.Value);
            }
            startPattern = compiledStart;
            // 4.20,21 reduce notAllowed and empty.
            bool b;

            do
            {
                b            = false;
                startPattern = startPattern.ReduceEmptyAndNotAllowed(ref b, new Hashtable());
            }while (b);

            Hashtable ht = new Hashtable();

            startPattern.setInternTable(ht);

            // Check Constraints: RELAX NG spec 7
            // 7.1.1-4, 7.3, 7.4
            startPattern.CheckConstraints(false, false, false, false, false, false);
            // 7.1.5
            CheckStartPatternContent(startPattern);

            // 4.19 (c) expandRef - actual replacement
            startPattern = compiledStart.ExpandRef(assembledDefs);

            // 7.2
            RdpContentType ct = startPattern.ContentType;

            // return its start pattern.
            IsCompiled = true;
            return(startPattern);
        }
예제 #21
0
		private void ResetCompileState ()
		{
			startPattern = null;
			assembledDefs.Clear ();
			assembledStart = null;
			compiledStart = null;
			elementReplacedDefs.Clear ();
			includedUris.Clear ();
			parentGrammar = null;
			refPatterns.Clear ();
			checkedDefs.Clear ();
			unresolvedPatterns.Clear ();
			ElementDefMap.Clear ();
		}
예제 #22
0
		private void AssembleCombine ()
		{
			// calculate combines.
			bool haveHeadStart = false;
			string combineStart = null;
			Hashtable haveHeadDefs = new Hashtable ();
			Hashtable combineDefs = new Hashtable ();

			// 1.calculate combine for starts.
			foreach (RelaxngStart start in starts)
				CheckCombine (ref haveHeadStart, 
					ref combineStart, start.Combine, "start");
			// 2.calculate combine for defines.
			foreach (RelaxngDefine def in defs) {
				bool haveHead = 
					haveHeadDefs.ContainsKey (def.Name) ?
					haveHead = (bool) haveHeadDefs [def.Name]
					: false;
				string combine = combineDefs [def.Name] as string;
				CheckCombine (ref haveHead, ref combine,
					def.Combine, String.Format ("define name={0}", def.Name));
				haveHeadDefs [def.Name] = haveHead;
				combineDefs [def.Name] = combine;
				continue;
			}

			// assemble starts and defines with "combine" attribute.

			// 3.assemble starts.
			if (starts.Count == 0) {
				if (ParentGrammar == null)
					throw new RelaxngException (this, "grammar must have at least one start component.");
			} else {
				assembledStart = ((RelaxngStart)starts [0]).Pattern;
				for (int i=1; i<starts.Count; i++) {
					RelaxngPattern p2 = ((RelaxngStart) starts [i]).Pattern;;
					if (combineStart == "interleave") {
						RelaxngInterleave intlv = new RelaxngInterleave ();
						intlv.Patterns.Add (assembledStart);
						intlv.Patterns.Add (p2);
						assembledStart = intlv;
					} else {
						RelaxngChoice c = new RelaxngChoice ();
						c.Patterns.Add (assembledStart);
						c.Patterns.Add (p2);
						assembledStart = c;
					}
				}
			}

			// 4.assemble defines
			foreach (RelaxngDefine def in defs) {
				string combine = combineDefs [def.Name] as string;
				RdpPattern p1 = 
					assembledDefs [def.Name] as RdpPattern;
				RdpPattern p2 = def.Compile (this);
				if (p1 != null) {
					if (combine == "interleave") {
						assembledDefs [def.Name] =
							new RdpInterleave (p1, p2);
					} else {
						assembledDefs [def.Name] =
							new RdpChoice (p1, p2);
					}
				} else {
					assembledDefs [def.Name] = p2;
				}
			}

		}
예제 #23
0
		// Compile from this simplified syntax to derivatives.
		internal override RdpPattern Compile (RelaxngGrammar grammar)
		{
			ResetCompileState ();

			parentGrammar = grammar;

			// First, process includes and divs. RELAX NG 4.1 - 4.15.
			ArrayList compiledDivs = new ArrayList ();
			foreach (RelaxngInclude inc in includes)
				compiledDivs.Add (inc.Compile (this));
			compiledDivs.AddRange (divs);
			foreach (RelaxngDiv div in compiledDivs)
				div.Compile (this);

			// Check constraints. RELAX NG 4.16
			foreach (RelaxngStart start in starts)
				start.Pattern.CheckConstraints ();
			foreach (RelaxngDefine define in defs)
				foreach (RelaxngPattern p in define.Patterns)
					p.CheckConstraints ();

			// Assemble combine into the same name defines/start.
			// see RELAX NG 4.17.
			AssembleCombine ();

			// 4.18 : <grammar> must have at least one <start>.
			if (assembledStart == null)
				throw new RelaxngException ("A grammar elements must contain at least one start element.");
			compiledStart = assembledStart.Compile (this);

			// Assemble all define components into top grammar and
			// return start patterns for descendant grammars.
			// see RELAX NG 4.18.
			CollectGrammars ();
			if (parentGrammar != null)
				return compiledStart;
			assembledStart = null; // no use anymore

			// 4.19 (a) remove non-reachable defines
/*
			compiledStart.MarkReachableDefs ();
			ArrayList tmp = new ArrayList ();
			foreach (DictionaryEntry entry in this.assembledDefs)
				if (!reachableDefines.ContainsKey (entry.Key))
					tmp.Add (entry.Key);
			foreach (string key in tmp)
				assembledDefs.Remove (key);
*/
			// 4.19 (b) check illegal recursion
			CheckRecursion (compiledStart, 0);
			// here we collected element-replaced definitions
			foreach (DictionaryEntry entry in elementReplacedDefs)
				assembledDefs.Add (entry.Key, entry.Value);
			startPattern = compiledStart;
			// 4.20,21 reduce notAllowed and empty.
			bool b;
			do {
				b = false;
				startPattern = startPattern.ReduceEmptyAndNotAllowed (ref b, new Hashtable ());
			} while (b);

			Hashtable ht = new Hashtable ();
			startPattern.setInternTable (ht);

			// Check Constraints: RELAX NG spec 7
			// 7.1.1-4, 7.3, 7.4
			startPattern.CheckConstraints (false, false, false, false, false, false);
			// 7.1.5
			CheckStartPatternContent (startPattern);

			// 4.19 (c) expandRef - actual replacement
			startPattern = compiledStart.ExpandRef (assembledDefs);

			// 7.2
			RdpContentType ct = startPattern.ContentType;

			// return its start pattern.
			IsCompiled = true;
			return startPattern;
		}
예제 #24
0
        private void AssembleCombine()
        {
            // calculate combines.
            bool      haveHeadStart = false;
            string    combineStart  = null;
            Hashtable haveHeadDefs  = new Hashtable();
            Hashtable combineDefs   = new Hashtable();

            // 1.calculate combine for starts.
            foreach (RelaxngStart start in starts)
            {
                CheckCombine(ref haveHeadStart,
                             ref combineStart, start.Combine, "start");
            }
            // 2.calculate combine for defines.
            foreach (RelaxngDefine def in defs)
            {
                bool haveHead =
                    haveHeadDefs.ContainsKey(def.Name) ?
                    haveHead = (bool)haveHeadDefs [def.Name]
                           : false;
                string combine = combineDefs [def.Name] as string;
                CheckCombine(ref haveHead, ref combine,
                             def.Combine, String.Format("define name={0}", def.Name));
                haveHeadDefs [def.Name] = haveHead;
                combineDefs [def.Name]  = combine;
                continue;
            }

            // assemble starts and defines with "combine" attribute.

            // 3.assemble starts.
            if (starts.Count == 0)
            {
                if (ParentGrammar == null)
                {
                    throw new RelaxngException(this, "grammar must have at least one start component.");
                }
            }
            else
            {
                assembledStart = ((RelaxngStart)starts [0]).Pattern;
                for (int i = 1; i < starts.Count; i++)
                {
                    RelaxngPattern p2 = ((RelaxngStart)starts [i]).Pattern;;
                    if (combineStart == "interleave")
                    {
                        RelaxngInterleave intlv = new RelaxngInterleave();
                        intlv.Patterns.Add(assembledStart);
                        intlv.Patterns.Add(p2);
                        assembledStart = intlv;
                    }
                    else
                    {
                        RelaxngChoice c = new RelaxngChoice();
                        c.Patterns.Add(assembledStart);
                        c.Patterns.Add(p2);
                        assembledStart = c;
                    }
                }
            }

            // 4.assemble defines
            foreach (RelaxngDefine def in defs)
            {
                string     combine = combineDefs [def.Name] as string;
                RdpPattern p1      =
                    assembledDefs [def.Name] as RdpPattern;
                RdpPattern p2 = def.Compile(this);
                if (p1 != null)
                {
                    if (combine == "interleave")
                    {
                        assembledDefs [def.Name] =
                            new RdpInterleave(p1, p2);
                    }
                    else
                    {
                        assembledDefs [def.Name] =
                            new RdpChoice(p1, p2);
                    }
                }
                else
                {
                    assembledDefs [def.Name] = p2;
                }
            }
        }
예제 #25
0
 // When we found define, use it.
 public RelaxngRefPattern(RelaxngPattern patternRef)
 {
     this.patternRef = patternRef;
 }
예제 #26
0
 static RelaxngReader()
 {
     relaxngXmlReader  = new XmlTextReader(typeof(RelaxngReader).Assembly.GetManifestResourceStream("relaxng.rng"));
     grammarForRelaxng =
         RelaxngPattern.Read(relaxngXmlReader);
 }
예제 #27
0
 public void Add(RelaxngPattern p)
 {
     List.Add(p);
 }
예제 #28
0
 public void Insert(int pos, RelaxngPattern p)
 {
     List.Insert(pos, p);
 }
예제 #29
0
		public NvdlRelaxngValidatorGenerator (RelaxngPattern p,
			NvdlConfig config)
		{
			// FIXME: use XmlResolver
			pattern = p;
		}