|
| 1 | +B4J=true |
| 2 | +Group=Default Group |
| 3 | +ModulesStructureVersion=1 |
| 4 | +Type=Class |
| 5 | +Version=8.9 |
| 6 | +@EndOfDesignText@ |
| 7 | +Sub Class_Globals |
| 8 | + Private fx As JFX |
| 9 | +End Sub |
| 10 | + |
| 11 | +'Initializes the object. You can add parameters to this method if needed. |
| 12 | +Public Sub Initialize |
| 13 | + |
| 14 | +End Sub |
| 15 | + |
| 16 | + |
| 17 | +public Sub export(segments As List,sourceLang As String,targetLang As String,path As String,includeTag As Boolean,isTMXTags As Boolean) |
| 18 | + Dim tmxNode As XmlNode |
| 19 | + tmxNode=CreateNode("tmx") |
| 20 | + tmxNode.Attributes.Put("version","1.4") |
| 21 | + Dim header As XmlNode |
| 22 | + header=CreateNode("header") |
| 23 | + header.Attributes.Put("creationtool","BasicCAT") |
| 24 | + header.Attributes.Put("creationtoolversion","1.0.0") |
| 25 | + header.Attributes.put("adminlang",sourceLang) |
| 26 | + header.Attributes.put("srclang",sourceLang) |
| 27 | + header.Attributes.put("segtype","sentence") |
| 28 | + header.Attributes.put("o-tmf","BasicCAT") |
| 29 | + Dim body As XmlNode |
| 30 | + body=CreateNode("body") |
| 31 | + Dim tuList As List |
| 32 | + tuList.Initialize |
| 33 | + For Each segment As List In segments |
| 34 | + Dim tu As XmlNode |
| 35 | + tu=CreateNode("tu") |
| 36 | + Dim tuvList As List |
| 37 | + tuvList.Initialize |
| 38 | + Dim targetMap As Map |
| 39 | + targetMap=segment.Get(2) |
| 40 | + For i=0 To 1 |
| 41 | + Dim seg As String=segment.Get(i) |
| 42 | + If includeTag=False Then |
| 43 | + seg=XMLUtils.TagsRemoved(seg,False) |
| 44 | + End If |
| 45 | + If i = 1 Then |
| 46 | + Dim targetTuv As XmlNode |
| 47 | + targetTuv=CreateNode("tuv") |
| 48 | + targetTuv.Attributes.Put("xml:lang",targetLang) |
| 49 | + If targetMap.ContainsKey("creator") Then |
| 50 | + targetTuv.attributes.Put("creationid",targetMap.Get("creator")) |
| 51 | + End If |
| 52 | + If targetMap.ContainsKey("createdTime") Then |
| 53 | + Dim creationDate As String |
| 54 | + DateTime.DateFormat="yyyyMMdd" |
| 55 | + DateTime.TimeFormat="HHmmss" |
| 56 | + creationDate=DateTime.Date(targetMap.Get("createdTime"))&"T"&DateTime.Time(targetMap.Get("createdTime"))&"Z" |
| 57 | + targetTuv.attributes.Put("creationdate",creationDate) |
| 58 | + End If |
| 59 | + Dim segNode As XmlNode |
| 60 | + segNode=CreateNode("seg") |
| 61 | + setNodeText(segNode,seg,isTMXTags) |
| 62 | + targetTuv.Children.Add(segNode) |
| 63 | + tuvList.Add(targetTuv) |
| 64 | + Else if i = 0 Then |
| 65 | + Dim sourceTuv As XmlNode |
| 66 | + sourceTuv=CreateNode("tuv") |
| 67 | + sourceTuv.Attributes.Put("xml:lang",sourceLang) |
| 68 | + Dim segNode As XmlNode |
| 69 | + segNode=CreateNode("seg") |
| 70 | + setNodeText(segNode,seg,isTMXTags) |
| 71 | + sourceTuv.Children.Add(segNode) |
| 72 | + tuvList.Add(sourceTuv) |
| 73 | + End If |
| 74 | + Next |
| 75 | + If targetMap.ContainsKey("note") Then |
| 76 | + If targetMap.Get("note")<>"" Then |
| 77 | + Dim note As XmlNode |
| 78 | + note=CreateNode("note") |
| 79 | + Dim textNode As XmlNode |
| 80 | + textNode=CreateNode("text") |
| 81 | + textNode.Text=targetMap.Get("note") |
| 82 | + note.Children.Add(textNode) |
| 83 | + tu.Children.InsertAt(0,note) |
| 84 | + End If |
| 85 | + End If |
| 86 | + tu.Children.AddAll(tuvList) |
| 87 | + tuList.Add(tu) |
| 88 | + Next |
| 89 | + body.Children=tuList |
| 90 | + tmxNode.Children.Add(header) |
| 91 | + tmxNode.Children.Add(body) |
| 92 | + File.WriteString(path,"",XMLUtils.asString(tmxNode)) |
| 93 | +End Sub |
| 94 | + |
| 95 | +private Sub setNodeText(node As XmlNode,text As String,isTMXTags As Boolean) |
| 96 | + If isTMXTags=True Then |
| 97 | + Try |
| 98 | + text=XMLUtils.HandleXMLEntities(text,True) |
| 99 | + text=Regex.Replace2("`(<.*?>)`",32,text,"$1") |
| 100 | + node.innerXML=convertToTMXTags(text) |
| 101 | + Return |
| 102 | + Catch |
| 103 | + Log(LastException) |
| 104 | + End Try |
| 105 | + End If |
| 106 | + node.Children.Clear |
| 107 | + Dim textNode As XmlNode |
| 108 | + textNode.Initialize |
| 109 | + textNode.Name="text" |
| 110 | + textNode.Text=text |
| 111 | + node.Children.Add(textNode) |
| 112 | +End Sub |
| 113 | + |
| 114 | +private Sub convertToTMXTags(xml As String) As String |
| 115 | + Dim sb As StringBuilder |
| 116 | + sb.Initialize |
| 117 | + Dim matcher As Matcher |
| 118 | + matcher=Regex.Matcher("</*(.*?)(\d+) */*>",xml) |
| 119 | + Dim previousEndIndex As Int=0 |
| 120 | + Do While matcher.Find |
| 121 | + sb.Append(xml.SubString2(previousEndIndex,matcher.GetStart(0))) |
| 122 | + previousEndIndex=matcher.GetEnd(0) |
| 123 | + If matcher.Group(1).StartsWith("g") Then |
| 124 | + Dim id As Int |
| 125 | + id=matcher.Group(2) |
| 126 | + If matcher.match.Contains("/") Then |
| 127 | + sb.Append($"<ept i="${id}">"$) |
| 128 | + sb.Append(XMLUtils.EscapeXml(matcher.match)) |
| 129 | + sb.Append("</ept>") |
| 130 | + Else |
| 131 | + sb.Append($"<bpt i="${id}">"$) |
| 132 | + sb.Append(XMLUtils.EscapeXml(matcher.match)) |
| 133 | + sb.Append("</bpt>") |
| 134 | + End If |
| 135 | + Else If matcher.Group(1).StartsWith("x") Then |
| 136 | + sb.Append("<ph>") |
| 137 | + sb.Append(XMLUtils.EscapeXml(matcher.Match)) |
| 138 | + sb.Append("</ph>") |
| 139 | + Else |
| 140 | + sb.Append(matcher.Match) |
| 141 | + End If |
| 142 | + Loop |
| 143 | + If previousEndIndex<>xml.Length-1 Then |
| 144 | + sb.Append(xml.SubString2(previousEndIndex,xml.Length)) |
| 145 | + End If |
| 146 | + Return sb.ToString |
| 147 | +End Sub |
| 148 | + |
| 149 | +private Sub CreateNode(name As String) As XmlNode |
| 150 | + Dim node As XmlNode |
| 151 | + node.Initialize |
| 152 | + node.Name=name |
| 153 | + node.Attributes.Initialize |
| 154 | + node.Children.Initialize |
| 155 | + Return node |
| 156 | +End Sub |
0 commit comments