Changeset 16637
- Timestamp:
- 2008-08-04T11:43:10+12:00 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
gli/trunk/src/org/greenstone/gatherer/collection/CollectionTreeNode.java
r15106 r16637 33 33 import org.greenstone.gatherer.file.FileNode; 34 34 import org.greenstone.gatherer.util.JarTools; 35 import java.util.Set; 36 import java.util.Iterator; 37 import java.nio.charset.Charset; 35 38 36 39 … … 48 51 private boolean is_srcreplaceable = false; 49 52 53 private String displayFileName = null; 54 50 55 public CollectionTreeNode(File file) 51 56 { … … 55 60 // To work with replace_srcdoc_with_html.pl 56 61 this.is_srcreplaceable = CollectionDesignManager.plugin_manager.isFileSrcReplaceable(file); 62 63 displayFileName = calcDisplayString(); 64 } 65 66 /** Similar to calcDisplayString, but this version only checks whether the filename 67 * String is UTF8. If so, it converts it into UTF8 and returns it. If the filename 68 * is not UTF8, it is returned unchanged. 69 */ 70 protected String quickCalcDisplayString() { 71 String filename = super.toString(); 72 try{ 73 String utf8filename = new String(filename.getBytes(), "UTF8"); 74 if(utf8filename.indexOf('\ufffd') == -1) { 75 return utf8filename; 76 } else { // contains the character indicating that it's invalid utf8 77 // return the original string 78 return filename; 79 } 80 } catch(java.io.UnsupportedEncodingException e) { 81 return filename; 82 } 83 } 84 85 /** This method returns a string representation of the filenodes in the Collection 86 * Tree, that can then be displayed in the tree. 87 * We'll initially assume that the filenames are utf8 encoded and so convert the 88 * filename into utf8 for proper presentation in the Collection tree pane. 89 * If the filenames are not utf8, then the conversion would have introduced funny 90 * characters. Therefore, when converting to utf8, if the converted filename 91 * contains the special character '\ufffd', then we know the conversion did not work 92 * and we return the original string which may or may not be properly presented by 93 * default. 94 * See http://java.sun.com/j2se/1.4.2/docs/api/java/nio/charset/CharsetDecoder.html 95 * which says "How a decoding error is handled depends upon the action requested for 96 * that type of error, which is described by an instance of the CodingErrorAction class. 97 * The possible error actions are to ignore the erroneous input, report the error to 98 * the invoker via the returned CoderResult object, or replace the erroneous input with 99 * the current value of the replacement string. The replacement has the initial value 100 * "\uFFFD"; its value may be changed via the replaceWith method." 101 * The following made me think that String(byte[], String charsetName) constructor may 102 * use the replacement value \uFFFD. 103 * http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20512969.html 104 * mentions the following which made me think of this: 105 * convertedStr = convertedStr.replace('\ufffd', ' '); 106 */ 107 protected String calcDisplayString() { 108 String filename = super.toString(); 109 110 try{ 111 String[] charsets = {"UTF-8", "ISO-8859-1", "US-ASCII", "UTF-16BE", "UTF-16LE", "UTF-16"}; 112 for(int i = 0; i < charsets.length; i++) { 113 String charset = charsets[i]; 114 String convertedfilename = new String(filename.getBytes(), charset); 115 if(convertedfilename.indexOf('\ufffd') != -1) { 116 // encountered the character that indicates when the conversion is invalid 117 convertedfilename = null; 118 continue; // try to use the next charset to encode the filename as 119 } 120 else { // valid conversion 121 System.err.println("Filename was encoded in: " + charset); 122 return convertedfilename; 123 } 124 } 125 } catch(java.io.UnsupportedEncodingException e) { 126 return filename; 127 } 128 129 return filename; // attempted conversions all failed 57 130 } 58 131 … … 77 150 return is_srcreplaceable; 78 151 } 152 153 /** This method returns a string representation of the filenodes in the Collection 154 * Tree, which is what will be displayed in the tree. It tries to convert it into 155 * some common encoding formats. Failing that, the unchanged filepath is returned. 156 * @see displayString 157 */ 158 public String toString() 159 { 160 if(displayFileName == null) { 161 displayFileName = calcDisplayString(); 162 } 163 return displayFileName; 164 } 79 165 }
Note:
See TracChangeset
for help on using the changeset viewer.