Changeset 11264


Ignore:
Timestamp:
2006-02-16T13:33:14+13:00 (18 years ago)
Author:
kjdon
Message:

recursively resolve the replacement values if resolve is set to true

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/util/GS2MacroResolver.java

    r9874 r11264  
    2929    // need to make it not add macros if they are already present
    3030    public GS2MacroResolver(GDBMWrapper gdbm) {
     31    super();
    3132    gdbm_src = gdbm;
    3233    }
    3334
    3435   
    35      public String resolve(String text, String lang, String scope,
     36    public String resolve(String text, String lang, String scope,
    3637              String doc_oid) {
    37     //System.err.println("resolving macros for "+text);
     38    if (text == null || text.equals("")) return text;
    3839    if (scope.equals(SCOPE_TEXT) && text_macros.size()==0) return text;
    3940    if (scope.equals(SCOPE_META) && metadata_macros.size() ==0) return text;
    40 
    4141    DBInfo node_info = null;
    4242    DBInfo root_info = null;
     
    5353        macros = metadata_macros;
    5454    }
    55    
    5655    for (int i=0; i<macros.size(); i++) {
     56        String new_text = null;
    5757        Macro m = (Macro)macros.get(i);
    5858        switch (m.type) {
     
    6262            m.text = dict.get(m.key, null);
    6363        }
    64         // now drop through to text case
    65         case TYPE_TEXT:
     64        // we assume that dictionary entries will contain no macros
     65        // otherwise we can't cache the answer because it might be
     66        // document specific
    6667        text = text.replaceAll(m.macro, m.text);
    6768        break;
     69        case TYPE_TEXT:
     70        // make sure we resolve any macros in the text
     71        // the (?s) treats the string as a single line, cos .
     72        // doesn't necessarily match line breaks
     73        if (text.matches("(?s).*"+m.macro+".*")) {
     74            if (m.resolve) {
     75            new_text = this.resolve(m.text, lang, scope, doc_oid);
     76            } else {
     77            new_text = m.text;
     78            }
     79            text = text.replaceAll(m.macro, new_text);
     80        }
     81        break;
    6882        case TYPE_META:
    69         if (text.indexOf(m.macro) != -1) {
     83        if (text.matches(".*"+m.macro+".*")) {
    7084            if (node_info == null) {
    7185            node_info = gdbm_src.getInfo(doc_oid);
     
    7488            }
    7589            }
    76            
    77             String value = node_info.getInfo(m.text);
    78             if (value != null) {
    79             text = text.replaceAll(m.macro, value);
    80             } else {
     90            new_text = node_info.getInfo(m.text);
     91            if (new_text == null || new_text.equals("")) {
    8192            // try the root node
    8293            if (root_info == null && !OID.isTop(doc_oid)) {
     
    8495            }
    8596            if (root_info == null) break;
    86             value = root_info.getInfo(m.text);
    87             if (value != null) {
    88                 text = text.replaceAll(m.macro, value);
     97            new_text = root_info.getInfo(m.text);
     98            }
     99            if (new_text != null) {
     100            if (m.resolve) {
     101                new_text = this.resolve(new_text, lang, scope, doc_oid);
    89102            }
     103            text = text.replaceAll(m.macro, new_text);
    90104            }
     105           
    91106        }
    92107       
     
    95110       
    96111    }
    97    
    98112    return text;
    99113   
Note: See TracChangeset for help on using the changeset viewer.