Clover icon

magnolia-module-groovy 2.4.2

  1. Project Clover database Thu Jan 14 2016 16:38:54 CET
  2. Package info.magnolia.module.groovy.validator

File GroovyValidator.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart9.png
10% of files have more coverage

Code metrics

10
18
3
1
91
44
11
0.61
6
3
3.67

Classes

Class Line # Actions
GroovyValidator 51 18 0% 11 5
0.8387096583.9%
 

Contributing tests

This file is covered by 5 tests. .

Source view

1    /**
2    * This file Copyright (c) 2014-2015 Magnolia International
3    * Ltd. (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10    * This file is distributed in the hope that it will be
11    * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12    * implied warranty of MERCHANTABILITY or FITNESS FOR A
13    * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14    * Redistribution, except as permitted by whichever of the GPL
15    * or MNA you select, is prohibited.
16    *
17    * 1. For the GPL license (GPL), you can redistribute and/or
18    * modify this file under the terms of the GNU General
19    * Public License, Version 3, as published by the Free Software
20    * Foundation. You should have received a copy of the GNU
21    * General Public License, Version 3 along with this program;
22    * if not, write to the Free Software Foundation, Inc., 51
23    * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24    *
25    * 2. For the Magnolia Network Agreement (MNA), this file
26    * and the accompanying materials are made available under the
27    * terms of the MNA which accompanies this distribution, and
28    * is available at http://www.magnolia-cms.com/mna.html
29    *
30    * Any modifications to this file must keep this entire header
31    * intact.
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    * Validates the Groovy source code.
50    */
 
51    public class GroovyValidator extends AbstractStringValidator {
52    private final static MgnlGroovyClassLoader CL_TO_VERIFY_SOURCE = new MgnlGroovyClassLoader(new UserContextHierarchyManagerProvider());
53    private Item item;
54   
 
55  5 toggle public GroovyValidator(Item item, String errorMessage) {
56  5 super(errorMessage);
57  5 this.item = item;
58    }
59   
 
60  5 toggle @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   
 
76  5 toggle 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    }