001package com.hfg.xml.parser;
002
003import com.hfg.util.StringUtil;
004import com.hfg.util.collection.BiMap;
005
006
007//------------------------------------------------------------------------------
008/**
009 Base lookup class for character entities.
010
011 @author J. Alex Taylor, hairyfatguy.com
012 */
013//------------------------------------------------------------------------------
014// com.hfg XML/HTML Coding Library
015//
016// This library is free software; you can redistribute it and/or
017// modify it under the terms of the GNU Lesser General Public
018// License as published by the Free Software Foundation; either
019// version 2.1 of the License, or (at your option) any later version.
020//
021// This library is distributed in the hope that it will be useful,
022// but WITHOUT ANY WARRANTY; without even the implied warranty of
023// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
024// Lesser General Public License for more details.
025//
026// You should have received a copy of the GNU Lesser General Public
027// License along with this library; if not, write to the Free Software
028// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
029//
030// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
031// jataylor@hairyfatguy.com
032//------------------------------------------------------------------------------
033
034public abstract class AbstractEntities
035{
036
037   //---------------------------------------------------------------------------
038   protected abstract BiMap<String, Integer> getEntityMap();
039
040   //---------------------------------------------------------------------------
041   /**
042    @param inCharEntity String such as 'nbsp' from the character entity '&amp;nbsp;'
043    @return String with the Numeric character representation (ex: '#160' given 'nbsp' as input).
044    */
045   public String getNumericEntity(String inCharEntity)
046   {
047      return getEntityMap().containsKey(inCharEntity) ? "#" + getEntityMap().get(inCharEntity) : null;
048   }
049
050   //---------------------------------------------------------------------------
051   /**
052    @param inCharEntity String such as 'nbsp' from the character entity '&amp;nbsp;'
053    @return String with the Numeric character representation (ex: '#xa0' given 'nbsp' as input).
054    */
055   public String getHexEntity(String inCharEntity)
056   {
057      String hexEntity = null;
058      if (getEntityMap().containsKey(inCharEntity))
059      {
060         String hexString = Integer.toHexString(getEntityMap().get(inCharEntity));
061         hexEntity = "#x" + (hexString.length() < 4 ? StringUtil.polyChar('0', 4 - hexString.length()) : "") + hexString;
062      }
063
064      return hexEntity;
065   }
066
067   //---------------------------------------------------------------------------
068   /**
069    @param inCharEntity String such as 'beta' from the character entity '&amp;beta;'
070    @return Unicode character representation (ex: 'β' given 'beta' as input).
071    */
072   public Character getUnicodeChar(String inCharEntity)
073   {
074      return getEntityMap().containsKey(inCharEntity) ? Character.toChars(getEntityMap().get(inCharEntity))[0] : null;
075   }
076
077   //---------------------------------------------------------------------------
078   /**
079    @param inNumericValue Numeric value for the Unicode character
080    @return  String such as 'beta' from the character entity '&amp;beta;'
081    */
082   public String getEntity(int inNumericValue)
083   {
084      return getEntityMap().containsValue(inNumericValue) ? getEntityMap().getKey(inNumericValue) : null;
085   }
086
087   //---------------------------------------------------------------------------
088   /**
089    @param inChar Unicode character representation
090    @return  String such as 'beta' from the character entity '&amp;beta;'
091    */
092   public String getEntity(char inChar)
093   {
094      return getEntity((int) inChar);
095   }
096
097}