it.tidalwave.bluebill.taxonomy.birds.TaxonTranslator Maven / Gradle / Ivy
The newest version!
/***********************************************************************************************************************
*
* blueBill Resources - open source birding
* Copyright (C) 2009-2011 by Tidalwave s.a.s. (http://www.tidalwave.it)
*
***********************************************************************************************************************
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
***********************************************************************************************************************
*
* WWW: http://bluebill.tidalwave.it
* SCM: https://java.net/hg/bluebill~resources-src
*
**********************************************************************************************************************/
package it.tidalwave.bluebill.taxonomy.birds;
import it.tidalwave.util.logging.Logger;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.regex.MatchResult;
import javax.annotation.Nonnull;
/***********************************************************************************************************************
*
* @author Fabrizio Giudici
* @version $Id$
*
**********************************************************************************************************************/
public abstract class TaxonTranslator
{
private static final String CLASS = TaxonTranslator.class.getName();
private static final Logger logger = Logger.getLogger(CLASS);
private final List translationPairs = new ArrayList();
protected final List typoFixes = new ArrayList();
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
public abstract void loadConfiguration()
throws IOException;
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
protected void loadConfiguration (final @Nonnull InputStream is)
throws IOException
{
final BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
for (;;)
{
String s = br.readLine();
if (s == null)
{
break;
}
s = s.replaceAll("#.*", "");
s = s.trim();
if (!"".equals(s))
{
final Scanner scanner = new Scanner(s);
scanner.findInLine("(.*): +(.*) +-> +(.*)");
final MatchResult match = scanner.match();
final String command = match.group(1);
final String arg1 = match.group(2).trim(); // FIXME: trimming should be done by findInLine()
final String arg2 = match.group(3);
if ("EQUIVALENCE".equals(command))
{
translationPairs.add(new String[] {"/Animalia/Chordata/Aves/" + arg1, "/Animalia/Chordata/Aves/" + arg2});
}
else if ("TYPO".equals(command))
{
typoFixes.add(new String[]{ arg1, arg2});
logger.info(">>>> registered typo fix: %s -> %s", arg1, arg2);
}
}
}
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
public String fixTypos (@Nonnull String s)
{
s = s.replace(" ", " ");
for (final String[] typoFix : typoFixes)
{
final String oldS = s;
s = s.replace(typoFix[0], typoFix[1]);
if (!s.equals(oldS))
{
logger.info("%s typo fixed into %s", oldS, s);
}
}
return s;
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
public String translated (final @Nonnull String path)
{
String translatedPath = path;
for (final String[] pair : translationPairs)
{
try
{
translatedPath = translatedPath.replaceAll(pair[0], pair[1]);
}
catch (RuntimeException e)
{
throw new RuntimeException("While processing: " + pair[0] + " -> " + pair[1], e);
}
}
return translatedPath;
}
}