Package fi.celia.convert.chars

Source Code of fi.celia.convert.chars.CharConvertRows

package fi.celia.convert.chars;

import java.lang.StringBuffer;

import java.util.Properties;
import java.util.logging.FileHandler;
import java.util.logging.Handler;
import java.util.logging.Logger;
import java.util.Calendar;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.ArrayList;
import java.util.List;

import java.io.*;


import java.util.List;
import java.util.ArrayList;

//import junit.framework.Test;

//import fi.nkl.ml4.ekirja.RJDate;

/**
* This class converts chars from input source dtbook into target document.
* How characters are converted from one characterset into possible into
* another characterset, has been described in a property file.
* That file contains input source characterset, write characterset,
* allowed character set(s), which are allowed from input character set
* and which characters are converted into antoher character.
*
* <p>
* A method convertchars will be exectuted. See documentation of the method..
* <p>
* @author Tuomas Kassila
*
*/
public class CharConvertRows
{

  // public static final String cnstTarkastaRivit = "TarkastaRivit.txt";
  // public static final String cnstKaikkienTarkastaRivit = "KaikkienTarkastaRivit.txt";

//   public static final String cnstVaripohjaAlkaa = "Väripohja:";

   public static String cnstDefPropertyFileName = "convertchars.properties";
   protected static boolean m_bApplication = true;
   public static final String cnstPossibleErrorExtension = ".error";
   private static Logger m_logger;
  // private boolean m_dDebug = false;
   private static final String CR = DecoderCharacterSet.getCarriageReturn();
   private boolean m_bMakeCharConverstion;
   private final boolean m_bHtmlArvotJavaMerkkijonoiksi;
  // private String m_strKorvattavat;
  // private String m_strMuutettavat;
   private String [] m_arrChangeChars;
   private String [] m_arrReplaceChars;
   private boolean m_bRegexReplace = false;
    
   private boolean bPossibleErrorRows = false;
   private List<String> listPossibleErrorRows = new ArrayList<String>();
   private GiveRowMessage m_giveErrorMessage;
   private boolean m_bPossibleErrorCharachters = false;
  
   private String m_strError = null;
  
   /**
    * Tähän sijoitetaan muutettava tieto.
    */
   private StringBuffer sb = new StringBuffer ();
   //private int indCurrentSp = 0;
   private BufferedReader br;
   private String text = null;  
   private DtBook2Genre m_dtBook2Genre = new DtBook2Genre();
   private String str_readcharacterset = null, str_writecharacterset = null,
    str_allowedcharacterset = null, str_allowedcharareas = null,
      str_excludecharareas = null;
   private String [] arrAllowedcharareas = null,
              arrExcludecharareas = null;

   private List<CharArea> listExcludeAreas ;
   private CharArea [] arrExcludeAreas ;

   private List<CharArea> listAllowedcharareas ;
   private CharArea [] arrAllowedChararea ;
  
   /**
    * Merkistäjen nimiä, joiden mahd. alaviivat korvataan - merkeillä
    * mikäli käytetään enum:n sisäisiä vastaavia nimiä:
    * enum { UTF_8 ...} josta tulee merkkijono UTF_8 ja, joka korvataan
    * koodissa UTF-8 :ksi. Ks kuten DecoderCharacterSet-luokassa.
    */
  public static final String UTF_8 = "UTF-8";
  public static final String UTF_16 = "UTF-16";
  public static final String UTF_16BE = "UTF-16BE";
  public static final String UTF_16LE = "UTF-16LE";
  public static final String ISO_8859_1 = "ISO-8859-1";
  public static final String US_ASCII = "US-ASCII";
 
  private String defLanguage_read = UTF_8.toString().replace('_', '-');
  private String defLanguage_write = UTF_8.toString().replace('_', '-');
  //private boolean m_bRemoveLFBetweenKKoodi = true;
  // private DecoderCharacterSet decoder;

  private  byte [] arrByteChangedChars;
 
 
  public String getDefLanguage_read() { return defLanguage_read; }
  public String getDefLanguage_write() { return defLanguage_write; }
  public void setDefLanguage_read(String readLanguage) { defLanguage_read = readLanguage; }
  public void setDefLanguage_write(String writeLanguage) { defLanguage_write = writeLanguage; }

  public CharConvertRows(boolean bTeeMerkkimuunnos,
              boolean bHtmlArvotJavaMerkkijonoiksi,
              GiveRowMessage p_ilmoitarivista)
      {
    m_bMakeCharConverstion = bTeeMerkkimuunnos;
    m_bHtmlArvotJavaMerkkijonoiksi = bHtmlArvotJavaMerkkijonoiksi;
    m_giveErrorMessage = p_ilmoitarivista;
      //init();
      }

  /**
   * Onko käynnissä oleva luokka instanssi sovellus (main) vai ei,
   * kutsutaanko toisen luokan sisältä, esim pipeline.
   * 
   * @return
   */
  public void setApplication(boolean value) { m_bApplication = value; }
  public boolean getApplication() { return m_bApplication; }
  public void setRegexReplace(boolean value) { m_bRegexReplace = value; }
  public boolean getRegexReplace() { return m_bRegexReplace; }
  public boolean getMahdVirheriveja() { return bPossibleErrorRows; }
  public List<String> getListMahdVirheriveja() { return this.listPossibleErrorRows; }
  public GiveRowMessage getIlmoitaRivista() { return m_giveErrorMessage; }
 
  /**
   * Ohjelman käyttä, kutsutaan kun väärät komentoparametrit
   */
   private static void usage()
   {
       System.err.println("Convert chars 1.0");
       System.err.println("Usage:" );
       System.err.println("" + CharConvertRows.class +" intputFile outputfile [convertcfgfile=convertcharscfg_file]" );
       System.err.println(" - output into file, or:" );
       System.err.println("" + CharConvertRows.class +" intputFile outputfile [read_charset]" );
       System.err.println(" - output into file, or:" );
       System.err.println("" + CharConvertRows.class +" intputFile " );
       System.err.println(" - output into screen" );      
       System.err.println("" + CharConvertRows.class +" ... convertcfgfile=convertcharscfg_file" );
       System.err.println(" - muutos merkkitiedosto (muunnetaan merkkejä toiseksi / ajo" );
       System.err.println(" - default used charachter set is UTF-8" );
       System.err.println(" - Allowed charachter sets are:" );
       System.err.println("   " + UTF_8 +", " + UTF_16+", " +
                     UTF_16BE+", " + UTF_16LE+", " +
                     ISO_8859_1+" ja " + US_ASCII);      
   }  

  public static void
  setLogger(Logger p_logger)
  {
    m_logger = p_logger;
  }   

   /**
    * Ohjelma alkaa tästä:
    * @param args Komentoparametrit
    */
   public static void main(String [] args )
   {
   
    try {
    final String loggerProperties = "muokkaaml4rivejalogger.properties";
    final String cnstLoggerFile = "java.util.logging.config.file";
    if (System.getProperty(cnstLoggerFile) == null)
      System.setProperty(cnstLoggerFile, loggerProperties);

    Handler fh = new FileHandler("CharConvertRows.log");
    Logger.getAnonymousLogger().addHandler(fh);
    m_logger = Logger.getAnonymousLogger();
    m_logger.addHandler(fh);
    //logger.setLevel(Level.FINEST);

    String msg = "The application executing starts...";

        if (args.length < 1)
        {
        usage();
          System.exit(1);
        }
        else
        {
             if (args.length > 5)
            {
             usage();
             System.exit(2);
            }
            
          GiveRowMessage ilmoitarivista = null; new GiveRowMessageImpl(m_logger, ".");

             boolean bLuePropertiesTiedostosta = true;
        boolean bHtmlArvotJavaMerkkijonoiksi = true;
          CharConvertRows removeExtraEOL = new CharConvertRows(
              bLuePropertiesTiedostosta,
              bHtmlArvotJavaMerkkijonoiksi,
              ilmoitarivista);
          removeExtraEOL.setDefLanguage_read(CharConvertRows.UTF_8);
          removeExtraEOL.setDefLanguage_write(CharConvertRows.UTF_8);
         
          try {
            ilmoitarivista = new GiveRowMessageImpl(m_logger, ".");
           
            if (args.length == 1)
              removeExtraEOL.convertchars(args[0], false, null, null,
                  null, null, false, false);
            else
               if (args.length == 2)
               {
                
                 String args2 = args[1];
                 if (args2 != null && args2.contains("convertcfgfile="))
                 {
                  
                  String convertcharsPropertyFileName = null;
             if (args2.contains("convertcfgfile")
                  && args2.contains("="))
              {
                int ind = args2.indexOf('=');
                if (ind > -1 && ind < args2.length())
                  convertcharsPropertyFileName = args2.substring(ind+1);
              }
              if (convertcharsPropertyFileName != null)
                removeExtraEOL.convertchars(args[0], false, null, null, null,
                        convertcharsPropertyFileName, false , false);
              else
              {     
                System.err.println("convertcfgfile-param error!: " + args[4]);
                  usage();
                  System.exit(3);
              }
                 }
              removeExtraEOL.convertchars(args[0], false, args[1], null,
                  null, null, false, false);
               }
            else
               if (args.length == 3)
                removeExtraEOL.convertchars(args[0], false, args[1], args[2],
                    null, null, false , false);
            else
               if (args.length == 4)
                removeExtraEOL.convertchars(args[0], false, args[1], args[2],
                    args[3], null, false, false);
            else
               if (args.length == 5)
               {
                 String convertcharsPropertyFileName = null;
            if (args[4].contains("convertcfgfile")
                && args[4].contains("="))
            {
              int ind = args[4].indexOf('=');
              if (ind > -1 && ind < args[4].length())
                convertcharsPropertyFileName = args[4].substring(ind+1);
            }
            if (convertcharsPropertyFileName != null)
              removeExtraEOL.convertchars(args[0], false, args[1], args[2], args[3],
                      convertcharsPropertyFileName, false, false);
            else
            {     
              System.err.println("convertcfgfile-param error!: " + args[4]);
                usage();
                System.exit(3);
            }
               }               

            info("Application ready.");
          }catch(Exception e){
            severe("Virhe: " +e.getMessage() +CR);
            try {
               ilmoitarivista.close();
            Thread.sleep(200);
            } catch(Exception e2){
              e2.printStackTrace();
            }
            e.printStackTrace();
            usage();
            System.exit(3);
          }
        }
    } catch (IOException ioe){
      ioe.printStackTrace();
    }
   }
  
   private static void
   severe(String msg)
   {
       System.err.println(msg);
       if (m_logger != null)
         m_logger.severe(msg);
   }

   private static void
   info(String msg)
   {
       System.out.println(msg);
       if (m_logger != null)
         m_logger.info(msg);
   }

   /**
    * Tämä methodi hakee propertytiedostosta korvaa html-merkki(jono)
    * toisella vastaavasti koodatulla merki(jono)lla. Key-value arvoparit
    * muunntetaan korvausalgoritmin tuntemalla muunna-korvaa merkkijonolla.
    * Esim: &#8201; = - => hexa arvosta siis tulee - merkki jne.
    * <p>
    * @param strMerkkimuunnosCfgFileName
    * @throws FileNotFoundException
    * @throws IOException
    */
   private void
   openConvertCharsCfg(String strMerkkimuunnosCfgFileName)
   throws  FileNotFoundException, IOException, CharConvertRowsException
   {
      if (!m_bMakeCharConverstion)
        return; // ei muunneta
       
      File fprop = new File (strMerkkimuunnosCfgFileName);
      FileInputStream fis = new FileInputStream(fprop);
       Properties prop = new Properties ();
       prop.load(fis);
       fis.close();
      
       Enumeration<Object> keys = prop.keys();
       String key, value;
       boolean bEkaKierros = true;
       HashMap<String, String> htValues = new HashMap<String, String>();
       char chKey;
       int iKey;
       String htKey, htValue;
      
       str_readcharacterset = null;
       str_writecharacterset = null;
    str_allowedcharacterset = null;
    str_allowedcharareas = null;
    str_excludecharareas = null;

       for(; keys.hasMoreElements();)        
       {
         key = (String)keys.nextElement();
         if (key == null)
           continue;
         htKey = key;
         value = (String)prop.getProperty(key);
         if (value == null)
           value = "";
        
         if (key.equals("readcharacterset") || key.equals("writecharacterset")
            || key.equals("allowedcharacterset")
            || key.equals("checkedcharareas")
           || key.equals("allowedcharareas"))
         {
           if (key.equals("readcharacterset") )
           {
             str_readcharacterset = value;
           }
           else
           if (key.equals("writecharacterset") )
           {
             str_writecharacterset = value;
           }
           else
           if (key.equals("allowedcharacterset") )
           {
             str_allowedcharacterset = value;
           }          
           else
           if (key.equals("allowedcharareas") )
           {
             str_allowedcharareas = value;
             arrAllowedcharareas = str_allowedcharareas.split(",");
             int eaMax = arrAllowedcharareas.length;
             String [] arrAreaValues;
             CharArea ca;
             int iAreaStart, iAreaEnd;
            
             listAllowedcharareas = new ArrayList<CharArea>();
             for(int i = 0; i < eaMax; i++)
             {
               iAreaStart = -1;
               iAreaEnd = -1;
               arrAreaValues = arrAllowedcharareas[i].split("-");
               if (arrAreaValues == null)
                 continue;
              
               if (arrAreaValues.length == 0)
                 continue;

               if (arrAreaValues[0].contains("&#"))
                 iAreaStart = Integer.valueOf(getHexAndDecimalValue2CharSring(arrAreaValues[0]).trim().charAt(0));
               else
                 iAreaStart = Integer.valueOf(arrAreaValues[0].trim());
               if (arrAreaValues.length == 1)
                 iAreaEnd = iAreaStart;
               else
               {
                 if (arrAreaValues[0].contains("&#"))
                   iAreaEnd = Integer.valueOf(getHexAndDecimalValue2CharSring(arrAreaValues[1]).trim().charAt(0));
                 else
                   iAreaEnd = Integer.valueOf(arrAreaValues[1].trim());
               }
               ca = new CharArea(iAreaStart, iAreaEnd);
               listAllowedcharareas.add(ca);
             }
            
             arrAllowedChararea = new CharArea[listAllowedcharareas.size()];
             for(int k = 0; k < listAllowedcharareas.size(); k++)
               arrAllowedChararea[k] = listAllowedcharareas.get(k);

           }
           else
           if (key.equals("checkedcharareas") )
        {
             str_excludecharareas = value;
             arrExcludecharareas = str_excludecharareas.split(",");
             int eaMax = arrExcludecharareas.length;
             String [] arrAreaValues;
             CharArea ca;
             int iAreaStart, iAreaEnd;
            
             listExcludeAreas = new ArrayList<CharArea>();
             String strAreaValues ;
             for(int i = 0; i < eaMax; i++)
             {
               iAreaStart = -1;
               iAreaEnd = -1;
               arrAreaValues = null;
               strAreaValues = arrExcludecharareas[i];
               if (strAreaValues == null)
                 continue;
               if (strAreaValues.contains("-"))
                 arrAreaValues = strAreaValues.split("-");
               if (arrAreaValues == null)
               {
                 if (strAreaValues.contains("&#"))
                   // iAreaStart = new Integer(getHexaJaDesimaaliArvotMerkeiksi(arrAreaValues[0]).trim().charAt(0)).intValue();
                   iAreaStart = Integer.valueOf(getTrimmedValueForInteger(strAreaValues).charAt(0));
                 else
                   iAreaStart = Integer.valueOf(getTrimmedValueForInteger(strAreaValues));
               }
               else
               {
                 if (arrAreaValues[0].contains("&#"))
                 {
                   iAreaStart = Integer.valueOf(getTrimmedValueForInteger(arrAreaValues[0]).charAt(0));
                 }
                 else
                   iAreaStart = Integer.valueOf(getTrimmedValueForInteger(arrAreaValues[0]));
               }
              
               if (iAreaStart == -1)
                 continue;
              
               if (arrAreaValues == null || arrAreaValues.length == 1)
               {                
                 iAreaEnd = iAreaStart;
               }
               else
               {
                 if (arrAreaValues[1].contains("&#"))
                   // iAreaEnd = new Integer(getHexaJaDesimaaliArvotMerkeiksi(arrAreaValues[1]).trim().charAt(0)).intValue();
                   iAreaEnd = Integer.valueOf(getTrimmedValueForInteger(arrAreaValues[1]).charAt(0));
                 else
                   //iAreaEnd = new Integer(arrAreaValues[1].trim()).intValue();
                   iAreaEnd = Integer.valueOf(getTrimmedValueForInteger(arrAreaValues[1]));
               }
               ca = new CharArea(iAreaStart, iAreaEnd);
               listExcludeAreas.add(ca);
             }
            
             // listExcludeAreas.toArray();
             arrExcludeAreas = new CharArea[listExcludeAreas.size()];
             for(int k = 0; k < listExcludeAreas.size(); k++)
               arrExcludeAreas[k] = listExcludeAreas.get(k);
        }

           continue;
         }

         htValue = value;
         /*
         if (!bEkaKierros)
         {
           spKorvattavat.append(DtBook2Genre.cnstHaeErotinMerkkeja);
           spMuutettavat.append(DtBook2Genre.cnstHaeErotinMerkkeja);
         }
         */
         if (m_bHtmlArvotJavaMerkkijonoiksi)
         {
           htKey = getPropertyHexaValue2CharString(key);
           chKey = htKey.charAt(0);
           iKey = chKey;
           htValue = getPropertyHexaValue2CharString(value);
         }
         htValues.put(htKey, htValue);        
        
         bEkaKierros = false;
       }
      
       StringBuffer configIsMissing = new StringBuffer();
       if (str_readcharacterset == null)
         configIsMissing.append("config value readcharacterset is missing!\n");
       if (str_writecharacterset == null)
         configIsMissing.append("config value writecharacterset is missing!\n");
       /*
       if (str_allowedcharacterset == null)
         configIsMissing.append("config value allowedcharacterset is missing!\n");
       if (str_allowedcharareas == null)
         configIsMissing.append("config value allowedcharareas is missing!\n");
       */
       if (str_excludecharareas == null)
         configIsMissing.append("config value excludecharareas is missing!\n");
      
       if (configIsMissing.length() > 0)
         throw new CharConvertRowsException("Error: " + configIsMissing.toString());
      
       // arvoilla kutsutaan: m_dtBook2Genre.removeOrConvertChars(..,..,..)
       //m_strKorvattavat = spKorvattavat.toString().trim();
       //m_strMuutettavat  = spMuutettavat.toString().trim();
       Object arrObjMuutettavat [] =  htValues.keySet().toArray();
       Object arrObjKorvattavat [] =  htValues.values().toArray();

       m_arrChangeChars = getStrinArray(arrObjMuutettavat);
       m_arrReplaceChars = getStrinArray(arrObjKorvattavat);
      
 
       if (bEkaKierros) // ei löytynyt arvoja properties tiedostosta        
         m_bMakeCharConverstion = false; // ei tehdä muunnosta myöhemmin
   }

   private String
   getTrimmedValueForInteger(String value)
   {
     if (value == null || value.length() == 0)
       return value;
    
     String value2 = getHexAndDecimalValue2CharSring(value);
     if (value2.length() > 0 && !Character.isWhitespace(value2.charAt(0)))
     {
       String tmp_value = value2.trim();
       if (tmp_value != null && tmp_value.length() != 0)
         value2 = tmp_value;    
         }
       return value2;
   }
  
   private String []
   getStrinArray(Object [] values)
   {
     if (values == null)
       return null;
    
       int max = values.length;      
       String [] m_arr = new String[max];
       for(int i = 0; i < max; i++)
         m_arr[i] = (String)values[i];
       return m_arr;
   }
  
   /**
    * Tämä methodi muuntaa property tiedostossa olevan arvon java
    * merkkijonoksi jos se sisältää html hexa muodossa, esim &#x00E9
    * merkkijonoja.
    * @param arvo Muutetava merkkijono
    * @return Muutettu merkkijono
    */  
   private String
   getPropertyHexaValue2CharString(String arvo)
   {
     if (arvo == null || arvo.trim().length() == 0)
       return arvo;
     return getHexAndDecimalValue2CharSring(arvo);
   }
  
