Skip to content
Browse files

GEOT-4215 - Add explicit delimiter to Transform process definitions

  • Loading branch information...
1 parent 05b1599 commit 7e3bebab616b2b9cb638e2e1f58830f620c773a1 Martin Davis committed Aug 8, 2012
View
35 ...orted/process-feature/src/main/java/org/geotools/process/feature/gs/TransformProcess.java
@@ -61,7 +61,8 @@
* name=name
* area=area( the_geom )
* </pre>
-
+ *
+ * Attribute definitions can be delimited by line breaks and/or by semicolons.
* <p>
* This is a very flexible process which can be used to:
* <ul>
@@ -116,12 +117,14 @@
/** Name of the AttribtueDescriptor to generate */
public String name;
- /** Expression used to generate the target calue; most simply a PropertyName */
+ /** Expression used to generate the target value; most simply a PropertyName */
public Expression expression;
/** Class binding (if known) */
public Class<?> binding;
}
+ private static final String DEF_DELIMITER = ";";
+
@DescribeResult(name = "result", description = "transformed features")
public SimpleFeatureCollection execute(
@DescribeParameter(name = "features", description = "The feature collection to transform") SimpleFeatureCollection features,
@@ -163,23 +166,23 @@ public SimpleFeatureCollection executeList(
HashSet<String> check = new HashSet<String>();
// clean up cross platform differences of line feed
- definition = definition.replaceAll("\r", "\n").replaceAll("[\n\r][\n\r]", "\n");
+ String[] defs = splitDefinitions(definition);
- for (String line : definition.split("\n")) {
+ for (String line : defs) {
int mark = line.indexOf("=");
if (mark != -1) {
String name = line.substring(0, mark).trim();
String expressionDefinition = line.substring(mark + 1).trim();
if (check.contains(name)) {
- throw new IllegalArgumentException("ReShape definition " + name
- + " already in use");
+ throw new IllegalArgumentException("Attribute " + name
+ + " defined more than once");
}
Expression expression;
try {
expression = ECQL.toExpression(expressionDefinition);
} catch (CQLException e) {
- throw new IllegalArgumentException("Reshape unable to parse " + name + "="
+ throw new IllegalArgumentException("Unable to parse expression " + name + "="
+ expressionDefinition + " " + e, e);
}
Definition def = new Definition();
@@ -192,6 +195,24 @@ public SimpleFeatureCollection executeList(
return list;
}
+ /**
+ * Splits single-string definition list into a list of definitions.
+ * Either line breaks or ';' can be used as definition delimiters.
+ *
+ * @param defList the definition list string
+ * @return the separate definitions
+ */
+ private static String[] splitDefinitions(String defList)
+ {
+ // clean up cross platform differences of linefeed
+ String defListLF = defList.replaceAll("\r", "\n").replaceAll("[\n\r][\n\r]", "\n");
+ // convert explicit delimiter to linefeed
+ defListLF = defList.replaceAll(";", "\n");
+
+ // split on linefeed
+ return defListLF.split("\n");
+ }
+
public static SimpleFeatureType toReShapeFeatureType(SimpleFeatureCollection delegate,
List<Definition> definitionList) {
View
48 ...d/process-feature/src/test/java/org/geotools/process/feature/gs/TransformProcessTest.java
@@ -71,6 +71,54 @@ public void testDefinition() throws Exception {
}
@Test
+ public void testDefinitionListLF() throws Exception {
+ String definition = "the_geom=the_geom\ncat=cat+1";
+ List<Definition> def = TransformProcess.toDefinition( definition );
+
+ assertEquals( 2, def.size() );
+
+ assertEquals( "the_geom", def.get(0).name );
+ assertTrue( def.get(0).expression instanceof PropertyName );
+ assertEquals( "cat", def.get(1).name );
+ }
+
+ @Test
+ public void testDefinitionListDelimiter() throws Exception {
+ String definition = "the_geom=the_geom; cat=cat+1";
+ List<Definition> def = TransformProcess.toDefinition( definition );
+
+ assertEquals( 2, def.size() );
+
+ assertEquals( "the_geom", def.get(0).name );
+ assertTrue( def.get(0).expression instanceof PropertyName );
+ assertEquals( "cat", def.get(1).name );
+ }
+
+ @Test
+ public void testDefinitionListDelimiterExtra() throws Exception {
+ String definition = "the_geom=the_geom; cat=cat+1;";
+ List<Definition> def = TransformProcess.toDefinition( definition );
+
+ assertEquals( 2, def.size() );
+
+ assertEquals( "the_geom", def.get(0).name );
+ assertTrue( def.get(0).expression instanceof PropertyName );
+ assertEquals( "cat", def.get(1).name );
+ }
+
+ @Test
+ public void testDefinitionListDelimiterLF() throws Exception {
+ String definition = "the_geom=the_geom;\n cat=cat+1;";
+ List<Definition> def = TransformProcess.toDefinition( definition );
+
+ assertEquals( 2, def.size() );
+
+ assertEquals( "the_geom", def.get(0).name );
+ assertTrue( def.get(0).expression instanceof PropertyName );
+ assertEquals( "cat", def.get(1).name );
+ }
+
+ @Test
public void testSum() throws Exception {
SimpleFeatureSource source = bugs.getFeatureSource("bugsites");

0 comments on commit 7e3beba

Please sign in to comment.
Something went wrong with that request. Please try again.