Author: atsushi
Date: 2005-02-27 06:51:49 -0500 (Sun, 27 Feb 2005)
New Revision: 41256
Modified:
trunk/mcs/class/Commons.Xml.Relaxng/Commons.Xml.Relaxng/ChangeLog
trunk/mcs/class/Commons.Xml.Relaxng/Commons.Xml.Relaxng/RelaxngValidatingReader.cs
trunk/mcs/class/Commons.Xml.Relaxng/Commons.Xml.Relaxng/XsdDatatypeProvider.cs
Log:
2005-02-27 Atsushi Enomoto <[EMAIL PROTECTED]>
* XsdDatatypeProvider.cs : Added simpleType parameter support.
* RelaxngValidatingReader.cs : added ReportDetails flag to report all
expected element names / attribute names. Removed all unused code.
Modified: trunk/mcs/class/Commons.Xml.Relaxng/Commons.Xml.Relaxng/ChangeLog
===================================================================
--- trunk/mcs/class/Commons.Xml.Relaxng/Commons.Xml.Relaxng/ChangeLog
2005-02-27 11:49:55 UTC (rev 41255)
+++ trunk/mcs/class/Commons.Xml.Relaxng/Commons.Xml.Relaxng/ChangeLog
2005-02-27 11:51:49 UTC (rev 41256)
@@ -1,3 +1,9 @@
+2005-02-27 Atsushi Enomoto <[EMAIL PROTECTED]>
+
+ * XsdDatatypeProvider.cs : Added simpleType parameter support.
+ * RelaxngValidatingReader.cs : added ReportDetails flag to report all
+ expected element names / attribute names. Removed all unused code.
+
2005-02-06 Atsushi Enomoto <[EMAIL PROTECTED]>
* RelaxngReader.cs : ns attribute on root element should also be
Modified:
trunk/mcs/class/Commons.Xml.Relaxng/Commons.Xml.Relaxng/RelaxngValidatingReader.cs
===================================================================
---
trunk/mcs/class/Commons.Xml.Relaxng/Commons.Xml.Relaxng/RelaxngValidatingReader.cs
2005-02-27 11:49:55 UTC (rev 41255)
+++
trunk/mcs/class/Commons.Xml.Relaxng/Commons.Xml.Relaxng/RelaxngValidatingReader.cs
2005-02-27 11:51:49 UTC (rev 41256)
@@ -32,6 +32,7 @@
//
using System;
using System.Collections;
+using System.Text;
using System.Xml;
using Commons.Xml.Relaxng.Derivative;
@@ -72,6 +73,7 @@
bool isEmptiable;
bool roughLabelCheck;
ArrayList strictCheckCache;
+ bool reportDetails;
internal string CurrentStateXml {
get { return RdpUtil.DebugRdpPattern (vState, new
Hashtable ()); }
@@ -81,76 +83,18 @@
get { return RdpUtil.DebugRdpPattern (prevState, new
Hashtable ()); }
}
+ #region Validation State support
+
+ public bool ReportDetails {
+ get { return reportDetails; }
+ set { reportDetails = value; }
+ }
+
public bool RoughLabelCheck {
get { return roughLabelCheck; }
set { roughLabelCheck = value; }
}
- #region These members will be removed soon!
- [Obsolete ("Use GetElementLabels(object) instead.")]
- public ICollection ExpectedElements {
- get {
- if (!labelsComputed)
- GetLabels (elementLabels,
attributeLabels);
- return elementLabels.Values;
- }
- }
-
- [Obsolete ("Use GetAttributeLabels(object) instead.")]
- public ICollection ExpectedAttributes {
- get {
- if (!labelsComputed)
- GetLabels (elementLabels,
attributeLabels);
- return attributeLabels.Values;
- }
- }
-
- [Obsolete ("Use GetElementLabels(object) and
GetAttributeLabels(object) instead.")]
- public void GetLabels (Hashtable elements, Hashtable attributes)
- {
- if (elements == null)
- throw new ArgumentNullException ("elements");
- if (attributes == null)
- throw new ArgumentNullException ("attributes");
- PrepareState ();
- vState.GetLabels (elements, attributes);
-
- if (roughLabelCheck)
- return;
-
- // Strict check that tries actual validation that will
- // cover rejection by notAllowed.
- if (strictCheckCache == null)
- strictCheckCache = new ArrayList ();
- else
- strictCheckCache.Clear ();
- foreach (XmlQualifiedName qname in attributes.Values)
- if (vState.AttDeriv (qname.Name,
qname.Namespace,null, this) is RdpNotAllowed)
- strictCheckCache.Add (qname);
- foreach (XmlQualifiedName qname in strictCheckCache)
- attributes.Remove (qname);
- strictCheckCache.Clear ();
- foreach (XmlQualifiedName qname in elements.Values)
- if (vState.StartTagOpenDeriv (qname.Name,
qname.Namespace) is RdpNotAllowed)
- strictCheckCache.Add (qname);
- foreach (XmlQualifiedName qname in strictCheckCache)
- elements.Remove (qname);
- }
-
- [Obsolete ("Use Emptiable(object) instead.")]
- public bool Emptiable ()
- {
- if (!labelsComputed) {
- PrepareState ();
- isEmptiable = !(vState.EndTagDeriv () is
RdpNotAllowed);
- }
- return isEmptiable;
- }
- #endregion
-
- #region Validation State support
-
-
// It is used to disclose its validation feature to public
class ValidationState
{
@@ -388,6 +332,19 @@
vState = pattern.StartPattern;
}
+ private string BuildLabels (bool elements)
+ {
+ StringBuilder sb = new StringBuilder ();
+ ValidationState s = new ValidationState (prevState);
+ ICollection col = elements ?
+ GetElementLabels (s) : GetAttributeLabels (s);
+ foreach (XmlQualifiedName qname in col) {
+ sb.Append (qname.ToString ());
+ sb.Append (' ');
+ }
+ return sb.ToString ();
+ }
+
public override bool Read ()
{
PrepareState ();
@@ -404,11 +361,14 @@
prevState = vState;
vState = vState.StartTagOpenDeriv (
reader.LocalName, reader.NamespaceURI);
- if (vState.PatternType ==
RelaxngPatternType.NotAllowed)
- throw createValidationError
(String.Format ("Invalid start tag found. LocalName = {0}, NS = {1}. ",
reader.LocalName, reader.NamespaceURI));
+ if (vState.PatternType ==
RelaxngPatternType.NotAllowed) {
+ string labels = String.Empty;
+ if (reportDetails)
+ labels = "Allowed elements are:
" + BuildLabels (true);
+ throw createValidationError
(String.Format ("Invalid start tag found. LocalName = {0}, NS = {1}. {2}",
reader.LocalName, reader.NamespaceURI, labels));
+ }
// AttsDeriv equals to for each AttDeriv
-// if (reader.AttributeCount > 0) {
string elementNS = reader.NamespaceURI;
if (reader.MoveToFirstAttribute ()) {
do {
@@ -416,10 +376,15 @@
continue;
prevState = vState;
- string attrNS =
/*reader.NamespaceURI == "" ? elementNS :*/ reader.NamespaceURI;
+ string attrNS =
reader.NamespaceURI;
vState = vState.AttDeriv
(reader.LocalName, attrNS, reader.GetAttribute (reader.Name), this);
- if (vState.PatternType ==
RelaxngPatternType.NotAllowed)
- throw
createValidationError (String.Format ("Invalid attribute found. LocalName =
{0}, NS = {1}. ", reader.LocalName, reader.NamespaceURI));
+ if (vState.PatternType ==
RelaxngPatternType.NotAllowed) {
+ string labels =
String.Empty;
+ if (reportDetails)
+ labels =
"Allowed attributes are: " + BuildLabels (false);
+
+ throw
createValidationError (String.Format ("Invalid attribute found. LocalName =
{0}, NS = {1}. {2}", reader.LocalName, reader.NamespaceURI, labels));
+ }
} while (reader.MoveToNextAttribute ());
MoveToElement ();
}
@@ -427,9 +392,14 @@
// StarTagCloseDeriv
prevState = vState;
vState = vState.StartTagCloseDeriv ();
- if (vState.PatternType ==
RelaxngPatternType.NotAllowed)
- throw createValidationError
(String.Format ("Invalid start tag closing found. LocalName = {0}, NS = {1}. ",
reader.LocalName, reader.NamespaceURI));
+ if (vState.PatternType ==
RelaxngPatternType.NotAllowed) {
+ string labels = String.Empty;
+ if (reportDetails)
+ labels = "Expected attributes
are: " + BuildLabels (false);
+ throw createValidationError
(String.Format ("Invalid start tag closing found. LocalName = {0}, NS = {1}.
{2}", reader.LocalName, reader.NamespaceURI, labels));
+ }
+
// if it is empty, then redirect to EndElement
if (reader.IsEmptyElement)
goto case XmlNodeType.EndElement;
@@ -438,16 +408,18 @@
// EndTagDeriv
prevState = vState;
vState = vState.EndTagDeriv ();
- if (vState.PatternType ==
RelaxngPatternType.NotAllowed)
- throw createValidationError
(String.Format ("Invalid end tag found. LocalName = {0}, NS = {1}. ",
reader.LocalName, reader.NamespaceURI));
+ if (vState.PatternType ==
RelaxngPatternType.NotAllowed) {
+ string labels = String.Empty;
+ if (reportDetails)
+ labels = "Expected elements
are: " + BuildLabels (true);
+ throw createValidationError
(String.Format ("Invalid end tag found. LocalName = {0}, NS = {1}. {2}",
reader.LocalName, reader.NamespaceURI, labels));
+ }
break;
case XmlNodeType.CDATA:
case XmlNodeType.Text:
case XmlNodeType.SignificantWhitespace:
// Whitespace cannot be skipped because data and
// value types are required to validate
whitespaces.
-// if (Util.IsWhitespace (Value))
-// break;
prevState = vState;
vState = vState.TextDeriv (this.Value, reader);
if (vState.PatternType ==
RelaxngPatternType.NotAllowed)
Modified:
trunk/mcs/class/Commons.Xml.Relaxng/Commons.Xml.Relaxng/XsdDatatypeProvider.cs
===================================================================
---
trunk/mcs/class/Commons.Xml.Relaxng/Commons.Xml.Relaxng/XsdDatatypeProvider.cs
2005-02-27 11:49:55 UTC (rev 41255)
+++
trunk/mcs/class/Commons.Xml.Relaxng/Commons.Xml.Relaxng/XsdDatatypeProvider.cs
2005-02-27 11:51:49 UTC (rev 41256)
@@ -95,8 +95,17 @@
public override RelaxngDatatype GetDatatype (string name,
string ns, RelaxngParamList parameters)
{
- // TODO: parameter support
+ RelaxngDatatype dt = GetPrimitiveType (name, ns);
+ if (dt == null)
+ return null;
+ else if (parameters == null || parameters.Count == 0)
+ return dt;
+ else
+ return new XsdSimpleRestrictionType (dt,
parameters);
+ }
+ private RelaxngDatatype GetPrimitiveType (string name, string
ns)
+ {
switch (ns) {
case System.Xml.Schema.XmlSchema.Namespace:
case "http://www.w3.org/2001/XMLSchema-datatypes":
@@ -111,6 +120,96 @@
}
+ public class XsdSimpleRestrictionType : RelaxngDatatype
+ {
+ XmlSchemaSimpleType type;
+ XSchema schema;
+
+ public XsdSimpleRestrictionType (RelaxngDatatype primitive,
RelaxngParamList parameters)
+ {
+ type = new XmlSchemaSimpleType ();
+ XmlSchemaSimpleTypeRestriction r =
+ new XmlSchemaSimpleTypeRestriction ();
+ type.Content = r;
+ r.BaseTypeName = new XmlQualifiedName (primitive.Name,
primitive.NamespaceURI);
+ foreach (RelaxngParam p in parameters) {
+ XmlSchemaFacet f = null;
+ switch (p.Name) {
+ case "maxExclusive":
+ f = new XmlSchemaMaxExclusiveFacet ();
+ break;
+ case "maxInclusive":
+ f = new XmlSchemaMaxInclusiveFacet ();
+ break;
+ case "minExclusive":
+ f = new XmlSchemaMinExclusiveFacet ();
+ break;
+ case "minInclusive":
+ f = new XmlSchemaMinInclusiveFacet ();
+ break;
+ case "pattern":
+ f = new XmlSchemaPatternFacet ();
+ break;
+ case "whiteSpace":
+ f = new XmlSchemaWhiteSpaceFacet ();
+ break;
+ case "length":
+ f = new XmlSchemaLengthFacet ();
+ break;
+ case "maxLength":
+ f = new XmlSchemaMaxLengthFacet ();
+ break;
+ case "minLength":
+ f = new XmlSchemaMinLengthFacet ();
+ break;
+ case "fractionDigits":
+ f = new XmlSchemaFractionDigitsFacet ();
+ break;
+ case "totalDigits":
+ f = new XmlSchemaTotalDigitsFacet ();
+ break;
+ default:
+ throw new RelaxngException
(String.Format ("XML Schema facet {0} is not recognized or not supported.",
p.Name));
+ }
+ f.Value = p.Value;
+ r.Facets.Add (f);
+ }
+
+ // Now we create XmlSchema to handle simple-type
+ // based validation (since there is no other way,
+ // because of sucky XmlSchemaSimpleType design).
+ schema = new XSchema ();
+ XmlSchemaElement el = new XmlSchemaElement ();
+ el.Name = "root";
+ el.SchemaType = type;
+ schema.Items.Add (el);
+ }
+
+ public override string Name {
+ get { return type.QualifiedName.Name; }
+ }
+
+ public override string NamespaceURI {
+ get { return type.QualifiedName.Namespace; }
+ }
+
+ public override object Parse (string value, XmlReader reader)
+ {
+ // Now we create XmlValidatingReader to handle
+ // simple-type based validation (since there is no
+ // other way, because of sucky XmlSchemaSimpleType
+ // design).
+ XmlValidatingReader v = new XmlValidatingReader (
+ new XmlTextReader (
+ String.Concat ("<root>", value,
"</root>"),
+ XmlNodeType.Document,
+ null));
+ v.Schemas.Add (schema);
+ v.Read (); // <root>
+ return v.ReadTypedValue ();
+ }
+ }
+
public class XsdPrimitiveType : RelaxngDatatype
{
XmlSchemaDatatype dt;
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches