001package org.hl7.fhir.utilities.i18n;
002
003import java.io.File;
004import java.io.FileNotFoundException;
005import java.io.IOException;
006import java.io.InputStream;
007import java.util.ArrayList;
008import java.util.List;
009
010import lombok.extern.slf4j.Slf4j;
011import org.hl7.fhir.utilities.CommaSeparatedStringBuilder;
012import org.hl7.fhir.utilities.FileUtilities;
013import org.hl7.fhir.utilities.Utilities;
014
015@Slf4j
016public class POSource {
017
018  private String lang;
019  private List<POObject> poObjects = new ArrayList<>();
020  private List<String> prefixes = new ArrayList<>();
021
022  public String getLang() {
023    return lang;
024  }
025
026  public List<POObject> getPOObjects() {
027    return poObjects;
028  }
029
030  public List<String> getPrefixes() {
031    return prefixes;
032  }
033
034  public static POSource loadPOFile(InputStream source) throws FileNotFoundException, IOException {
035    return loadPOFile(FileUtilities.streamToLines(source));
036  }
037  
038  public static POSource loadPOFile(String dest) throws FileNotFoundException, IOException {
039    return loadPOFile(FileUtilities.fileToLines(new File(dest)));
040  }
041  
042  private static POSource loadPOFile(String[] lines) throws FileNotFoundException, IOException {
043    POSource poSource = new POSource();
044    POObject poObject = null;
045    int i = 0;
046    while (i < lines.length) {
047      String line = lines[i];
048      if (Utilities.noString(line)) {
049        // else 
050      } else if (line.startsWith("#:")) {
051        if (poObject == null || poObject.getId() != null) {
052          poObject = new POObject();
053          poSource.poObjects.add(poObject);
054        }
055        poObject.setId(line.substring(2).trim());
056      } else if (line.startsWith("# ") && line.contains(" -> ")) {
057        poSource.lang = line.substring(line.indexOf("->")+2).trim();
058      } else if (line.startsWith("# ")) {
059        if (poObject == null || poObject.getComment() != null) {
060          poObject = new POObject();
061          poSource.poObjects.add(poObject);
062        }
063        poObject.setComment(line.substring(1).trim());
064        if (poObject.getComment().contains("!!when")) {
065          poObject.setOldMsgId(poObject.getComment().substring(poObject.getComment().indexOf("!!when")));
066          poObject.setComment(poObject.getComment().substring(0, poObject.getComment().indexOf("!!when")-1));
067          poObject.setOldMsgId(poObject.getOldMsgId().substring(poObject.getOldMsgId().indexOf(": ")+2).trim());
068          poObject.setOldMsgId(poObject.getOldMsgId().substring(0, poObject.getOldMsgId().length()-1));
069        }
070      } else if (poObject == null) {
071        poSource.prefixes.add(line);
072      } else if (line.startsWith("#,")) {
073        // retired use of #| because it caused problems with the tools
074        String flags = line.substring(2).trim();
075        for (String flag : flags.split(",")) {
076          poObject.getFlags().add(flag.trim());
077        }
078      } else if (line.startsWith("#|")) {
079        // retired use of #| because it caused problems with the tools
080        String s = line.substring(2).trim();
081        if (s.startsWith("msgid ")) {
082          s = trimQuotes(s.substring(6));
083        }
084        while (i < lines.length-1 && lines[i+1].startsWith("\"")) {
085          i++;
086          s += trimQuotes(lines[i]);
087        }
088        poObject.setOldMsgId(s);
089      } else if (line.startsWith("msgid ")) {
090        String s = trimQuotes(line.substring(5).trim());
091        if (s.endsWith("("+poObject.getId()+")")) {
092          s = s.substring(0, s.length() - (poObject.getId().length()+3));
093        }
094        while (i < lines.length-1 && lines[i+1].startsWith("\"")) {
095          i++;
096          s += trimQuotes(lines[i]);
097        }
098        poObject.setMsgid(s);
099      } else if (line.startsWith("msgid_plural ")) {
100        String s = trimQuotes(line.substring(12).trim());
101        while (i < lines.length-1 && lines[i+1].startsWith("\"")) {
102          i++;
103          s += trimQuotes(lines[i]);
104        }
105        poObject.setMsgidPlural(s);
106      } else if (line.startsWith("msgstr ")) {
107        String s = trimQuotes(line.substring(6).trim());
108        while (i < lines.length-1 && lines[i+1].startsWith("\"")) {
109          i++;
110          s += trimQuotes(lines[i]);
111        }
112        poObject.getMsgstr().add(s);
113      } else if (line.startsWith("msgctxt ")) {
114        // ignore this
115      } else if (line.startsWith("msgstr[")) {
116        String s = line.substring(7);
117        int ii = s.indexOf("]");
118        int c = Integer.valueOf(s.substring(0, ii));
119        s = trimQuotes(s.substring(ii+1).trim());
120        while (i < lines.length-1 && lines[i+1].startsWith("\"")) {
121          i++;
122          s += trimQuotes(lines[i]);
123        }
124        while (s.startsWith("!!")) {
125          s = s.substring(2);
126        }
127        if (c != poObject.getMsgstr().size()) {
128          log.warn("index issue");
129        } else { // if (!Utilities.noString(s)) {
130          poObject.getMsgstr().add(s);
131        }
132      } else {
133        log.warn("unknown line: "+line);
134      }
135      i++;
136    }
137    return poSource;
138  }
139
140
141  private static String trimQuotes(String s) {
142    s = s.trim();
143    if (s.startsWith("\"")) {
144      s = s.substring(1);
145    }
146    if (s.endsWith("\"")) {
147      s = s.substring(0, s.length()-1);
148    }
149    return s.trim().replace("\\\"", "\"");
150  }
151
152
153  public int savePOFile(String poFilePath, int numberOfPlurals, int noTrans) throws IOException {
154    // source.prefixes.clear();
155    
156    StringBuilder sb = new StringBuilder();
157    for (String prefix : prefixes) {
158      sb.append(prefix);
159      sb.append("\r\n");
160    }
161    sb.append("\r\n");
162    for (POObject poObject : poObjects) {
163      // for POEdit
164      if (poObject.getOldMsgId() != null) {
165        sb.append("# "+poObject.getComment()+" (!!when last translated was: "+poObject.getOldMsgId()+")\r\n");
166      } else {
167        sb.append("# "+poObject.getComment()+"\r\n");
168      }
169      sb.append("#: "+poObject.getId()+"\r\n");
170      if (!poObject.getFlags().isEmpty()) {
171        sb.append("#, "+ CommaSeparatedStringBuilder.join(",", poObject.getFlags()) + "\r\n");
172      }
173      if (poObject.isDuplicate()) {
174        sb.append("msgctxt \""+poObject.getId()+"\"\r\n");
175      } 
176      String m = Utilities.noString(poObject.getMsgid()) ? "-- no content: do not translate #"+(++noTrans )+" --" : poObject.getMsgid();
177      sb.append("msgid \""+wrapQuotes(m)+"\"\r\n");
178      if (poObject.getMsgidPlural() != null) {
179        sb.append("msgid_plural \""+wrapQuotes(poObject.getMsgidPlural())+"\"\r\n");
180        while (poObject.getMsgstr().size() < numberOfPlurals) {
181          poObject.getMsgstr().add("");
182        }
183        for (int i = 0; i < poObject.getMsgstr().size(); i++) {
184          String s = poObject.getMsgstr().get(i);
185//          if (tfxMode && Utilities.noString(s)) {
186//            s = Utilities.noString(i == 0 ? o.msgid : o.msgidPlural) ? "-- no content: do not translate --" : "";
187//          }
188          sb.append("msgstr["+i+"] \""+wrapQuotes(s)+"\"\r\n");
189        }
190      } else {
191        if (poObject.getMsgstr().size() == 0) {
192          sb.append("msgstr \"\"\r\n");
193        } else {
194          sb.append("msgstr \""+wrapQuotes(poObject.getMsgstr().get(0))+"\"\r\n");
195        }
196      } 
197      sb.append("\r\n");
198    }
199    FileUtilities.stringToFile(sb.toString(), poFilePath);
200    return noTrans;
201  }
202
203  private static String wrapQuotes(String s) {
204    return s.replace("\"", "\\\"");
205  }
206
207}