/*======================================================================== Copyright (C) 2006 by Geir-Tore Lindsve, Torbjørn Meistad and Yngve Raudberget, hereby refered to as "the authors". All rights reserved Permission is hereby granted, without written agreement and without license or royalty fees, to use, reproduce, prepare derivative works, distribute, and display this software and its documentation for NONCOMMERCIAL RESEARCH AND EDUCATIONAL PURPOSES, provided that (1) the above copyright notice and the following two paragraphs appear in all copies of the source code and (2) redistributions, including without limitation binaries, reproduce these notices in the supporting documentation. IN NO EVENT SHALL THE AUTHORS, OR DISTRIBUTORS OF THIS SOFTWARE BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. THE AUTHORS SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. ========================================================================*/ using System; using System.Collections; using System.Collections.Generic; using System.Text; using System.IO; using System.Xml; using System.Xml.Schema; using CLab; using CLab.Exceptions; using CLab.Data; using System.Reflection; using com.calitha.goldparser; namespace CLab.Parsers { /// /// TextParser for converting from plain text CP to XML, or XML to CP /// Parsing from XML utilizes the class. /// public class CLabTextParser { /// /// Parses XML input to plain text CP /// /// Filename of XML input file /// CP string representation of XML input file public String XMLtoCP(string filename) { String output = ""; CP cp = new CP(filename); //Print out contents of the CP object //Prints header if (!cp.Description.Equals("") | !cp.Author.Equals("") | !cp.Date.Equals("")) { if (!cp.Description.Equals("")) { output += "// Description: " + cp.Description + "\n"; } if (!cp.Author.Equals("")) { output += "// Author: " + cp.Author + "\n"; } if (!cp.Date.Equals("")) { output += "// Date: " + cp.Date + "\n"; } } //Print types output += "\ntype\n"; for (int i = 0; i < cp.Types.Count; i++) { output += " " + cp.Types[i] + "\n"; } //Print variables output += "\nvariable\n"; for (int i = 0; i < cp.Variables.Count; i++) { output += " " + cp.Variables[i] + "\n"; } //Print rules output += "\nrule\n"; for (int i = 0; i < cp.Rules.Count; i++) { output += " " + cp.Rules[i] + ";\n"; } cp = null; return output.Trim(); } /// /// Parses plain text CP to XML format /// /// String with CP input to parse /// Memorystream with XML data /// /// public MemoryStream CPtoXML(String input) { MemoryStream stream = new MemoryStream(); int commentType = 0; String description = ""; String author = ""; String date = ""; XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.IndentChars = " "; settings.NewLineOnAttributes = true; settings.CloseOutput = false; settings.Encoding = Encoding.UTF8; XmlWriter xWriter = XmlWriter.Create(stream, settings); Assembly ase = Assembly.GetExecutingAssembly(); Stream ClabGrammar = ase.GetManifestResourceStream("CLab.Auxiliary.ClabGrammar.cgt"); try { TokenParser tp = new TokenParser(ClabGrammar, xWriter); xWriter.WriteStartDocument(); xWriter.WriteStartElement("", "problem", ""); xWriter.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance"); xWriter.WriteAttributeString("xsi", "noNamespaceSchemaLocation", null, "clabschema.xsd"); String[] inputLines = input.Split('\n'); foreach (String S in inputLines) { if (S.StartsWith("//") || S.StartsWith("/*") || S.StartsWith("*/") || S.StartsWith("*")) { // Handle comments if (S.Contains("Desc:") | S.Contains("Description:")) { commentType = 1; String[] temp = S.Split(':'); description += temp[1] + " "; } else if (S.Contains("Auth:") | S.Contains("Author:")) { commentType = 2; String[] temp = S.Split(':'); author += temp[1] + " "; } else if (S.Contains("Date:")) { commentType = 3; String[] temp = S.Split(':'); date += temp[1] + " "; } else { if (!S.StartsWith("///")) { String temp = S.Substring(2).Trim(); switch (commentType) { case 1: description += S.Substring(2).Trim(); break; case 2: author += S.Substring(2).Trim(); break; case 3: date += S.Substring(2).Trim(); break; default: break; } } } } else { if (commentType != 0) { xWriter.WriteStartElement("header"); if (!description.Equals("")) { xWriter.WriteStartElement("description"); xWriter.WriteString(description.Trim()); xWriter.WriteEndElement(); } if (!author.Equals("")) { xWriter.WriteStartElement("author"); xWriter.WriteString(author.Trim()); xWriter.WriteEndElement(); } if (!date.Equals("")) { xWriter.WriteStartElement("date"); xWriter.WriteString(date.Trim()); xWriter.WriteEndElement(); } xWriter.WriteEndElement(); commentType = 0; break; } } } tp.Parse(input); xWriter.WriteEndElement(); xWriter.WriteEndDocument(); } finally { if (xWriter != null) { xWriter.Flush(); xWriter.Close(); } } return stream; } } }