<commit_before>package net.openl10n.flies.adapter.po;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.openl10n.flies.common.LocaleId;
import net.openl10n.flies.resources.OutputSource;
import net.openl10n.flies.rest.dto.extensions.comment.SimpleComment;
import net.openl10n.flies.rest.dto.extensions.gettext.HeaderEntry;
import net.openl10n.flies.rest.dto.extensions.gettext.PoHeader;
import net.openl10n.flies.rest.dto.extensions.gettext.PoTargetHeader;
import net.openl10n.flies.rest.dto.extensions.gettext.PotEntryHeader;
import net.openl10n.flies.rest.dto.resource.Resource;
import net.openl10n.flies.rest.dto.resource.TextFlow;
import net.openl10n.flies.rest.dto.resource.TextFlowTarget;
import net.openl10n.flies.rest.dto.resource.TranslationsResource;

import org.apache.commons.lang.StringUtils;
import org.fedorahosted.tennera.jgettext.HeaderFields;
import org.fedorahosted.tennera.jgettext.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PoWriter2
{
   private static final Logger log = LoggerFactory.getLogger(PoWriter2.class);
   private final org.fedorahosted.tennera.jgettext.PoWriter poWriter = new org.fedorahosted.tennera.jgettext.PoWriter();

   public PoWriter2()
   {
   }

   /**
    * Generates a pot file from Resource (document), using the publican
    * directory layout.
    * 
    * @param baseDir
    * @param doc
    * @throws IOException
    */
   public void writePot(File baseDir, Resource doc) throws IOException
   {
      // write the POT file to pot/$name.pot
      File potDir = new File(baseDir, "pot");
      potDir.mkdirs();
      File potFile = new File(potDir, doc.getName() + ".pot");
      OutputSource outputSource = new OutputSource(potFile);
      write(outputSource, doc, null);
   }

   /**
    * Generates a po file from a Resource and a TranslationsResource, using the
    * publican directory layout.
    * 
    * @param baseDir
    * @param doc
    * @param locale
    * @param targetDoc
    * @throws IOException
    */
   public void writePo(File baseDir, Resource doc, LocaleId locale, TranslationsResource targetDoc) throws IOException
   {
      // write the PO file to $locale/$name.po
      File localeDir = new File(baseDir, locale.toString());
      localeDir.mkdirs();
      File poFile = new File(localeDir, doc.getName() + ".po");
      OutputSource outputSource = new OutputSource(poFile);
      write(outputSource, doc, targetDoc);
   }

   /**
    * Generates a pot or po file from a Resource and/or TranslationsResource,
    * using the publican directory layout. If targetDoc is non-null, a po file
    * will be generated from Resource+TranslationsResource, otherwise a pot file
    * will be generated from the Resource only.
    * 
    * @param outputSource
    * @param document
    * @param targetDoc
    * @throws IOException
    */
   private void write(OutputSource outputSource, Resource document, TranslationsResource targetDoc) throws IOException
   {
      Writer writer = createWriter(outputSource);

      PoHeader poHeader = document.getExtensions(true).findByType(PoHeader.class);
      HeaderFields hf = new HeaderFields();<commit_msg>PoWriter2 now checks whether directory was created successfully<commit_after>package net.openl10n.flies.adapter.po;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.openl10n.flies.common.LocaleId;
import net.openl10n.flies.resources.OutputSource;
import net.openl10n.flies.rest.dto.extensions.comment.SimpleComment;
import net.openl10n.flies.rest.dto.extensions.gettext.HeaderEntry;
import net.openl10n.flies.rest.dto.extensions.gettext.PoHeader;
import net.openl10n.flies.rest.dto.extensions.gettext.PoTargetHeader;
import net.openl10n.flies.rest.dto.extensions.gettext.PotEntryHeader;
import net.openl10n.flies.rest.dto.resource.Resource;
import net.openl10n.flies.rest.dto.resource.TextFlow;
import net.openl10n.flies.rest.dto.resource.TextFlowTarget;
import net.openl10n.flies.rest.dto.resource.TranslationsResource;

import org.apache.commons.lang.StringUtils;
import org.fedorahosted.tennera.jgettext.HeaderFields;
import org.fedorahosted.tennera.jgettext.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PoWriter2
{
   private static final Logger log = LoggerFactory.getLogger(PoWriter2.class);
   private final org.fedorahosted.tennera.jgettext.PoWriter poWriter = new org.fedorahosted.tennera.jgettext.PoWriter();

   public PoWriter2()
   {
   }

   private void mkdirs(File dir) throws IOException
   {
      if (!dir.exists())
      {
         if (!dir.mkdirs())
            throw new IOException("unable to create output directory: " + dir);
      }
   }

   /**
    * Generates a pot file from Resource (document), using the publican
    * directory layout.
    * 
    * @param baseDir
    * @param doc
    * @throws IOException
    */
   public void writePot(File baseDir, Resource doc) throws IOException
   {
      // write the POT file to pot/$name.pot
      File potDir = new File(baseDir, "pot");
      mkdirs(potDir);
      File potFile = new File(potDir, doc.getName() + ".pot");
      OutputSource outputSource = new OutputSource(potFile);
      write(outputSource, doc, null);
   }

   /**
    * Generates a po file from a Resource and a TranslationsResource, using the
    * publican directory layout.
    * 
    * @param baseDir
    * @param doc
    * @param locale
    * @param targetDoc
    * @throws IOException
    */
   public void writePo(File baseDir, Resource doc, LocaleId locale, TranslationsResource targetDoc) throws IOException
   {
      // write the PO file to $locale/$name.po
      File localeDir = new File(baseDir, locale.toString());
      mkdirs(localeDir);
      File poFile = new File(localeDir, doc.getName() + ".po");
      OutputSource outputSource = new OutputSource(poFile);
      write(outputSource, doc, targetDoc);
   }

   /**
    * Generates a pot or po file from a Resource and/or TranslationsResource,
    * using the publican directory layout. If targetDoc is non-null, a po file
    * will be generated from Resource+TranslationsResource, otherwise a pot file
    * will be generated from the Resource only.
    * 
    * @param outputSource
    * @param document
    * @param targetDoc
    * @throws IOException
    */
   private void write(OutputSource outputSource, Resource document, TranslationsResource targetDoc) throws IOException
   {
      Writer writer = createWriter(outputSource);

      PoHeader poHeader = document.getExtensions(true).findByType(PoHeader.class);
      HeaderFields hf = new HeaderFields();
{'fraction_non_alphanumeric': 0.09802558398220244, 'fraction_numerical': 0.008203559510567298, 'mean_word_length': 3.779401993355482, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7594474', 'n_tokens_mistral': 2247, 'n_tokens_neox': 2166, 'n_words': 534}