/// <summary> /// Convert all USX files with .usx extensions in the given usxDir and directories /// below it to USFX in usfxFile. (Scanning directories below it is to /// allow a pure unzip of an ETEN DBL bundle to be put into the USX directory. /// </summary> /// <param name="usxDir">Directory containing .usx files</param> /// <param name="usfxFile">path and file name of USFX file to write</param> /// <returns></returns> public bool Convert(string usxDir, string usfxFile) { try { processedUsxBooks = string.Empty; scrp = new Scriptures(); scrp.OpenUsfx(usfxFile); ScanUsxDir(usxDir); scrp.CloseUsfx(); } catch (Exception ex) { Logit.WriteError("Error converting USX files in " + usxDir + " to " + usfxFile); Logit.WriteError(ex.Message); return(false); } return(true); }
/// <summary> /// Read an imp or OSIS file and convert at least the main canonical text to USFX. /// </summary> /// <param name="infile">input imp or OSIS file name</param> /// <param name="outfile">output USFX file name</param> public void readImpOsis(string infile, string outfile) { string line; string inname = infile; string bookAbbr = String.Empty; string bcv = String.Empty; string bk; int currentChapter = 0; int currentVerse = 0; int c = 0; int v = 0; string id = String.Empty; string type = String.Empty; string osisID = String.Empty; string sID = String.Empty; string eID = String.Empty; string lemma = String.Empty; string morph = String.Empty; string added = String.Empty; string marker = String.Empty; string subType = String.Empty; string src = String.Empty; string savlm = String.Empty; string n = String.Empty; string who = String.Empty; StreamReader sr; try { sr = new StreamReader(infile, Encoding.UTF8); line = sr.ReadLine().TrimStart(); sr.Close(); if (line.StartsWith("$$$")) { inname = outfile + ".tMpXmL"; Logit.WriteLine("Converting imp file " + infile + " to imp xml file " + inname); ImpOsis2Xml(infile, inname); } else if (!line.StartsWith("<?xml version=\"1.0\"")) { Logit.WriteError("I don't know what to do with this file: " + infile); return; } xr = new XmlTextReader(inname); holyBooks.OpenUsfx(outfile); Logit.WriteLine("Converting from " + inname + " to USFX file " + outfile); while (xr.Read()) { if ((delayedVerse > 0) && (xr.Name != "milestone")) { if (!inParagraph) { StartNewParagraph("p"); } if (delayedVerse > 0) { WriteEmptyElementWithAttributes("v", "id", delayedVerse.ToString()); delayedVerse = 0; } RestartWJ(); delayedVerse = 0; } if (xr.NodeType == XmlNodeType.Element) { id = GetNamedAttribute("id"); type = GetNamedAttribute("type"); osisID = GetNamedAttribute("osisID"); sID = GetNamedAttribute("sID"); eID = GetNamedAttribute("eID"); lemma = GetNamedAttribute("lemma"); morph = GetNamedAttribute("morph"); added = GetNamedAttribute("added"); marker = GetNamedAttribute("marker"); who = GetNamedAttribute("who"); subType = GetNamedAttribute("subType"); src = GetNamedAttribute("src"); savlm = GetNamedAttribute("savlm"); n = GetNamedAttribute("n"); sfm = GetNamedAttribute("sfm"); switch (xr.Name) { case "header": SkipElement(); break; case "impxml": Logit.WriteLine("Parsing " + inname + " as imp xml."); break; case "osis": Logit.WriteLine("Parsing " + inname + " as OSIS"); break; case "book": StartBook(id); break; case "chapter": bcv = ParseOsisId(osisID, out bk, out c, out v); EndParagraph(); WriteEmptyElementWithAttributes("c", "id", c.ToString()); currentChapter = c; break; case "v": WriteEmptyElementWithAttributes("v", "id", id); break; case "p": StartNewParagraph("p", sfm); break; case "verse": if (eID.Length > 0) { SuspendWJ(); WriteEmptyElementWithAttributes("ve"); } else { bcv = ParseOsisId(osisID, out bk, out c, out v); if (c != currentChapter) { EndParagraph(); WriteEmptyElementWithAttributes("c", "id", c.ToString()); currentChapter = c; } delayedVerse = v; currentVerse = v; } break; case "transChange": holyBooks.xw.WriteStartElement("add"); break; case "div": switch (type) { case "book": bcv = ParseOsisId(osisID, out bk, out c, out v); StartBook(bk); break; case "colophon": StartNewParagraph("p", "ie"); break; } break; case "milestone": if ((type == "x-extra-p") || (type == "x-p")) { SuspendWJ(); StartNewParagraph("p"); if (marker.Length > 0) { holyBooks.xw.WriteString(marker + " "); } } break; case "w": lemma = (lemma + " " + savlm).Trim(); morph = (morph + " " + src).Trim(); wHasContent = (!xr.IsEmptyElement) && ((lemma.Length + morph.Length) > 0); if (wHasContent) { holyBooks.xw.WriteStartElement("w"); if (lemma.Length > 0) { holyBooks.xw.WriteAttributeString("s", lemma.Replace("strong:", "")); } if (morph.Length > 0) { holyBooks.xw.WriteAttributeString("m", morph.Trim()); } if (xr.IsEmptyElement) { holyBooks.xw.WriteEndElement(); } } else { // Otherwise, don't bother with the tag, because we really don't know semantically what it means. Logit.WriteLine("Warning: empty <w> element ignored at " + bcv); } break; case "title": switch (type) { case "main": StartNewParagraph("mt"); break; case "psalm": StartNewParagraph("d"); break; case "acrostic": StartNewParagraph("s"); break; case "chapter": if (!inParagraph) { StartNewParagraph("p"); } SkipElement(); break; } break; case "note": // type="study" if (type == "study") { holyBooks.xw.WriteStartElement("f"); holyBooks.xw.WriteAttributeString("caller", "+"); } else { SkipElement(); } break; case "divineName": holyBooks.xw.WriteStartElement("nd"); break; case "foreign": holyBooks.xw.WriteStartElement("tl"); if (n.Length > 0) { holyBooks.xw.WriteString(" " + n + " "); } break; case "ip": holyBooks.xw.WriteStartElement("p"); holyBooks.xw.WriteAttributeString("sfm", "ip"); break; case "q": if (marker != String.Empty) { Console.WriteLine("Unsupported marker in <q> at {0}: \"{1}\"", bcv, marker); } if (who == "Jesus") { StartWJ(); } break; } } else if (xr.NodeType == XmlNodeType.EndElement) { switch (xr.Name) { case "w": if (wHasContent) { holyBooks.xw.WriteEndElement(); } break; case "book": EndBook(); currentChapter = 0; break; case "divineName": case "transChange": case "foreign": case "note": case "ip": holyBooks.xw.WriteEndElement(); break; case "verse": // <ve /> SuspendWJ(); WriteEmptyElementWithAttributes("ve"); break; case "q": EndWJ(); break; case "title": EndParagraph(); break; case "p": EndParagraph(); break; } } else if (xr.NodeType == XmlNodeType.Text) { holyBooks.xw.WriteString(xr.Value); } else if (xr.NodeType == XmlNodeType.Whitespace) { holyBooks.xw.WriteWhitespace(xr.Value); } } holyBooks.CloseUsfx(); xr.Close(); } catch (Exception ex) { Logit.WriteError("Error at " + bcv); Logit.WriteError(ex.Message); Logit.WriteError(ex.StackTrace); } }