001/*
002 * Copyright c 2018 Rusi Popov, MDA Tools.net All rights reserved.
003 *
004 * This program and the accompanying materials are made available under the terms of the
005 * Eclipse Public License v2.0 which accompanies this distribution, and is available at
006 * http://www.eclipse.org/legal/epl-v20.html
007 */
008package net.mdatools.modelant.core.wrap;
009
010import net.mdatools.modelant.core.api.Translate;
011import net.mdatools.modelant.core.api.wrap.WrapperFactory;
012
013/**
014 * A general mechanism for factory-specific names mapping.
015 * Allows defining names translation, names' plural forms and any other factory-specific proeprties
016 * @author Rusi Popov (popovr@mdatools.net)
017 */
018public class BaseTranslation implements Translate {
019
020  private final WrapperFactory factory;
021
022  public BaseTranslation(WrapperFactory factory) {
023    this.factory = factory;
024  }
025
026  /**
027   * @param original
028   * @return
029   * @see net.mdatools.modelant.core.api.Translate#translate(java.lang.String)
030   */
031  public final String translate(String original) {
032    String result;
033
034    original = original.trim();
035
036    result = getProperty( original );
037    if ( result == null ) {
038      result = original;
039    }
040    return result;
041  }
042
043  /**
044   * @see net.mdatools.modelant.core.api.Translate#translatePlural(java.lang.String)
045   */
046  public final String translatePlural(String word) {
047    String result;
048
049    word = word.trim();
050
051    result = getProperty( Translate.PREFIX_PLURAL+word );
052    if ( result == null ) {
053      result = word+"s";
054    }
055    return result;
056  }
057
058  /**
059   * @see net.mdatools.modelant.core.api.Translate#getProperty(java.lang.String)
060   */
061  public final String getProperty(String key) {
062    String result;
063
064    result = factory.getProperty( key );
065    if (result != null ) { // an exact match found
066      result = result.trim();
067
068    } else {
069      result = factory.getProperty( key.toLowerCase() );
070
071      if (result != null) { // a general match found
072        result = result.trim();
073
074      } else {
075        result = factory.getProperty( key.toUpperCase() );
076
077        if (result != null) { // a general match found
078          result = result.trim();
079        }
080      }
081    }
082    return result;
083  }
084}