예제 #1
0
 /// <summary>
 ///   Writes the <see cref="SchematronDocument"/> to the specified path.
 /// </summary>
 /// <param name="schematron">The <see cref="SchematronDocument"/> to write.</param>
 /// <param name="path">The name of the file to write.</param>
 public void WriteDocument(SchematronDocument schematron, string path)
 {
     using (StreamWriter writer = File.CreateText(path))
      {
     WriteDocument(schematron, writer);
      }
 }
예제 #2
0
        /// <summary>
        ///   Creates a new instance of the <see cref="SchematronValidationEventArgs"/>.
        /// </summary>
        /// <param name="schematron">The <see cref="SchematronDocument"/> that detected the event.</param>
        /// <param name="queryEngine">The <see cref="IQueryLanguage"/> that detected the event.</param>
        /// <param name="pattern">The active <see cref="Pattern"/>.</param>
        /// <param name="rule">The <see cref="Sepia.Schematron.Rule"/> that caused the event to be raised.</param>
        /// <param name="assertion">The <see cref="Sepia.Schematron.Assertion"/> that caused the event to be raised.</param>
        /// <param name="context">An <see cref="object"/> that provides the context for the <paramref name="rule"/> and <paramref name="assertion"/>.</param>
        /// <param name="instance">An <see cref="XPathNavigator"/> to the document node that cause the event to be raised.</param>
        public SchematronValidationEventArgs(SchematronDocument schematron, IQueryLanguage queryEngine, Pattern pattern, Rule rule, Assertion assertion, object context, XPathNavigator instance)
        {
            this.schematron = schematron;
             this.queryEngine = queryEngine;
             this.pattern = pattern;
             this.rule = rule;
             this.assertion = assertion;
             this.instance = instance.Clone();

             if (assertion == null)
             {
            message = "A schematron validation event occured.";
             }
             else
             {
            message = assertion.Message.ToString(instance, context);
             }

             List<string> diagnostics = new List<string>();
             if (assertion != null && !string.IsNullOrEmpty(assertion.Diagnostics))
             {
            foreach (string id in assertion.Diagnostics.Split(' '))
            {
               Diagnostic diagnostic = schematron.Diagnostics[id];
               diagnostics.Add(diagnostic.Message.ToString(instance, context));
            }
             }
             this.diagnostics = diagnostics.ToArray();
        }
예제 #3
0
        /// <summary>
        ///   Produces a "minimal syntax" schematron from the specified <see cref="SchematronDocument"/>.
        /// </summary>
        /// <param name="schematron">A <see cref="SchematronDocument"/>.</param>
        /// <returns>
        ///   A minmal syntax <see cref="SchematronDocument"/> representation of the <paramref name="schematron"/>.
        /// </returns>
        public SchematronDocument Compile(SchematronDocument schematron)
        {
            if (schematron == null)
            throw new ArgumentNullException("schematron");

             this.schematron = schematron;
             minimal = new SchematronDocument();

             CopyDocument();
             ResolveInclusions();
             ResolvePhases();
             ResolvePatterns();

             return minimal;
        }
예제 #4
0
 /// <summary>
 ///   Writes the <see cref="SchematronDocument"/> to the specified <see cref="StringBuilder"/>.
 /// </summary>
 /// <param name="schematron">The <see cref="SchematronDocument"/> to write.</param>
 /// <param name="s">The <see cref="StringBuilder"/> used to the write the document.</param>
 public void WriteDocument(SchematronDocument schematron, StringBuilder s)
 {
     WriteDocument(schematron, XmlWriter.Create(s, Settings));
 }
예제 #5
0
 /// <summary>
 ///   Writes the <see cref="SchematronDocument"/> to the specified <see cref="Stream"/>.
 /// </summary>
 /// <param name="schematron">The <see cref="SchematronDocument"/> to write.</param>
 /// <param name="stream">The <see cref="Stream"/> used to the write the document.</param>
 public void WriteDocument(SchematronDocument schematron, Stream stream)
 {
     WriteDocument(schematron, XmlWriter.Create(stream, Settings));
 }
