using System;
using System.Text;
using System.Xml;
using System.Runtime.InteropServices;
///
/// C# interface for the s1kd-tools.
///
namespace S1kdTools {
///
/// Applicability definitions used for filtering.
///
public class Applicability {
unsafe private void *internalDefs;
[DllImport("libs1kd")]
unsafe private static extern void *s1kdNewApplicability();
[DllImport("libs1kd")]
unsafe private static extern void s1kdFreeApplicability(void *app);
[DllImport("libs1kd")]
unsafe private static extern void s1kdAssign(void *app, string id, string type, string val);
unsafe private void InitInternalDefs()
{
internalDefs = s1kdNewApplicability();
}
unsafe private void FreeInternalDefs()
{
s1kdFreeApplicability(internalDefs);
}
unsafe private void AssignInternalDef(string id, string type, string val)
{
s1kdAssign(internalDefs, id, type, val);
}
unsafe public void *InternalDefs {
get { return internalDefs; }
}
///
/// Creates a new set of applicability definitions.
///
public Applicability()
{
InitInternalDefs();
}
~Applicability()
{
FreeInternalDefs();
}
///
/// Add an applicability definition to the set.
///
public void Assign(string id, string type, string val)
{
AssignInternalDef(id, type, val);
}
}
///
/// The mode used when filtering a CSDB object.
///
public enum FilterMode
{
Default, /// The default filtering mode.
Reduce, /// Remove wholly resolved annotations.
Simplify, /// Remove resolved parts of annotations.
Prune, /// Only remove false parts of annotations.
}
///
/// BREX check options.
///
public class BrexCheckOptions
{
internal int bits = 0;
private enum Option
{
Values = 1,
Sns = 2,
StrictSns = 4,
UnstrictSns = 8,
Notations = 16,
NormalLog = 32,
VerboseLog = 64
}
private bool Get(Option opt)
{
return (bits & (int) opt) == (int) opt;
}
private void Set(Option opt, bool val)
{
if (val) {
bits |= (int) opt;
} else {
bits &= ~((int ) opt);
}
}
///
/// Check object values.
///
public bool CheckValues {
get { return Get(Option.Values); }
set { Set(Option.Values, value); }
}
///
/// Check BREX SNS rules.
///
public bool CheckSns {
get { return Get(Option.Sns); }
set { Set(Option.Sns, value); }
}
///
/// Use the strict SNS check method.
///
public bool StrictSns {
get { return Get(Option.StrictSns); }
set { Set(Option.StrictSns, value); }
}
///
/// Use the unstrict SNS check method.
///
public bool UnstrictSns {
get { return Get(Option.UnstrictSns); }
set { Set(Option.UnstrictSns, value); }
}
///
/// Check DTD NOTATIONs.
///
public bool CheckNotations {
get { return Get(Option.Notations); }
set { Set(Option.Notations, value); }
}
///
/// Output errors to console.
///
public bool NormalLog {
get { return Get(Option.NormalLog); }
set { Set(Option.NormalLog, value); }
}
///
/// Output errors and informative messages to console.
///
public bool VerboseLog {
get { return Get(Option.VerboseLog); }
set { Set(Option.VerboseLog, value); }
}
}
///
/// A CSDB object.
///
public class CsdbObject
{
private XmlDocument doc;
[DllImport("libs1kd")]
unsafe private static extern int s1kdCheckDefaultBREX(string object_xml, int object_size, int options, out string report_xml, out int report_size);
[DllImport("libs1kd")]
unsafe private static extern int s1kdCheckBREX(string object_xml, int object_size, String brex_xml, int brex_size, int options, out string report_xml, out int report_size);
[DllImport("libs1kd")]
unsafe private static extern int s1kdFilter(string object_xml, int object_size, void *app, FilterMode mode, out string result_xml, out int result_size);
[DllImport("libs1kd")]
unsafe private static extern string s1kdGetMetadata(string object_xml, int object_size, string name);
[DllImport("libs1kd")]
unsafe private static extern int s1kdSetMetadata(string object_xml, int object_size, string name, string val, out string result_xml, out int result_size);
unsafe private XmlDocument CheckAgainstBREXImpl(XmlDocument brex, BrexCheckOptions options)
{
string objectXml = doc.OuterXml;
string brexXml = brex.OuterXml;
string reportXml;
int size;
s1kdCheckBREX(objectXml, objectXml.Length, brexXml, brexXml.Length, options.bits, out reportXml, out size);
XmlDocument report = new XmlDocument();
report.LoadXml(reportXml);
return report;
}
unsafe private XmlDocument CheckAgainstDefaultBREXImpl(BrexCheckOptions options)
{
string objectXml = doc.OuterXml;
string reportXml;
int size;
s1kdCheckDefaultBREX(objectXml, objectXml.Length, options.bits, out reportXml, out size);
XmlDocument report = new XmlDocument();
report.LoadXml(reportXml);
return report;
}
unsafe private CsdbObject FilterImpl(Applicability app, FilterMode mode)
{
string objectXml = doc.OuterXml;
string resultXml;
int size;
int err = s1kdFilter(objectXml, objectXml.Length, app.InternalDefs, mode, out resultXml, out size);
if (err != 0) {
throw new Exception("Filtering failed (" + err + ")");
}
CsdbObject result = new CsdbObject();
result.XmlDocument.LoadXml(resultXml);
return result;
}
unsafe private string GetMetadataImpl(string name)
{
string objectXml = doc.OuterXml;
return s1kdGetMetadata(objectXml, objectXml.Length, name);
}
unsafe private void SetMetadataImpl(string name, string val)
{
string objectXml = doc.OuterXml;
string resultXml;
int size;
int err = s1kdSetMetadata(objectXml, objectXml.Length, name, val, out resultXml, out size);
if (err != 0) {
throw new Exception("Set metadata failed (" + err + ")");
}
doc.LoadXml(resultXml);
}
///
/// Internal XML document containing the XML tree for the CSDB object.
///
public XmlDocument XmlDocument {
get { return doc; }
}
///
/// Create a new, empty CSDB object.
///
public CsdbObject()
{
doc = new XmlDocument();
}
///
/// Create a new CSDB object from an existing XML document.
///
public CsdbObject(XmlDocument doc)
{
this.doc = doc;
}
///
/// Create a new CSDB object from an XML file.
///
public CsdbObject(string path)
{
doc = new XmlDocument();
doc.Load(path);
}
///
/// Get metadata from the CSDB object.
///
/// The name of the metadata to get
///
/// The metadata value
///
private string GetMetadata(string name)
{
return GetMetadataImpl(name);
}
///
/// Set the value of a given piece of metadata in the CSDB object.
///
/// The name of the metadata to set
/// The new value of the metadata
private void SetMetadata(string name, string val)
{
SetMetadataImpl(name, val);
}
/* Metadata exposed as properties: */
///
/// Code of the CSDB object
///
public string Code {
get { return GetMetadata("code"); }
}
///
/// Data module code
///
public string DmCode {
get { return GetMetadata("dmCode"); }
set { SetMetadata("dmCode", value); }
}
///
/// In-work issue number
///
public string InWork {
get { return GetMetadata("inWork"); }
}
///
/// Issue number of S1000D the CSDB object conforms to
///
public string Issue {
get { return GetMetadata("issue"); }
set { SetMetadata("issue", value); }
}
///
/// The issue date of the CSDB object
///
public string IssueDate {
get { return GetMetadata("issueDate"); }
set { SetMetadata("issueDate", value); }
}
///
/// Full issue number of the CSDB object
///
public string IssueInfo {
get { return GetMetadata("issueInfo"); }
}
///
/// The official issue number of the CSDB object
///
public string IssueNumber {
get { return GetMetadata("issueNumber"); }
}
///
/// The short S1000D schema name of the CSDB object
///
public string Schema {
get { return GetMetadata("schema"); }
}
///
/// Check the CSDB object against the appropriate S1000D default BREX.
///
/// BREX check options
///
/// An XML report of the results of the BREX check
///
public XmlDocument CheckAgainstDefaultBREX(BrexCheckOptions options)
{
return CheckAgainstDefaultBREXImpl(options);
}
///
/// Check the CSDB object against the appropriate S1000D default BREX.
///
///
/// An XML report of the results of the BREX check
///
public XmlDocument CheckAgainstDefaultBREX()
{
BrexCheckOptions options = new BrexCheckOptions();
return CheckAgainstDefaultBREXImpl(options);
}
///
/// Check the CSDB object against a specified BREX data module.
///
/// The BREX data module to check against
/// BREX check options
///
/// An XML report of the results of the BREX check
///
public XmlDocument CheckAgainstBREX(CsdbObject brex, BrexCheckOptions options)
{
return CheckAgainstBREXImpl(brex.XmlDocument, options);
}
///
/// Check the CSDB object against a specified BREX data module.
///
/// The BREX data module to check against
///
/// An XML report of the results of the BREX check
///
public XmlDocument CheckAgainstBREX(CsdbObject brex)
{
BrexCheckOptions options = new BrexCheckOptions();
return CheckAgainstBREXImpl(brex.XmlDocument, options);
}
///
/// Check the CSDB object against a BREX data module XML file.
///
/// The path to the BREX file
/// BREX check options
///
/// An XML report of the results of the BREX check
///
public XmlDocument CheckAgainstBREX(string path, BrexCheckOptions options)
{
CsdbObject brex = new CsdbObject(path);
return CheckAgainstBREX(brex, options);
}
///
/// Check the CSDB object against a BREX data module XML file.
///
/// The path to the BREX file
///
/// An XML report of the results of the BREX check
///
public XmlDocument CheckAgainstBREX(string path)
{
CsdbObject brex = new CsdbObject(path);
BrexCheckOptions options = new BrexCheckOptions();
return CheckAgainstBREX(brex, options);
}
///
/// Filter the object on a given set of applicability definitions.
///
/// The applicability definitions to filter on
/// The filtering mode to use
///
/// The filtered CSDB object instance
///
public CsdbObject Filter(Applicability app, FilterMode mode)
{
return FilterImpl(app, mode);
}
}
}