Imported Upstream version 3.6.0

Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
This commit is contained in:
Jo Shields
2014-08-13 10:39:27 +01:00
commit a575963da9
50588 changed files with 8155799 additions and 0 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,104 @@
//
// Commons.Xml.Relaxng.General.cs
//
// Author:
// Atsushi Enomoto <ginga@kit.hi-ho.ne.jp>
//
// 2003 Atsushi Enomoto "No rights reserved."
//
// Copyright (c) 2004 Novell Inc.
// All rights reserved
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections;
using System.IO;
using System.Text;
using System.Xml;
using Commons.Xml.Relaxng.Derivative;
namespace Commons.Xml.Relaxng
{
internal class Util
{
public static string NormalizeWhitespace (string s)
{
if (s.Length == 0)
return s;
StringBuilder sb = null;
int noSpaceIndex = 0;
bool inSpace = false;
for (int i = 0; i < s.Length; i++) {
switch (s [i]) {
case ' ':
case '\r':
case '\t':
case '\n':
if (inSpace)
continue;
if (sb == null)
sb = new StringBuilder (s.Length);
if (noSpaceIndex < i) {
if (sb.Length > 0)
sb.Append (' ');
sb.Append (s, noSpaceIndex, i - noSpaceIndex);
}
inSpace = true;
break;
default:
if (inSpace) {
noSpaceIndex = i;
inSpace = false;
}
break;
}
}
if (sb == null)
return s;
if (!inSpace && noSpaceIndex < s.Length) {
sb.Append (' ');
sb.Append (s, noSpaceIndex, s.Length - noSpaceIndex);
}
return sb.ToString ();
}
public static bool IsWhitespace (string s)
{
for (int i = 0; i < s.Length; i++) {
switch (s [i]) {
case ' ':
case '\t':
case '\n':
case '\r':
continue;
default:
return false;
}
}
return true;
}
}
}

View File

@@ -0,0 +1,68 @@
//
// RelaxngDatatype.cs
//
// Author:
// Atsushi Enomoto <atsushi@ximian.com>
//
// Copyright (c) 2004 Novell Inc.
// All rights reserved
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Xml;
namespace Commons.Xml.Relaxng
{
public abstract class RelaxngDatatype
{
public abstract string Name { get; }
public abstract string NamespaceURI { get; }
internal virtual bool IsContextDependent {
// safe default value
get { return true; }
}
public abstract object Parse (string text, XmlReader reader);
public virtual bool Compare (object o1, object o2)
{
return o1 != null ? o1.Equals (o2) : o2 == null;
}
public virtual bool CompareString (string s1, string s2, XmlReader reader)
{
return Compare (Parse (s1, reader), Parse (s2, reader));
}
public virtual bool IsValid (string text, XmlReader reader)
{
try {
Parse (text, reader);
} catch (Exception) {
return false;
}
return true;
}
}
}

View File

@@ -0,0 +1,76 @@
//
// RelaxngDatatypeProvider.cs
//
// Author:
// Atsushi Enomoto <atsushi@ximian.com>
//
// Copyright (c) 2004 Novell Inc.
// All rights reserved
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Xml;
using System.Xml.Schema;
namespace Commons.Xml.Relaxng
{
public abstract class RelaxngDatatypeProvider
{
public abstract RelaxngDatatype GetDatatype (string name, string ns, RelaxngParamList parameters);
}
internal class RelaxngNamespaceDatatypeProvider : RelaxngDatatypeProvider
{
static RelaxngNamespaceDatatypeProvider instance;
static RelaxngDatatype stringType = RelaxngString.Instance;
static RelaxngDatatype tokenType = RelaxngToken.Instance;
static RelaxngNamespaceDatatypeProvider ()
{
instance = new RelaxngNamespaceDatatypeProvider ();
}
public static RelaxngNamespaceDatatypeProvider Instance {
get { return instance; }
}
private RelaxngNamespaceDatatypeProvider () {}
public override RelaxngDatatype GetDatatype (string name, string ns, RelaxngParamList parameters)
{
if (ns != String.Empty)
throw new RelaxngException ("Not supported data type URI");
if (parameters != null && parameters.Count > 0)
throw new RelaxngException ("Parameter is not allowed for this datatype: " + name);
switch (name) {
case "string":
return stringType;
case "token":
return tokenType;
}
return null;
}
}
}

