View Javadoc
1   package com.github.searls.jasmine.io.scripts;
2   
3   import java.util.LinkedHashSet;
4   import java.util.Set;
5   
6   public class ContextPathScriptResolver implements ScriptResolver {
7   
8     private final ScriptResolver scriptResolver;
9     private final String baseContextPath;
10    private final String sourceContextPath;
11    private final String specContextPath;
12  
13    public ContextPathScriptResolver(ScriptResolver scriptResolver,
14                                     String sourceContextPath,
15                                     String specContextPath) {
16      this(scriptResolver, "", sourceContextPath, specContextPath);
17    }
18  
19    public ContextPathScriptResolver(ScriptResolver scriptResolver,
20                                     String baseContextPath,
21                                     String sourceContextPath,
22                                     String specContextPath) {
23      this.scriptResolver = scriptResolver;
24      this.baseContextPath = baseContextPath;
25      this.sourceContextPath = sourceContextPath;
26      this.specContextPath = specContextPath;
27    }
28  
29    @Override
30    public String getSourceDirectory() {
31      return this.sourceContextPath;
32    }
33  
34    @Override
35    public String getSpecDirectory() {
36      return this.specContextPath;
37    }
38  
39    @Override
40    public String getBaseDirectory() {
41      return this.baseContextPath;
42    }
43  
44    @Override
45    public Set<String> getSources() throws ScriptResolverException {
46      return relativeToContextPath(
47        this.scriptResolver.getSourceDirectory(),
48        this.sourceContextPath,
49        this.scriptResolver.getSources());
50    }
51  
52    @Override
53    public Set<String> getSpecs() throws ScriptResolverException {
54      return relativeToContextPath(
55        this.scriptResolver.getSpecDirectory(),
56        this.specContextPath,
57        this.scriptResolver.getSpecs());
58    }
59  
60    @Override
61    public Set<String> getPreloads() throws ScriptResolverException {
62      Set<String> scripts = this.scriptResolver.getPreloads();
63      scripts = relativeToContextPath(
64        this.scriptResolver.getSourceDirectory(),
65        this.sourceContextPath,
66        scripts);
67      scripts = relativeToContextPath(
68        this.scriptResolver.getSpecDirectory(),
69        this.specContextPath,
70        scripts);
71      scripts = relativeToContextPath(
72        this.scriptResolver.getBaseDirectory(),
73        this.baseContextPath,
74        scripts);
75      return scripts;
76    }
77  
78    @Override
79    public Set<String> getAllScripts() throws ScriptResolverException {
80      LinkedHashSet<String> allScripts = new LinkedHashSet<String>();
81      allScripts.addAll(this.getPreloads());
82      allScripts.addAll(this.getSources());
83      allScripts.addAll(this.getSpecs());
84      return allScripts;
85    }
86  
87    private Set<String> relativeToContextPath(String realPath, String contextPath, Set<String> absoluteScripts) {
88      Set<String> relativeScripts = new LinkedHashSet<String>();
89      for (String absoluteScript : absoluteScripts) {
90        relativeScripts.add(absoluteScript.replace(realPath, contextPath));
91      }
92      return relativeScripts;
93    }
94  }