001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.tools.seleniumbuilder;
016    
017    import com.liferay.portal.kernel.util.StringPool;
018    import com.liferay.portal.kernel.util.StringUtil;
019    import com.liferay.portal.kernel.util.Validator;
020    import com.liferay.portal.kernel.xml.Attribute;
021    import com.liferay.portal.kernel.xml.Element;
022    
023    import java.util.HashMap;
024    import java.util.HashSet;
025    import java.util.List;
026    import java.util.Map;
027    import java.util.Set;
028    import java.util.regex.Matcher;
029    import java.util.regex.Pattern;
030    
031    import org.apache.tools.ant.DirectoryScanner;
032    
033    /**
034     * @author Michael Hashimoto
035     */
036    public class SeleniumBuilderContext {
037    
038            public SeleniumBuilderContext(String baseDir) throws Exception {
039                    this(baseDir, "com/liferay/portalweb/portal/util/liferayselenium/");
040            }
041    
042            public SeleniumBuilderContext(String baseDir, String liferaySeleniumDir)
043                    throws Exception {
044    
045                    _baseDir = baseDir;
046    
047                    _seleniumBuilderFileUtil = new SeleniumBuilderFileUtil(_baseDir);
048    
049                    DirectoryScanner directoryScanner = new DirectoryScanner();
050    
051                    directoryScanner.setBasedir(_baseDir);
052                    directoryScanner.setIncludes(
053                            new String[] {
054                                    "**\\*.action", "**\\*.function", "**\\*.macro", "**\\*.path",
055                                    "**\\*.testcase"
056                            });
057    
058                    directoryScanner.scan();
059    
060                    String[] fileNames = directoryScanner.getIncludedFiles();
061    
062                    for (String fileName : fileNames) {
063                            addFile(fileName);
064                    }
065    
066                    String[] seleniumFileNames = {
067                            liferaySeleniumDir + "LiferaySelenium.java",
068                            liferaySeleniumDir + "SeleniumWrapper.java"
069                    };
070    
071                    for (String seleniumFileName : seleniumFileNames) {
072                            String content = _seleniumBuilderFileUtil.getNormalizedContent(
073                                    seleniumFileName);
074    
075                            Matcher matcher = _pattern.matcher(content);
076    
077                            while (matcher.find()) {
078                                    String methodSignature = matcher.group();
079    
080                                    int x = methodSignature.indexOf(" ", 7);
081                                    int y = methodSignature.indexOf("(");
082    
083                                    String seleniumCommandName = methodSignature.substring(
084                                            x + 1, y);
085    
086                                    int count = 0;
087    
088                                    int z = methodSignature.indexOf(")");
089    
090                                    String parameters = methodSignature.substring(y + 1, z);
091    
092                                    if (!parameters.equals("")) {
093                                            count = StringUtil.count(parameters, ",") + 1;
094                                    }
095    
096                                    _seleniumParameterCounts.put(seleniumCommandName, count);
097                            }
098                    }
099    
100                    _seleniumParameterCounts.put("open", 1);
101            }
102    
103            public void addFile(String fileName) throws Exception {
104                    fileName = _normalizeFileName(fileName);
105    
106                    if (fileName.endsWith(".action")) {
107                            String actionName = _getName(fileName);
108    
109                            if (_actionFileNames.containsKey(actionName)) {
110                                    _seleniumBuilderFileUtil.throwValidationException(
111                                            1008, fileName, actionName);
112                            }
113    
114                            _actionFileNames.put(actionName, fileName);
115    
116                            _actionNames.add(actionName);
117    
118                            _actionRootElements.put(actionName, _getRootElement(fileName));
119                    }
120                    else if (fileName.endsWith(".function")) {
121                            String functionName = _getName(fileName);
122    
123                            _functionClassNames.put(functionName, _getClassName(fileName));
124    
125                            _functionFileNames.put(functionName, fileName);
126    
127                            _functionJavaFileNames.put(
128                                    functionName, _getJavaFileName(fileName));
129    
130                            Element rootElement = _getRootElement(fileName);
131    
132                            _functionLocatorCounts.put(
133                                    functionName, _getLocatorCount(rootElement));
134    
135                            if (_functionNames.contains(functionName)) {
136                                    _seleniumBuilderFileUtil.throwValidationException(
137                                            1008, fileName, functionName);
138                            }
139    
140                            _functionNames.add(functionName);
141    
142                            _functionPackageNames.put(functionName, _getPackageName(fileName));
143    
144                            _functionReturnTypes.put(
145                                    functionName, _getReturnType(functionName));
146    
147                            _functionRootElements.put(functionName, rootElement);
148    
149                            _functionSimpleClassNames.put(
150                                    functionName, _getSimpleClassName(fileName));
151                    }
152                    else if (fileName.endsWith(".macro")) {
153                            String macroName = _getName(fileName);
154    
155                            _macroClassNames.put(macroName, _getClassName(fileName));
156    
157                            _macroFileNames.put(macroName, fileName);
158    
159                            _macroJavaFileNames.put(macroName, _getJavaFileName(fileName));
160    
161                            if (_macroNames.contains(macroName)) {
162                                    _seleniumBuilderFileUtil.throwValidationException(
163                                            1008, fileName, macroName);
164                            }
165    
166                            _macroNames.add(macroName);
167    
168                            _macroPackageNames.put(macroName, _getPackageName(fileName));
169    
170                            _macroSimpleClassNames.put(
171                                    macroName, _getSimpleClassName(fileName));
172    
173                            _macroRootElements.put(macroName, _getRootElement(fileName));
174                    }
175                    else if (fileName.endsWith(".path")) {
176                            String pathName = _getName(fileName);
177    
178                            _actionClassNames.put(pathName, _getClassName(fileName, "Action"));
179    
180                            _actionJavaFileNames.put(
181                                    pathName, _getJavaFileName(fileName, "Action"));
182    
183                            _actionNames.add(pathName);
184    
185                            _actionPackageNames.put(pathName, _getPackageName(fileName));
186    
187                            _actionSimpleClassNames.put(
188                                    pathName, _getSimpleClassName(fileName, "Action"));
189    
190                            _pathClassNames.put(pathName, _getClassName(fileName));
191    
192                            _pathFileNames.put(pathName, fileName);
193    
194                            _pathJavaFileNames.put(pathName, _getJavaFileName(fileName));
195    
196                            if (_pathNames.contains(pathName)) {
197                                    _seleniumBuilderFileUtil.throwValidationException(
198                                            1008, fileName, pathName);
199                            }
200    
201                            _pathNames.add(pathName);
202    
203                            _pathPackageNames.put(pathName, _getPackageName(fileName));
204    
205                            _pathRootElements.put(pathName, _getRootElement(fileName));
206    
207                            _pathSimpleClassNames.put(pathName, _getSimpleClassName(fileName));
208                    }
209                    else if (fileName.endsWith(".testcase")) {
210                            String testCaseName = _getName(fileName);
211    
212                            _testCaseClassNames.put(testCaseName, _getClassName(fileName));
213    
214                            Element rootElement = _getRootElement(fileName);
215    
216                            _testCaseCommandNames.put(
217                                    testCaseName, _getTestCaseCommandNames(rootElement));
218    
219                            _testCaseFileNames.put(testCaseName, fileName);
220    
221                            _testCaseHTMLFileNames.put(
222                                    testCaseName, _getHTMLFileName(fileName));
223    
224                            _testCaseJavaFileNames.put(
225                                    testCaseName, _getJavaFileName(fileName));
226    
227                            if (_testCaseNames.contains(testCaseName)) {
228                                    _seleniumBuilderFileUtil.throwValidationException(
229                                            1008, fileName, testCaseName);
230                            }
231    
232                            _testCaseNames.add(testCaseName);
233    
234                            _testCasePackageNames.put(testCaseName, _getPackageName(fileName));
235    
236                            _testCaseRootElements.put(testCaseName, rootElement);
237    
238                            _testCaseSimpleClassNames.put(
239                                    testCaseName, _getSimpleClassName(fileName));
240                    }
241                    else {
242                            throw new IllegalArgumentException("Invalid file " + fileName);
243                    }
244            }
245    
246            public String getActionClassName(String actionName) {
247                    return _actionClassNames.get(actionName);
248            }
249    
250            public String getActionFileName(String actionName) {
251                    return _actionFileNames.get(actionName);
252            }
253    
254            public String getActionJavaFileName(String actionName) {
255                    return _actionJavaFileNames.get(actionName);
256            }
257    
258            public Set<String> getActionNames() {
259                    return _actionNames;
260            }
261    
262            public String getActionPackageName(String actionName) {
263                    return _actionPackageNames.get(actionName);
264            }
265    
266            public Element getActionRootElement(String actionName) {
267                    return _actionRootElements.get(actionName);
268            }
269    
270            public String getActionSimpleClassName(String actionName) {
271                    return _actionSimpleClassNames.get(actionName);
272            }
273    
274            public String getBaseDir() {
275                    return _baseDir;
276            }
277    
278            public String getFunctionClassName(String functionName) {
279                    return _functionClassNames.get(functionName);
280            }
281    
282            public String getFunctionFileName(String functionName) {
283                    return _functionFileNames.get(functionName);
284            }
285    
286            public String getFunctionJavaFileName(String functionName) {
287                    return _functionJavaFileNames.get(functionName);
288            }
289    
290            public int getFunctionLocatorCount(String functionName) {
291                    return _functionLocatorCounts.get(functionName);
292            }
293    
294            public Set<String> getFunctionNames() {
295                    return _functionNames;
296            }
297    
298            public String getFunctionPackageName(String functionName) {
299                    return _functionPackageNames.get(functionName);
300            }
301    
302            public String getFunctionReturnType(String functionName) {
303                    return _functionReturnTypes.get(functionName);
304            }
305    
306            public Element getFunctionRootElement(String functionName) {
307                    return _functionRootElements.get(functionName);
308            }
309    
310            public String getFunctionSimpleClassName(String functionName) {
311                    return _functionSimpleClassNames.get(functionName);
312            }
313    
314            public String getMacroClassName(String macroName) {
315                    return _macroClassNames.get(macroName);
316            }
317    
318            public String getMacroFileName(String macroName) {
319                    return _macroFileNames.get(macroName);
320            }
321    
322            public String getMacroJavaFileName(String macroName) {
323                    return _macroJavaFileNames.get(macroName);
324            }
325    
326            public Set<String> getMacroNames() {
327                    return _macroNames;
328            }
329    
330            public String getMacroPackageName(String macroName) {
331                    return _macroPackageNames.get(macroName);
332            }
333    
334            public Element getMacroRootElement(String macroName) {
335                    return _macroRootElements.get(macroName);
336            }
337    
338            public String getMacroSimpleClassName(String macroName) {
339                    return _macroSimpleClassNames.get(macroName);
340            }
341    
342            public String getPath(Element rootElement, String locatorKey) {
343                    String pathName = "";
344    
345                    Element bodyElement = rootElement.element("body");
346    
347                    Element tableElement = bodyElement.element("table");
348    
349                    Element tbodyElement = tableElement.element("tbody");
350    
351                    List<Element> trElements = tbodyElement.elements();
352    
353                    for (Element trElement : trElements) {
354                            List<Element> tdElements = trElement.elements("td");
355    
356                            Element pathLocatorElement = tdElements.get(1);
357    
358                            Element pathLocatorKeyElement = tdElements.get(0);
359    
360                            String pathLocatorKey = pathLocatorKeyElement.getText();
361    
362                            if (pathLocatorKey.equals(locatorKey)) {
363                                    return pathLocatorElement.getText();
364                            }
365    
366                            if (pathLocatorKey.equals("EXTEND_ACTION_PATH")) {
367                                    pathName = pathLocatorElement.getText();
368                            }
369                    }
370    
371                    if (Validator.isNotNull(pathName)) {
372                            Element pathRootElement = getPathRootElement(pathName);
373    
374                            return getPath(pathRootElement, locatorKey);
375                    }
376    
377                    return locatorKey;
378            }
379    
380            public String getPathClassName(String pathName) {
381                    return _pathClassNames.get(pathName);
382            }
383    
384            public String getPathFileName(String pathName) {
385                    return _pathFileNames.get(pathName);
386            }
387    
388            public String getPathJavaFileName(String pathName) {
389                    return _pathJavaFileNames.get(pathName);
390            }
391    
392            public Set<String> getPathLocatorKeys(Element rootElement) {
393                    Set<String> pathLocatorKeys = new HashSet<String>();
394    
395                    Element bodyElement = rootElement.element("body");
396    
397                    Element tableElement = bodyElement.element("table");
398    
399                    Element tbodyElement = tableElement.element("tbody");
400    
401                    List<Element> trElements = tbodyElement.elements();
402    
403                    for (Element trElement : trElements) {
404                            List<Element> tdElements = trElement.elements("td");
405    
406                            Element pathLocatorKeyElement = tdElements.get(0);
407    
408                            String pathLocatorKey = pathLocatorKeyElement.getText();
409    
410                            if (pathLocatorKey.equals("EXTEND_ACTION_PATH")) {
411                                    Element pathLocatorElement = tdElements.get(1);
412    
413                                    String pathName = pathLocatorElement.getText();
414    
415                                    Element pathRootElement = getPathRootElement(pathName);
416    
417                                    pathLocatorKeys.addAll(getPathLocatorKeys(pathRootElement));
418                            }
419                            else {
420                                    pathLocatorKeys.add(pathLocatorKey);
421                            }
422                    }
423    
424                    return pathLocatorKeys;
425            }
426    
427            public Set<String> getPathNames() {
428                    return _pathNames;
429            }
430    
431            public String getPathPackageName(String pathName) {
432                    return _pathPackageNames.get(pathName);
433            }
434    
435            public Element getPathRootElement(String pathName) {
436                    return _pathRootElements.get(pathName);
437            }
438    
439            public String getPathSimpleClassName(String pathName) {
440                    return _pathSimpleClassNames.get(pathName);
441            }
442    
443            public int getSeleniumParameterCount(String seleniumCommandName) {
444                    return _seleniumParameterCounts.get(seleniumCommandName);
445            }
446    
447            public String getTestCaseClassName(String testCaseName) {
448                    return _testCaseClassNames.get(testCaseName);
449            }
450    
451            public String getTestCaseFileName(String testCaseName) {
452                    return _testCaseFileNames.get(testCaseName);
453            }
454    
455            public String getTestCaseHTMLFileName(String testCaseName) {
456                    return _testCaseHTMLFileNames.get(testCaseName);
457            }
458    
459            public String getTestCaseJavaFileName(String testCaseName) {
460                    return _testCaseJavaFileNames.get(testCaseName);
461            }
462    
463            public Set<String> getTestCaseNames() {
464                    return _testCaseNames;
465            }
466    
467            public String getTestCasePackageName(String testCaseName) {
468                    return _testCasePackageNames.get(testCaseName);
469            }
470    
471            public Element getTestCaseRootElement(String testCaseName) {
472                    return _testCaseRootElements.get(testCaseName);
473            }
474    
475            public String getTestCaseSimpleClassName(String testCaseName) {
476                    return _testCaseSimpleClassNames.get(testCaseName);
477            }
478    
479            public void validateActionElements(String actionName) {
480                    String actionFileName = getActionFileName(actionName);
481    
482                    Element rootElement = getActionRootElement(actionName);
483    
484                    if (rootElement == null) {
485                            return;
486                    }
487    
488                    if (!_pathNames.contains(actionName)) {
489                            _seleniumBuilderFileUtil.throwValidationException(
490                                    2002, actionFileName, actionName);
491                    }
492    
493                    List<Element> caseElements =
494                            _seleniumBuilderFileUtil.getAllChildElements(rootElement, "case");
495    
496                    for (Element caseElement : caseElements) {
497                            _validateLocatorKeyElement(actionFileName, actionName, caseElement);
498                    }
499    
500                    List<Element> commandElements =
501                            _seleniumBuilderFileUtil.getAllChildElements(
502                                    rootElement, "command");
503    
504                    Set<String> commandElementNames = new HashSet<String>();
505    
506                    for (Element commandElement : commandElements) {
507                            String commandName = commandElement.attributeValue("name");
508    
509                            if (commandElementNames.contains(commandName)) {
510                                    _seleniumBuilderFileUtil.throwValidationException(
511                                            1009, actionFileName, commandElement, commandName);
512                            }
513    
514                            commandElementNames.add(commandName);
515    
516                            if (!_isFunctionName(commandName)) {
517                                    _seleniumBuilderFileUtil.throwValidationException(
518                                            2001, actionFileName, commandElement, commandName);
519                            }
520                    }
521    
522                    List<Element> executeElements =
523                            _seleniumBuilderFileUtil.getAllChildElements(
524                                    rootElement, "execute");
525    
526                    for (Element executeElement : executeElements) {
527                            _validateFunctionElement(actionFileName, executeElement);
528                    }
529            }
530    
531            public void validateElements(String fileName) {
532                    String name = _getName(fileName);
533    
534                    if (fileName.endsWith(".action")) {
535                            validateActionElements(name);
536                    }
537                    else if (fileName.endsWith(".function")) {
538                            validateFunctionElements(name);
539                    }
540                    else if (fileName.endsWith(".macro")) {
541                            validateMacroElements(name);
542                    }
543                    else if (fileName.endsWith(".testcase")) {
544                            validateTestCaseElements(name);
545                    }
546            }
547    
548            public void validateFunctionElements(String functionName) {
549                    Element rootElement = getFunctionRootElement(functionName);
550    
551                    if (rootElement == null) {
552                            return;
553                    }
554    
555                    String functionFileName = getFunctionFileName(functionName);
556    
557                    List<Element> commandElements =
558                            _seleniumBuilderFileUtil.getAllChildElements(
559                                    rootElement, "command");
560    
561                    Set<String> commandElementNames = new HashSet<String>();
562    
563                    for (Element commandElement : commandElements) {
564                            String commandName = commandElement.attributeValue("name");
565    
566                            if (commandElementNames.contains(commandName)) {
567                                    _seleniumBuilderFileUtil.throwValidationException(
568                                            1009, functionFileName, commandElement, commandName);
569                            }
570                            else {
571                                    commandElementNames.add(commandName);
572                            }
573                    }
574    
575                    List<Element> conditionAndExecuteElements =
576                            _seleniumBuilderFileUtil.getAllChildElements(
577                                    rootElement, "condition");
578    
579                    conditionAndExecuteElements.addAll(
580                            _seleniumBuilderFileUtil.getAllChildElements(
581                                    rootElement, "execute"));
582    
583                    for (Element conditionAndExecuteElement : conditionAndExecuteElements) {
584                            String function = conditionAndExecuteElement.attributeValue(
585                                    "function");
586                            String selenium = conditionAndExecuteElement.attributeValue(
587                                    "selenium");
588    
589                            if (function != null) {
590                                    _validateFunctionElement(
591                                            functionFileName, conditionAndExecuteElement);
592                            }
593                            else if (selenium != null) {
594                                    _validateSeleniumElement(
595                                            functionFileName, conditionAndExecuteElement);
596                            }
597                    }
598            }
599    
600            public void validateMacroElements(String macroName) {
601                    Element rootElement = getMacroRootElement(macroName);
602    
603                    if (rootElement == null) {
604                            return;
605                    }
606    
607                    String macroFileName = getMacroFileName(macroName);
608    
609                    validateVarElements(rootElement, macroFileName);
610    
611                    List<Element> commandElements =
612                            _seleniumBuilderFileUtil.getAllChildElements(
613                                    rootElement, "command");
614    
615                    Set<String> commandElementNames = new HashSet<String>();
616    
617                    for (Element commandElement : commandElements) {
618                            String commandName = commandElement.attributeValue("name");
619    
620                            if (commandElementNames.contains(commandName)) {
621                                    _seleniumBuilderFileUtil.throwValidationException(
622                                            1009, macroFileName, commandElement, commandName);
623                            }
624                            else {
625                                    commandElementNames.add(commandName);
626                            }
627                    }
628    
629                    List<Element> conditionAndExecuteElements =
630                            _seleniumBuilderFileUtil.getAllChildElements(
631                                    rootElement, "condition");
632    
633                    conditionAndExecuteElements.addAll(
634                            _seleniumBuilderFileUtil.getAllChildElements(
635                                    rootElement, "execute"));
636    
637                    for (Element conditionAndExecuteElement : conditionAndExecuteElements) {
638                            String action = conditionAndExecuteElement.attributeValue("action");
639                            String macro = conditionAndExecuteElement.attributeValue("macro");
640    
641                            if (action != null) {
642                                    _validateActionElement(
643                                            macroFileName, conditionAndExecuteElement);
644                            }
645                            else if (macro != null) {
646                                    _validateMacroElement(
647                                            macroFileName, conditionAndExecuteElement);
648                            }
649                    }
650            }
651    
652            public void validateTestCaseElements(String testCaseName) {
653                    Element rootElement = getTestCaseRootElement(testCaseName);
654    
655                    if (rootElement == null) {
656                            return;
657                    }
658    
659                    String testCaseFileName = getTestCaseFileName(testCaseName);
660    
661                    validateVarElements(rootElement, testCaseFileName);
662    
663                    List<Element> commandElements =
664                            _seleniumBuilderFileUtil.getAllChildElements(
665                                    rootElement, "command");
666    
667                    Set<String> commandElementNames = new HashSet<String>();
668    
669                    for (Element commandElement : commandElements) {
670                            String commandName = commandElement.attributeValue("name");
671    
672                            if (commandElementNames.contains(commandName)) {
673                                    _seleniumBuilderFileUtil.throwValidationException(
674                                            1009, testCaseFileName, commandElement, commandName);
675                            }
676                            else {
677                                    commandElementNames.add(commandName);
678                            }
679                    }
680    
681                    List<Element> executeElements =
682                            _seleniumBuilderFileUtil.getAllChildElements(
683                                    rootElement, "execute");
684    
685                    for (Element executeElement : executeElements) {
686                            String action = executeElement.attributeValue("action");
687                            String macro = executeElement.attributeValue("macro");
688    
689                            if (action != null) {
690                                    _validateActionElement(testCaseFileName, executeElement);
691                            }
692                            else if (macro != null) {
693                                    _validateMacroElement(testCaseFileName, executeElement);
694                            }
695                    }
696            }
697    
698            public void validateVarElements(Element rootElement, String fileName) {
699                    List<Element> varElements =
700                            _seleniumBuilderFileUtil.getAllChildElements(rootElement, "var");
701    
702                    for (Element varElement : varElements) {
703                            String varLocatorKey = varElement.attributeValue("locator-key");
704                            String varPath = varElement.attributeValue("path");
705    
706                            if (Validator.isNotNull(varLocatorKey) &&
707                                    Validator.isNotNull(varPath)) {
708    
709                                    if (!_pathRootElements.containsKey(varPath)) {
710                                            _seleniumBuilderFileUtil.throwValidationException(
711                                                    1014, fileName, varElement, varPath);
712                                    }
713    
714                                    if (!_isValidLocatorKey(varPath, null, varLocatorKey)) {
715                                            _seleniumBuilderFileUtil.throwValidationException(
716                                                    1010, fileName, varElement, varLocatorKey);
717                                    }
718                            }
719                    }
720            }
721    
722            private String _getClassName(String fileName) {
723                    return _seleniumBuilderFileUtil.getClassName(fileName);
724            }
725    
726            private String _getClassName(String fileName, String classSuffix) {
727                    return _seleniumBuilderFileUtil.getClassName(fileName, classSuffix);
728            }
729    
730            private String _getHTMLFileName(String fileName) {
731                    return _seleniumBuilderFileUtil.getHTMLFileName(fileName);
732            }
733    
734            private String _getJavaFileName(String fileName) {
735                    return _seleniumBuilderFileUtil.getJavaFileName(fileName);
736            }
737    
738            private String _getJavaFileName(String fileName, String classSuffix) {
739                    return _seleniumBuilderFileUtil.getJavaFileName(fileName, classSuffix);
740            }
741    
742            private int _getLocatorCount(Element rootElement) throws Exception {
743                    return _seleniumBuilderFileUtil.getLocatorCount(rootElement);
744            }
745    
746            private String _getName(String fileName) {
747                    return _seleniumBuilderFileUtil.getName(fileName);
748            }
749    
750            private String _getPackageName(String fileName) {
751                    return _seleniumBuilderFileUtil.getPackageName(fileName);
752            }
753    
754            private String _getReturnType(String name) throws Exception {
755                    return _seleniumBuilderFileUtil.getReturnType(name);
756            }
757    
758            private Element _getRootElement(String fileName) throws Exception {
759                    return _seleniumBuilderFileUtil.getRootElement(fileName);
760            }
761    
762            private String _getSimpleClassName(String fileName) {
763                    return _seleniumBuilderFileUtil.getSimpleClassName(fileName);
764            }
765    
766            private String _getSimpleClassName(String fileName, String classSuffix) {
767                    return _seleniumBuilderFileUtil.getSimpleClassName(
768                            fileName, classSuffix);
769            }
770    
771            private Set<String> _getTestCaseCommandNames(Element rootElement) {
772                    List<Element> commandElements =
773                            _seleniumBuilderFileUtil.getAllChildElements(
774                                    rootElement, "command");
775    
776                    Set<String> commandNames = new HashSet<String>();
777    
778                    for (Element commandElement : commandElements) {
779                            commandNames.add(commandElement.attributeValue("name"));
780                    }
781    
782                    return commandNames;
783            }
784    
785            private boolean _isActionName(String name) {
786                    for (String actionName : _actionNames) {
787                            if (actionName.equals(name)) {
788                                    return true;
789                            }
790                    }
791    
792                    return false;
793            }
794    
795            private boolean _isFunctionCommand(String name, String command) {
796                    if (!_isFunctionName(name)) {
797                            return false;
798                    }
799    
800                    Element rootElement = getFunctionRootElement(name);
801    
802                    List<Element> commandElements =
803                            _seleniumBuilderFileUtil.getAllChildElements(
804                                    rootElement, "command");
805    
806                    for (Element commandElement : commandElements) {
807                            String commandName = commandElement.attributeValue("name");
808    
809                            if (commandName.equals(command)) {
810                                    return true;
811                            }
812                    }
813    
814                    return false;
815            }
816    
817            private boolean _isFunctionName(String name) {
818                    for (String functionName : _functionNames) {
819                            if (functionName.equals(StringUtil.upperCaseFirstLetter(name))) {
820                                    return true;
821                            }
822                    }
823    
824                    return false;
825            }
826    
827            private boolean _isMacroCommand(String name, String command) {
828                    if (!_isMacroName(name)) {
829                            return false;
830                    }
831    
832                    Element rootElement = getMacroRootElement(name);
833    
834                    List<Element> commandElements =
835                            _seleniumBuilderFileUtil.getAllChildElements(
836                                    rootElement, "command");
837    
838                    for (Element commandElement : commandElements) {
839                            String commandName = commandElement.attributeValue("name");
840    
841                            if (commandName.equals(command)) {
842                                    return true;
843                            }
844                    }
845    
846                    return false;
847            }
848    
849            private boolean _isMacroName(String name) {
850                    for (String macroName : _macroNames) {
851                            if (macroName.equals(name)) {
852                                    return true;
853                            }
854                    }
855    
856                    return false;
857            }
858    
859            private boolean _isSeleniumCommand(String command) {
860                    if (_seleniumParameterCounts.containsKey(command)) {
861                            return true;
862                    }
863    
864                    return false;
865            }
866    
867            private boolean _isValidLocatorKey(
868                    String actionName, String caseComparator, String locatorKey) {
869    
870                    Element pathRootElement = getPathRootElement(actionName);
871    
872                    Set<String> pathLocatorKeys = getPathLocatorKeys(pathRootElement);
873    
874                    String[] partialKeys = {};
875    
876                    if (locatorKey.contains("${") && locatorKey.contains("}")) {
877                            caseComparator = "partial";
878    
879                            partialKeys = locatorKey.split("\\$\\{[^}]*?\\}");
880                    }
881    
882                    for (String pathLocatorKey : pathLocatorKeys) {
883                            if (caseComparator == null) {
884                                    if (pathLocatorKey.equals(locatorKey)) {
885                                            return true;
886                                    }
887                            }
888                            else {
889                                    if (caseComparator.equals("contains") &&
890                                            pathLocatorKey.contains(locatorKey)) {
891    
892                                            return true;
893                                    }
894                                    else if (caseComparator.equals("endsWith") &&
895                                                     pathLocatorKey.endsWith(locatorKey)) {
896    
897                                            return true;
898                                    }
899                                    else if (caseComparator.equals("partial")) {
900                                            boolean containsAll = true;
901    
902                                            for (String s : partialKeys) {
903                                                    if (!pathLocatorKey.contains(s)) {
904                                                            containsAll = false;
905                                                    }
906                                            }
907    
908                                            if (containsAll) {
909                                                    return true;
910                                            }
911                                    }
912                                    else if (caseComparator.equals("startsWith") &&
913                                                     pathLocatorKey.startsWith(locatorKey)) {
914    
915                                            return true;
916                                    }
917                            }
918                    }
919    
920                    return false;
921            }
922    
923            private String _normalizeFileName(String fileName) {
924                    return _seleniumBuilderFileUtil.normalizeFileName(fileName);
925            }
926    
927            private void _validateActionElement(String fileName, Element element) {
928                    String action = element.attributeValue("action");
929    
930                    int x = action.indexOf(StringPool.POUND);
931    
932                    if (x == -1) {
933                            _seleniumBuilderFileUtil.throwValidationException(
934                                    1006, fileName, element, "action");
935                    }
936    
937                    String actionName = action.substring(0, x);
938    
939                    if (!_isActionName(actionName)) {
940                            _seleniumBuilderFileUtil.throwValidationException(
941                                    1011, fileName, element, "action", actionName);
942                    }
943    
944                    String actionCommand = action.substring(x + 1);
945    
946                    if (!_isFunctionName(actionCommand)) {
947                            _seleniumBuilderFileUtil.throwValidationException(
948                                    1012, fileName, element, "action", actionCommand);
949                    }
950    
951                    _validateLocatorKeyElement(fileName, actionName, element);
952            }
953    
954            private void _validateFunctionElement(String fileName, Element element) {
955                    String function = element.attributeValue("function");
956    
957                    int x = function.indexOf(StringPool.POUND);
958    
959                    if (x == -1) {
960                            _seleniumBuilderFileUtil.throwValidationException(
961                                    1006, fileName, element, "function");
962                    }
963    
964                    String functionName = function.substring(0, x);
965    
966                    if (!_isFunctionName(functionName)) {
967                            _seleniumBuilderFileUtil.throwValidationException(
968                                    1011, fileName, element, "function", functionName);
969                    }
970    
971                    String functionCommand = function.substring(x + 1);
972    
973                    if (!_isFunctionCommand(functionName, functionCommand)) {
974                            _seleniumBuilderFileUtil.throwValidationException(
975                                    1012, fileName, element, "function", functionCommand);
976                    }
977            }
978    
979            private void _validateLocatorKeyElement(
980                    String fileName, String actionName, Element element) {
981    
982                    String comparator = element.attributeValue("comparator");
983    
984                    List<Attribute> attributes = element.attributes();
985    
986                    for (Attribute attribute : attributes) {
987                            String attributeName = attribute.getName();
988    
989                            if (attributeName.startsWith("locator-key")) {
990                                    String attributeValue = attribute.getValue();
991    
992                                    if (!_isValidLocatorKey(
993                                                    actionName, comparator, attributeValue)) {
994    
995                                            _seleniumBuilderFileUtil.throwValidationException(
996                                                    1010, fileName, element, attributeValue);
997                                    }
998                            }
999                    }
1000            }
1001    
1002            private void _validateMacroElement(String fileName, Element element) {
1003                    String macro = element.attributeValue("macro");
1004    
1005                    int x = macro.indexOf(StringPool.POUND);
1006    
1007                    if (x == -1) {
1008                            _seleniumBuilderFileUtil.throwValidationException(
1009                                    1006, fileName, element, "macro");
1010                    }
1011    
1012                    String macroName = macro.substring(0, x);
1013    
1014                    if (!_isMacroName(macroName)) {
1015                            _seleniumBuilderFileUtil.throwValidationException(
1016                                    1011, fileName, element, "macro", macroName);
1017                    }
1018    
1019                    String macroCommand = macro.substring(x + 1);
1020    
1021                    if (!_isMacroCommand(macroName, macroCommand)) {
1022                            _seleniumBuilderFileUtil.throwValidationException(
1023                                    1012, fileName, element, "macro", macroCommand);
1024                    }
1025            }
1026    
1027            private void _validateSeleniumElement(String fileName, Element element) {
1028                    String selenium = element.attributeValue("selenium");
1029    
1030                    if (!_isSeleniumCommand(selenium)) {
1031                            _seleniumBuilderFileUtil.throwValidationException(
1032                                    1012, fileName, element, "selenium", selenium);
1033                    }
1034            }
1035    
1036            private static Pattern _pattern = Pattern.compile(
1037                    "public [a-z]* [A-Za-z0-9_]*\\(.*?\\)");
1038    
1039            private Map<String, String> _actionClassNames =
1040                    new HashMap<String, String>();
1041            private Map<String, String> _actionFileNames =
1042                    new HashMap<String, String>();
1043            private Map<String, String> _actionJavaFileNames =
1044                    new HashMap<String, String>();
1045            private Set<String> _actionNames = new HashSet<String>();
1046            private Map<String, String> _actionPackageNames =
1047                    new HashMap<String, String>();
1048            private Map<String, Element> _actionRootElements =
1049                    new HashMap<String, Element>();
1050            private Map<String, String> _actionSimpleClassNames =
1051                    new HashMap<String, String>();
1052            private String _baseDir;
1053            private Map<String, String> _functionClassNames =
1054                    new HashMap<String, String>();
1055            private Map<String, String> _functionFileNames =
1056                    new HashMap<String, String>();
1057            private Map<String, String> _functionJavaFileNames =
1058                    new HashMap<String, String>();
1059            private Map<String, Integer> _functionLocatorCounts =
1060                    new HashMap<String, Integer>();
1061            private Set<String> _functionNames = new HashSet<String>();
1062            private Map<String, String> _functionPackageNames =
1063                    new HashMap<String, String>();
1064            private Map<String, String> _functionReturnTypes =
1065                    new HashMap<String, String>();
1066            private Map<String, Element> _functionRootElements =
1067                    new HashMap<String, Element>();
1068            private Map<String, String> _functionSimpleClassNames =
1069                    new HashMap<String, String>();
1070            private Map<String, String> _macroClassNames =
1071                    new HashMap<String, String>();
1072            private Map<String, String> _macroFileNames = new HashMap<String, String>();
1073            private Map<String, String> _macroJavaFileNames =
1074                    new HashMap<String, String>();
1075            private Set<String> _macroNames = new HashSet<String>();
1076            private Map<String, String> _macroPackageNames =
1077                    new HashMap<String, String>();
1078            private Map<String, Element> _macroRootElements =
1079                    new HashMap<String, Element>();
1080            private Map<String, String> _macroSimpleClassNames =
1081                    new HashMap<String, String>();
1082            private Map<String, String> _pathClassNames = new HashMap<String, String>();
1083            private Map<String, String> _pathFileNames = new HashMap<String, String>();
1084            private Map<String, String> _pathJavaFileNames =
1085                    new HashMap<String, String>();
1086            private Set<String> _pathNames = new HashSet<String>();
1087            private Map<String, String> _pathPackageNames =
1088                    new HashMap<String, String>();
1089            private Map<String, Element> _pathRootElements =
1090                    new HashMap<String, Element>();
1091            private Map<String, String> _pathSimpleClassNames =
1092                    new HashMap<String, String>();
1093            private SeleniumBuilderFileUtil _seleniumBuilderFileUtil;
1094            private Map<String, Integer> _seleniumParameterCounts =
1095                    new HashMap<String, Integer>();
1096            private Map<String, String> _testCaseClassNames =
1097                    new HashMap<String, String>();
1098            private Map<String, Set<String>> _testCaseCommandNames =
1099                    new HashMap<String, Set<String>>();
1100            private Map<String, String> _testCaseFileNames =
1101                    new HashMap<String, String>();
1102            private Map<String, String> _testCaseHTMLFileNames =
1103                    new HashMap<String, String>();
1104            private Map<String, String> _testCaseJavaFileNames =
1105                    new HashMap<String, String>();
1106            private Set<String> _testCaseNames = new HashSet<String>();
1107            private Map<String, String> _testCasePackageNames =
1108                    new HashMap<String, String>();
1109            private Map<String, Element> _testCaseRootElements =
1110                    new HashMap<String, Element>();
1111            private Map<String, String> _testCaseSimpleClassNames =
1112                    new HashMap<String, String>();
1113    
1114    }