static BaseTerm ToTermEx(Node root)
            {
                BaseTerm [] args = new BaseTerm [3];
                #if fullyTagged
                args [0] = new AtomTerm (root.TagName.ToAtom ());
                #else
                string tagName = root.TagName.ToAtom ();
                #endif
                args [1] = ListTerm.EMPTYLIST;
                Decimal d;

                foreach (KeyValuePair<string, string> kv in root.Attributes) // XML Declaration
                {
                  BaseTerm pair;

                  if (Decimal.TryParse (kv.Value, styleAllowDecPnt, Utils.CIC, out d))
                pair =  new OperatorTerm (EqualOpDescr, new AtomTerm (kv.Key), new DecimalTerm (d));
                  else
                pair =  new OperatorTerm (EqualOpDescr, new AtomTerm (kv.Key), new StringTerm (kv.Value));

                  args [1] = new ListTerm (pair, args [1]);
                }

                args [2] = ListTerm.EMPTYLIST;

                if (root.ChildNodes.Count > 0)
                {
                  foreach (Node n in root.ChildNodes)
                  {
                BaseTerm e;
                e = null;
                switch (n.type)
                {
                  case XmlNodeType.Element:
                e = ToTermEx (n);
                break;
                  case XmlNodeType.Comment:
                e = new CompoundTerm (COMMENT, new StringTerm (n.text.Trim ().EscapeDoubleQuotes ()));
                break;
                  case XmlNodeType.Text:
                if (Decimal.TryParse (n.text, styleAllowDecPnt, Utils.CIC, out d))
                #if fullyTagged
                  e = new CompoundTerm (TEXT, new DecimalTerm (d));
                else
                  e = new CompoundTerm (TEXT, new StringTerm (n.text.Trim ().EscapeDoubleQuotes ()));
                #else
                  e = new DecimalTerm (d);
                else
                  e = new StringTerm (n.text.Trim ().EscapeDoubleQuotes ());
                #endif
                break;
                  case XmlNodeType.CDATA:
                e = new CompoundTerm (CDATA, new StringTerm (n.text.Trim ().EscapeDoubleQuotes ()));
                break;
                  case XmlNodeType.ProcessingInstruction:
                e = new CompoundTerm ("processing_instruction", new AtomTerm (n.name.ToAtom ()),
                              new StringTerm (n.text.Trim ().EscapeDoubleQuotes ()));
                break;
                  case XmlNodeType.SignificantWhitespace:
                  case XmlNodeType.Whitespace:
                break;
            static void ToStringEx(Node root, StringBuilder sb, int depth)
            {
                sb.Append (Spaces (depth) + "<" + root.TagName);

                foreach (KeyValuePair<string, string> kv in root.Attributes)
                  sb.Append (String.Format (@" {0}=""{1}""", kv.Key, kv.Value));

                sb.Append (">" + root.Text);

                if (root.ChildNodes.Count > 0)
                {
                  sb.Append (Environment.NewLine);

                  foreach (Node n in root.ChildNodes)
                  {
                switch (n.type)
                {
                  case XmlNodeType.Element:
                ToStringEx (n, sb, depth + 2);
                break;
                  case XmlNodeType.Comment:
                sb.Append ("<!--" + n.text + "-->");
                break;
                  case XmlNodeType.Text:
                sb.Append ("[" + n.text.Trim () + "]");
                break;
                  case XmlNodeType.CDATA:
                  case XmlNodeType.ProcessingInstruction:
                  case XmlNodeType.SignificantWhitespace:
                  case XmlNodeType.Whitespace:
                break;
                  default:
                break;
                }
                  }
                  sb.Append (String.Format ("{0}</{1}>{2}", Spaces (depth), root.TagName, Environment.NewLine));
                }
                else
                  sb.Append ("</" + root.TagName + ">" + Environment.NewLine);
            }
            public void ToNode(XmlTextReader reader, int level)
            {
                Node child; // essentially: in the loop, add new nodes to this.childNodes

                while (reader.Read ())
                {
                  //IO.WriteLine ("Read name={0} value={1} reader.NodeType={2} Level={3}", reader.Name, reader.Value.Trim (), reader.NodeType, level);
                  switch (reader.NodeType)
                  {
                case XmlNodeType.Element:  // create a new subelement
                  bool isEmpty = reader.IsEmptyElement;
                  child = new Node ();
                  child.type = XmlNodeType.Element;
                  child.name = reader.LocalName;
                  child.text = reader.Prefix;

                  while (reader.MoveToNextAttribute ())
                child.AddAttribute (reader.Name, reader.Value);

                  if (isEmpty)
                  {
                childNodes.Insert (0, child);

                continue;
                  }

                  child.ToNode (reader, level + 1);
                  childNodes.Insert (0, child);

                  break;
                case XmlNodeType.Attribute:
                  this.AddAttribute (reader.Name, reader.Value);
                  break;
                case XmlNodeType.EndElement:
                  return;
                case XmlNodeType.Comment:
                case XmlNodeType.Text:
                case XmlNodeType.CDATA:
                  child = new Node ();
                  child.type = reader.NodeType;
                  child.Text = reader.Value;
                  childNodes.Insert (0, child);
                  break;
                case XmlNodeType.ProcessingInstruction:
                  child = new Node ();
                  child.type = reader.NodeType;
                  child.name = reader.Name;
                  child.text = reader.Value;
                  childNodes.Insert (0, child);
                  break;
                case XmlNodeType.XmlDeclaration:
                  while (reader.MoveToNextAttribute ())
                this.AddAttribute (reader.Name, reader.Value);
                  break;
                case XmlNodeType.Document:
                case XmlNodeType.DocumentFragment:
                case XmlNodeType.DocumentType:
                case XmlNodeType.EndEntity:
                case XmlNodeType.Entity:
                case XmlNodeType.EntityReference:
                case XmlNodeType.None:
                case XmlNodeType.Notation:
                case XmlNodeType.SignificantWhitespace:
                case XmlNodeType.Whitespace:
                  // ignore
                  break;
                default:
                  throw new ApplicationException (
                string.Format ("*** Unhandled XmlTextReader.NodeType: {0}", reader.NodeType));
                  }
                }

                return;
            }
            // Conversion of an XML-structure (in a string or in a file) to a Prolog BaseTerm
            public static BaseTerm XmlToTerm(BaseTerm settings, string s, bool inFile)
            {
                XmlTextReader xrd = null;
                StreamReader sr = null;
                Encoding encoding = GetEncodingFromString ("UTF-8");
                Node result;
                string settingValue = null;

                // get settings -- currently, only 'encoding' is recognized
                if (settings != null)
                  foreach (BaseTerm setting in (ListTerm)settings) // traverse ...
                  {
                string settingName = setting.FunctorToString;

                if (setting.Arity == 1)
                  settingValue = setting.Arg (0).FunctorToString;
                else
                  IO.Error ("Illegal setting in xml_term/3: '{0}'", setting);

                switch (settingName)
                {
                  // Expected string or file encoding. Superseded by explicit encoding attribute setting found in xml
                  case "encoding":
                encoding = GetEncodingFromString (settingValue); // default is UTF-8
                break;
                  default:
                IO.Error ("Unknown setting in xml_term/3: '{0}'", setting);
                break;
                }
                  }

                try
                {
                  if (inFile)
                  {
                sr = new StreamReader (s, encoding);
                xrd = new XmlTextReader (sr);
                  }
                  else
                xrd = new XmlTextReader (new StringReader (s));

                  //xrd.ProhibitDtd = true; // new in the .NET Framework version 2.0
                  xrd.Namespaces = false;
                  result = new Node ();
                  result.TagName = "<root>";
                  result.type = XmlNodeType.Element;

                  result.ToNode (xrd, 0); // first, create an intermediate representation (a Node) containing the XML structure
                }
                catch (Exception e)
                {
                  string source = inFile ? string.Format (" file '{0}'", s) : null;

                  throw new ApplicationException (
                string.Format ("Error in XML input{0}. Message was:\r\n{1}", source, e.Message));
                }
                finally
                {
                  if (sr != null) sr.Close ();
                  if (xrd != null) xrd.Close ();
                }

                return result.ToTerm (); // Convert the Node to a Prolog BaseTerm
            }
示例#5
0
        private static Term ToTermEx(Node root)
        {
            Term[] args = new Term[3];

              if (root.Text == null || root.Text == "")
            args[0] = new Term(Utils.MakeAtom(root.TagName));
              else
            args[0] = new Term(":",
                             new Term(Utils.MakeAtomic(root.Text)),
                             new Term(Utils.MakeAtomic(root.TagName)),
                             FType.comp, OType.xfy, 600);
              args[1] = Term.NULLLIST;

              foreach (DictionaryEntry de in root.Attributes) // XML Declaration
              {
            Term pair = new Term(Parser.EQ, new Term((string)de.Key), new Term((string)de.Value, FType.text), FType.comp, OType.xfx, 700);
            args[1] = new ListTerm(pair, args[1]);
              }

              args[2] = Term.NULLLIST; // []

              if (root.ChildNodes.Count > 0)
              {
            foreach (Node n in root.ChildNodes)
            {
              Term e;
              e = null;
              switch (n.type)
              {
            case XmlNodeType.Element:
              e = ToTermEx(n);
              break;
            case XmlNodeType.Comment:
              e = new Term("comment", new Term(n.text.Trim(), FType.text), OType.noop, 0);
              break;
            case XmlNodeType.Text:
              e = new Term("text", new Term(n.text.Trim(), FType.text), OType.noop, 0);
              break;
            case XmlNodeType.CDATA:
              e = new Term("cdata", new Term(n.text.Trim(), FType.text), OType.noop, 0);
              break;
            case XmlNodeType.ProcessingInstruction:
              e = new Term("instructions", new Term(Utils.MakeAtomic(n.name)),
                                            new Term(n.text.Trim(), FType.text), FType.comp, OType.noop, 0);
              break;
            case XmlNodeType.SignificantWhitespace:
            case XmlNodeType.Whitespace:
              break;
            default:
              break;
              }
              if (e != null) args[2] = new ListTerm(e, args[2]);
            }
              }
              return new Term("element", args);
        }
示例#6
0
        /// Conversion of an XML-structure (in a string or in a file) to a Prolog Term
        public static Term XmlToTerm(string s, bool inFile)
        {
            XmlTextReader xrd;

              if (inFile)
            xrd = new XmlTextReader(s);
              else
            xrd = new XmlTextReader(new StringReader(s));

              //xrd.ProhibitDtd = true; // new in the .NET Framework version 2.0
              Node result = new Node();
              result.TagName = "<root>";
              result.type = XmlNodeType.Element;
              try
              {
            result.ToNode(xrd, 0); // first, create an intermediate representation (a Node) containing the XML structure
              }
              finally
              {
            xrd.Close();
              }
              return result.ToTerm(); // Convert the Node to a Prolog Term
        }