   /**
    * Tämä methodi muuntaa merkkijonossa olevat hexa arvo java
    * muotoon eli &#x00E9; muutetaan 233:iä vastaavaksi merkiksi jne.
    * @param arvo Muutetava merkkijono
    * @return Muutettu merkkijono
    */
   private String
   getHexAndDecimalValue2CharSring(String arvo)
   {
     // tutkitaanko onko muunnettavaa
     if (arvo == null || arvo.trim().length() == 0)
       return arvo;

     String cnstEtsiAlku = "&#";
     String cnstEtsiLoppu = ";";
     int indStart = arvo.indexOf(cnstEtsiAlku);    
     int indEnd = arvo.indexOf(cnstEtsiLoppu);
     // jos löytyi esim &#x00E9; tyyppinen arvo eli alkaa &#x
     // ja sen perässä päättyy ;
     String tmp_arvo;

     // onko yleensä muutettavaa:
     if (indStart > -1 && indEnd > -1 && indStart < indEnd)
     { 
       // hae ensin hexa arvot +muuta
       cnstEtsiAlku = "&#x";
       cnstEtsiLoppu = ";";
       indStart = arvo.indexOf(cnstEtsiAlku);    
       indEnd = arvo.indexOf(cnstEtsiLoppu);
       tmp_arvo = arvo;
       if (indStart > -1 && indEnd > -1 && indStart < indEnd)
       {               
         tmp_arvo = getMuutaHexaTaiDesiMerkkijonoksi(tmp_arvo,
                     cnstEtsiAlku, cnstEtsiLoppu);
         indStart = tmp_arvo.indexOf(cnstEtsiAlku);
         indEnd = tmp_arvo.indexOf(cnstEtsiLoppu);
         // onko vielä muutettavaa hexa arvoja:
         while(indStart > -1 && indEnd > -1 && indStart < indEnd)
         {
           tmp_arvo = getMuutaHexaTaiDesiMerkkijonoksi(tmp_arvo, cnstEtsiAlku, cnstEtsiLoppu);
           indStart = tmp_arvo.indexOf(cnstEtsiAlku);
           indEnd = tmp_arvo.indexOf(cnstEtsiLoppu);
        
       }
      
       // sitten haetaa pelkällä &# arvoilla:
       cnstEtsiAlku = "&#";
       cnstEtsiLoppu = ";";
       indStart = tmp_arvo.indexOf(cnstEtsiAlku);    
       indEnd = tmp_arvo.indexOf(cnstEtsiLoppu);
       if (indStart > -1 && indEnd > -1 && indStart < indEnd)
       { 
         // hae ensin hexa arvot +muuta
         tmp_arvo = getMuutaHexaTaiDesiMerkkijonoksi(tmp_arvo, cnstEtsiAlku, cnstEtsiLoppu);
         indStart = tmp_arvo.indexOf(cnstEtsiAlku);
         indEnd = tmp_arvo.indexOf(cnstEtsiLoppu);
         while(indStart > -1 && indEnd > -1 && indStart < indEnd)
         {
           tmp_arvo = getMuutaHexaTaiDesiMerkkijonoksi(tmp_arvo, cnstEtsiAlku, cnstEtsiLoppu);
           indStart = tmp_arvo.indexOf(cnstEtsiAlku);
           indEnd = tmp_arvo.indexOf(cnstEtsiLoppu);
         }
       }

       return tmp_arvo;
     }
     return arvo;
   }

   /**
    * Tämä metodi lukee merkkijonoa ja jos se löytää haettavien merkkien välistä
    * hexa merkkijonon palauttaa alkup. merkkijonon muutettuna. Muutos hexa merkiksi
    * tai merkkijonoksi.
    * @param arvo Muutettava
    * @param cnstEtsiAlku Etsittävä aloittava merkkijon, tässä &#x
    * @param cnstEtsiLoppu Etsittävä lopettava merkkijon, tässä ;
    * @return Muutetu merkkijono.
    */
   private String
   getMuutaHexaTaiDesiMerkkijonoksi(String arvo, final String cnstEtsiAlku, final String cnstEtsiLoppu)
   {
     // tutkitaanko onko muunnettavaa
     if (arvo == null || arvo.trim().length() == 0)
       return arvo;
    
     String tmp = arvo.trim();
     int indStart = tmp.indexOf(cnstEtsiAlku);    
     int indEnd = tmp.indexOf(cnstEtsiLoppu);
     if (indStart > -1 && indEnd > -1 && indStart < indEnd)
     {
       String tmp_alku = tmp.substring(0, indStart);      
       int iplus = (indEnd >= tmp.length() ? 0 : 1);
       String tmp_loppu = tmp.substring(indEnd+iplus);
       String parseroituArvo = tmp.substring(
           (indStart + cnstEtsiAlku.length()),
           indEnd);
       int iArvo = -1;
       if ("&#x".equals(cnstEtsiAlku))
           iArvo = Integer.parseInt( parseroituArvo.trim(), 16 /* radix */ );
       else
       if ("&#".equals(cnstEtsiAlku))
            iArvo = Integer.parseInt(parseroituArvo.trim());
       else
            return arvo;
      
      // if (8201 == iArvo) // # 'THIN SPACE' vaihdetaan tavuviivaksi:
        // iArvo = iArvo;

       char ch = (char)iArvo;
       String uusi_arvo = Character.toString(ch);
       String tmp_arvo = tmp_alku + uusi_arvo + tmp_loppu;
       return tmp_arvo;
     }
     return arvo;
   }

   /**
    * Tämä methodi hakee ja korvaa html-merkki(jono)
    * toisella vastaavasti koodatulla merki(jono)lla.
  *
    * @param value Muunnettava merkkijono
    * @return Muunnettu merkkijono.
    */
   private ConvertedRow
   makeCharConvert(String value)
   {
     ConvertedRow changed = m_dtBook2Genre.removeOrConvertChars(
           value, m_arrChangeChars, m_arrReplaceChars, m_bRegexReplace,
           arrExcludeAreas);
       return changed;
   }

   /**
    * Tämä methodi suorittaa tyhjien rivien poistoja ja lisäyksiä tiettyjen ml4-koodien edestä tai
    * takaa. Tupla ml4 koodeja poistetaan ensin, sitten regular expression ilmauksilla muutetaan
    * erikoiskoodien rivinvaihtoja, lähinnä ennen koodeja. Huomiotavaa on, että myäs toinen
    * parametri .replaceAll(...) methodissa on regular expression!!
    * <p>
    * @param fileName Muutettava tiedoston nimi.
    * @param outFileName  Tiedoston nimi, jonne tulos kirjoitetaan
    * @param readCharSet Luku merkistä, jolla syättätiedosto on talletettu aikaisemmin
    * @param writeCharSet Se merkistä, jolla tulostiedosto tullaan tallettamaan
    * @throws Exception Virhetilanteessa
    */
   public void
   convertchars(String fileName,
             boolean bDtbookContainsMixedCharsets,
             String outFileName,
             String readCharSet,
             String writeCharSet,
             String muokkaaRivejaPropertyFileName,
             boolean bJatkaLoppuunVaikkaKysymysmerkkeja,
             boolean bShowCharCodes )
   throws Exception
   {
       String strML4Data = null; // 
       convertchars(strML4Data, bDtbookContainsMixedCharsets, fileName,
                  outFileName, readCharSet,
                  writeCharSet, muokkaaRivejaPropertyFileName,
                  bJatkaLoppuunVaikkaKysymysmerkkeja,
                  bShowCharCodes);
   }       

   public void
   convertchars(String strML4Data,
               boolean bDtbookContainsMixedCharsets,
               String fileName,               
                String outFileName,
                String readCharSet,
                String writeCharSet,
                String convertPropertyFileName,
                boolean bContinueToEndEvenQuestionMarks,
                boolean bShowCharCodes )
   throws Exception
   {
          if (fileName == null)
          {
          severe("fileName is null!");
            return;
          }
          if (writeCharSet == null)
            writeCharSet = readCharSet;
         
          if (readCharSet == null || readCharSet.trim().equals(""))
            readCharSet = defLanguage_read;
          if (writeCharSet == null || writeCharSet.trim().equals(""))
            writeCharSet = defLanguage_write;     

          info("Converting file rows...");     

          info("read characterset=" + readCharSet);
          info("write characterset=" + writeCharSet);
         
          // info("Avataanko tarkasta rivit tiedostoja:");
          /*
          String path = ".";
          if (xslDtBook2ML4Path != null)
            path = xslDtBook2ML4Path;         
          File fKTR = new File(path +File.separator + cnstKaikkienTarkastaRivit);
        FileOutputStream fosKTR = new FileOutputStream(fKTR, append); 
          writerKaikkienTarkastaRivit = new OutputStreamWriter(fosKTR, "UTF-8");
          */

          if (bDtbookContainsMixedCharsets)
          {
          Calendar rightNow = Calendar.getInstance();
          int p = rightNow.get(Calendar.DATE);
          int k = rightNow.get(Calendar.MONTH)+1;
          int v = rightNow.get(Calendar.YEAR);
          int h = rightNow.get(Calendar.HOUR_OF_DAY);
          //if (h == 0)
            //h = 12;
          int m = rightNow.get(Calendar.MINUTE);
          int s = rightNow.get(Calendar.SECOND);

              String msg = String.format("%d.%d.%d %d:%d,%d", p, k, v, h, m, s) +"\n"
            + "Tiedosto sisältää muita merkkejä kuin UTF-8! (" + fileName+ ") " +"\n"
            + "(Dtbook2ml4 ja Dtbook2genre-ohjelmat korjaavat LATIN1 eli ISO-8859-1\n"
            + "merkit UTF-8 merkeiksi ajon aikana. Tiedostoa ei ole muutettu.)\n\n";
              m_logger.severe(msg);
              m_giveErrorMessage.possibleErrorRow(msg);
          }

          DecoderCharacterSet.CHARACTERSET writeCharacterSet =
                getCharacterSet(writeCharSet);
          DecoderCharacterSet.CHARACTERSET readCharacterSet =
            getCharacterSet(readCharSet);

          //info("3--");     
         
         
          if (convertPropertyFileName != null &&
              convertPropertyFileName.trim().length() != 0)       
            openConvertCharsCfg(convertPropertyFileName);
          else       
          {
              String def_muokkariveja_cfg = cnstDefPropertyFileName;
          String muokkariveja_cfg = def_muokkariveja_cfg;
          openConvertCharsCfg(muokkariveja_cfg);
          }
     
         
          /*
       if (key.equals("readcharacterset") )
         {
           str_readcharacterset = value;
         }
         else
         if (key.equals("writecharacterset") )
         {
           str_writecharacterset = value;
         }
         else
         if (key.equals("allowedcharacterset") )
         {
           str_allowedcharacterset = value;
         }          
         else
         if (key.equals("allowedcharareas") )
         {
           str_allowedcharareas = value;
         }
         else
         if (key.equals("excludecharareas") )
      {
           str_excludecharareas = value;
      }
      */
         
          if (str_readcharacterset != null)
          {
            if (str_readcharacterset.toUpperCase().equals(DecoderCharacterSet.CHARACTERSET.ISO_8859_1.toString().replace('_','-').toUpperCase()))
              readCharacterSet = DecoderCharacterSet.CHARACTERSET.ISO_8859_1;
            else
               if (str_readcharacterset.toUpperCase().equals(DecoderCharacterSet.CHARACTERSET.US_ASCII.toString().replace('_','-').toUpperCase()))
                 readCharacterSet = DecoderCharacterSet.CHARACTERSET.US_ASCII;
            else
               if (str_readcharacterset.toUpperCase().equals(DecoderCharacterSet.CHARACTERSET.ISO_8859_1.toString().replace('_','-').toUpperCase()))
                 readCharacterSet = DecoderCharacterSet.CHARACTERSET.ISO_8859_1;
               else
               if (str_readcharacterset.toUpperCase().equals(DecoderCharacterSet.CHARACTERSET.UTF_16.toString().replace('_','-').toUpperCase()))
                 readCharacterSet = DecoderCharacterSet.CHARACTERSET.UTF_16;
               else
              if (str_readcharacterset.toUpperCase().equals(DecoderCharacterSet.CHARACTERSET.UTF_16BE.toString().replace('_','-').toUpperCase()))
                 readCharacterSet = DecoderCharacterSet.CHARACTERSET.UTF_16BE;
               else
               if (str_readcharacterset.toUpperCase().equals(DecoderCharacterSet.CHARACTERSET.UTF_16LE.toString().replace('_','-').toUpperCase()))
                 readCharacterSet = DecoderCharacterSet.CHARACTERSET.UTF_16LE;
               else
               if (str_readcharacterset.toUpperCase().equals(DecoderCharacterSet.CHARACTERSET.UTF_8.toString().replace('_','-').toUpperCase()))
                 readCharacterSet = DecoderCharacterSet.CHARACTERSET.UTF_8;           
          }
           
         //  decoder = new DecoderCharacterSet( readCharacterSet,  writeCharacterSet);
    // DecoderCharacterSet.CHARACTERSET.UTF_8.toString().replace('_','-')
          try {
          info("Open file: " + fileName);
            InputStreamReader isr;
              // DecoderCharacterSet.readMixed88591AndUTF8FileConvertIntoUTF8(fileName);

          File f = new File(fileName);
          if (strML4Data == null || strML4Data.trim().length() == 0)
          {
            //fr = new FileReader (f);
            isr = new InputStreamReader(
                   new FileInputStream(f)
                   , readCharacterSet.toString().replace('_','-')
                   );
          }
          else
          {
            isr = new InputStreamReader(new ByteArrayInputStream(strML4Data.getBytes()));
          }

          BufferedReader br2 = new BufferedReader (isr);
        // String strHoleText = br2.readLine()
        // int iholeText_length = strHoleText.length();
          br = new BufferedReader (isr);
        
        //info("4--");
      } catch ( Exception e){
        //info("5--");
        e.printStackTrace();
        throw e;
      }
           
      //info("6--");
     
      info("checking second character rules...");
      text = replaceCharData(fileName);
           
      info("Handling a input file...");
      info("checking first character rules...");
      // vaihda rivinvaihdot käyttiksestä sekä input tiedostosta
      // riippumattomaksi ajon käyttiksen rivinvaihdoiksi:
      // CR on saanut arvon: CR = DecoderCharacterSet.getCarriageReturn();
      // joka osaa palauttaa CR arvon käyttiksen mukaan
      /*
      sb = new StringBuffer(text /? DecoderCharacterSet.convertCRAfterOS(text) ?/);
      text = sb.toString();
      */
     
      /*
      info("checkCharsUnder specific CharacterSet...");
      m_bPossibleErrorCharachters = checkCharsUnderCharacterSet(outFileName, text, readCharSet);
    if (m_bPossibleErrorCharachters && !bContinueToEndEvenQuestionMarks)
    {
      File of = new File(outFileName);

      String baseFileName = of.getName();
      int ind2 = baseFileName.lastIndexOf('.');
      if (ind2 > -1)
         baseFileName = baseFileName.substring(0, ind2);
      String uusiTiedostoNimi = of.getParent()
         +File.separator + baseFileName + cnstPossibleErrorExtension;
      File fileDelete = new File(outFileName);
      if (fileDelete.exists())
      {
        if (!fileDelete.delete())
          m_logger.severe(
      "Cannot delete " + outFileName +" file "
      + uusiTiedostoNimi +" before write it!");
      }
      outFileName = uusiTiedostoNimi;
    }
    */
     
    /*
    info("checkCharsUnder specific CharacterAreas...");
      m_bPossibleErrorCharachters = checkCharsUnderCharachterArea(outFileName, text, readCharSet);
    if (m_bPossibleErrorCharachters && !bContinueToEndEvenQuestionMarks)
    {
      File of = new File(outFileName);

      String baseFileName = of.getName();
      int ind2 = baseFileName.lastIndexOf('.');
      if (ind2 > -1)
         baseFileName = baseFileName.substring(0, ind2);
      String uusiTiedostoNimi = of.getParent()
         +File.separator + baseFileName + cnstPossibleErrorExtension;
      File fileDelete = new File(outFileName);
      if (fileDelete.exists())
      {
        if (!fileDelete.delete())
          m_logger.severe(
      "Cannot delete " + outFileName +" file "
      + uusiTiedostoNimi +" before write it!");
      }
      outFileName = uusiTiedostoNimi;
    }
    */
       
    // replace windows carriage returns into normal cr characters  :
      try {

          String modified = null;
          text = text.replaceAll("\r\n", "\n");
          /*
          modified = text;
                          
          text = modified;
          */
          //info("9--");
        
         
        //info("10--");
         
      if (outFileName == null)
          System.out.println(sb.toString());
        else
        {
          String strToFileData = null;
          File fo = null;
           try {

             info("Writing file: " + outFileName +" ... ");
             fo = new File(outFileName);
             FileOutputStream fos = new FileOutputStream(fo)
             OutputStreamWriter bos = new OutputStreamWriter(fos, writeCharacterSet.toString().replace('_','-')
                                                         /* writeWithCharSet */);
             BufferedWriter writer = new BufferedWriter (bos);
             //fos.write(decoder.convertCharacterSetBytes(sb.toString(), null));
             if (File.pathSeparatorChar == ';') // windows
               strToFileData = sb.toString().replaceAll("\n", CR);
             else
             strToFileData = sb.toString();
             writer.append(strToFileData);
             // seuraava rivi saa *tietyissä* user/unix/lang jne,
             //  siis ei aina eli kun käyttäjä celia promptista ajo, ok,
             // mutta cron-ajo +.sh:ssa lang="fi_FI.UTF8"
             // olosuhteissa virheellisen talletuksen: writer.write(strToFileData);
             writer.close();
             strToFileData = null;
             //fos.flush();
             //fos.close();
               if (m_bPossibleErrorCharachters && !bContinueToEndEvenQuestionMarks)
               {
               String msg = "Rivien joukosta löytyi mahdollisia virherivejä.\n"
                 +"Katso " + GiveRowMessage.cnstCheckThisFile +", jossa virheilmoitukset. Tulostustiedoston nimi muutettu: "
                 + outFileName +":ksi !!\n"
                 +"Ajo pysäytetty! Aja parametrin jatkaKH, jolloin voit pakottaa\n"
                 +"suorituksen jatkamisen tälläisesta virheestä huolimatta.";
               m_giveErrorMessage.close();
               throw new CharConvertRowsException(msg);
               }
                            
          if (writeCharSet.equals(ISO_8859_1.replace('_', '-')))
          {
              String msg = "Validoidaan kirjoitettu ml4-tiedosto.";
            info(msg);
            Validoi88591.checkThisFile(fo, false,
              ".ml4", strToFileData);
          }
         
             info("Rivien muokkaus valmis.");
              
               //info("11--");
           } catch ( Exception e){
           //info("12--");
             String errMsg = e.getMessage();
           severe("Tulostustiedoston avaus ei onnistunut: " + errMsg );
             e.printStackTrace();
             throw e;
           } finally {
           }
                     
           if (writeCharSet.equals(ISO_8859_1.replace('_', '-'))
             && fo != null && strToFileData != null)
           {
           fo = new File(outFileName);
           //try {
             Validoi88591.checkThisFile(fo, false,
             ".ml4", strToFileData);
           //} catch(Exception e2){
           //}        
           }

        }
        //info("13--");
      } catch ( Exception e){
        //severe("Virhe: " + e.getMessage());
          //e.printStackTrace();
          throw e;
          //info("14--");
      }
   }
  
   private boolean
   checkCharsUnderCharacterSet(String outFileName, String text, String strCharacterSet)
   throws Exception
   {
     int i, iRow, iRowChar, indErrorPos, iChError,
     iLen = text.length();    
    char ch, chCharSet;
     StringBuffer sbErrorRow = new StringBuffer();
     StringBuffer sbErrorData = new StringBuffer();
     boolean bErrorFounded = false;
     String strCh, strCharSet, strErrorRow;
    
     iRowChar = 0;
     iRow = 1;
    
     for(i = 0; i < iLen; i++)
     {
       iRowChar++;
       ch = text.charAt(i);
       if (ch == '\n')
       {
        iRowChar = 0;
        if (bErrorFounded)
        {
             m_bPossibleErrorCharachters = true;
             sbErrorData.append(sbErrorRow.toString());
        }
         bErrorFounded = false;
        strErrorRow = "";
        sbErrorRow = new StringBuffer ();
        // dddd sbErrorRowPosition
        iRow++;
       }
       
       strCh = "" + ch;
       strCharSet = new String(strCh.getBytes(strCharacterSet));
       chCharSet = strCharSet.charAt(0);
       if (chCharSet == '?' && ch != '?') // virhe muunnos
       {
         indErrorPos = iRowChar;
         bErrorFounded = true;
         sbErrorRow.append("[");
         sbErrorRow.append(ch);
         sbErrorRow.append("]");
         strErrorRow = sbErrorRow.toString();
         iChError = ch;
         giveErrorMessage(outFileName,
             "\nutf-8 merkki: " +iChError +"\n"
             +"utf-8 hex-arvo: "
             +byteArrayToHexString(strCh.getBytes()) +"\n"
             +strErrorRow, iRow, indErrorPos);
       }
       else
         sbErrorRow.append(ch);
     }

     return m_bPossibleErrorCharachters;
   }
  
   private String
   replaceCharData(String fileName)
   throws IOException, Exception
   {
      text = br.readLine();
     
      sb = new StringBuffer();
      int iRow = 0;
      String strBefore,
      strAfter;
      int ind;
     
    //pattern = Pattern.compile(searchPossibleMissedChars);
      ConvertedRow convertedRow = null;
      boolean bErrorFounded = false;
      StringBuffer sbErrorRow = new StringBuffer();     
      StringBuffer sbErrorRowPosition = new StringBuffer();
      int iRowLen, iRowLenPos;
     
      while(text != null)
      {
        iRow++;
        iRowLen = sbErrorRow.length();
        bErrorFounded = false;
        if (iRowLen > 0)
          iRowLen = iRowLen -1;
        sbErrorRow.delete(0, iRowLen);
        // sbErrorRow = new StringBuffer();
        iRowLenPos = sbErrorRowPosition.length();
        if (iRowLenPos > 0)
          iRowLenPos = iRowLenPos -1;
        sbErrorRowPosition.delete(0, iRowLenPos);
        // sbErrorRowPosition = new StringBuffer();
        if (text.equals("")) // jos alkup. rivi sisältää pelkän CR:n
          text = "\n"; // lisää se
        else
        {
          String tmp = text;
              if (tmp.length() == 0)
                text = "\n"; // poista välilyonnit
              else
              { // testaa onko trimmattu tmp vain white
                char [] chars = tmp.toCharArray();
                int max = chars.length;
                boolean isWhiteSpace = true;
                for(int i = 0; i < max; i++)
                {
                  if (!Character.isWhitespace(chars[i]))
                  {
                    isWhiteSpace = false;
                    break;
                  }                   
                }
                 
                if (isWhiteSpace
                  text = "\n"; // poista välilyonnit
                else
                {
       
              //text = DecoderCharacterSet.changeSpecialChars(text);
              if (m_bMakeCharConverstion)
              {         
                convertedRow = makeCharConvert(text);
                if (convertedRow != null)
                {
                 
                  //String unModInputRow = convertedRow.getInputRow();
                  String strOutputRow = convertedRow.getOutputRow();
                  if (strOutputRow != null)
                  {
                    String strErrorRow = null;
                    byte [] arrByteUnModInputRow = convertedRow.getArrChangedCharFlags();
                    int maxU = strOutputRow.length();
                    char ch;
                    byte modified;
                    int maxE ;
                    CharArea ca;
                    int iCh;
                    boolean bCharError = false;
                   
                    for(int i = 0 ; i < maxU; i++)
                    {
                      ch = strOutputRow.charAt(i);
                      maxE = arrExcludeAreas.length;
                      iCh = ch;
                      bCharError = false;
                     
                      for(int k = 0 ; k < maxE; k++)
                      {
                        ca = arrExcludeAreas[k];
                        if (ca == null)
                          continue;
                       
                        if (iCh >= ca.getChStartChar() && iCh <= ca.getChEndChar())
                        {
                          int indErrorPos = i;
                           bErrorFounded = true;
                           bCharError = true;
                           sbErrorRow.append("[");
                           sbErrorRow.append(ch);
                           sbErrorRow.append("]");
                           String strCh = "" +ch;
                           sbErrorRowPosition.append(
                                                      "\nrow:          " + iRow +"\n");
                           sbErrorRowPosition.append("column:       " + (i+1) +"\n");
                           sbErrorRowPosition.append("position:     " + (i+1) +"\n");
                           sbErrorRowPosition.append("position end: " + (i+1) +"\n");
                           sbErrorRowPosition.append("'" + ch +"' => utf-8 char: " +iCh +" "
                                    +"utf-8 in hex: "
                                    +byteArrayToHexString(strCh.getBytes()) +"\n");
                           // strErrorRow = sbErrorRow.toString();
                           // int iChError = ch;
                           // giveErrorMessage(fileName,                                   
                            /// strErrorRow, iRow, indErrorPos);
                           break;
                        }                       
                      }   
                     
                      if (!bCharError)
                        sbErrorRow.append(ch);
                    }
                  }
                                   
                   }           
                }
              }
        }
        if (bErrorFounded)
        {                          
         giveErrorMessage(fileName,
            sbErrorRow.toString() +"\n"
            + sbErrorRowPosition.toString() +"\n", iRow, -1);

        }

        sb.append(text +"\n");       
        text = br.readLine();
        }
      }
    br.close();
       
        //info("7--");
       
    text = sb.toString();
     
    return text;
   }
  
   private String
   getStringReplacedByChar(String value, char chSeach, String strReplace)
   {
     if (value == null || value.trim().length() == 0)
       return value;
     int ind = text.indexOf(chSeach);
     if (ind == -1)
       return value;
    
     StringBuffer ret = new StringBuffer (value.length());
     int max = value.length();
     char ch;
    
     for(int i = 0; i < max; i++)
     {
       ch = value.charAt(i);
       if (ch == chSeach)
         ret.append(strReplace);
       else
         ret.append(ch);
     }
     return ret.toString();
   }
  
   public boolean
   isVirheKonversioMerkki(final int iStart,
       final char chVirhemerkki,
       final String text,
       final Matcher matcher)
   {
     if (text == null)
       return false;
     if (iStart < 0)
       return false;
     int len = text.length();
     if (iStart > len)
       return false;
    
     String sana = getSana(iStart, text);
     if (sana != null)
     {
       int indMerkki = sana.indexOf(chVirhemerkki);
       if (indMerkki > -1)
       {
         int sanaLen  = sana.length();
         if (sanaLen > 0)
         {
           if ((sanaLen-1) == indMerkki)
             return false; // ? sanan lopussa, ok
           char chVikaMerkki = sana.charAt(sanaLen-1);
           if (isSallittuSananLopettavaMerkkiJalkeenKysymysMerkkia(chVikaMerkki))
             return false; // ? sanan lopussa, ok
           if (indMerkki < (sanaLen-1))
           {
             String strLoppu = sana.substring(indMerkki+1);
             if (strLoppu != null)
             {
               int iLoppuLen = strLoppu.length();
               int i = 0;
               while(i < iLoppuLen)
               {
                 if(!isSallittuSananLopettavaMerkkiJalkeenKysymysMerkkia(strLoppu.charAt(i)))
                   return true;
                 i++;
               }              
             }
            
           }
         }        
         return true;
       }      
     }
 
     return false;
   }

   private boolean
   isSallittuSananLopettavaMerkkiJalkeenKysymysMerkkia(char ch)
   {
     if (Character.isISOControl(ch) ||
       Character.isWhitespace(ch) ||
       ch == '"' || ch == ')' || ch == ']'
       || ch == '”' || ch == '»' || ch == ';' || ch == ',' || ch == '.'
       || ch == '’' || ch == '`' || ch == '\'' )
       return true;
     return false;
   }
  
   private String
   getSana(int iStart, String text)
   {
    
     if (iStart < 0 || text == null)
       return null;
     int len = text.length();
     if (len < 1 || iStart >= len)
       return null;
     String strAlku = getAlkuOsaSanasta(text.substring(0, iStart));
     String strLoppu = getLoppuOsaSanasta(
         ((iStart+1) < len) ? text.substring(iStart+1):"");
     char merkki = text.charAt(iStart);         
     if ((strAlku == null || strAlku.length() == 0)
       && (strLoppu == null || strLoppu.length() == 0))
       return ("" + merkki).trim();
     if ((strAlku != null && strAlku.length() != 0)
       && (strLoppu == null || strLoppu.length() == 0))
       return (strAlku + merkki).trim();
     if ((strAlku == null || strAlku.length() == 0)
         && (strLoppu != null && strLoppu.length() != 0))
       return (merkki +strLoppu).trim();
     return (strAlku + merkki +strLoppu).trim();
   }
  
   private String
   getAlkuOsaSanasta(String arvo)
   {
     if (arvo == null)
       return "";
     int len = arvo.length();
     if (len == 0)
       return "";
    
     int pos = len-1;    
     char ch = arvo.charAt(pos);    
     while(pos > 0 && !Character.isWhitespace(ch))
     {
       pos--;
       ch = arvo.charAt(pos);      
     }
     String ret = arvo.substring(pos, len)
     return ret;
   }
     
   private String
   getLoppuOsaSanasta(String arvo)
   {
     if (arvo == null)
       return "";
     int len = arvo.length();
     if (len == 0)
       return "";
    
     int pos = 0;    
     char ch = arvo.charAt(pos);    
     while(pos < len && !Character.isWhitespace(ch))
     {
       pos++;
       if (pos < len)
         ch = arvo.charAt(pos);
     }
     return arvo.substring(0, pos);
   }
  
   private void
   giveErrorMessage(String fileName, String text,
             int iRow, int indRowPos)
   throws Exception
   {
     if (text == null)
       return;
     String error_row = text;
     /*
     if (iRow < 0)
     {
       String row_start = text.substring(0, indRowPos);
       String strErrorChar = "[" +text.charAt(indRowPos) +"]";
       String row_end = (indRowPos < text.length()
         ? text.substring(indRowPos+1) : "");
       error_row  = row_start + strErrorChar +
                   row_end;
      }
      */
    Calendar rightNow = Calendar.getInstance();
    int p = rightNow.get(Calendar.DATE);
    int k = rightNow.get(Calendar.MONTH)+1;
    int v = rightNow.get(Calendar.YEAR);
    int h = rightNow.get(Calendar.HOUR_OF_DAY);
    //if (h == 0)
      //h = 12;
    int m = rightNow.get(Calendar.MINUTE);
    int s = rightNow.get(Calendar.SECOND);

    String msg = "";
     if (iRow == 0)
       msg = String.format("%d.%d.%d %d:%d,%d", p, k, v, h, m, s) +"\n"
          + /* "Error position? (" + fileName+ ") " + */"\n"
          +"column:" + (indRowPos+1) +" " + error_row;
     else
     if (indRowPos < 0)
       msg = String.format("%d.%d.%d %d:%d,%d", p, k, v, h, m, s) +"\n"
          + /* "Error position? (" + fileName+ ") " + */ "\n"
          + iRow +": " + error_row;       
     else
       msg = String.format("%d.%d.%d %d:%d,%d", p, k, v, h, m, s) +"\n"
          + /* "Error position? (" + fileName+ ") " + */ "\n"
          + iRow +":" + (indRowPos+1) +" " + error_row;
     m_logger.severe(msg);
     writeCheckErrorRows(msg);
     // listPossibleErrorRows.add(msg);
     bPossibleErrorRows = true;
   }
  
   private void
   writeCheckErrorRows(String msg)
   throws Exception
   {
     try {
       m_giveErrorMessage.possibleErrorRow(msg);
     } catch (Exception e){
       m_logger.severe("kirjoitaTarkastaRivit: " + e.getMessage()
           +"\n" +msg);
       throw e;
     }
   }
  
   /**
    * Palauttaa merkistän nimen merkkijonoa vastaavan DecoderCharacterSet.CHARACTERSET:n
    * @param writeWithCharSet
    * @return
    * @throws Exception
    */
   private DecoderCharacterSet.CHARACTERSET  
   getCharacterSet(String writeWithCharSet)
   throws Exception
   {
      if (writeWithCharSet == null)
      throw new Exception("Väärä character set: "
          + writeWithCharSet);
      else
    if (writeWithCharSet.equalsIgnoreCase("UTF-8") )
      return DecoderCharacterSet.CHARACTERSET.UTF_8;
      else
    if (writeWithCharSet.equalsIgnoreCase("UTF-16") )
      return DecoderCharacterSet.CHARACTERSET.UTF_16;
      else
    if (writeWithCharSet.equalsIgnoreCase("UTF-16BE") )
      return DecoderCharacterSet.CHARACTERSET.UTF_16BE;
      else
    if (writeWithCharSet.equalsIgnoreCase("UTF-16LE"))
      return DecoderCharacterSet.CHARACTERSET.UTF_16LE;
    else
    if (writeWithCharSet.equalsIgnoreCase("ISO-8859-1"))
      return DecoderCharacterSet.CHARACTERSET.ISO_8859_1;
    else
    if (writeWithCharSet.equalsIgnoreCase("US-ASCII"))
      return DecoderCharacterSet.CHARACTERSET.US_ASCII;
    else
      throw new Exception("Väärä character set: "
            + writeWithCharSet);
   }
  
   private static final byte[] HEXBYTES = { (byte) '0', (byte) '1', (byte) '2', (byte) '3',
        (byte) '4', (byte) '5', (byte) '6', (byte) '7', (byte) '8', (byte) '9', (byte) 'a',
        (byte) 'b', (byte) 'c', (byte) 'd', (byte) 'e', (byte) 'f' };

    /**
     * Converts a byte array into a hexadecimal string
     *
     *
     * @param b
     *          byte array
     *
     * @return hex string
     */
    public static String byteArrayToHexString(byte[] b) {

      int len = b.length;
      char[] s = new char[len * 2];

      for (int i = 0, j = 0; i < len; i++) {
        int c = ((int) b[i]) & 0xff;

        s[j++] = (char) HEXBYTES[c >> 4 & 0xf];
        s[j++] = (char) HEXBYTES[c & 0xf];
      }

      return new String(s);
    }
   
    public String getErrorString() { return m_strError; }
    public void setErrorString(String value) { m_strError = value; }

    public String getOutputData()
    {
      return sb.toString();
    }
   
    public String getPrintData()
    {
      String errMsg = m_giveErrorMessage.getErrorText();
      /*
      List<String> possibleErrMsg = m_giveErrorMessage.getListPossibleErrowRows();
      if (errMsg != null || (possibleErrMsg != null && possibleErrMsg.size() > 0))
      {
        if (errMsg == null)
          errMsg = "";
        if (possibleErrMsg == null)
          possibleErrMsg = new ArrayList<String>();

        return new StringBuffer(errMsg +" " + possibleErrMsg);
      }
      */
      // return new StringBuffer(errMsg);
      return errMsg;
      // return null;
    }

    public String toStringError()
    {
      // listPossibleErrorRows
      return null; // TODO: toteutus !!!
    }
  } // end of class
TOP

Related Classes of fi.celia.convert.chars.CharConvertRows

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.