[Nice-commit] Nice/src/nice/tools/doc utilities.nice,NONE,1.1 htmlwriter.nice,1.26,1.27
Brought to you by:
bonniot
From: Francis B. <fb...@us...> - 2004-11-26 14:55:09
|
Update of /cvsroot/nice/Nice/src/nice/tools/doc In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv1388/src/nice/tools/doc Modified Files: htmlwriter.nice Added Files: utilities.nice Log Message: Just trying to rationalise the source code a bit Index: htmlwriter.nice =================================================================== RCS file: /cvsroot/nice/Nice/src/nice/tools/doc/htmlwriter.nice,v retrieving revision 1.26 retrieving revision 1.27 diff -C2 -d -r1.26 -r1.27 *** htmlwriter.nice 25 Nov 2004 19:28:17 -0000 1.26 --- htmlwriter.nice 26 Nov 2004 14:54:56 -0000 1.27 *************** *** 375,445 **** } - /** Converts a String that may contain characters such as '<' to one that will display - * properly in html. Submitted by S. Bayer to http://www.rgagnon.com/javadetails/java-0306.html, - * although I've made some changes. - */ - String htmlEncode(String s) { - StringBuffer sb = new StringBuffer(s.length()); - //true if last char was blank - this is for handling non-breaking spaces - boolean lastWasBlankChar = false; - - for (int i = 0; i < s.length(); i++) { - char c = s.charAt(i); - if (c == ' ') { - // blank gets extra work, - // this solves the problem you get if you replace all - // blanks with , if you do that you lose - // word breaking - if (lastWasBlankChar) { - lastWasBlankChar = false; - sb.append(" "); - } - else { - lastWasBlankChar = true; - sb.append(' '); - } - } - else { - lastWasBlankChar = false; - // HTML Special Chars - if (c == '"') - sb.append("""); - else if (c == '&') - sb.append("&"); - else if (c == '<') - sb.append("<"); - else if (c == '>') - sb.append(">"); - else if (c == '\n') - // Handle Newline - sb.append("<br/>"); - else { - //char and int are not related in nice, so we can't do a direct cast - //hence, we use int(c) instead of (int)c - int ci = int(c); - /*This was originally 160 in S. Bayer version, but I couldn't see the sense of that - *so I changed it to 128, as 127 is the maximum value of an ascii character - */ - if(ci < 128) - // nothing special only 7 Bit - sb.append(c); - else { - // Not 7 Bit use the unicode system - sb.append("&#"); - sb.append(ci); - sb.append(';'); - } - } - } - } - return sb.toString(); - } - - /** Convenience method that converts a LocatedString to a String and then htmlEncodes it. - * @param s the string to encode - */ - String htmlEncode(LocatedString s) { - return htmlEncode(s.toString()); - } /** Returns a string containing everything required up to and including the <body> tag --- 375,378 ---- *************** *** 499,532 **** } - /** - * Checks a filename to make sure it doesn't contain funny characters. - */ - String escapeFilename(String filename) { - // Make sure we don't generate another index.html, as it would overwrite - // the index (with care to case-insensitive filesystems) - if (filename.equalsIgnoreCase("index")) - // XXX: we should make sure this is unique - return "_index"; - - char[] chars = filename.toCharArray(); - StringBuffer buf = new StringBuffer(); - for(char c : chars) { - if(!Character.isLetterOrDigit(c)) { - buf.append('$'); - buf.append(hexEncode(int(c))); - } - else { - buf.append(c); - } - } - return buf.toString(); - } - - String hexEncode(int i) { - String num = Integer.toHexString(i); - for(int pad = num.length(); pad < 4; pad++) - num = "0" + num; - return num.toUpperCase(); - } /** --- 432,435 ---- *************** *** 624,626 **** return buffer.toString(); ! } \ No newline at end of file --- 527,529 ---- return buffer.toString(); ! } --- NEW FILE: utilities.nice --- package nice.tools.doc; /* * Provides miscellaneous methods for tasks such as htmlencoding and * escaping filenames * @author Francis Barber * @date 26/11/2004 */ /** * Checks a filename to make sure it doesn't contain funny characters. */ String escapeFilename(String filename) { // Make sure we don't generate another index.html, as it would overwrite // the index (with care to case-insensitive filesystems) if (filename.equalsIgnoreCase("index")) // XXX: we should make sure this is unique return "_index"; char[] chars = filename.toCharArray(); StringBuffer buf = new StringBuffer(); for(char c : chars) { if(!Character.isLetterOrDigit(c)) { buf.append('$'); buf.append(hexEncode(int(c))); } else { buf.append(c); } } return buf.toString(); } String hexEncode(int i) { String num = Integer.toHexString(i); for(int pad = num.length(); pad < 4; pad++) num = "0" + num; return num.toUpperCase(); } /** Converts a String that may contain characters such as '<' to one that will display * properly in html. Submitted by S. Bayer to http://www.rgagnon.com/javadetails/java-0306.html, * although I've made some changes. */ String htmlEncode(String s) { StringBuffer sb = new StringBuffer(s.length()); //true if last char was blank - this is for handling non-breaking spaces boolean lastWasBlankChar = false; for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (c == ' ') { // blank gets extra work, // this solves the problem you get if you replace all // blanks with , if you do that you lose // word breaking if (lastWasBlankChar) { lastWasBlankChar = false; sb.append(" "); } else { lastWasBlankChar = true; sb.append(' '); } } else { lastWasBlankChar = false; // HTML Special Chars if (c == '"') sb.append("""); else if (c == '&') sb.append("&"); else if (c == '<') sb.append("<"); else if (c == '>') sb.append(">"); else if (c == '\n') // Handle Newline sb.append("<br/>"); else { //char and int are not related in nice, so we can't do a direct cast //hence, we use int(c) instead of (int)c int ci = int(c); /*This was originally 160 in S. Bayer version, but I couldn't see the sense of that *so I changed it to 128, as 127 is the maximum value of an ascii character */ if(ci < 128) // nothing special only 7 Bit sb.append(c); else { // Not 7 Bit use the unicode system sb.append("&#"); sb.append(ci); sb.append(';'); } } } } return sb.toString(); } /** Convenience method that converts a LocatedString to a String and then htmlEncodes it. * @param s the string to encode */ String htmlEncode(LocatedString s) { return htmlEncode(s.toString()); } |