Changeset 4780
- Timestamp:
- 07/02/08 14:10:29 (2 months ago)
- Files:
-
- openmrs-modules/groovyforms/src/org/openmrs/module/groovyforms/util/GroovyFormsClassUtil.groovy (modified) (2 diffs)
- openmrs-modules/groovyforms/test/org/openmrs/module/groovyforms/GroovyFormClassUtilTest.groovy (copied) (copied from openmrs-modules/groovyforms/test/org/openmrs/module/groovyforms/GroovyClassUtilTest.groovy) (5 diffs)
- openmrs-modules/groovyforms/web/src/org/openmrs/module/groovyforms/web/CreateGroovyFormServlet.groovy (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
openmrs-modules/groovyforms/src/org/openmrs/module/groovyforms/util/GroovyFormsClassUtil.groovy
r4746 r4780 15 15 16 16 import org.apache.commons.logging.LogFactory 17 import org.codehaus.groovy.control.CompilationFailedException 17 18 import org.openmrs.module.groovyforms.metadata.model.GroovyFormsDomainModelMetaData 19 import org.openmrs.util.OpenmrsClassLoader 18 20 19 21 /* … … 74 76 f 75 77 } 78 static def getClassLoader() { 79 def gcl = new GroovyClassLoader(OpenmrsClassLoader.getInstance()) 80 gcl 81 } 82 83 /** 84 * This method is used to relay errors to the user 85 * @param clazz the class 86 * @return the exception message or null if it was successful 87 */ 88 static def checkSyntax(clazz) { 89 def sb = new StringBuilder() 90 sb << "import org.openmrs.*\n\n\n" 91 sb << clazz 92 def res = null 93 try { 94 getClassLoader().parseClass(sb.toString()) 95 96 } catch (CompilationFailedException e) { 97 res = "Exception: ${e.message}" 98 } 99 res 100 101 } 102 103 /** 104 * Check if it is result groovy code. 105 * @param clazz the class 106 * @return whether or not it is result groovy code 107 */ 108 static def isresultGroovy(clazz) { 109 def sb = new StringBuilder() 110 sb << "import org.openmrs.*\n\n\n" 111 sb << clazz 112 try { 113 getClassLoader().parseClass(sb.toString()) 114 } catch (CompilationFailedException e) { 115 return false 116 } 117 return true 118 } 76 119 } openmrs-modules/groovyforms/test/org/openmrs/module/groovyforms/GroovyFormClassUtilTest.groovy
r4746 r4780 14 14 package org.openmrs.module.groovyforms 15 15 16 import org.openmrs.module.groovyforms.util.GroovyFormsClassUtil as GCU17 18 16 import static org.junit.Assert.assertEquals 19 17 import org.junit.Test … … 21 19 import org.openmrs.Patient 22 20 import org.openmrs.module.groovyforms.metadata.model.GroovyFormsDomainModelMetaData 21 import org.openmrs.module.groovyforms.util.GroovyFormsClassUtil 23 22 24 class GroovyClassUtilTest { 23 24 class GroovyFormsClassUtilTest { 25 25 26 26 27 27 @Test 28 28 void testProcessClass() { 29 def model = G CU.getProperties(new Foo())29 def model = GroovyFormsClassUtil.getProperties(new Foo()) 30 30 GroovyFormsDomainModelMetaData bar = model[0] 31 31 GroovyFormsDomainModelMetaData x = model[1] … … 42 42 @Test 43 43 void testNoPropertiesFound() { 44 def model = G CU.getProperties(new Bar())44 def model = GroovyFormsClassUtil.getProperties(new Bar()) 45 45 assertEquals(model, null); 46 46 } … … 48 48 @Test 49 49 void testValidClass() { 50 assertTrue G CU.isValidGroovy('class Form { String str }')50 assertTrue GroovyFormsClassUtil.isValidGroovy('class Form { String str }') 51 51 } 52 52 53 53 @Test 54 54 void testValidClassWithOpenMRSModelClasses() { 55 assertTrue G CU.isValidGroovy("class MyForm { Patient p; Concept c }")55 assertTrue GroovyFormsClassUtil.isValidGroovy("class MyForm { Patient p; Concept c }") 56 56 } 57 57 … … 59 59 void testInvalidClass() { 60 60 // no closing semi-colon 61 assertFalse G CU.isValidGroovy("class Form { ")61 assertFalse GroovyFormsClassUtil.isValidGroovy("class Form { ") 62 62 63 63 // no semi-colon whatsoever 64 assertFalse G CU.isValidGroovy("class Foo")64 assertFalse GroovyFormsClassUtil.isValidGroovy("class Foo") 65 65 66 66 // missing name for field 67 assertFalse G CU.isValidGroovy("class Foo { String } ")67 assertFalse GroovyFormsClassUtil.isValidGroovy("class Foo { String } ") 68 68 69 69 } openmrs-modules/groovyforms/web/src/org/openmrs/module/groovyforms/web/CreateGroovyFormServlet.groovy
r4764 r4780 19 19 import javax.servlet.http.HttpServletResponse 20 20 import org.apache.commons.logging.LogFactory 21 import org. codehaus.groovy.control.CompilationFailedException21 import org.openmrs.module.groovyforms.util.GroovyFormsClassUtil 22 22 23 23 class CreateGroovyFormServlet extends HttpServlet { 24 def shell25 24 static final def log = LogFactory.getLog(CreateGroovyFormServlet.class) 26 privatestatic final long serialVersionUID = 066373513262051L25 static final long serialVersionUID = 066373513262051L 27 26 28 27 @Override … … 34 33 def name = request.getParameter("formName") 35 34 def version = request.getParameter("version") 36 def res = this.checkSyntax(clazz)35 def res = GroovyFormsClassUtil.checkSyntax(clazz) 37 36 if (clazz) { 38 if ( checkSyntax(clazz)) {37 if (res) { 39 38 response.contentType = "text/xml" 40 39 response.setHeader "Cache-Conrol", "no-cache" … … 61 60 if (log.infoEnabled) 62 61 log.info("Initializing...") 63 shell = getClassLoader() 64 } 65 66 def getClassLoader() { 67 def gcl = new GroovyClassLoader(this.getClass().getClassLoader()) 68 gcl 69 } 70 71 /** 72 * This method is used to relay errors to the user 73 * @oaram clazz the class 74 * @return the exception message or null if it was successful 75 */ 76 def checkSyntax(clazz) { 77 def sb = new StringBuilder() 78 sb << "import org.openmrs.*\n\n\n" 79 sb << clazz 80 def res = null 81 try { 82 getClassLoader().parseClass(sb.toString()) 83 84 } catch (CompilationFailedException e) { 85 res = "Exception: ${e.message}" 86 } 87 res 88 89 } 90 91 /** 92 * Check if it is result groovy code. 93 * @param clazz the class 94 * @return whether or not it is result groovy code 95 */ 96 def isresultGroovy(clazz) { 97 def sb = new StringBuilder() 98 sb << "import org.openmrs.*\n\n\n" 99 sb << clazz 100 try { 101 getClassLoader().parseClass(sb.toString()) 102 } catch (CompilationFailedException e) { 103 return false 104 } 105 return true 62 classLoader = GroovyFormsClassUtil.getClassLoader() 106 63 } 107 64 }