Changeset 25671 for main


Ignore:
Timestamp:
2012-05-24T14:00:41+12:00 (12 years ago)
Author:
sjm84
Message:

Reformatting this file ahead of some changes

File:
1 edited

Legend:

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

    r25635 r25671  
    3030import java.util.Stack;
    3131
    32 public class GS2MacroResolver
    33     extends MacroResolver
     32public class GS2MacroResolver extends MacroResolver
    3433{
    3534
    36     protected SimpleCollectionDatabase coll_db = null;
     35    protected SimpleCollectionDatabase coll_db = null;
    3736
    38     private static Pattern p_back_slash = Pattern.compile("\\\"");// create a pattern "\\\"", but it matches both " and \"
    39    
    40     // need to make it not add macros if they are already present
    41     public GS2MacroResolver(SimpleCollectionDatabase db) {
    42     super();
    43     coll_db = db;
    44     }
     37    private static Pattern p_back_slash = Pattern.compile("\\\"");// create a pattern "\\\"", but it matches both " and \"
    4538
    46     public GS2MacroResolver() {
    47     super();
    48     }
     39    // need to make it not add macros if they are already present
     40    public GS2MacroResolver(SimpleCollectionDatabase db)
     41    {
     42        super();
     43        coll_db = db;
     44    }
    4945
    50     public void setDB(SimpleCollectionDatabase db) {
    51     this.coll_db = db;
    52     }
     46    public GS2MacroResolver()
     47    {
     48        super();
     49    }
    5350
    54    
    55     public String resolve(String text, String lang, String scope,
    56               String doc_oid) {
    57     if (text == null || text.equals("")) return text;
    58     if (scope.equals(SCOPE_TEXT) && text_macros.size()==0) return text;
    59     if (scope.equals(SCOPE_META) && metadata_macros.size() ==0) return text;
    60     DBInfo node_info = null;
    61     DBInfo root_info = null;
    62     boolean new_lang = false;
    63     if (this.lang == null || !this.lang.equals(lang) ) {
    64         new_lang = true;
    65         this.lang = lang;
    66     }
     51    public void setDB(SimpleCollectionDatabase db)
     52    {
     53        this.coll_db = db;
     54    }
    6755
    68     Stack<Macro> macros = new Stack<Macro>();//ArrayList macros;
    69     if (scope.equals(SCOPE_TEXT)) {
    70         macros.addAll(text_macros);
    71     } else {
    72         macros.addAll(metadata_macros);
     56    public String resolve(String text, String lang, String scope, String doc_oid)
     57    {
     58        if (text == null || text.equals(""))
     59            return text;
     60        if (scope.equals(SCOPE_TEXT) && text_macros.size() == 0)
     61            return text;
     62        if (scope.equals(SCOPE_META) && metadata_macros.size() == 0)
     63            return text;
     64        DBInfo node_info = null;
     65        DBInfo root_info = null;
     66        boolean new_lang = false;
     67        if (this.lang == null || !this.lang.equals(lang))
     68        {
     69            new_lang = true;
     70            this.lang = lang;
     71        }
     72
     73        Stack<Macro> macros = new Stack<Macro>();//ArrayList macros;
     74        if (scope.equals(SCOPE_TEXT))
     75        {
     76            macros.addAll(text_macros);
     77        }
     78        else
     79        {
     80            macros.addAll(metadata_macros);
     81        }
     82        //for (int i=0; i<macros.size(); i++) {
     83        while (!macros.empty())
     84        {
     85            String new_text = null;
     86            Macro m = macros.pop();//.get(i);
     87            switch (m.type)
     88            {
     89            case TYPE_DICT:
     90                if (m.text == null || new_lang)
     91                {
     92                    Dictionary dict = new Dictionary(m.bundle, lang);
     93                    m.text = dict.get(m.key, null);
     94                }
     95                // we assume that dictionary entries will contain no macros
     96                // otherwise we can't cache the answer because it might be
     97                // document specific
     98                text = StringUtils.replace(text, m.macro, m.text);
     99                break;
     100            case TYPE_TEXT:
     101                // make sure we resolve any macros in the text
     102                // the (?s) treats the string as a single line, cos .
     103                // doesn't necessarily match line breaks
     104                //if (text.matches("(?s).*"+m.macro+".*")) {
     105
     106                /*
     107                 * Pattern p_text = Pattern.compile(".*" + m.macro +
     108                 * ".*",Pattern.DOTALL); Matcher match_text =
     109                 * p_text.matcher(text);
     110                 */
     111
     112                // sm252
     113                // String.contains is far faster than regex!
     114                if (text.contains(m.macro))
     115                { //match_text.matches()) { //text.matches("(?s).*"+m.macro+".*")) {
     116                    if (m.resolve)
     117                    {
     118                        new_text = this.resolve(m.text, lang, scope, doc_oid);
     119                    }
     120                    else
     121                    {
     122                        new_text = m.text;
     123                    }
     124                    text = StringUtils.replace(text, m.macro, new_text);
     125                    if (m.macro.endsWith("\\\\"))
     126                    { // to get rid of "\" from the string likes: "src="http://www.greenstone.org:80/.../mw.gif\">"
     127
     128                        Matcher m_slash = p_back_slash.matcher(text);
     129                        String clean_str = "";
     130                        int s = 0;
     131                        while (m_slash.find())
     132                        {
     133                            if (!text.substring(m_slash.end() - 2, m_slash.end() - 1).equals("\\"))
     134                            {
     135                                clean_str = clean_str + text.substring(s, m_slash.end() - 1); // it matches ", so get a substring before "
     136                            }
     137                            else
     138                            {
     139                                clean_str = clean_str + text.substring(s, m_slash.end() - 2);// it matches \", so get a substring before \
     140                            }
     141                            s = m_slash.end();// get the index of the last match
     142                            clean_str = clean_str + "\"";
     143                        }
     144                        text = clean_str + text.substring(s, text.length());
     145                    }
     146                }
     147                break;
     148            case TYPE_META:
     149                //Pattern p = Pattern.compile(".*" + m.macro + ".*",Pattern.DOTALL);
     150                //Matcher match = p.matcher(text);
     151                // sm252
     152                if (text.contains(m.macro))
     153                { //(match.matches()) { //text.matches("(?s).*"+m.macro+".*")) {
     154                    if (node_info == null)
     155                    {
     156                        node_info = coll_db.getInfo(doc_oid);
     157                        if (node_info == null)
     158                        {
     159                            break;
     160                        }
     161                    }
     162                    new_text = node_info.getInfo(m.text);
     163                    if (new_text == null || new_text.equals(""))
     164                    {
     165                        // try the root node
     166                        if (root_info == null && !OID.isTop(doc_oid))
     167                        {
     168                            root_info = coll_db.getInfo(OID.getTop(doc_oid));
     169                        }
     170                        if (root_info == null)
     171                            break;
     172                        new_text = root_info.getInfo(m.text);
     173                    }
     174                    if (new_text != null)
     175                    {
     176                        if (m.resolve)
     177                        {
     178                            new_text = this.resolve(new_text, lang, scope, doc_oid);
     179                        }
     180                        text = StringUtils.replace(text, m.macro, new_text);
     181                    }
     182
     183                }
     184
     185                break;
     186            } // switch
     187
     188        }
     189        return text;
     190
    73191    }
    74     //for (int i=0; i<macros.size(); i++) {
    75     while(!macros.empty()) {
    76         String new_text = null;
    77         Macro m = macros.pop();//.get(i);
    78         switch (m.type) {
    79         case TYPE_DICT:
    80         if (m.text==null || new_lang) {
    81             Dictionary dict = new Dictionary(m.bundle, lang);
    82             m.text = dict.get(m.key, null);
    83         }
    84         // we assume that dictionary entries will contain no macros
    85         // otherwise we can't cache the answer because it might be
    86         // document specific
    87         text = StringUtils.replace(text, m.macro, m.text);
    88         break;
    89         case TYPE_TEXT:
    90         // make sure we resolve any macros in the text
    91         // the (?s) treats the string as a single line, cos .
    92         // doesn't necessarily match line breaks
    93         //if (text.matches("(?s).*"+m.macro+".*")) {
    94        
    95         /*Pattern p_text = Pattern.compile(".*" + m.macro + ".*",Pattern.DOTALL);
    96         Matcher match_text = p_text.matcher(text);*/
    97 
    98         // sm252
    99         // String.contains is far faster than regex!
    100         if (text.contains(m.macro)) { //match_text.matches()) { //text.matches("(?s).*"+m.macro+".*")) {
    101             if (m.resolve) {
    102             new_text = this.resolve(m.text, lang, scope, doc_oid);
    103             } else {
    104             new_text = m.text;
    105             }
    106             text = StringUtils.replace(text, m.macro, new_text);
    107             if (m.macro.endsWith("\\\\")){ // to get rid of "\" from the string likes: "src="http://www.greenstone.org:80/.../mw.gif\">"
    108            
    109             Matcher m_slash = p_back_slash.matcher(text);
    110             String clean_str = "";
    111             int s=0;
    112             while (m_slash.find()){
    113                 if (!text.substring(m_slash.end()-2,m_slash.end()-1).equals("\\")){
    114                 clean_str = clean_str + text.substring(s,m_slash.end()-1); // it matches ", so get a substring before "
    115                 }else{
    116                 clean_str = clean_str + text.substring(s,m_slash.end()-2);// it matches \", so get a substring before \
    117                 }
    118                 s = m_slash.end();// get the index of the last match
    119                 clean_str = clean_str + "\"";
    120             }
    121             text = clean_str + text.substring(s,text.length());
    122             }
    123         }
    124         break;
    125         case TYPE_META:
    126         //Pattern p = Pattern.compile(".*" + m.macro + ".*",Pattern.DOTALL);
    127         //Matcher match = p.matcher(text);
    128         // sm252
    129         if (text.contains(m.macro)) { //(match.matches()) { //text.matches("(?s).*"+m.macro+".*")) {
    130             if (node_info == null) {
    131             node_info = coll_db.getInfo(doc_oid);
    132             if (node_info == null) {
    133                 break;
    134             }
    135             }
    136             new_text = node_info.getInfo(m.text);
    137             if (new_text == null || new_text.equals("")) {
    138             // try the root node
    139             if (root_info == null && !OID.isTop(doc_oid)) {
    140                 root_info = coll_db.getInfo(OID.getTop(doc_oid));
    141             }
    142             if (root_info == null) break;
    143             new_text = root_info.getInfo(m.text);
    144             }
    145             if (new_text != null) {
    146             if (m.resolve) {
    147                 new_text = this.resolve(new_text, lang, scope, doc_oid);
    148             }
    149             text =  StringUtils.replace(text, m.macro, new_text);
    150             }
    151            
    152         }
    153        
    154         break;
    155         } // switch
    156        
    157     }
    158     return text;
    159    
    160     }
    161192
    162193}
Note: See TracChangeset for help on using the changeset viewer.