/*
* [HTMLEntity.java]
*
* Summary: Describes one String literal token for display.
*
* Copyright: (c) 2004-2017 Roedy Green, Canadian Mind Products, http://mindprod.com
*
* Licence: This software may be copied and used freely for any purpose but military.
* http://mindprod.com/contact/nonmil.html
*
* Requires: JDK 1.8+
*
* Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/
*
* Version History:
* 2.0 2009-04-19 tidy comments, more accurate colour names
*/
package com.mindprod.jtokens.html;
import com.mindprod.jtokens.Token;
import java.awt.Color;
import java.awt.Font;
import java.util.regex.Pattern;
import static com.mindprod.jtokens.TokenColourScheme.HTML_FOREGROUND_FOR_ALPHA_ENTITY;
import static com.mindprod.jtokens.TokenColourScheme.HTML_FOREGROUND_FOR_DECIMAL_ENTITY;
import static com.mindprod.jtokens.TokenColourScheme.HTML_FOREGROUND_FOR_UNKNOWN_ENTITY;
import static com.mindprod.jtokens.TokenColourScheme.HTML_FOREGROUND_HTML_HEX_ENTITY;
import static com.mindprod.jtokens.TokenFonts.EMPHASIS_FONT_SIZE_IN_POINTS;
import static com.mindprod.jtokens.TokenFonts.MONO_FONTS;
/**
* Describes one String literal token for display.
*
* @author Roedy Green, Canadian Mind Products
* @version 2.0 2009-04-19 tidy comments, more accurate colour names
* @since 2004-04-24
*/
public final class HTMLEntity extends Token
{
/**
* version number for the class
*/
static final long serialVersionUID = 1L;
/**
* css classes to use for various categories of entity.
*/
private static final String[] ENTITY_CSS_CLASSES = { "unknownentity", "entity", "hexentity", "decimalentity" };
/**
* Colours to use for various categories of entity.
*/
private static final Color[] FOREGROUNDS_FOR_ENTITIES = {
HTML_FOREGROUND_FOR_UNKNOWN_ENTITY,
HTML_FOREGROUND_FOR_ALPHA_ENTITY,
HTML_FOREGROUND_HTML_HEX_ENTITY,
HTML_FOREGROUND_FOR_DECIMAL_ENTITY };
/**
* fonts to use for various categories of entity.
*/
private static final Font[] FONTS_FOR_ENTITIES = {
bestFont( MONO_FONTS, Font.BOLD, EMPHASIS_FONT_SIZE_IN_POINTS ),
bestFont( MONO_FONTS, Font.PLAIN, EMPHASIS_FONT_SIZE_IN_POINTS ),
bestFont( MONO_FONTS, Font.PLAIN, EMPHASIS_FONT_SIZE_IN_POINTS ),
bestFont( MONO_FONTS, Font.PLAIN, EMPHASIS_FONT_SIZE_IN_POINTS ) };
/**
* regex to look for decimal entities #999
*/
private static final Pattern DECIMAL_ENTITY_PATTERN = Pattern.compile( "#\\d+" );
/**
* regex to look for hex entities #xffff
*/
private static final Pattern HEX_ENTITY_PATTERN = Pattern.compile( "#x\\p{XDigit}+" );
/**
* Is this entity an official known one?
*/
private final boolean known;
/**
* Constructor
*
* @param entity literal text without the enclosing "
* @param known true if this is an official known entity
*/
public HTMLEntity( String entity, boolean known )
{
// save without lead & trail ;
super( entity );
this.known = known;
}
/**
* categorise this entity
* 0 unknown, 1 known alpha, 2 hex, 3 decimal
*
* @return 0..3 category
*/
private int getCategory()
{
if ( known )
{
return 1;
}
else if ( text.startsWith( "#x" ) && HEX_ENTITY_PATTERN.matcher( text ).matches() )
{
return 2;
}
else if ( text.startsWith( "#" ) && DECIMAL_ENTITY_PATTERN.matcher( text ).matches() )
{
return 3;
}
else
{
return 0;
}
}
/**
* font to render this token.
*
* @return Font, in the correct size.
*/
public Font getFont()
{
return FONTS_FOR_ENTITIES[ getCategory() ];
}
/**
* foreground colour to render this token.
*
* @return Color object.
*/
public Color getForeground()
{
return FOREGROUNDS_FOR_ENTITIES[ getCategory() ];
}
/**
* get the text surrounded by CSS html * @return decorated HTML in a &"
+ text
+ ";";
}
/**
* gets the raw text to render this token
*
* @return The plain String representation of the text
*/
public String getText()
{
return '&' + text + ';';
}
/**
* Can this token be collapsed with following space or with a following identical token?
*
* @return true if can be collapsed simply by combining text
*/
public boolean isCollapsible()
{
// autoinsert of &..; would be confused. Also don't want to mix known
// and unknown in same token..
return false;
}
/**
* Is this token useless? In other words would rendering it have no visible effect? NL and Space are not considered
* useless, unless they have 0 counts. Usually a token is useless because it has and empty or null text string.
*
* @return true if this token can be thrown away as useless
*/
public boolean isUseless()
{
return false;
}
}