1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
package info.magnolia.module.groovy.validator; |
35 |
|
|
36 |
|
import info.magnolia.module.groovy.support.HierarchyManagerProvider.UserContextHierarchyManagerProvider; |
37 |
|
import info.magnolia.module.groovy.support.classes.MgnlGroovyClassLoader; |
38 |
|
import info.magnolia.ui.vaadin.integration.jcr.JcrNodeAdapter; |
39 |
|
|
40 |
|
import javax.jcr.RepositoryException; |
41 |
|
|
42 |
|
import org.apache.commons.lang3.StringUtils; |
43 |
|
import org.codehaus.groovy.control.CompilationFailedException; |
44 |
|
|
45 |
|
import com.vaadin.data.Item; |
46 |
|
import com.vaadin.data.validator.AbstractStringValidator; |
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
|
|
| 83.9% |
Uncovered Elements: 5 (31) |
Complexity: 11 |
Complexity Density: 0.61 |
|
51 |
|
public class GroovyValidator extends AbstractStringValidator { |
52 |
|
private final static MgnlGroovyClassLoader CL_TO_VERIFY_SOURCE = new MgnlGroovyClassLoader(new UserContextHierarchyManagerProvider()); |
53 |
|
private Item item; |
54 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
55 |
5 |
public GroovyValidator(Item item, String errorMessage) {... |
56 |
5 |
super(errorMessage); |
57 |
5 |
this.item = item; |
58 |
|
} |
59 |
|
|
|
|
| 66.7% |
Uncovered Elements: 4 (12) |
Complexity: 5 |
Complexity Density: 0.62 |
|
60 |
5 |
@Override... |
61 |
|
protected boolean isValidValue(String value) { |
62 |
5 |
if (StringUtils.isBlank(value)) { |
63 |
0 |
return false; |
64 |
|
} |
65 |
5 |
try { |
66 |
5 |
verifySource(value, (JcrNodeAdapter) item); |
67 |
3 |
return true; |
68 |
|
} catch (CompilationFailedException e) { |
69 |
2 |
setErrorMessage("Compilation error: " + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage())); |
70 |
|
} catch (RepositoryException e) { |
71 |
0 |
setErrorMessage(e.getMessage()); |
72 |
|
} |
73 |
2 |
return false; |
74 |
|
} |
75 |
|
|
|
|
| 92.9% |
Uncovered Elements: 1 (14) |
Complexity: 5 |
Complexity Density: 0.62 |
|
76 |
5 |
protected void verifySource(final String source, final JcrNodeAdapter item) throws RepositoryException, CompilationFailedException {... |
77 |
5 |
final com.vaadin.data.Property<Boolean> scriptProperty = item.getItemProperty("script"); |
78 |
5 |
final boolean isScript = scriptProperty != null && scriptProperty.getValue() != null ? scriptProperty.getValue() : false; |
79 |
|
|
80 |
5 |
if (isScript) { |
81 |
1 |
CL_TO_VERIFY_SOURCE.verify(source, false, null); |
82 |
|
} else { |
83 |
4 |
String path = item.getJcrItem().getPath(); |
84 |
|
|
85 |
4 |
String nodeName = MgnlGroovyClassLoader.resolveScriptNameFromSource(source); |
86 |
4 |
path = path.endsWith("/") ? path + nodeName : StringUtils.substringBeforeLast(path, "/") + "/" + nodeName; |
87 |
|
|
88 |
4 |
CL_TO_VERIFY_SOURCE.verify(source, true, path); |
89 |
|
} |
90 |
|
} |
91 |
|
} |