Changeset 8616


Ignore:
Timestamp:
2004-11-22T15:05:37+13:00 (19 years ago)
Author:
kjdon
Message:

modified the macro resolution - now replace items can be metadata as well, and have a scope - text or metadata. and this is used for text as well as metadata now. _httpdocimg_ is the only macro to remain in the code

Location:
trunk/gsdl3/src/java/org/greenstone/gsdl3
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/service/GS2MGPPRetrieve.java

    r5098 r8616  
    9898        return result;
    9999    }
    100 
     100    String lang = request.getAttribute(GSXML.LANG_ATT);
    101101    Element doc_list = this.doc.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
    102102    result.appendChild(doc_list);
     
    118118        long doc_num = this.gdbm_src.oid2Docnum(doc_id);
    119119        String doc_content = this.mgpp_src.getDocument(textdir, this.default_level, doc_num);
    120         doc_content = resolveImages(doc_content, doc_id);
     120        doc_content = resolveTextMacros(doc_content, doc_id, lang);
    121121
    122122        // For now, stick it in a text node - eventually should be parsed as xml??
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/service/GS2MGRetrieve.java

    r7823 r8616  
    101101    }
    102102   
     103    String lang = request.getAttribute(GSXML.LANG_ATT);
    103104    Element doc_list = this.doc.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
    104105    result.appendChild(doc_list);
     
    133134        doc_content = doc_content.replaceAll("\u0002|\u0003", "");
    134135        // replace _httpimg_ with the correct address
    135         doc_content = resolveImages(doc_content, doc_id);
     136        doc_content = resolveTextMacros(doc_content, doc_id, lang);
    136137        GSXML.addDocText(this.doc, doc, doc_content);
    137138        } else {
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/service/GS2Retrieve.java

    r8574 r8616  
    8585    {
    8686    this.gdbm_src = new GDBMWrapper();
    87     this.macro_resolver = new GS2MacroResolver();
     87    this.macro_resolver = new GS2MacroResolver(gdbm_src);
    8888    }
    8989
     
    132132        for (int i=0; i<replaces.getLength(); i++) {
    133133        Element e = (Element)replaces.item(i);
     134        String scope = e.getAttribute("scope");
     135        if (scope.equals("")) {
     136            scope = GS2MacroResolver.SCOPE_ALL;
     137        }
    134138        String from = e.getAttribute("macro");
    135139        String to = e.getAttribute("text");
    136         if (to.equals("")) {
    137             String key = e.getAttribute("key");
    138             String bundle = e.getAttribute("bundle");
    139             macro_resolver.addMacro(from, bundle, key);
     140        if (!to.equals("")) {
     141            macro_resolver.addMacro(GS2MacroResolver.TYPE_TEXT, from, to, scope);
    140142        } else {
    141             macro_resolver.addMacro(from, to);
    142         }
     143            String meta = e.getAttribute("metadata");
     144            if (!meta.equals("")) {
     145            macro_resolver.addMacro(GS2MacroResolver.TYPE_META, from, to, scope);
     146            } else {
     147            String key = e.getAttribute("key");
     148            String bundle = e.getAttribute("bundle");
     149            macro_resolver.addMacro(GS2MacroResolver.TYPE_DICT, from, bundle, key, scope);
     150            }
     151        }
    143152        }
    144153    }
     
    769778            String key = (String)it.next();
    770779            String value = info.getInfo(key);
    771             GSXML.addMetadata(this.doc, node_meta_list, key, this.macro_resolver.resolveMacros(value, lang));
     780            GSXML.addMetadata(this.doc, node_meta_list, key, this.macro_resolver.resolve(value, lang, GS2MacroResolver.SCOPE_META, node_id, info));
    772781        }
    773782        } else { // just get the selected ones
     
    794803    if (pos ==-1) {
    795804        // just a plain meta entry eg dc.Title
    796         return macro_resolver.resolveMacros((String)info.getInfo(metadata), lang);
     805        return macro_resolver.resolve((String)info.getInfo(metadata), lang, GS2MacroResolver.SCOPE_META, node_id, info);
    797806    }
    798807   
     
    854863
    855864    if (!multiple) {
    856         result.append(this.macro_resolver.resolveMacros(relation_info.getInfo(metadata), lang));
     865        result.append(this.macro_resolver.resolve(relation_info.getInfo(metadata), lang, GS2MacroResolver.SCOPE_META, relation_id, relation_info));
    857866    } else {
    858867        // we have multiple meta
     
    866875            result.append(separator);
    867876            }
    868             result.append(this.macro_resolver.resolveMacros((String)values.elementAt(i), lang));
     877            result.append(this.macro_resolver.resolve((String)values.elementAt(i), lang, GS2MacroResolver.SCOPE_META, relation_id, relation_info));
    869878        }
    870879        }
     
    883892        if (!multiple) {
    884893        result.insert(0, separator);
    885         result.insert(0, this.macro_resolver.resolveMacros(relation_info.getInfo(metadata), lang));
     894        result.insert(0, this.macro_resolver.resolve(relation_info.getInfo(metadata), lang, GS2MacroResolver.SCOPE_META, relation_id, relation_info));
    886895        } else {
    887896        Vector values = relation_info.getMultiInfo(metadata);
     
    889898            for (int i=values.size()-1; i>=0; i--) {
    890899            result.insert(0, separator);
    891             result.insert(0, this.macro_resolver.resolveMacros((String)values.elementAt(i), lang));
     900            result.insert(0, this.macro_resolver.resolve((String)values.elementAt(i), lang, GS2MacroResolver.SCOPE_META, relation_id, relation_info));
    892901            }
    893902        }
     
    904913
    905914    /** needs to get info from gdbm database - if the calling code gets it already it may pay to pass it in instead */
    906     protected String resolveImages(String doc_content, String doc_id)
     915    protected String resolveTextMacros(String doc_content, String doc_id, String lang)
    907916    {
    908917    String top_doc_id = OID.getTop(doc_id);
     
    910919    String archivedir = info.getInfo("archivedir");
    911920    String image_dir  = this.site_http_address + "/collect/"+this.cluster_name+"/index/assoc/"+archivedir;
    912 
     921   
    913922    // Resolve all "_httpdocimg_"s
    914923    doc_content = doc_content.replaceAll("_httpdocimg_", image_dir);
     924    // resolve any collection specific macros
     925    doc_content = macro_resolver.resolve(doc_content, lang, GS2MacroResolver.SCOPE_TEXT, top_doc_id, info);
    915926    return doc_content;
    916927    }
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/service/GS3MGRetrieve.java

    r8439 r8616  
    182182        doc_content = "this is the content for section hash id "+ doc_id+", mg doc num "+doc_int+"\n";
    183183        }
     184       
    184185        //ystem.out.println("Doc content: " + doc_content + "|");
    185186        //ystem.out.println("Doc ID: " + doc_id);
     
    192193        doc.setAttribute(GSXML.NODE_ID_ATT, doc_id);
    193194        GSXML.addDocText(this.doc, doc, doc_content);
     195        Element nodeContent = (Element)GSXML.getChildByTagName(doc, "nodeContent");
     196        Element file_elem = this.doc.createElement("file");
     197        file_elem.setAttribute("mimeType", "image/jpeg");
     198        file_elem.setAttribute("href", "import/meinahole.jpg");
     199        nodeContent.appendChild(file_elem);
    194200        doc_list.appendChild(doc);
    195201    }
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/util/GS2MacroResolver.java

    r8573 r8616  
    22
    33import java.util.ArrayList;
     4import org.greenstone.gdbm.GDBMWrapper;
     5import org.greenstone.gdbm.DBInfo;
    46
    57public class GS2MacroResolver {
    68
    7     ArrayList macros = null;
     9    public static final String SCOPE_TEXT = "text";
     10    public static final String SCOPE_META = "metadata";
     11    public static final String SCOPE_ALL = "all";
     12
     13    public static final int TYPE_TEXT = 0;
     14    public static final int TYPE_META = 1;
     15    public static final int TYPE_DICT = 2;
     16   
     17    protected ArrayList text_macros = null;
     18    protected ArrayList metadata_macros = null;
     19    protected GDBMWrapper gdbm_src = null;
     20   
    821    String lang = null;
    922    // need to make it not add macros if they are already present
    10     public GS2MacroResolver() {
    11     this.macros = new ArrayList();
     23    public GS2MacroResolver(GDBMWrapper wrapper) {
     24    this.text_macros = new ArrayList();
     25    this.metadata_macros = new ArrayList();
     26    this.gdbm_src = wrapper;
    1227    }
    13     public void addMacro(String macro, String text) {
     28   
     29    public void addMacro(int type, String macro, String text_or_metadata, String scope) {
    1430    Macro m = new Macro();
     31    m.type = type;
    1532    m.macro = macro;
    16     m.text = text;
    17     this.macros.add(m);
     33    m.text = text_or_metadata;
     34    addMacro(m, scope);
    1835    }
    1936
    20     public void addMacro(String macro, String bundle, String key) {
     37    public void addMacro(int type, String macro, String bundle, String key, String scope) {
    2138    Macro m = new Macro();
     39    m.type = type;
    2240    m.macro = macro;
    2341    m.bundle = bundle;
    2442    m.key = key;
    25     this.macros.add(m);
     43    addMacro(m, scope);
    2644    }
    2745
     46    protected void addMacro(Macro m, String scope) {
     47
     48    if (scope.equals(SCOPE_TEXT)) {
     49        this.text_macros.add(m);
     50    }
     51    else if (scope.equals(SCOPE_META)) {
     52        this.metadata_macros.add(m);
     53    }
     54    else if (scope.equals(SCOPE_ALL)) {
     55        this.text_macros.add(m);
     56        this.metadata_macros.add(m);
     57       
     58    }
     59
     60    }
    2861   
    29     public String resolveMacros(String text, String lang) {
     62    public String resolve(String text, String lang, String scope, String doc_oid, DBInfo info) {
    3063    System.err.println("resolving macros for "+text);
    31     if (macros.size()==0) return text;
     64    if (scope.equals(SCOPE_TEXT) && text_macros.size()==0) return text;
     65    if (scope.equals(SCOPE_META) && metadata_macros.size() ==0) return text;
    3266   
    3367    boolean new_lang = false;
    34     if (this.lang ==null) {
     68    if (this.lang == null || !this.lang.equals(lang) ) {
    3569        new_lang = true;
    3670        this.lang = lang;
     71    }
     72
     73    ArrayList macros;
     74    if (scope.equals(SCOPE_TEXT)) {
     75        macros = text_macros;
    3776    } else {
    38         if (!this.lang.equals(lang)) {
    39         new_lang = true;
    40         this.lang = lang;
    41         }
     77        macros = metadata_macros;
    4278    }
     79   
    4380    for (int i=0; i<macros.size(); i++) {
    4481        Macro m = (Macro)macros.get(i);
    45         if (m.key != null) {
     82        switch (m.type) {
     83        case TYPE_DICT:
    4684        if (m.text==null || new_lang) {
    4785            Dictionary dict = new Dictionary(m.bundle, lang);
    4886            m.text = dict.get(m.key, null);
    4987        }
     88        // now drop through to text case
     89        case TYPE_TEXT:
     90        text = text.replaceAll(m.macro, m.text);
     91        break;
     92        case TYPE_META:
     93        if (text.matches(m.macro)) {
     94            if (info == null) {
     95            info = gdbm_src.getInfo(doc_oid);
     96            if (info == null) {
     97                break;
     98            }
     99            String value = info.getInfo(m.text);
     100            if (value != null) {
     101                text = text.replaceAll(m.macro, value);
     102            }
     103            }
     104        }
    50105       
    51         }
    52         if (m.text != null) {
    53         text = text.replaceAll(m.macro, m.text);
     106        break;
    54107        }
    55108    }
     
    64117    private class Macro {
    65118
     119    public int type;
    66120    public String macro = null;
    67121    public String text = null;
    68122    public String key = null;
    69123    public String bundle = null;
     124   
    70125
    71126   
Note: See TracChangeset for help on using the changeset viewer.