Changeset 16838


Ignore:
Timestamp:
2008-08-15T18:37:16+12:00 (13 years ago)
Author:
ak19
Message:

Moved the displayString and toString() added recently in CollectionTreeNode into the superclass FileNode class, since WorkspaceTreeNode may also want to attempt to display filenames with proper encoding (if UTF8)

Location:
gli/trunk/src/org/greenstone/gatherer
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • gli/trunk/src/org/greenstone/gatherer/collection/CollectionTreeNode.java

    r16834 r16838  
    5151    private boolean is_srcreplaceable = false;
    5252
    53     private String displayFileName = null;
    54 
    5553    public CollectionTreeNode(File file)
    5654    {
     
    6159    this.is_srcreplaceable = CollectionDesignManager.plugin_manager.isFileSrcReplaceable(file);
    6260   
    63     displayFileName = calcDisplayString();
    64     }
    65 
    66 
    67     /** This method returns a string representation of the filenodes in the Collection
    68      * Tree, that can then be displayed in the tree.
    69      * We'll initially assume that the filenames are utf8 encoded and so convert the
    70      * filename into utf8 for proper presentation in the Collection tree pane.
    71      * If the filenames are not utf8, then the conversion would have introduced funny
    72      * characters. Therefore, when converting to utf8, if the converted filename
    73      * contains the special character '\ufffd', then we know the conversion did not work
    74      * and we return the original string which may or may not be properly presented by
    75      * default.
    76      * See http://java.sun.com/j2se/1.4.2/docs/api/java/nio/charset/CharsetDecoder.html
    77      * which says "How a decoding error is handled depends upon the action requested for
    78      * that type of error, which is described by an instance of the CodingErrorAction class.
    79      * The possible error actions are to ignore the erroneous input, report the error to
    80      * the invoker via the returned CoderResult object, or replace the erroneous input with
    81      * the current value of the replacement string. The replacement has the initial value
    82      * "\uFFFD"; its value may be changed via the replaceWith method."
    83      * The following made me think that String(byte[], String charsetName) constructor may
    84      * use the replacement value \uFFFD.
    85      * http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20512969.html
    86      * mentions the following which made me think of this:
    87      * convertedStr = convertedStr.replace('\ufffd', ' ');
    88      */
    89     protected String calcDisplayString() {
    90     String filename = super.toString();
    91     try{
    92         String utf8filename = new String(filename.getBytes(), "UTF8");
    93         if(utf8filename.indexOf('\ufffd') == -1) {
    94         return utf8filename;
    95         } else { // contains the character indicating that it's invalid utf8
    96         // return the original string
    97         return filename;
    98         }
    99     } catch(java.io.UnsupportedEncodingException e) {
    100         return filename;
    101     }
    10261    }
    10362
     
    12281    return is_srcreplaceable;
    12382    }
    124 
    125     /** This method returns a string representation of the filenodes in the Collection
    126      * Tree, which is what will be displayed in the tree. It tries to convert it into
    127      * some common encoding formats. Failing that, the unchanged filepath is returned.
    128      * @see displayString
    129      */
    130     public String toString()
    131     {
    132     if(displayFileName == null) {   
    133       displayFileName = calcDisplayString();
    134     }
    135     return displayFileName;
    136     }
    13783}
  • gli/trunk/src/org/greenstone/gatherer/file/FileNode.java

    r11762 r16838  
    2020    protected FileSystemModel model = null;
    2121    protected MutableTreeNode parent = null;
     22    /** The string that is displayed as the filename. Attempts to be in the correct encoding. */
     23    protected String displayFileName = null;
    2224
    2325
     
    3234        this.allows_children = false;
    3335    }
    34     }
    35 
     36    displayFileName = calcDisplayString();
     37    }
     38
     39
     40     /** This method returns a string representation of the filenodes in the Collection
     41     * Tree, that can then be displayed in the tree.
     42     * We'll initially assume that the filenames are utf8 encoded and so convert the
     43     * filename into utf8 for proper presentation in the Collection tree pane.
     44     * If the filenames are not utf8, then the conversion would have introduced funny
     45     * characters. Therefore, when converting to utf8, if the converted filename
     46     * contains the special character '\ufffd', then we know the conversion did not work
     47     * and we return the original string which may or may not be properly presented by
     48     * default.
     49     * See http://java.sun.com/j2se/1.4.2/docs/api/java/nio/charset/CharsetDecoder.html
     50     * which says "How a decoding error is handled depends upon the action requested for
     51     * that type of error, which is described by an instance of the CodingErrorAction class.
     52     * The possible error actions are to ignore the erroneous input, report the error to
     53     * the invoker via the returned CoderResult object, or replace the erroneous input with
     54     * the current value of the replacement string. The replacement has the initial value
     55     * "\uFFFD"; its value may be changed via the replaceWith method."
     56     * The following made me think that String(byte[], String charsetName) constructor may
     57     * use the replacement value \uFFFD.
     58     * http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20512969.html
     59     * mentions the following which made me think of this:
     60     * convertedStr = convertedStr.replace('\ufffd', ' ');
     61     */
     62    protected String calcDisplayString() {
     63    String filename = file.getName();
     64    try{
     65        String utf8filename = new String(filename.getBytes(), "UTF8");
     66        if(utf8filename.indexOf('\ufffd') == -1) {
     67        return utf8filename;
     68        } else { // contains the character indicating that it's invalid utf8
     69        // return the original string
     70        return filename;
     71        }
     72    } catch(java.io.UnsupportedEncodingException e) {
     73        return filename;
     74    }
     75    }
    3676
    3777    /** Returns the children of the node as an Enumeration. */
     
    312352    }
    313353    else {
    314         return file.getName();
     354        if(displayFileName == null) {   
     355        displayFileName = calcDisplayString();
     356        }
     357        return displayFileName;     
    315358    }
    316359    }
Note: See TracChangeset for help on using the changeset viewer.