예제 #6
0
        void WriteSchema(SchematronDocument schematron)
        {
            writer.WriteStartElement(schema, namespaceURI);

             if (!string.IsNullOrEmpty(schematron.ID)) writer.WriteAttributeString(id, schematron.ID);
             if (schematron.DefaultPhase != Phase.All) writer.WriteAttributeString(defaultPhase, schematron.DefaultPhase);
             if (schematron.QueryLanguage != Schematron.DefaultQueryLanguage) writer.WriteAttributeString(queryBinding, schematron.QueryLanguage);
             if (!string.IsNullOrEmpty(schematron.SchemaVersion)) writer.WriteAttributeString(schemaVersion, schematron.SchemaVersion);
             if (!string.IsNullOrEmpty(schematron.Fpi)) writer.WriteAttributeString(fpi, schematron.Fpi);
             if (!string.IsNullOrEmpty(schematron.Icon)) writer.WriteAttributeString(icon, schematron.Icon);
             if (!string.IsNullOrEmpty(schematron.See)) writer.WriteAttributeString(see, schematron.See);

             if (schematron.Title.Count > 0)
            WriteAnnotation(schematron.Title, title);
             WriteAnnotation(schematron.Annotation);
             if (schematron.HasNamespaces)
             {
            foreach (NamespaceDefinition ns in schematron.Namespaces)
               WriteNamespaceDefinition(ns);
             }

             if (schematron.HasParameters)
             {
            foreach (string key in schematron.Parameters)
            {
               writer.WriteStartElement(let, namespaceURI);
               writer.WriteAttributeString(name, key);
               writer.WriteAttributeString(value, schematron.Parameters[key]);
               writer.WriteEndElement();
            }
             }

             if (schematron.HasPhases)
             {
            foreach (Phase p in schematron.Phases)
               WritePhase(p);
             }

             foreach (Pattern p in schematron.Patterns)
            WritePattern(p);

             if (schematron.HasDiagnostics)
             {
            writer.WriteStartElement(diagnostics, namespaceURI);
            foreach (Diagnostic d in schematron.Diagnostics)
               WriteDiagnostic(d);
            writer.WriteEndElement();
             }

             writer.WriteEndElement();
        }
예제 #7
0
        /// <summary>
        ///   Writes the <see cref="SchematronDocument"/> to the specified <see cref="XmlWriter"/>.
        /// </summary>
        /// <param name="schematron">The <see cref="SchematronDocument"/> to write.</param>
        /// <param name="writer">The <see cref="XmlWriter"/> used to the write the document.</param>
        public void WriteDocument(SchematronDocument schematron, XmlWriter writer)
        {
            if (schematron == null)
            throw new ArgumentNullException("schematron");
             if (writer == null)
            throw new ArgumentNullException("writer");

             this.writer = writer;

             writer.WriteStartDocument();

             WriteSchema(schematron);

             writer.WriteEndDocument();
             writer.Flush();
        }
예제 #8
0
 /// <summary>
 ///   Writes the <see cref="SchematronDocument"/> to the specified <see cref="TextWriter"/>.
 /// </summary>
 /// <param name="schematron">The <see cref="SchematronDocument"/> to write.</param>
 /// <param name="writer">The <see cref="TextWriter"/> used to the write the document.</param>
 public void WriteDocument(SchematronDocument schematron, TextWriter writer)
 {
     WriteDocument(schematron, XmlWriter.Create(writer, Settings));
 }
