Ignore:
Timestamp:
02/07/14 17:11:39 (10 years ago)
Author:
pharms
Message:
  • added support for id replacements
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/autoquest-plugin-html/src/main/java/de/ugoe/cs/autoquest/plugin/html/HTMLLogParser.java

    r1339 r1354  
    1515package de.ugoe.cs.autoquest.plugin.html; 
    1616 
     17import java.io.File; 
     18import java.io.FileInputStream; 
     19import java.io.FileNotFoundException; 
     20import java.io.IOException; 
    1721import java.util.Arrays; 
     22import java.util.HashMap; 
    1823import java.util.List; 
    1924import java.util.Map; 
     25import java.util.Properties; 
    2026import java.util.regex.Matcher; 
    2127import java.util.regex.Pattern; 
     
    6268     */ 
    6369    private Pattern htmlElementSpecPattern = 
    64         Pattern.compile("(\\w+)(\\[(\\d+)\\]|\\(htmlId=([\\w-]+)\\))?"); 
     70        Pattern.compile("(\\w+)(\\[(\\d+)\\]|\\(htmlId=([\\w-#]+)\\))?"); 
    6571     
    6672    /** 
     
    7076     */ 
    7177    private Map<String, List<String>> parseParams; 
     78 
     79    /** 
     80     * <p> 
     81     * a map containing replacement specifications for ids of GUI elements 
     82     * </p> 
     83     */ 
     84    private Map<String, String> idReplacements; 
    7285 
    7386    /** 
     
    8295         
    8396        for (String paramKey : parseParams.keySet()) { 
    84             if (!"clearId".equals(paramKey) && !"clearIndex".equals(paramKey)) { 
     97            if (!"clearId".equals(paramKey) && !"clearIndex".equals(paramKey) && 
     98                !"idReplacements".equals(paramKey)) 
     99            { 
    85100                throw new IllegalArgumentException("unknown parse parameter key " + paramKey); 
    86101            } 
     
    163178                } 
    164179                 
    165                 if (clearHTMLId(tagName, index, htmlId, parent)) { 
     180                String idReplacement = replaceHTMLId(tagName, index, htmlId, parent); 
     181                if (idReplacement != null) { 
     182                    htmlId = idReplacement; 
     183                } 
     184                else if (clearHTMLId(tagName, index, htmlId, parent)) { 
    166185                    htmlId = null; 
    167186                } 
     
    210229    private boolean clearIndex(String tagName, int index, String id, HTMLGUIElement parent) { 
    211230        return clearSomething("clearIndex", tagName, index, id, parent); 
     231    } 
     232 
     233    /** 
     234     * <p> 
     235     * TODO: comment 
     236     * </p> 
     237     * 
     238     * @param tagName 
     239     * @param index 
     240     * @param htmlId 
     241     * @param parent 
     242     * @return 
     243     */ 
     244    private String replaceHTMLId(String tagName, int index, String htmlId, HTMLGUIElement parent) 
     245        throws SAXException 
     246    { 
     247        if ((idReplacements == null) && (parseParams.containsKey("idReplacements"))) { 
     248            idReplacements = new HashMap<String, String>(); 
     249            for (String fileName : parseParams.get("idReplacements")) { 
     250                Properties props = new Properties(); 
     251                try { 
     252                    props.load(new FileInputStream(new File(fileName))); 
     253                } 
     254                catch (FileNotFoundException e) { 
     255                    throw new SAXException("could not find file " + fileName, e); 
     256                } 
     257                catch (IOException e) { 
     258                    throw new SAXException("error reading file " + fileName, e); 
     259                } 
     260                 
     261                for (Map.Entry<Object, Object> entry : props.entrySet()) { 
     262                    idReplacements.put((String) entry.getKey(), (String) entry.getValue()); 
     263                } 
     264            } 
     265        } 
     266         
     267        if (idReplacements != null) { 
     268            for (Map.Entry<String, String> replacementSpec : idReplacements.entrySet()) { 
     269                String tagSpec = replacementSpec.getKey(); 
     270 
     271                if (tagSpec.startsWith("/")) { 
     272                    throw new IllegalArgumentException("can not handle absolute specifications"); 
     273                } 
     274                else if (tagSpec.endsWith("/")) { 
     275                    throw new IllegalArgumentException("specifications may not end with a /"); 
     276                } 
     277 
     278                String[] tagSpecs = tagSpec.split("/"); 
     279 
     280                if (tagMatchesTagSpec(tagName, index, htmlId, parent, tagSpecs)) { 
     281                    return replacementSpec.getValue(); 
     282                } 
     283            } 
     284        } 
     285         
     286        return null; 
    212287    } 
    213288 
     
    294369            if (idCondition != null) { 
    295370                if (!idCondition.equals(id)) { 
    296                     return false; 
     371                    // check if the id condition would match with ignoring specific characters 
     372                    if ((id != null) && (idCondition.indexOf('#') > -1)) { 
     373                        // first of all, the length must match 
     374                        if (idCondition.length() != id.length()) { 
     375                            return false; 
     376                        } 
     377                         
     378                        for (int i = 0; i < idCondition.length(); i++) { 
     379                            if ((idCondition.charAt(i) != '#') && 
     380                                (idCondition.charAt(i) != id.charAt(i))) 
     381                            { 
     382                                // if there is a character that is neither ignored not matches 
     383                                // the condition at a specific position, return "no match" 
     384                                return false; 
     385                            } 
     386                        } 
     387                         
     388                    } 
     389                    else { 
     390                        // no condition ignoring specific characters 
     391                        return false; 
     392                    } 
    297393                } 
    298394            } 
Note: See TracChangeset for help on using the changeset viewer.