View File

@@ -0,0 +1,151 @@
//
// RelaxngDefaultDatatypes.cs
//
// Author:
// Atsushi Enomoto <atsushi@ximian.com>
//
// Copyright (c) 2004 Novell Inc.
// All rights reserved
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Xml;
namespace Commons.Xml.Relaxng
{
public class RelaxngString : RelaxngDatatype
{
static RelaxngString instance;
static RelaxngString ()
{
instance = new RelaxngString ();
}
internal static RelaxngString Instance {
get { return instance; }
}
public override string Name { get { return "string"; } }
public override string NamespaceURI { get { return String.Empty; } }
internal override bool IsContextDependent {
get { return false; }
}
public override bool IsValid (string text, XmlReader reader)
{
return true;
}
public override object Parse (string text, XmlReader reader)
{
return text;
}
public override bool Compare (object o1, object o2)
{
return (string) o1 == (string) o2;
}
}
public class RelaxngToken : RelaxngDatatype
{
static RelaxngToken instance;
static RelaxngToken ()
{
instance = new RelaxngToken ();
}
internal static RelaxngToken Instance {
get { return instance; }
}
public override string Name { get { return "token"; } }
public override string NamespaceURI { get { return String.Empty; } }
internal override bool IsContextDependent {
get { return false; }
}
public override bool IsValid (string text, XmlReader reader)
{
return true;
}
public override object Parse (string text, XmlReader reader)
{
return Util.NormalizeWhitespace (text);
}
int SkipWhitespaces (string s, int i)
{
while (i < s.Length) {
switch (s [i]) {
case '\n': case '\r': case ' ': case '\t':
i++;
continue;
}
break;
}
return i;
}
public override bool Compare (object o1, object o2)
{
string s1 = o1 as string;
string s2 = o2 as string;
int i1 = 0;
int i2 = 0;
while (i1 < s1.Length && i2 < s2.Length) {
i1 = SkipWhitespaces (s1, i1);
i2 = SkipWhitespaces (s2, i2);
while (i1 < s1.Length && i2 < s2.Length) {
if (s1 [i1] != s2 [i2])
return false;
i1++;
i2++;
if (i1 == s1.Length || i2 == s2.Length)
break;
if (XmlChar.IsWhitespace (s1 [i1])) {
if (!XmlChar.IsWhitespace (s2 [i2]))
return false;
else
break;
}
else if (XmlChar.IsWhitespace (s2 [i2]))
return false;
}
}
i1 = SkipWhitespaces (s1, i1);
i2 = SkipWhitespaces (s2, i2);
return i1 == s1.Length && i2 == s2.Length;
}
public override bool CompareString (string s1, string s2, XmlReader reader)
{
return Compare (s1, s2);
}
}
}

View File

