Skip to content

Commit 25c4bcc

Browse files
committed
initial commit: regex app
1 parent 1d1f24c commit 25c4bcc

4 files changed

Lines changed: 141 additions & 0 deletions

File tree

java/pom.xml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<groupId>com.code402</groupId>
7+
<artifactId>aws-api-gateway-example</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
<name>aws-api-gateway-example</name>
10+
<!-- FIXME change it to the project's website -->
11+
<url>http://www.example.com</url>
12+
<properties>
13+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
14+
<maven.compiler.source>1.8</maven.compiler.source>
15+
<maven.compiler.target>1.8</maven.compiler.target>
16+
</properties>
17+
<dependencies>
18+
<dependency>
19+
<groupId>dk.brics.automaton</groupId>
20+
<artifactId>automaton</artifactId>
21+
<version>1.11-8</version>
22+
</dependency>
23+
<dependency>
24+
<groupId>com.amazonaws</groupId>
25+
<artifactId>aws-lambda-java-core</artifactId>
26+
<version>1.2.0</version>
27+
</dependency>
28+
</dependencies>
29+
<build>
30+
<plugins>
31+
<plugin>
32+
<artifactId>maven-assembly-plugin</artifactId>
33+
<configuration>
34+
<descriptorRefs>
35+
<descriptorRef>jar-with-dependencies</descriptorRef>
36+
</descriptorRefs>
37+
</configuration>
38+
</plugin>
39+
</plugins>
40+
</build>
41+
</project>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.code402;
2+
3+
import com.amazonaws.services.lambda.runtime.Context;
4+
import com.amazonaws.services.lambda.runtime.RequestHandler;
5+
import dk.brics.automaton.*;
6+
import java.util.ArrayList;
7+
import java.util.Arrays;
8+
9+
public class App implements RequestHandler<Request, Response> {
10+
public Response match(String haystack, String needle) {
11+
ArrayList<String> hits = new ArrayList<String>();
12+
RegExp r;
13+
try {
14+
r = new RegExp(needle, RegExp.NONE);
15+
} catch (IllegalArgumentException iae) {
16+
return new Response(null, iae.getMessage());
17+
}
18+
19+
RunAutomaton a = new RunAutomaton(r.toAutomaton());
20+
21+
AutomatonMatcher m = a.newMatcher(haystack);
22+
while (m.find()) {
23+
hits.add(m.group());
24+
}
25+
26+
int len = hits.size();
27+
if (len > 10) len = 10;
28+
String[] strings = Arrays.copyOf(hits.toArray(), len, String[].class);
29+
return new Response(strings, null);
30+
}
31+
32+
public Response handleRequest(Request request, Context context) {
33+
System.out.println(
34+
"Received request: needle=" + request.getNeedle() + ", haystack=" + request.getHaystack());
35+
return match(request.haystack, request.needle);
36+
}
37+
38+
public static void main(String[] args) {
39+
String haystack = args[0];
40+
String needle = args[1];
41+
Response rv = (new App()).match(haystack, needle);
42+
43+
if (rv.error != null) {
44+
System.err.println(rv.error);
45+
System.exit(1);
46+
}
47+
48+
for (int i = 0; i < rv.getHits().length; i++) {
49+
System.out.println(rv.getHits()[i]);
50+
}
51+
}
52+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.code402;
2+
3+
public class Request {
4+
String needle, haystack;
5+
6+
public String getNeedle() {
7+
return needle;
8+
}
9+
10+
public String getHaystack() {
11+
return haystack;
12+
}
13+
14+
public void setNeedle(String needle) {
15+
this.needle = needle;
16+
}
17+
18+
public void setHaystack(String haystack) {
19+
this.haystack = haystack;
20+
}
21+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.code402;
2+
3+
public class Response {
4+
String[] hits;
5+
String error;
6+
7+
public String[] getHits() {
8+
return hits;
9+
}
10+
11+
public String getError() {
12+
return error;
13+
}
14+
15+
public void setHits(String[] hits) {
16+
this.hits = hits;
17+
}
18+
19+
public void setError(String error) {
20+
this.error = error;
21+
}
22+
23+
public Response(String[] hits, String error) {
24+
this.hits = hits;
25+
this.error = error;
26+
}
27+
}

0 commit comments

Comments
 (0)