All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.hl7.fhir.utilities.i18n.XLIFFProducer Maven / Gradle / Ivy

There is a newer version: 6.4.1
Show newest version
package org.hl7.fhir.utilities.i18n;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;

import org.hl7.fhir.utilities.TextFile;
import org.hl7.fhir.utilities.Utilities;
import org.hl7.fhir.utilities.xml.XMLUtil;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

public class XLIFFProducer extends LanguageFileProducer {

  public class XLiffLanguageProducerLanguageSession extends LanguageProducerLanguageSession {

    private StringBuilder xml;
    int i = 0;

    public XLiffLanguageProducerLanguageSession(String id, String baseLang, String targetLang) {
      super(id, baseLang, targetLang);
      xml = new StringBuilder();
      ln("\r\n");
      ln("");
      ln("  ");
      ln("    ");
    }

    protected void ln(String line) {
      xml.append(line+"\r\n");  
    }

    @Override
    public void finish() throws IOException {
      ln("    ");
      ln("  ");
      ln("");
      TextFile.stringToFile(xml.toString(), Utilities.path(getRootFolder(), getFolderName(), id+".xliff"));
      filecount++;
    }

    @Override
    public void entry(TextUnit unit) {
      i++;
      ln("      ");
      if (unit.getContext1() != null) {
        ln("        ");
        ln("          "+Utilities.escapeXml(unit.getContext1())+"");
        ln("        ");
      }
      ln("        "+Utilities.escapeXml(unit.getSrcText())+"");
      ln("        "+Utilities.escapeXml(unit.getTgtText())+"");
      ln("      ");
    }

  }

  public class XLiffLanguageProducerSession extends LanguageProducerSession {


    public XLiffLanguageProducerSession(String id, String baseLang) {
      super (id, baseLang);
    }

    @Override
    public LanguageProducerLanguageSession forLang(String targetLang) {
      return new XLiffLanguageProducerLanguageSession(id, baseLang, targetLang);
    }

    @Override
    public void finish() throws IOException {
      // nothing
    }

  }

  private int filecount;

  public XLIFFProducer(String rootFolder, String folderName, boolean useLangFolder) {
    super(rootFolder, folderName, useLangFolder);
  }

  public XLIFFProducer() {
    super();
  }
  
  @Override
  public LanguageProducerSession startSession(String id, String baseLang) throws IOException {
    return new XLiffLanguageProducerSession(id, baseLang);
  }

  @Override
  public void finish() {
    // nothing
  }
  
  @Override
  public List loadSource(InputStream source) throws IOException, ParserConfigurationException, SAXException {
    List list = new ArrayList<>();
    Document dom = XMLUtil.parseToDom(TextFile.streamToBytes(source));
    Element xliff = dom.getDocumentElement();
    if (!xliff.getNodeName().equals("xliff")) {
      throw new IOException("Not an XLIFF document");
    }
    
    for (Element file : XMLUtil.getNamedChildren(xliff, "file")) {
      Element body = XMLUtil.getNamedChild(file, "body");
      for (Element transUnit : XMLUtil.getNamedChildren(body, "trans-unit")) {
        Element notes = XMLUtil.getNamedChild(transUnit, "notes");
        TranslationUnit tu = new TranslationUnit(file.getAttribute("target-language"), transUnit.getAttribute("resname"),
            notes == null ? null : XMLUtil.getNamedChildText(notes, "note"),
            XMLUtil.getNamedChildText(transUnit, "source"), XMLUtil.getNamedChildText(transUnit, "target"));
        if (!Utilities.noString(tu.getSrcText()) && !Utilities.noString(tu.getTgtText())) {
          list.add(tu);
        }
      }
    }
    return list;
  }


  private void check(String string, boolean equals) {
    // TODO Auto-generated method stub
    
  }

  @Override
  public int fileCount() {
    return filecount;
  }
  


  protected void ln(StringBuilder xml, String line) {
    xml.append(line+"\r\n");  
  }
  
  @Override
  public void produce(String id, String baseLang, String targetLang, List translations, String filename) throws IOException {
    StringBuilder xml = new StringBuilder();
      ln(xml, "\r\n");
      ln(xml, "");
      ln(xml, "  ");
      ln(xml, "    ");
      for (TranslationUnit tu : translations) {
        int i = 0;
        ln(xml, "      ");
        if (tu.getContext1() != null) {
          i++;
          ln(xml, "             ");
          ln(xml, "               "+Utilities.escapeXml(tu.getContext1())+"");
          ln(xml, "             ");
        }
        ln(xml, "        "+Utilities.escapeXml(tu.getSrcText())+"");
        ln(xml, "        "+Utilities.escapeXml(tu.getTgtText())+"");
        ln(xml, "      ");
      }
    
      ln(xml, "    ");
      ln(xml, "  ");
      ln(xml, "");
    TextFile.stringToFile(xml.toString(), getTargetFileName(targetLang, filename));
  }

  
  
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy