[Swift-commit] r6387 - in branches/release-0.94/src/org/griphyn/vdl: engine mapping mapping/file
hategan at ci.uchicago.edu
hategan at ci.uchicago.edu
Sun Mar 17 02:49:58 CDT 2013
Author: hategan
Date: 2013-03-17 02:49:58 -0500 (Sun, 17 Mar 2013)
New Revision: 6387
Modified:
branches/release-0.94/src/org/griphyn/vdl/engine/Karajan.java
branches/release-0.94/src/org/griphyn/vdl/mapping/AbstractMapper.java
branches/release-0.94/src/org/griphyn/vdl/mapping/Mapper.java
branches/release-0.94/src/org/griphyn/vdl/mapping/MapperFactory.java
branches/release-0.94/src/org/griphyn/vdl/mapping/file/AbstractFileMapper.java
branches/release-0.94/src/org/griphyn/vdl/mapping/file/ArrayFileMapper.java
branches/release-0.94/src/org/griphyn/vdl/mapping/file/CSVMapper.java
branches/release-0.94/src/org/griphyn/vdl/mapping/file/ConcurrentMapper.java
branches/release-0.94/src/org/griphyn/vdl/mapping/file/ExternalMapper.java
branches/release-0.94/src/org/griphyn/vdl/mapping/file/FixedArrayFileMapper.java
branches/release-0.94/src/org/griphyn/vdl/mapping/file/RegularExpressionMapper.java
branches/release-0.94/src/org/griphyn/vdl/mapping/file/SimpleFileMapper.java
branches/release-0.94/src/org/griphyn/vdl/mapping/file/SingleFileMapper.java
branches/release-0.94/src/org/griphyn/vdl/mapping/file/StructuredRegularExpressionMapper.java
branches/release-0.94/src/org/griphyn/vdl/mapping/file/TestMapper.java
Log:
added compile-time validation of mapper parameters
Modified: branches/release-0.94/src/org/griphyn/vdl/engine/Karajan.java
===================================================================
--- branches/release-0.94/src/org/griphyn/vdl/engine/Karajan.java 2013-03-17 07:47:59 UTC (rev 6386)
+++ branches/release-0.94/src/org/griphyn/vdl/engine/Karajan.java 2013-03-17 07:49:58 UTC (rev 6387)
@@ -64,6 +64,7 @@
import org.griphyn.vdl.engine.VariableScope.WriteType;
import org.griphyn.vdl.karajan.CompilationException;
import org.griphyn.vdl.karajan.Loader;
+import org.griphyn.vdl.mapping.MapperFactory;
import org.griphyn.vdl.toolkit.VDLt2VDLx;
import org.griphyn.vdl.type.NoSuchTypeException;
import org.safehaus.uuid.UUIDGenerator;
@@ -465,7 +466,9 @@
if (mapping != null) {
StringTemplate mappingST = new StringTemplate("mapping");
- mappingST.setAttribute("descriptor", mapping.getDescriptor());
+ String mapperType = mapping.getDescriptor();
+ mappingST.setAttribute("descriptor", mapperType);
+ checkMapperParams(mapperType, mapping);
for (int i = 0; i < mapping.sizeOfParamArray(); i++) {
Param param = mapping.getParamArray(i);
mappingST.setAttribute("params", mappingParameter(param, scope));
@@ -491,6 +494,27 @@
scope.bodyTemplate.setAttribute("declarations", variableST);
}
+ private void checkMapperParams(String mapperType, Mapping mapping) throws CompilationException {
+ if (!MapperFactory.isValidMapperType(mapperType)) {
+ throw new CompilationException("Unknown mapper type: '" + mapperType + "'");
+ }
+
+ Set<String> validParams = MapperFactory.getValidParams(mapperType);
+ if (validParams == null && mapping.sizeOfParamArray() > 0) {
+ throw new CompilationException(mapperType + " does not support any parameters");
+ }
+ if (validParams.contains("*")) {
+ // mapper accepts any parameter (e.g. external_mapper)
+ return;
+ }
+ for (int i = 0; i < mapping.sizeOfParamArray(); i++) {
+ Param param = mapping.getParamArray(i);
+ if (!validParams.contains(param.getName())) {
+ throw new CompilationException(mapperType + " does not support a '" + param.getName() + "' parameter");
+ }
+ }
+ }
+
private StringTemplate mappingParameter(Param param, VariableScope scope) throws CompilationException {
StringTemplate paramST = template("vdl_parameter");
paramST.setAttribute("name", param.getName());
Modified: branches/release-0.94/src/org/griphyn/vdl/mapping/AbstractMapper.java
===================================================================
--- branches/release-0.94/src/org/griphyn/vdl/mapping/AbstractMapper.java 2013-03-17 07:47:59 UTC (rev 6386)
+++ branches/release-0.94/src/org/griphyn/vdl/mapping/AbstractMapper.java 2013-03-17 07:49:58 UTC (rev 6387)
@@ -17,6 +17,9 @@
package org.griphyn.vdl.mapping;
+import java.util.HashSet;
+import java.util.Set;
+
import org.apache.log4j.Logger;
import org.griphyn.vdl.mapping.file.FileGarbageCollector;
@@ -30,8 +33,26 @@
public static final Logger logger = Logger.getLogger(AbstractMapper.class);
public static final MappingParam PARAM_INPUT = new MappingParam("input", Boolean.FALSE);
- protected MappingParamSet params;
+
+ protected void getValidMappingParams(Set<String> s) {
+ addParams(s, PARAM_INPUT);
+ }
+
+ protected void addParams(Set<String> s, MappingParam... params) {
+ for (MappingParam p : params) {
+ s.add(p.getName());
+ }
+ }
+
+ @Override
+ public final Set<String> getSupportedParamNames() {
+ Set<String> s = new HashSet<String>();
+ getValidMappingParams(s);
+ return s;
+ }
+ protected MappingParamSet params;
+
public synchronized void setParam(MappingParam param, Object value) {
if (params == null) {
params = new MappingParamSet();
Modified: branches/release-0.94/src/org/griphyn/vdl/mapping/Mapper.java
===================================================================
--- branches/release-0.94/src/org/griphyn/vdl/mapping/Mapper.java 2013-03-17 07:47:59 UTC (rev 6386)
+++ branches/release-0.94/src/org/griphyn/vdl/mapping/Mapper.java 2013-03-17 07:49:58 UTC (rev 6387)
@@ -18,6 +18,7 @@
package org.griphyn.vdl.mapping;
import java.util.Collection;
+import java.util.Set;
/** This interface must be implemented by a Java class that represents
a Swift mapper between SwiftScript variables and external data
@@ -80,4 +81,6 @@
boolean isPersistent(Path path);
+
+ Set<String> getSupportedParamNames();
}
Modified: branches/release-0.94/src/org/griphyn/vdl/mapping/MapperFactory.java
===================================================================
--- branches/release-0.94/src/org/griphyn/vdl/mapping/MapperFactory.java 2013-03-17 07:47:59 UTC (rev 6386)
+++ branches/release-0.94/src/org/griphyn/vdl/mapping/MapperFactory.java 2013-03-17 07:49:58 UTC (rev 6387)
@@ -22,6 +22,7 @@
import java.util.HashMap;
import java.util.Map;
+import java.util.Set;
import org.griphyn.vdl.mapping.file.AirsnMapper;
import org.griphyn.vdl.mapping.file.ArrayFileMapper;
@@ -38,11 +39,14 @@
import org.griphyn.vdl.mapping.file.TestMapper;
public class MapperFactory {
- private static Map<String,Class<Mapper>> mappers =
- new HashMap<String,Class<Mapper>>();
+ private static Map<String, Class<? extends Mapper>> mappers;
+
+ private static Map<String, Set<String>> validParams;
static {
-
+ mappers = new HashMap<String, Class<? extends Mapper>>();
+ validParams = new HashMap<String, Set<String>>();
+
// the following are general purpose file mappers
registerMapper("simple_mapper", SimpleFileMapper.class);
registerMapper("single_file_mapper", SingleFileMapper.class);
@@ -62,11 +66,10 @@
registerMapper("test_mapper", TestMapper.class);
}
- public synchronized static Mapper getMapper(String type, MappingParamSet params)
- throws InvalidMapperException {
- Class<Mapper> cls = mappers.get(type);
+ public synchronized static Mapper getMapper(String type, MappingParamSet params) throws InvalidMapperException {
+ Class<? extends Mapper> cls = mappers.get(type);
if (cls == null) {
- throw new InvalidMapperException("no such mapper: "+type);
+ throw new InvalidMapperException("No such mapper: "+type);
}
try {
Mapper mapper = cls.newInstance();
@@ -77,15 +80,29 @@
throw new InvalidMapperException(type + ": " + e.getMessage(), e);
}
}
+
+ public synchronized static boolean isValidMapperType(String type) {
+ return mappers.containsKey(type);
+ }
+
+ @SuppressWarnings("unchecked")
+ public static void registerMapper(String type, String cls) throws ClassNotFoundException {
+ registerMapper(type, (Class<? extends Mapper>) MapperFactory.class.getClassLoader().loadClass(cls));
+ }
- public static void registerMapper(String type, String cls)
- throws ClassNotFoundException {
- registerMapper(type, MapperFactory.class.getClassLoader().loadClass(cls));
+ public synchronized static void registerMapper(String type, Class<? extends Mapper> cls) {
+ mappers.put(type, cls);
+ try {
+ Mapper m = cls.newInstance();
+ validParams.put(type, m.getSupportedParamNames());
+ }
+ catch (Exception e) {
+ throw new RuntimeException("Cannot instantiate a '" + type + "'", e);
+ }
}
- @SuppressWarnings("unchecked")
- public synchronized static void registerMapper(String type,
- Class<?> cls) {
- mappers.put(type, (Class<Mapper>) cls);
- }
+
+ public static Set<String> getValidParams(String type) {
+ return validParams.get(type);
+ }
}
Modified: branches/release-0.94/src/org/griphyn/vdl/mapping/file/AbstractFileMapper.java
===================================================================
--- branches/release-0.94/src/org/griphyn/vdl/mapping/file/AbstractFileMapper.java 2013-03-17 07:47:59 UTC (rev 6386)
+++ branches/release-0.94/src/org/griphyn/vdl/mapping/file/AbstractFileMapper.java 2013-03-17 07:49:58 UTC (rev 6387)
@@ -25,6 +25,7 @@
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
+import java.util.Set;
import org.apache.log4j.Logger;
import org.globus.cog.util.Base64;
@@ -71,6 +72,13 @@
public static final MappingParam PARAM_PATTERN = new MappingParam("pattern", null);
public static final MappingParam PARAM_LOCATION = new MappingParam("location", null);
public static final MappingParam PARAM_NOAUTO = new MappingParam("noauto", "false");
+
+
+ @Override
+ protected void getValidMappingParams(Set<String> s) {
+ addParams(s, PARAM_PREFIX, PARAM_SUFFIX, PARAM_PATTERN, PARAM_LOCATION, PARAM_NOAUTO);
+ super.getValidMappingParams(s);
+ }
public static final Logger logger = Logger.getLogger(AbstractFileMapper.class);
Modified: branches/release-0.94/src/org/griphyn/vdl/mapping/file/ArrayFileMapper.java
===================================================================
--- branches/release-0.94/src/org/griphyn/vdl/mapping/file/ArrayFileMapper.java 2013-03-17 07:47:59 UTC (rev 6386)
+++ branches/release-0.94/src/org/griphyn/vdl/mapping/file/ArrayFileMapper.java 2013-03-17 07:49:58 UTC (rev 6387)
@@ -37,6 +37,12 @@
static Logger logger = Logger.getLogger(ArrayFileMapper.class);
public static final MappingParam PARAM_FILES = new MappingParam("files");
+
+ @Override
+ protected void getValidMappingParams(Set<String> s) {
+ addParams(s, PARAM_FILES);
+ super.getValidMappingParams(s);
+ }
public Collection<Path> existing() {
List<Path> l = new ArrayList<Path>();
Modified: branches/release-0.94/src/org/griphyn/vdl/mapping/file/CSVMapper.java
===================================================================
--- branches/release-0.94/src/org/griphyn/vdl/mapping/file/CSVMapper.java 2013-03-17 07:47:59 UTC (rev 6386)
+++ branches/release-0.94/src/org/griphyn/vdl/mapping/file/CSVMapper.java 2013-03-17 07:49:58 UTC (rev 6387)
@@ -27,6 +27,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
+import java.util.Set;
import java.util.StringTokenizer;
import org.griphyn.vdl.mapping.AbsFile;
@@ -55,7 +56,14 @@
public static final MappingParam PARAM_HDELIMITER = new MappingParam("hdelim");
/** delimiters between content fields. default is space, tab, comma */
- public static final MappingParam PARAM_DELIMITER = new MappingParam("delim", " \t,");
+ public static final MappingParam PARAM_DELIMITER = new MappingParam("delim", " \t,");
+
+
+ @Override
+ protected void getValidMappingParams(Set<String> s) {
+ addParams(s, PARAM_FILE, PARAM_HEADER, PARAM_SKIP, PARAM_HDELIMITER, PARAM_DELIMITER);
+ super.getValidMappingParams(s);
+ }
/** list of column names */
private List cols = new ArrayList();
Modified: branches/release-0.94/src/org/griphyn/vdl/mapping/file/ConcurrentMapper.java
===================================================================
--- branches/release-0.94/src/org/griphyn/vdl/mapping/file/ConcurrentMapper.java 2013-03-17 07:47:59 UTC (rev 6386)
+++ branches/release-0.94/src/org/griphyn/vdl/mapping/file/ConcurrentMapper.java 2013-03-17 07:49:58 UTC (rev 6387)
@@ -35,6 +35,13 @@
public class ConcurrentMapper extends AbstractFileMapper {
public static final MappingParam PARAM_THREAD_PREFIX = new MappingParam("thread_prefix", "");
+
+ @Override
+ protected void getValidMappingParams(Set<String> s) {
+ addParams(s, PARAM_THREAD_PREFIX);
+ super.getValidMappingParams(s);
+ }
+
private Map<Path, PhysicalFormat> remappedPaths;
public ConcurrentMapper() {
Modified: branches/release-0.94/src/org/griphyn/vdl/mapping/file/ExternalMapper.java
===================================================================
--- branches/release-0.94/src/org/griphyn/vdl/mapping/file/ExternalMapper.java 2013-03-17 07:47:59 UTC (rev 6386)
+++ branches/release-0.94/src/org/griphyn/vdl/mapping/file/ExternalMapper.java 2013-03-17 07:49:58 UTC (rev 6387)
@@ -25,7 +25,6 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
-import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -44,7 +43,15 @@
private Map<Path, AbsFile> map;
private Map<String, Path> rmap;
- public static final MappingParam PARAM_EXEC = new MappingParam("exec");
+ public static final MappingParam PARAM_EXEC = new MappingParam("exec");
+
+
+ @Override
+ protected void getValidMappingParams(Set<String> s) {
+ addParams(s, PARAM_EXEC);
+ s.add("*");
+ super.getValidMappingParams(s);
+ }
private static final String[] STRING_ARRAY = new String[0];
Modified: branches/release-0.94/src/org/griphyn/vdl/mapping/file/FixedArrayFileMapper.java
===================================================================
--- branches/release-0.94/src/org/griphyn/vdl/mapping/file/FixedArrayFileMapper.java 2013-03-17 07:47:59 UTC (rev 6386)
+++ branches/release-0.94/src/org/griphyn/vdl/mapping/file/FixedArrayFileMapper.java 2013-03-17 07:49:58 UTC (rev 6387)
@@ -42,6 +42,14 @@
an array. */
public class FixedArrayFileMapper extends AbstractMapper {
public static final MappingParam PARAM_FILES = new MappingParam("files");
+
+
+ @Override
+ protected void getValidMappingParams(Set<String> s) {
+ addParams(s, PARAM_FILES);
+ super.getValidMappingParams(s);
+ }
+
private List<String> files;
public FixedArrayFileMapper() {
Modified: branches/release-0.94/src/org/griphyn/vdl/mapping/file/RegularExpressionMapper.java
===================================================================
--- branches/release-0.94/src/org/griphyn/vdl/mapping/file/RegularExpressionMapper.java 2013-03-17 07:47:59 UTC (rev 6386)
+++ branches/release-0.94/src/org/griphyn/vdl/mapping/file/RegularExpressionMapper.java 2013-03-17 07:49:58 UTC (rev 6387)
@@ -22,6 +22,7 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
+import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -36,7 +37,14 @@
public class RegularExpressionMapper extends AbstractMapper {
public static final MappingParam PARAM_SOURCE = new MappingParam("source");
public static final MappingParam PARAM_MATCH = new MappingParam("match");
- public static final MappingParam PARAM_TRANSFORM = new MappingParam("transform");
+ public static final MappingParam PARAM_TRANSFORM = new MappingParam("transform");
+
+
+ @Override
+ protected void getValidMappingParams(Set<String> s) {
+ addParams(s, PARAM_SOURCE, PARAM_MATCH, PARAM_TRANSFORM);
+ super.getValidMappingParams(s);
+ }
public RegularExpressionMapper() {
}
Modified: branches/release-0.94/src/org/griphyn/vdl/mapping/file/SimpleFileMapper.java
===================================================================
--- branches/release-0.94/src/org/griphyn/vdl/mapping/file/SimpleFileMapper.java 2013-03-17 07:47:59 UTC (rev 6386)
+++ branches/release-0.94/src/org/griphyn/vdl/mapping/file/SimpleFileMapper.java 2013-03-17 07:49:58 UTC (rev 6387)
@@ -20,11 +20,20 @@
*/
package org.griphyn.vdl.mapping.file;
+import java.util.Set;
+
import org.griphyn.vdl.mapping.MappingParam;
import org.griphyn.vdl.mapping.MappingParamSet;
public class SimpleFileMapper extends AbstractFileMapper {
public static final MappingParam PARAM_PADDING = new MappingParam("padding", new Integer(4));
+
+
+ @Override
+ protected void getValidMappingParams(Set<String> s) {
+ addParams(s, PARAM_PADDING);
+ super.getValidMappingParams(s);
+ }
public SimpleFileMapper() {
super();
Modified: branches/release-0.94/src/org/griphyn/vdl/mapping/file/SingleFileMapper.java
===================================================================
--- branches/release-0.94/src/org/griphyn/vdl/mapping/file/SingleFileMapper.java 2013-03-17 07:47:59 UTC (rev 6386)
+++ branches/release-0.94/src/org/griphyn/vdl/mapping/file/SingleFileMapper.java 2013-03-17 07:49:58 UTC (rev 6387)
@@ -20,6 +20,7 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
+import java.util.Set;
import org.griphyn.vdl.mapping.AbsFile;
import org.griphyn.vdl.mapping.AbstractMapper;
@@ -32,8 +33,14 @@
public class SingleFileMapper extends AbstractMapper {
public static final MappingParam PARAM_FILE = new MappingParam("file");
+
+ @Override
+ protected void getValidMappingParams(Set<String> s) {
+ addParams(s, PARAM_FILE);
+ super.getValidMappingParams(s);
+ }
- public SingleFileMapper() {
+ public SingleFileMapper() {
super();
}
Modified: branches/release-0.94/src/org/griphyn/vdl/mapping/file/StructuredRegularExpressionMapper.java
===================================================================
--- branches/release-0.94/src/org/griphyn/vdl/mapping/file/StructuredRegularExpressionMapper.java 2013-03-17 07:47:59 UTC (rev 6386)
+++ branches/release-0.94/src/org/griphyn/vdl/mapping/file/StructuredRegularExpressionMapper.java 2013-03-17 07:49:58 UTC (rev 6387)
@@ -21,6 +21,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
+import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -41,7 +42,14 @@
public static final MappingParam PARAM_SOURCE = new MappingParam("source");
public static final MappingParam PARAM_MATCH = new MappingParam("match");
- public static final MappingParam PARAM_TRANSFORM = new MappingParam("transform");
+ public static final MappingParam PARAM_TRANSFORM = new MappingParam("transform");
+
+
+ @Override
+ protected void getValidMappingParams(Set<String> s) {
+ addParams(s, PARAM_SOURCE, PARAM_MATCH, PARAM_TRANSFORM);
+ super.getValidMappingParams(s);
+ }
public StructuredRegularExpressionMapper() {
}
Modified: branches/release-0.94/src/org/griphyn/vdl/mapping/file/TestMapper.java
===================================================================
--- branches/release-0.94/src/org/griphyn/vdl/mapping/file/TestMapper.java 2013-03-17 07:47:59 UTC (rev 6386)
+++ branches/release-0.94/src/org/griphyn/vdl/mapping/file/TestMapper.java 2013-03-17 07:49:58 UTC (rev 6387)
@@ -19,6 +19,7 @@
import java.util.Collection;
import java.util.Collections;
+import java.util.Set;
import org.griphyn.vdl.mapping.AbsFile;
import org.griphyn.vdl.mapping.AbstractMapper;
@@ -34,6 +35,12 @@
public static final MappingParam PARAM_REMAPPABLE = new MappingParam("remappable", false);
public static final MappingParam PARAM_STATIC = new MappingParam("static", true);
+ @Override
+ protected void getValidMappingParams(Set<String> s) {
+ addParams(s, PARAM_FILE, PARAM_TEMP, PARAM_REMAPPABLE, PARAM_STATIC);
+ super.getValidMappingParams(s);
+ }
+
private PhysicalFormat remap, map;
@Override
More information about the Swift-commit
mailing list