Changeset 23791

Show
Ignore:
Timestamp:
15.03.2011 17:06:39 (8 years ago)
Author:
davidb
Message:

Changes to take advantage of the more efficient Apache Commons string manipulation classes.

Location:
main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util
Files:
6 modified

Legend:

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

    r15756 r23791  
    2424import java.net.URLDecoder; 
    2525 
     26// Apache Commons 
     27import org.apache.commons.lang3.*; 
     28 
     29import java.util.Stack; 
     30 
    2631public class GS2MacroResolver  
    2732    extends MacroResolver 
     
    2934 
    3035    protected SimpleCollectionDatabase coll_db = null; 
     36 
     37    private static Pattern p_back_slash = Pattern.compile("\\\"");// create a pattern "\\\"", but it matches both " and \" 
    3138     
    3239    // need to make it not add macros if they are already present 
     
    5865    }  
    5966 
    60     ArrayList macros; 
     67    Stack macros;//ArrayList macros; 
    6168    if (scope.equals(SCOPE_TEXT)) { 
    6269        macros = text_macros; 
     
    6471        macros = metadata_macros; 
    6572    } 
    66     for (int i=0; i<macros.size(); i++) { 
     73    //for (int i=0; i<macros.size(); i++) { 
     74    while(!macros.empty()) { 
    6775        String new_text = null; 
    68         Macro m = (Macro)macros.get(i); 
     76        Macro m = (Macro)macros.pop();//.get(i); 
    6977        switch (m.type) { 
    7078        case TYPE_DICT: 
     
    7684        // otherwise we can't cache the answer because it might be  
    7785        // document specific 
    78         text = text.replaceAll(m.macro, m.text); 
     86        text = StringUtils.replace(text, m.macro, m.text); 
    7987        break; 
    8088        case TYPE_TEXT: 
     
    8391        // doesn't necessarily match line breaks 
    8492        //if (text.matches("(?s).*"+m.macro+".*")) { 
    85         Pattern p_text = Pattern.compile(".*" + m.macro + ".*",Pattern.DOTALL); 
    86         Matcher match_text = p_text.matcher(text); 
    87         if (match_text.matches()) { 
     93         
     94        /*Pattern p_text = Pattern.compile(".*" + m.macro + ".*",Pattern.DOTALL); 
     95        Matcher match_text = p_text.matcher(text);*/ 
     96 
     97        // sm252 
     98        // String.contains is far faster than regex! 
     99        if (text.contains(m.macro)) { //match_text.matches()) { //text.matches("(?s).*"+m.macro+".*")) { 
    88100            if (m.resolve) { 
    89101            new_text = this.resolve(m.text, lang, scope, doc_oid); 
     
    91103            new_text = m.text; 
    92104            } 
    93             text = text.replaceAll(m.macro, new_text); 
     105            text = StringUtils.replace(text, m.macro, new_text); 
    94106            if (m.macro.endsWith("\\\\")){ // to get rid of "\" from the string likes: "src="http://www.greenstone.org:80/.../mw.gif\">" 
    95             Pattern p_back_slash = Pattern.compile("\\\"");// create a pattern "\\\"", but it matches both " and \" 
     107             
    96108            Matcher m_slash = p_back_slash.matcher(text); 
    97109            String clean_str = ""; 
     
    111123        break; 
    112124        case TYPE_META: 
    113         Pattern p = Pattern.compile(".*" + m.macro + ".*",Pattern.DOTALL); 
    114         Matcher match = p.matcher(text); 
    115         if (match.matches()) { 
     125        //Pattern p = Pattern.compile(".*" + m.macro + ".*",Pattern.DOTALL); 
     126        //Matcher match = p.matcher(text); 
     127        // sm252 
     128        if (text.contains(m.macro)) { //(match.matches()) { //text.matches("(?s).*"+m.macro+".*")) { 
    116129            if (node_info == null) { 
    117130            node_info = coll_db.getInfo(doc_oid); 
     
    133146                new_text = this.resolve(new_text, lang, scope, doc_oid); 
    134147            } 
    135             text = text.replaceAll(m.macro, new_text); 
     148            text =  StringUtils.replace(text, m.macro, new_text); 
    136149            } 
    137150             
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/GSConstants.java

    r16869 r23791  
    2727    public static final String LIBRARY_NAME = "library_name"; 
    2828    public static final String DEFAULT_LANG = "default_lang"; 
    29  
     29    public static final String ALLOW_CLIENT_SIDE_XSLT = "allow_client_side_xslt"; 
    3030 
    3131    public static final String META_RELATION_SEP = "_"; 
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/GSXML.java

    r23627 r23791  
    264264  } 
    265265   
     266  public static HashMap extractParams(Element xml, boolean deep) { 
     267    return extractParams(xml, deep, null); 
     268  } 
     269 
    266270  /** takes a paramList element, and gets a HashMap of name-value pairs 
    267271   * if deep=true, extracts embedded params, otherwise just top level 
    268272   * params*/ 
    269   public static HashMap extractParams(Element xml, boolean deep) { 
     273  public static HashMap extractParams(Element xml, boolean deep, String toFind) { 
    270274     
    271275    if (!xml.getNodeName().equals(PARAM_ELEM+LIST_MODIFIER)) { 
     
    286290        String name=param.getAttribute(NAME_ATT); 
    287291        String value=getValue(param); //att or content 
     292 
     293    // For only one parameter 
     294    if(toFind != null && name.equals(toFind)) { 
     295        param_map.put(name, value); 
     296        return param_map; 
     297    }        
     298    else if(toFind != null) 
     299        continue; 
     300 
    288301        int pos = name.indexOf('.'); 
    289302        if (pos == -1) { // a base param 
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/SimpleCollectionDatabase.java

    r22973 r23791  
    2121import org.apache.log4j.*; 
    2222 
     23import org.apache.commons.lang3.StringUtils; 
     24 
    2325public class SimpleCollectionDatabase implements OID.OIDTranslatable { 
    2426   
     
    104106    DBInfo info = new DBInfo(); 
    105107     
    106     String [] lines = key_info.split("\n"); 
     108    String [] lines = StringUtils.split(key_info, "\n"); 
    107109    String key; 
    108110    String value; 
     
    193195      return top; 
    194196    } 
    195     contains = contains.replaceAll("\"", doc_id); 
    196     String [] children = contains.split(";"); 
     197    contains = StringUtils.replace(contains, "\"", doc_id); 
     198    String [] children = StringUtils.split(contains, ";"); 
    197199    if (suff.equals("fc")) { 
    198200      return children[0]; 
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/XMLConverter.java

    r18434 r23791  
    4141import java.io.FileInputStream; 
    4242import java.io.FileReader; 
     43import java.util.regex.*; 
    4344 
    4445import org.apache.log4j.*; 
     46 
     47// Apache Commons 
     48import org.apache.commons.lang3.*; 
     49 
     50import java.util.*; 
     51import java.lang.reflect.*; 
    4552 
    4653/** XMLConverter - utility class for greenstone 
     
    5966 
    6067    private static boolean outputEscaping = true; 
    61  
    6268 
    6369     /** the no-args constructor */ 
     
    216222    } 
    217223 
     224    /* For the purposes of logger.debug statements, where this is called and hence outputted, 
     225    returns an empty string if debugging is not enabled */ 
     226    public static String getPrettyStringLogger(Node xmlNode, Logger log) { 
     227 
     228    if(log.isDebugEnabled()) 
     229        return getPrettyString(xmlNode); 
     230     
     231    return ""; 
     232 
     233    } 
     234 
    218235    private static void getString(Node xmlNode, StringBuffer xmlRepresentation,  
    219236                 int depth, boolean pretty) 
     
    242259        String sid  = dt.getSystemId(); 
    243260         
    244         String doctype_str = "<!DOCTYPE " + dt.getName() + " PUBLIC \"" + pid + "\" \"" + sid + "\">\n"; 
     261        // Use previously assigned name, not dt.getName() again 
     262        String doctype_str = "<!DOCTYPE " + name + " PUBLIC \"" + pid + "\" \"" + sid + "\">\n"; 
    245263         
    246264        xmlRepresentation.append(doctype_str); 
     
    321339 
    322340        // Perform output escaping, if required 
     341        // Apache Commons replace method is far superior to String.replaceAll - very fast! 
    323342        if (outputEscaping) { 
    324         text = text.replaceAll("&", "&amp;");  // Must be done first!! 
    325         text = text.replaceAll("<", "&lt;"); 
    326         text = text.replaceAll(">", "&gt;"); 
    327         text = text.replaceAll("\"", "&quot;"); 
    328         text = text.replaceAll("\'", "&apos;"); 
     343 
     344        text = StringUtils.replace(text, "&", "&amp;"); 
     345        text = StringUtils.replace(text, "<", "&lt;"); 
     346        text = StringUtils.replace(text, ">", "&gt;"); 
     347        text = StringUtils.replace(text, "'", "&apos;"); 
     348        text = StringUtils.replace(text, "\"", "&quot;"); 
    329349        } 
    330350 
    331351        // Remove any control-C characters 
    332         text = text.replaceAll("" + (char) 3, ""); 
     352        text = StringUtils.replace(text, "" + (char)3, ""); 
     353 
    333354        xmlRepresentation.append(text); 
    334355    } 
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/XSLTUtil.java

    r18308 r23791  
    2828import org.w3c.dom.Node;  
    2929 
     30import org.apache.commons.lang3.StringUtils; 
     31 
    3032/** a class to contain various static methods that are used by the xslt 
    3133 * stylesheets 
     
    8890 
    8991    public static String getNumberedItem(String list, int number) { 
    90     String [] items = list.split(",", -1);   
     92    String [] items = StringUtils.split(list, ",", -1);  
    9193    if (items.length > number) { 
    9294        return items[number]; 
     
    112114    String [] args = null; 
    113115    if (args_str!=null && !args_str.equals("")) { 
    114         args = args_str.split(";"); 
     116        args = StringUtils.split(args_str, ";"); 
    115117    } 
    116118    Dictionary dict = new Dictionary("interface_"+interface_name, lang);