///<summary> /// Validate the LIFT file contained in the XmlTextReader. Progress reporting is /// supported. ///</summary> static private string GetSchemaValidationErrors(string path, IValidationProgress progress) { using (XmlTextReader documentReader = new XmlTextReader(path)) { progress.Status = "Checking for Schema errors..."; var resourceStream = typeof(LiftMultiText).Assembly.GetManifestResourceStream("Palaso.Lift.Validation.lift.rng"); if (resourceStream == null) { throw new Exception(); } RelaxngValidatingReader reader = new RelaxngValidatingReader( documentReader, new XmlTextReader(resourceStream)); reader.ReportDetails = true; string lastGuy = "lift"; int line = 0; int step = 0; if (progress != null) { try { if (documentReader.BaseURI != null) { string sFilePath = documentReader.BaseURI.Replace("file://", ""); if (sFilePath.StartsWith("/")) { // On Microsoft Windows, the BaseURI may be "file:///C:/foo/bar.lift" if (sFilePath.Length > 3 && Char.IsLetter(sFilePath[1]) && sFilePath[2] == ':' && sFilePath[3] == '/') { sFilePath = sFilePath.Substring(1); } } System.IO.FileInfo fi = new System.IO.FileInfo(sFilePath); // Unfortunately, XmlTextReader gives access to only line numbers, // not actual file positions while reading. A check of 8 Flex // generated LIFT files showed a range of 43.9 - 52.1 chars per // line. The biatah sample distributed with WeSay has an average // of only 23.1 chars per line. We'll compromise by guessing 33. // The alternative is to read the entire file to get the actual // line count. int maxline = (int)(fi.Length / 33); if (maxline < 8) { progress.MaxRange = 8; } else if (maxline < 100) { progress.MaxRange = maxline; } else { progress.MaxRange = 100; } step = (maxline + 99) / 100; } if (step <= 0) { step = 1; } } catch { step = 100; } } try { while (!reader.EOF) { // Debug.WriteLine(reader.v reader.Read(); lastGuy = reader.Name; if (progress != null && reader.LineNumber != line) { line = reader.LineNumber; if (line % step == 0) { progress.Step(1); } } } } catch (Exception e) { if (reader.Name == "version" && (lastGuy == "lift" || lastGuy == "")) { return(String.Format( "This file claims to be version {0} of LIFT, but this version of the program uses version {1}", reader.Value, LiftVersion)); } string m = string.Format("{0}\r\nError near: {1} {2} '{3}'", e.Message, lastGuy, reader.Name, reader.Value); return(m); } return(null); } }
///<summary> /// Validate the LIFT file contained in the XmlTextReader. Progress reporting is /// supported. ///</summary> static private string GetSchemaValidationErrors(string path,IValidationProgress progress) { using (XmlTextReader documentReader = new XmlTextReader(path)) { progress.Status = "Checking for Schema errors..."; var resourceStream = typeof (LiftMultiText).Assembly.GetManifestResourceStream("Palaso.Lift.Validation.lift.rng"); if (resourceStream == null) throw new Exception(); RelaxngValidatingReader reader = new RelaxngValidatingReader( documentReader, new XmlTextReader(resourceStream)); reader.ReportDetails = true; string lastGuy = "lift"; int line = 0; int step = 0; if (progress != null) { try { if (documentReader.BaseURI != null) { string sFilePath = documentReader.BaseURI.Replace("file://", ""); if (sFilePath.StartsWith("/")) { // On Microsoft Windows, the BaseURI may be "file:///C:/foo/bar.lift" if (sFilePath.Length > 3 && Char.IsLetter(sFilePath[1]) && sFilePath[2] == ':' && sFilePath[3] == '/') { sFilePath = sFilePath.Substring(1); } } System.IO.FileInfo fi = new System.IO.FileInfo(sFilePath); // Unfortunately, XmlTextReader gives access to only line numbers, // not actual file positions while reading. A check of 8 Flex // generated LIFT files showed a range of 43.9 - 52.1 chars per // line. The biatah sample distributed with WeSay has an average // of only 23.1 chars per line. We'll compromise by guessing 33. // The alternative is to read the entire file to get the actual // line count. int maxline = (int) (fi.Length/33); if (maxline < 8) progress.MaxRange = 8; else if (maxline < 100) progress.MaxRange = maxline; else progress.MaxRange = 100; step = (maxline + 99)/100; } if (step <= 0) step = 1; } catch { step = 100; } } try { while (!reader.EOF) { // Debug.WriteLine(reader.v reader.Read(); lastGuy = reader.Name; if (progress != null && reader.LineNumber != line) { line = reader.LineNumber; if (line%step == 0) progress.Step(1); } } } catch (Exception e) { if (reader.Name == "version" && (lastGuy == "lift" || lastGuy == "")) { return String.Format( "This file claims to be version {0} of LIFT, but this version of the program uses version {1}", reader.Value, LiftVersion); } string m = string.Format("{0}\r\nError near: {1} {2} '{3}'", e.Message, lastGuy, reader.Name, reader.Value); return m; } return null; } }