View Javadoc

1   ////////////////////////////////////////////////////////////////////////////////
2   // MillScript: an Open Spice interpreter and batch website creation tool
3   // Copyright (C) 2001-2004 Open World Ltd
4   // Copyright (C) 2004 Kevin Rogers
5   //
6   // This file is part of MillScript.
7   //
8   // MillScript is free software; you can redistribute it and/or modify it under
9   // the terms of the GNU General Public License as published by the Free
10  // Software Foundation; either version 2 of the License, or (at your option)
11  // any later version.
12  //
13  // MillScript is distributed in the hope that it will be useful, but WITHOUT
14  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
16  // more details.
17  //
18  // You should have received a copy of the GNU General Public License along with
19  // MillScript; if not, write to the Free Software Foundation, Inc., 59 Temple
20  // Place, Suite 330, Boston, MA  02111-1307  USA
21  ////////////////////////////////////////////////////////////////////////////////
22  package org.millscript.millscript.tools;
23  
24  import org.millscript.commons.util.map.EHashMap;
25  
26  /**
27   * This class implements HTML character entity encoding.
28   */
29  public class CharacterEntity {
30  
31      /**
32       * The mapping from an entities name to its Unicode character.
33       */
34      private EHashMap< String, Character > nameToCode = new EHashMap< String, Character >();
35  
36      /**
37       * The mapping from Unicode character to its entity name.
38       */
39      private EHashMap< Character, String > codeToName = new EHashMap< Character, String >();
40  
41      /**
42       * Constructs a new <code>CharacterEntity</code>, with only the mandatory
43       * HTML entities defined.
44       */
45      public CharacterEntity() {
46          // We always have &amp;
47          addEntity( "amp", new Character( '&' ) );
48          // We always have &lt;
49          addEntity( "lt", new Character( '<' ) );
50          // We always have &gt;
51          addEntity( "gt", new Character( '>' ) );
52          // We always have &quot;
53          addEntity( "quot", new Character( '"' ) );
54      }
55  
56      /**
57       * Makes the specified entity available for encoding.
58       *
59       * @param name  the entity name
60       * @param ch    the Unicode character the entity represents
61       */
62      public void addEntity( final String name, final Character ch ) {
63          nameToCode.insert( name, ch );
64          codeToName.insert( ch, name );
65      }
66  
67      /**
68       * Returns the entity for the specified character. The entity will be named
69       * if we have a mapping for that character, otherwise it will be numeric.
70       *
71       * @param ch    the character to entity encode
72       * @return  a named or numeric entity for the specified character.
73       */
74      public String getEntityFor( final char ch ) {
75          final String name = codeToName.get( new Character( ch ) );
76          if ( name == null ) {
77              // For general entities we would use a maximum of 8 characters,
78              // e.g. &#65535;
79              final StringBuffer entity = new StringBuffer( 8 );
80              entity.append( "&#" );
81              entity.append( ((int)ch) );
82              entity.append( ';' );
83              return entity.toString();
84          } else {
85              // For general entities we would use a maximum of 10 characters,
86              // e.g. &thetasym;
87              final StringBuffer entity = new StringBuffer( 10 );
88              entity.append( "&" );
89              entity.append( name );
90              entity.append( ';' );
91              return entity.toString();
92          }
93      }
94  
95      /**
96       * Returns the Unicode character for the specified entity name.
97       *
98       * @param entityName    the entity name to get a character for
99       * @return  a Character for the specified entity name or <code>null</code>
100      * if there is no mapping
101      */
102     public Character getCharacterFor( final String entityName ) {
103         // We can simply look up the character and return it, as this will
104         // return null if the entity is undefined.
105         return nameToCode.get( entityName );
106     }
107 
108 }