예제 #9
0
        internal SchematronDocument ReadSchema(SchematronDocument schematron, bool validateWithSchematron)
        {
            // Check that the namespace is a schematron namespace.
             XmlElement e = xml.DocumentElement;
             if (e.NamespaceURI != Schematron.IsoNamespace && e.NamespaceURI != Schematron.OriginalNamespace)
            throw new Exception(String.Format("'{0}' is not a namepsace for schematron document."));
             schematronNamespaceURI = e.NamespaceURI;

             // Check that the document validates against the W3C XSD schema.
             xml.Schemas = Schematron.Default.XsdSet;
             xml.Validate(null);

             // Check that the document validates against the Schematron schema.
             if (validateWithSchematron)
             {
            SchematronValidator validator = new SchematronValidator();
            if (e.NamespaceURI == Schematron.OriginalNamespace)
            {
               validator.SchemaDocument = Schematron.Default.OriginalSchematronSchema;
               validator.ValidationPhase = "Full";
            }
            else if (e.NamespaceURI == Schematron.IsoNamespace)
            {
               validator.SchemaDocument = Schematron.Default.IsoSchematronSchema;
            }
            validator.Validate(xml);
             }

             // Read the document element and all children.
             schematron.ID = e.GetAttribute(id);
             schematron.Fpi = e.GetAttribute(fpi);
             schematron.Icon = e.GetAttribute(icon);
             schematron.See = e.GetAttribute(see);
             schematron.SchemaVersion = e.GetAttribute(schemaVersion);
             if (e.HasAttribute(defaultPhase)) schematron.DefaultPhase = e.GetAttribute(defaultPhase);
             if (e.HasAttribute(queryBinding)) schematron.QueryLanguage = e.GetAttribute(queryBinding);

             if (e.HasChildNodes)
             {
            foreach (XmlNode childNode in e.ChildNodes)
            {
               XmlElement child = childNode as XmlElement;
               if (child != null &&  child.NamespaceURI == schematronNamespaceURI)
               {
                  if (child.LocalName == ns)
                     schematron.Namespaces.Add(ReadNamespaceDefinition(child));
                  else if (child.LocalName == pattern)
                     schematron.Patterns.Add(ReadPattern(child));
                  else if (child.LocalName == phase)
                     schematron.Phases.Add(ReadPhase(child));
                  else if (child.LocalName == diagnostics)
                     schematron.Diagnostics = ReadDiagnostics(child);
                  else if (child.LocalName == p)
                     schematron.Annotation.Add(child);
                  else if (child.LocalName == title)
                     schematron.Title = ReadAnnotation(child);
                  else if (child.LocalName == p)
                     schematron.Annotation.Add(child);
                  else if (child.LocalName == let)
                     schematron.Parameters.Add(child.GetAttribute(name), child.GetAttribute(value));
                  else
                     throw new Exception(String.Format("'{0}' is an unknown schematron element.", child.Name));
               }
            }
             }

             return schematron;
        }
예제 #10
0
        /// <summary>
        ///   Reads a Schematron document and returns its object representation.
        /// </summary>
        /// <param name="uri">
        ///   The URI of the schematron document.
        /// </param>
        /// <returns>
        ///   A <see cref="SchematronDocument"/>
        /// </returns>
        /// <remarks>
        ///   The following checks are performed on the document:
        ///   <list type="bullet">
        ///   <item>It is a well-formed XML document.</item>
        ///   <item>The <see cref="XmlDocument.DocumentElement"/> namespace is a schematron namespace.</item>
        ///   <item>The document validates against the W3C XSD schema for a <see cref="SchematronDocument"/>.</item>
        ///   <item>The document validates against the Schematron schema for a <see cref="SchematronDocument"/>.</item>
        ///   </list>
        ///  <para>
        ///   <b>ReadSchematron</b> accepts both "http://purl.oclc.org/dsdl/schematron" and "http://www.ascc.net/xml/schematron"
        ///   as valid namespaces.
        /// </para>
        /// </remarks>
        public static SchematronDocument ReadSchematron(string uri)
        {
            if (log.IsInfoEnabled)
            log.Info("Reading " + uri);

            SchematronDocument doc = new SchematronDocument();
            doc.Load(uri);
            return doc;
        }
예제 #11
0
 /// <summary>
 ///   Creates a new instance of the <see cref="SchematronValidator"/> class with the specified <see cref="SchematronDocument"/>.
 /// </summary>
 /// <param name="schemaDocument">
 ///   The <see cref="SchematronDocument"/> that will be used to validate an <see cref="XmlDocument"/>.
 /// </param>
 public SchematronValidator(SchematronDocument schemaDocument)
 {
     this.SchemaDocument = schemaDocument;
 }
예제 #12
0
        SchematronDocument LoadSchematron(string name, bool schematronValidation)
        {
            if (log.IsInfoEnabled)
            log.Info("Loading " + name);

             XmlDocument xml = new XmlDocument();
             Assembly asm = Assembly.GetExecutingAssembly();
             using (Stream s = asm.GetManifestResourceStream(name))
             {
            if (s == null)
               throw new Exception(String.Format("The resource '{0}' is missing.", name));

               SchematronDocument doc = new SchematronDocument();
               doc.Load(s, schematronValidation);
               return doc;
             }
        }
예제 #13
0
 void Reset()
 {
     this.Annotation = null;
      this.compiledDocument = null;
      this.Diagnostics = null;
      this.Namespaces = null;
      this.Patterns = null;
      this.Phases = null;
      this.Title = null;
 }