Ignore:
Timestamp:
2003-08-18T13:55:21+12:00 (21 years ago)
Author:
jmt12
Message:

Fix 203B143

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gli/src/org/greenstone/gatherer/util/Utility.java

    r4933 r5153  
    5353 */
    5454public class Utility {
     55    static final public Dimension BUTTON_SIZE = new Dimension(160, 35);
     56    static final public Dimension LABEL_SIZE = new Dimension(125, 35);
    5557    /** The default size of a gatherer progress bar, in either the download view or the build view. */
    5658    static final public Dimension PROGRESS_BAR_SIZE = new Dimension(580,65);
     
    102104    static final public String GREENSTONEDIRECTORYMETADATA_TEMPLATE = "xml/metadata.xml";
    103105    /** Definition of an important directory name, in this case the private web cache directory for the collection. */
    104     static final public String GCACHE_DIR = "gcache" + File.separator;
     106    static final public String GCACHE_DIR = "cache" + File.separator;
    105107    static final public String GLI_ARCHIVE = "GLI.jar";
    106108    /** Definition of an important directory name, in this case the location of help documentation. */
    107109    static final public String HELP_DIR = BASE_DIR + "help" + File.separator;
    108110    /** Definition of an important directory name, in this case the import directory for the collection. */
    109     static final public String IMPORT_DIR = "gimport" + File.separator;
     111    static final public String IMPORT_DIR = "import" + File.separator;
    110112    /** Definition of an important directory name, in this case the index directory for the collection. */
    111113    static final public String INDEX_DIR = "index" + File.separator;
     
    117119    /** Definition of an important directory name, in this case the location of the default metadata sets. */
    118120    static final public String METADATA_DIR = BASE_DIR + "metadata" + File.separator;
    119     static final public String METADATA_EXTRACTED = "extracted.mds";
    120121    /** The location the gatherer expects to find metadata set information. */
    121122    static final public String METADATA_SET_TEMPLATE = "xml/template.mds";
     
    123124    static final public String METADATA_XML = "metadata.xml";
    124125    static final public String NAME_ELEMENT = "Name";
    125     /** Definition of an important directory name, in this case the import directory for the collection. */
    126     static final public String OLD_IMPORT_DIR = "import" + File.separator;
    127126    /** The default name of the perl executable under unix. */
    128127    static final public String PERL_EXECUTABLE_UNIX = "perl";
     
    196195    return new TreePath(temp);
    197196    }
    198     /** Decodes a string of text so its safe to use in a Greenstone configuration file. Esentially replaces "\n" with a newline.
    199      * @param raw The <strong>String</strong> before decoding, read from the configuration file..
    200      * @return A <strong>String</strong> ready to be placed in a component.
    201      */
    202     static public String decodeGreenstone(String raw) {
    203     raw = raw.replaceAll("&apos;", "\'");
    204     raw = raw.replaceAll("&gt;", ">");
    205     raw = raw.replaceAll("&lt;", "<");
    206     raw = raw.replaceAll("&quot;", "\"");
    207     raw = raw.replaceAll("&#39;", "\'");
    208     raw = raw.replaceAll("\\\\n", "\n");
    209     return raw;
    210     }
    211197    /** Takes a rfc2616 'safe' String and translates it back into its 'unsafe' form. Basically the native c wget decode_string() function, but without pointer stuff. If searches through the String looking for the pattern %xy where x and y are hexidecimal digits and where xy maps to a character.<BR> If x or y are not hexidecimal or % is followed by a \0 then the pattern is left as is.
    212198     * @param encoded The url-safe <strong>String</strong> to be decoded.
     
    263249     * @return A <strong>String</strong> which is safe to write to the configuration file.
    264250     */
     251    static final private char AMPERSTAMP_CHAR = '&';
     252    static final private char ESCAPE_CHAR = '\\';
     253    static final private char GREATER_THAN_CHAR = '>';
     254    static final private char LESS_THAN_CHAR = '<';
     255    static final private char NEWLINE_CHAR = '\n';
     256    static final private char QUOTE_CHAR = '\'';
     257    static final private char SPEECH_CHAR = '\"';
     258    static final private String ENCODED_AMPERSTAMP_STR = "&amp;";
     259    static final private String ENCODED_GREATER_THAN_STR = "&gt;";
     260    static final private String ENCODED_LESS_THAN_STR = "&lt;";
     261    static final private String ENCODED_SPEECH_STR = "&quot;";
     262    static final private String ESCAPED_NEWLINE_STR = "\\n";
     263
     264    /** Decodes a string of text so its safe to use in a Greenstone configuration file. Esentially replaces "\n" with a newline.
     265     * @param raw The <strong>String</strong> before decoding, read from the configuration file..
     266     * @return A <strong>String</strong> ready to be placed in a component.
     267     */
     268    static public String decodeGreenstone(String raw) {
     269    raw = raw.replaceAll("&apos;", "\'");
     270    raw = raw.replaceAll("&gt;", ">");
     271    raw = raw.replaceAll("&lt;", "<");
     272    raw = raw.replaceAll("&quot;", "\"");
     273    raw = raw.replaceAll("&#39;", "\'");
     274    raw = raw.replaceAll("\\\\n", "\n");
     275    return raw;
     276    }
     277
    265278    static public String encodeGreenstone(String raw) {
    266     raw = raw.replaceAll("<", "&lt;");
    267     raw = raw.replaceAll(">", "&gt;");
    268     return raw.replaceAll("\n", "\\\\n");
     279    // Once again regex fails to provide the power necessary for me to change strings. What I need to do is replace "<" and ">" with "&lt;" and "&gt;", and replace "\<" and "\>" with "<" and ">".
     280    StringBuffer processed = new StringBuffer();
     281    int index = 0;
     282    while(index < raw.length()) {
     283        char c = raw.charAt(index);
     284        switch(c) {
     285        // Replace a normal new line character with "\n"
     286            case NEWLINE_CHAR:
     287            processed.append(ESCAPED_NEWLINE_STR);
     288            break;
     289        // Replace "\<" with "<", or with "\&lt;" if this is for XML. Similar requirements for "\>".
     290            case ESCAPE_CHAR:
     291            if(index + 1 < raw.length()) {
     292            char d = raw.charAt(index + 1);
     293            if(d == LESS_THAN_CHAR) {
     294                processed.append(LESS_THAN_CHAR);
     295                index++;
     296                break;
     297            }
     298            else if(d == GREATER_THAN_CHAR) {
     299                processed.append(GREATER_THAN_CHAR);
     300                index++;
     301                break;
     302            }
     303            }
     304            // I have no idea how this would happen, but I better watch for it anyway
     305            processed.append(c);
     306            break;
     307        // Replace "<" with "&lt;"
     308            case LESS_THAN_CHAR:
     309            processed.append(ENCODED_LESS_THAN_STR);
     310            break;
     311        // Replace ">" with "&gt;"
     312            case GREATER_THAN_CHAR:
     313            processed.append(ENCODED_GREATER_THAN_STR);
     314            break;
     315            default:
     316            processed.append(c);
     317        }
     318        index++;
     319    }
     320    return processed.toString();
     321    }
     322    /** When retrieve text for, or from the collect.cfg file it may contain characters that can't go into a DOM such as "<" and ">". We also might already have encoded versions "&lt;" and "&gt;". Thus we must encode the former, and double encode the latter. */
     323    static public String encodeXML(String raw) {
     324    StringBuffer processed = new StringBuffer();
     325    int index = 0;
     326    while(index < raw.length()) {
     327        char c = raw.charAt(index);
     328        switch(c) {
     329        case GREATER_THAN_CHAR:
     330        processed.append(ENCODED_GREATER_THAN_STR);
     331        break;
     332        case LESS_THAN_CHAR:
     333        processed.append(ENCODED_LESS_THAN_STR);
     334        break;
     335        case AMPERSTAMP_CHAR:
     336        processed.append(ENCODED_AMPERSTAMP_STR);
     337        break;
     338        default:
     339        processed.append(c);
     340        }
     341        index++;
     342    }
     343    return processed.toString();
    269344    }
    270345
Note: See TracChangeset for help on using the changeset viewer.