View Javadoc

1   ////////////////////////////////////////////////////////////////////////////////
2   // MillScript: an Open Spice interpreter and batch website creation tool
3   // Copyright (C) 2001-2004 Open World Ltd
4   //
5   // This file is part of MillScript.
6   //
7   // MillScript is free software; you can redistribute it and/or modify it under
8   // the terms of the GNU General Public License as published by the Free
9   // Software Foundation; either version 2 of the License, or (at your option)
10  // any later version.
11  //
12  // MillScript is distributed in the hope that it will be useful, but WITHOUT
13  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  // more details.
16  //
17  // You should have received a copy of the GNU General Public License along with
18  // MillScript; if not, write to the Free Software Foundation, Inc., 59 Temple
19  // Place, Suite 330, Boston, MA  02111-1307  USA
20  ////////////////////////////////////////////////////////////////////////////////
21  import java.lang.*;
22  import java.util.*;
23  
24  public final class accentEncode {
25  	/**
26  		This is blatently nicked from millscript.
27  	*/
28  	public static String expand( char ch ) {
29          return(
30              ch == 0xA0 ? "nbsp" :       //  160
31              //  ch == 0xA9 ? "copy" :       //  169
32              //  ch == 0xAE ? "reg" :        //  174
33              //  ch == 0xB0 ? "deg" :        //  176
34              //  ch == 0xC0 ? "Agrave" :     //  192
35              //  ch == 0xC2 ? "Acirc" :      //  194
36              //  ch == 0xF6 ? "ouml" :       //  246
37              //  ch == 0xFC ? "uuml" :       //  252
38              null
39          );
40      }
41  
42      public static String accentFilter( String s ) {
43  		final StringBuffer b = new StringBuffer();
44  		final int len = s.length();
45  		for ( int i = 0; i < len; i++ ) {
46  			char ch = s.charAt( i );
47  			if ( ch > 127 || ch == 38 || Character.isISOControl( ch ) && !Character.isWhitespace( ch ) ) {
48  				String entity = expand( ch );
49  				if ( entity == null ) {
50  					b.append( "&#" + ((int)ch) + ";" );
51  				} else {
52  					b.append( "&" + entity + ";" );
53  				}
54  			} else {
55   				b.append(
56  					ch == 0x0A ? "<br>" :	// newline
57  					ch == 0x0D ? "<br>" : 	// line feed
58  					(new Character(ch)).toString()		// everything else
59  				);
60  	 		}
61  		}
62  		return b.toString();
63      }
64  }