@@ -0,0 +1,67 @@
//
// Commons.Xml.Relaxng.General.cs
//
// Author:
// Atsushi Enomoto <ginga@kit.hi-ho.ne.jp>
//
// 2003 Atsushi Enomoto "No rights reserved."
//
// Copyright (c) 2004 Novell Inc.
// All rights reserved
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections;
using System.IO;
using System.Xml;
using Commons.Xml.Relaxng.Derivative;
namespace Commons.Xml.Relaxng
{
public class RelaxngException : Exception
{
// string debugXml;
public RelaxngException () : base () {}
public RelaxngException (string message) : base (message) {}
public RelaxngException (string message, Exception innerException)
: base (message, innerException) {}
internal RelaxngException (string message, RdpPattern invalidatedPattern)
: base (message)
{
// debugXml = RdpUtil.DebugRdpPattern (invalidatedPattern, new Hashtable ());
}
public RelaxngException (RelaxngElementBase source, string message)
: this (source, message, null)
{
}
public RelaxngException (RelaxngElementBase source, string message, Exception innerException)
: base (message + String.Format (" {0} ({1}, {2})", source.BaseUri, source.LineNumber, source.LinePosition), innerException)
{
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,77 @@
//
// RelaxngMergedProvider.cs
//
// Author:
// Atsushi Enomoto <atsushi@ximian.com>
//
// Copyright (c) 2004 Novell Inc.
// All rights reserved
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections;
using Commons.Xml.Relaxng.XmlSchema;
using XSchema = System.Xml.Schema.XmlSchema;
namespace Commons.Xml.Relaxng
{
public class RelaxngMergedProvider : RelaxngDatatypeProvider
{
static RelaxngMergedProvider defaultProvider;
static RelaxngMergedProvider ()
{
RelaxngMergedProvider p = new RelaxngMergedProvider ();
#if !PNET
p ["http://www.w3.org/2001/XMLSchema-datatypes"] = XsdDatatypeProvider.Instance;
p [XSchema.Namespace] = XsdDatatypeProvider.Instance;
#endif
p [String.Empty] = RelaxngNamespaceDatatypeProvider.Instance;
defaultProvider = p;
}
public static RelaxngMergedProvider DefaultProvider {
get { return defaultProvider; }
}
Hashtable table = new Hashtable ();
public RelaxngMergedProvider ()
{
}
public RelaxngDatatypeProvider this [string ns] {
get { return table [ns] as RelaxngDatatypeProvider; }
set { table [ns] = value; }
}
public override RelaxngDatatype GetDatatype (string name, string ns, RelaxngParamList parameters) {
// TODO: parameter support (write schema and get type)
RelaxngDatatypeProvider p = table [ns] as RelaxngDatatypeProvider;
if (p == null)
return null;
return p.GetDatatype (name, ns, parameters);
}
}
}

View File

@@ -0,0 +1,337 @@
//
// Commons.Xml.Relaxng.RelaxngNameClass.cs
//
// Author:
// Atsushi Enomoto <ginga@kit.hi-ho.ne.jp>
//
// 2003 Atsushi Enomoto "No rights reserved."
//
// Copyright (c) 2004 Novell Inc.
// All rights reserved
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections;
using System.IO;
using System.Net;
using System.Xml;
using Commons.Xml.Relaxng.Derivative;
using Commons.Xml.Relaxng.Rnc;
namespace Commons.Xml.Relaxng
{
public class RelaxngNameClassList : CollectionBase
{
public RelaxngNameClassList ()
{
}
public void Add (RelaxngNameClass p)
{
List.Add (p);
}
public RelaxngNameClass this [int i] {
get { return this.List [i] as RelaxngNameClass; }
set { this.List [i] = value; }
}
public void Insert (int pos, RelaxngNameClass p)
{
List.Insert (pos, p);
}
public void Remove (RelaxngNameClass p)
{
List.Remove (p);
}
}
public abstract class RelaxngNameClass : RelaxngElementBase
{
protected RelaxngNameClass ()
{
}
internal abstract RdpNameClass Compile (RelaxngGrammar g);
internal abstract void CheckConstraints (bool rejectAnyName, bool rejectNsName);
internal bool FindInvalidType (RdpNameClass nc, bool allowNsName)
{
RdpNameClassChoice choice = nc as RdpNameClassChoice;
if (choice != null)
return FindInvalidType (choice.LValue, allowNsName)
|| FindInvalidType (choice.RValue, allowNsName);
else if (nc is RdpAnyName)
return true;
else if (nc is RdpNsName && !allowNsName)
return true;
else
return false;
}
}
public class RelaxngAnyName : RelaxngNameClass
{
RelaxngExceptNameClass except;
public RelaxngAnyName ()
{
}
public RelaxngExceptNameClass Except {
get { return except; }
set { except = value; }
}
public override void Write (XmlWriter writer)
{
writer.WriteStartElement ("", "anyName", RelaxngGrammar.NamespaceURI);
if (except != null)
except.Write (writer);
writer.WriteEndElement ();
}
internal override void WriteRnc (RncWriter writer)
{
writer.WriteAnyName (this);
}
internal override RdpNameClass Compile (RelaxngGrammar g)
{
if (except != null) {
RdpNameClass exc = except.Compile (g);
if (FindInvalidType (exc, true))
throw new RelaxngException (except, "anyName except cannot have anyName children.");
return new RdpAnyNameExcept (exc);
} else
return RdpAnyName.Instance;
}
internal override void CheckConstraints (bool rejectAnyName, bool rejectNsName)
{
if (rejectAnyName)
throw new RelaxngException (this, "Not allowed anyName was found.");
if (except != null)
foreach (RelaxngNameClass nc in except.Names)
nc.CheckConstraints (true, rejectNsName);
}
}
public class RelaxngNsName : RelaxngNameClass
{
string ns;
RelaxngExceptNameClass except;
public RelaxngNsName ()
{
}
public string Namespace {
get { return ns; }
set { ns = value; }
}
public RelaxngExceptNameClass Except {
get { return except; }
set { except = value; }
}
public override void Write (XmlWriter writer)
{
writer.WriteStartElement ("", "nsName", RelaxngGrammar.NamespaceURI);
if (except != null)
except.Write (writer);
writer.WriteEndElement ();
}
internal override void WriteRnc (RncWriter writer)
{
writer.WriteNsName (this);
}
internal override RdpNameClass Compile (RelaxngGrammar g)
{
if (except != null) {
RdpNameClass exc = except.Compile (g);
if (FindInvalidType (exc, false))
throw new RelaxngException (except, "nsName except cannot have anyName nor nsName children.");
return new RdpNsNameExcept (ns, exc);
} else {
return new RdpNsName (ns);
}
}
internal override void CheckConstraints (bool rejectAnyName, bool rejectNsName)
{
if (rejectNsName)
throw new RelaxngException (this, "Not allowed nsName was found.");
if (except != null)
foreach (RelaxngNameClass nc in except.Names)
nc.CheckConstraints (true, true);
}
}
public class RelaxngName : RelaxngNameClass
{
string ns;
string ncname;
public RelaxngName ()
{
}
public RelaxngName (string ncname, string ns)
{
XmlConvert.VerifyNCName (ncname);
this.ncname = ncname;
this.ns = ns;
}
public string LocalName {
get { return ncname; }
set {
XmlConvert.VerifyNCName (value);
ncname = value;
}
}
public string Namespace {
get { return ns; }
set { ns = value; }
}
public override void Write (XmlWriter writer)
{
writer.WriteStartElement ("", "name", RelaxngGrammar.NamespaceURI);
writer.WriteAttributeString ("ns", ns);
// Here we just skip qname
writer.WriteString (ncname);
writer.WriteEndElement ();
}
internal override void WriteRnc (RncWriter writer)
{
writer.WriteName (this);
}
internal override RdpNameClass Compile (RelaxngGrammar g)
{
return new RdpName (ncname, ns);
}
internal override void CheckConstraints (bool rejectAnyName, bool rejectNsName)
{
// no error
}
}
public class RelaxngNameChoice : RelaxngNameClass
{
RelaxngNameClassList names = new RelaxngNameClassList ();
public RelaxngNameChoice ()
{
}
public RelaxngNameClassList Children {
get { return names; }
set { names = value; }
}
public override void Write (XmlWriter writer)
{
writer.WriteStartElement ("", "choice", RelaxngGrammar.NamespaceURI);
foreach (RelaxngNameClass nc in Children)
nc.Write (writer);
writer.WriteEndElement ();
}
internal override void WriteRnc (RncWriter writer)
{
writer.WriteNameChoice (this);
}
internal override RdpNameClass Compile (RelaxngGrammar g)
{
// Flatten names into RdpChoice. See 4.12.
if (names.Count == 0)
return null;
RdpNameClass p = ((RelaxngNameClass) names [0]).Compile (g);
if (names.Count == 1)
return p;
for (int i=1; i<names.Count; i++)
p = new RdpNameClassChoice (p, ((RelaxngNameClass) names [i]).Compile (g));
return p;
}
internal override void CheckConstraints (bool rejectAnyName, bool rejectNsName)
{
foreach (RelaxngNameClass nc in names)
nc.CheckConstraints (rejectAnyName, rejectNsName);
}
}
public class RelaxngExceptNameClass : RelaxngElementBase
{
RelaxngNameClassList names = new RelaxngNameClassList ();
public RelaxngExceptNameClass ()
{
}
public RelaxngNameClassList Names {
get { return names; }
}
public override void Write (XmlWriter writer)
{
writer.WriteStartElement ("", "except", RelaxngGrammar.NamespaceURI);
foreach (RelaxngNameClass nc in Names)
nc.Write (writer);
writer.WriteEndElement ();
}
internal override void WriteRnc (RncWriter writer)
{
writer.WriteNameExcept (this);
}
internal RdpNameClass Compile (RelaxngGrammar g)
{
// Flatten names into RdpGroup. See 4.12.
if (names.Count == 0)
return null;
RdpNameClass p = ((RelaxngNameClass) names [0]).Compile (g);
for (int i=1; i<names.Count; i++) {
p = new RdpNameClassChoice (
((RelaxngNameClass) names [i]).Compile (g),
p);
}
return p;
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,68 @@
//
// Commons.Xml.Relaxng.RelaxngPattern.cs
//
// Author:
// Atsushi Enomoto <ginga@kit.hi-ho.ne.jp>
//
// 2003 Atsushi Enomoto "No rights reserved."
//
// Copyright (c) 2004 Novell Inc.
// All rights reserved
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections;
using System.Collections.Specialized;
namespace Commons.Xml.Relaxng
{
//
// Pattern Classes
//
public enum RelaxngPatternType
{
Empty = 1,
NotAllowed = 2,
Text = 3,
Choice = 4,
Interleave = 5,
Group = 6,
OneOrMore = 7,
List = 8,
Data = 9,
DataExcept = 10, // only use in derivative
Value = 11,
Attribute = 12,
Element = 13,
After = 14, // only use in derivative
Ref = 15, // no use in derivative
Grammar = 16, // no use in derivative
ZeroOrMore = 17, // no use in derivative
Mixed = 18, // no use in derivative
Optional = 19, // no use in derivative
// Include = 20, // no use in derivative
ExternalRef = 21, // no use in derivative
ParentRef = 22, // no use in derivative
}
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,292 @@
//
// XsdDatatypeProvider.cs
//
// Author:
// Atsushi Enomoto <atsushi@ximian.com>
//
// Copyright (c) 2004 Novell Inc.
// All rights reserved
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections;
using System.Text;
using System.Xml;
using System.Xml.Schema;
using XSchema = System.Xml.Schema.XmlSchema;
namespace Commons.Xml.Relaxng.XmlSchema
{
public class XsdDatatypeProvider : RelaxngDatatypeProvider
{
static XsdDatatypeProvider instance = new XsdDatatypeProvider ();
static Hashtable table;
static XsdQNameWrapper qnameType = new XsdQNameWrapper ();
private XsdDatatypeProvider ()
{
if (table != null)
return;
table = new Hashtable ();
// TODO: fill all type names.
string [] names = new string [] {
"anySimpleType",
"string",
"normalizedString",
"token",
"language",
"NMTOKEN", "NMTOKENS",
"Name", "NCName",
"ID", "IDREF", "IDREFS",
"ENTITY", "ENTITIES", //"NOTATION",
"decimal",
"integer", "long", "int", "short", "byte",
"nonPositiveInteger", "negativeInteger",
"nonNegativeInteger", "positiveInteger",
"unsignedLong", "unsignedInt",
"unsignedShort", "unsignedByte",
"double", "float",
"base64Binary", "hexBinary",
"boolean",
"anyURI",
"duration", "dateTime", "date", "time",
// "QName",
"gYearMonth", "gMonthDay",
"gYear", "gMonth", "gDay",
};
StringBuilder sb = new StringBuilder ();
sb.Append ("<xs:schema xmlns:xs='" + XSchema.Namespace + "'>");
foreach (string name in names)
sb.Append ("<xs:element name='" + name + "' type='xs:" + name + "'/>");
sb.Append ("</xs:schema>");
XSchema schema = XSchema.Read (new XmlTextReader (sb.ToString (), XmlNodeType.Document, null), null);
schema.Compile (null);
foreach (XmlSchemaElement el in schema.Elements.Values)
table.Add (el.Name, new XsdPrimitiveType (el.Name, el.ElementType as XmlSchemaDatatype));
}
public static XsdDatatypeProvider Instance {
get { return instance; }
}
public override RelaxngDatatype GetDatatype (string name, string ns, RelaxngParamList parameters)
{
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":
break;
default:
return null;
}
if (name == "QName")
return qnameType;
return table [name] as RelaxngDatatype;
}
}
public class XsdSimpleRestrictionType : RelaxngDatatype
{
XmlSchemaSimpleType type;
XSchema schema;
public XsdSimpleRestrictionType (RelaxngDatatype primitive, RelaxngParamList parameters)
{
type = new XmlSchemaSimpleType ();
XmlSchemaSimpleTypeRestriction r =
new XmlSchemaSimpleTypeRestriction ();
type.Content = r;
string ns = primitive.NamespaceURI;
// Remap XML Schema datatypes namespace -> XML Schema namespace.
if (ns == "http://www.w3.org/2001/XMLSchema-datatypes")
ns = XSchema.Namespace;
r.BaseTypeName = new XmlQualifiedName (primitive.Name, ns);
foreach (RelaxngParam p in parameters) {
XmlSchemaFacet f = null;
string value = p.Value;
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 ();
// .NET/Mono Regex has a bug that it does not support "IsLatin-1Supplement"
// (it somehow breaks at '-').
value = value.Replace ("\\p{IsLatin-1Supplement}", "[\\x80-\\xFF]");
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 = 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);
schema.Compile (null);
}
public override string Name {
get { return type.QualifiedName.Name; }
}
public override string NamespaceURI {
get { return type.QualifiedName.Namespace; }
}
internal override bool IsContextDependent {
get { return type.Datatype != null && type.Datatype.TokenizedType == XmlTokenizedType.QName; }
}
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).
if (value != null)
value = value.Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;");
XmlValidatingReader v = new XmlValidatingReader (
new XmlTextReader (
String.Concat ("<root>", value, "</root>"),
XmlNodeType.Document,
null));
v.Schemas.Add (schema);
v.Read (); // <root>
try {
return v.ReadTypedValue ();
} finally {
v.Read (); // </root>
}
}
}
public class XsdPrimitiveType : RelaxngDatatype
{
XmlSchemaDatatype dt;
string name;
public XsdPrimitiveType (string name, XmlSchemaDatatype xstype)
{
this.name = name;
dt = xstype;
}
internal override bool IsContextDependent {
get { return dt.TokenizedType == XmlTokenizedType.QName; }
}
public override string Name {
get { return name; }
}
public override string NamespaceURI {
get { return "http://www.w3.org/2001/XMLSchema-datatypes"; }
}
public override object Parse (string text, XmlReader reader)
{
return dt.ParseValue (text,
reader != null ? reader.NameTable : null,
null);
}
}
// since QName resolution will fail, it must be implemented differently.
public class XsdQNameWrapper : RelaxngDatatype
{
public XsdQNameWrapper ()
{
}
public override string Name {
get { return "QName"; }
}
public override string NamespaceURI {
get { return "http://www.w3.org/2001/XMLSchema-datatypes"; }
}
internal override bool IsContextDependent {
get { return true; }
}
public override object Parse (string s, XmlReader reader)
{
int colonAt = s.IndexOf (':');
string localName = colonAt < 0 ? s : s.Substring (colonAt + 1);
// string localName = nameTable.Add (colonAt < 0 ? s : s.Substring (colonAt + 1));
return new XmlQualifiedName (localName, reader.LookupNamespace (
colonAt < 0 ? "" : s.Substring (0, colonAt - 1)));
}
}
}