001package org.hl7.fhir.utilities.i18n;
002
003import java.io.IOException;
004import java.io.InputStream;
005import java.util.ArrayList;
006import java.util.List;
007
008import org.hl7.fhir.utilities.Utilities;
009
010public class PoGetTextProducer extends LanguageFileProducer {
011
012  private int filecount;
013  private boolean incLangInFilename;
014  private String srcLang;
015
016  public PoGetTextProducer(String rootFolder, String folderName, boolean useLangFolder) {
017    super(rootFolder, folderName, useLangFolder);
018  }
019
020  public PoGetTextProducer() {
021    super();
022  }
023
024  public PoGetTextProducer(String srcLang) {
025    super();
026    this.srcLang = srcLang;
027  }
028
029  @Override
030  public LanguageProducerSession startSession(String id, String baseLang) throws IOException {
031    return new POGetTextProducerSession(id, baseLang);
032  }
033
034  @Override
035  public void finish() {
036    // nothing
037  }
038
039  public class POGetTextProducerSession extends LanguageProducerSession {
040
041    public POGetTextProducerSession(String id, String baseLang) {
042      super (id, baseLang);
043    }
044
045    @Override
046    public LanguageProducerLanguageSession forLang(String targetLang) {
047      return new POGetTextLanguageProducerLanguageSession(id, baseLang, targetLang);
048    }
049
050    @Override
051    public void finish() throws IOException {
052      // nothing
053    }
054  }
055
056  public class POGetTextLanguageProducerLanguageSession extends LanguageProducerLanguageSession {
057
058    POSource source = new POSource();
059
060    public POGetTextLanguageProducerLanguageSession(String id, String baseLang, String targetLang) {
061      super(id, baseLang, targetLang);
062    }
063
064    @Override
065    public void finish() throws IOException {
066      source.savePOFile(getFileName(id, baseLang, targetLang), 1, 0);
067      filecount++;
068    }
069
070
071    @Override
072    public void entry(TextUnit unit) {
073      POObject po = new POObject(unit.getId(), unit.getSrcText(), unit.getTgtText());
074      po.setComment(unit.getContext());
075      source.getPOObjects().add(po);
076    }
077
078  }
079
080
081  @Override
082  public int fileCount() {
083    return filecount;
084  }
085
086  @Override
087  public List<TranslationUnit> loadSource(InputStream source) throws IOException {
088    List<TranslationUnit> list = new ArrayList<>();
089    POSource polist = POSource.loadPOFile(source);
090    for (POObject po : polist.getPOObjects()) {
091      TranslationUnit tu = new TranslationUnit(srcLang == null ? polist.getLang() : srcLang, po.getId(), null, po.getMsgid(), po.getMsgstr().isEmpty() ? null : po.getMsgstrFirst());
092      if (tu.getId() != null) {
093        list.add(tu);
094      }
095    }
096    return list;
097  }
098
099  private String getFileName(String id, String baseLang, String targetLang) throws IOException {
100    return Utilities.path(getRootFolder(), isUseLangFolder() ? targetLang : ".",  getFolderName(), id+(incLangInFilename ? "-"+baseLang+"-"+targetLang+".po" : ""));
101  }
102
103  public boolean isIncLangInFilename() {
104    return incLangInFilename;
105  }
106
107  public void setIncLangInFilename(boolean incLangInFilename) {
108    this.incLangInFilename = incLangInFilename;
109  }
110
111  protected void ln(StringBuilder po, String line) {
112    po.append(line+"\r\n");  
113  }
114  
115  @Override
116  public void produce(String id, String baseLang, String targetLang, List<TranslationUnit> translations, String filename) throws IOException {
117    POSource source = new POSource();
118    
119    for (TranslationUnit unit : translations) {
120      POObject po = new POObject(unit.getId(), unit.getSrcText(), unit.getTgtText());
121      po.setComment(unit.getContext());
122      po.setOldMsgId(unit.getOriginal());
123      source.getPOObjects().add(po);
124    }
125    source.savePOFile(getFileName(id, baseLang, targetLang)+".po", 1, 0);      
126  }
127
128  private String stripEoln(String s) {
129    if (s == null) {
130      return "";
131    }
132    s = s.replace("\r\n\r\n", " ").replace("\n\n", " ").replace("\r\r", " ");
133    s = s.replace("\r\n", " ").replace("\n", " ").replace("\r", " ");
134//    // yes, the double escaping is intentional here - it appears necessary
135//    s = s.replace("\\r\\n\\r\\n", " ").replace("\\n\\n", " ").replace("\\r\\r", " ");
136//    s = s.replace("\\r\\n", " ").replace("\\n", " ").replace("\\r", " ");
137    return s;
138  }
139
140  
141
142}