Skip to content

Commit

Permalink
Merge pull request #1 from adam-vessey/master
Browse files Browse the repository at this point in the history
Fix README and extensibility.
  • Loading branch information
adam-vessey committed Jul 24, 2013
2 parents 6a580b0 + 7235328 commit 4e71d6f
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 5 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,25 @@ In Xalan, it should be possible to call the code like:
<xsl:variable name="date_to_parse">08/13/2013</xsl:variable>
<xsl:variable xmlns:java="http://xml.apache.org/xalan/java"
name="date"
select="java:ca.discoverygarden.gsearch_extensions.JodaAdapter($date_to_parse)"/>
select="java:ca.discoverygarden.gsearch_extensions.JodaAdapter.transformForSolr($date_to_parse)"/>
```

For better logging, one could also call like:
```
<xsl:variable xmlns:java="http://xml.apache.org/xalan/java"
name="date"
select="java:ca.discoverygarden.gsearch_extensions.JodaAdapter($date_to_parse, $pid, $datastream)"/>
select="java:ca.discoverygarden.gsearch_extensions.JodaAdapter.transformForSolr($date_to_parse, $pid, $datastream)"/>
```
where `$pid` and `$datastream` are the identifiers of the object and datastream, respectively.

The three base parsers assume that they are given dates in UTC if no timezone is provided, and are attempted in order:
- `M/d/y`
So `7/23/2013` should result in `2013-07-23T00:00:00.000Z`.
- `M/d/y H:m`
So `7/23/2013 11:36` should result in `2013-07-23T11:36:00.000Z`.
- ISO Date Time, as per http://joda-time.sourceforge.net/apidocs/org/joda/time/format/ISODateTimeFormat.html#dateTimeParser%28%29
Timezone offsets will be transformed off, so `2013-07-23T02:36-03:00` will be transformed to `2013-07-23T11:36:00.000Z`.

CUSTOMIZATION
-------------

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>ca.discoverygarden</groupId>
<artifactId>gsearch_extensions</artifactId>
<version>0.0.1-SNAPSHOT</version>
<version>0.1.0</version>
<packaging>jar</packaging>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,18 @@
public class JodaAdapter {
final protected static Logger logger = Logger.getLogger(JodaAdapter.class);

final static Vector<DateTimeFormatter> parsers = new Vector<DateTimeFormatter>();
final static DateTimeFormatter isoFormatter = ISODateTimeFormat.dateTime().withZoneUTC();
final protected static Vector<DateTimeFormatter> parsers = new Vector<DateTimeFormatter>();
final protected static DateTimeFormatter isoFormatter = ISODateTimeFormat.dateTime().withZoneUTC();
static {
resetParsers();
}

/**
* Resets the list of parsers used to the base three.
*/
public static void resetParsers() {
parsers.clear();

// Setup base parsers.
parsers.add(DateTimeFormat.forPattern("M/d/y").withZoneUTC());
parsers.add(DateTimeFormat.forPattern("M/d/y H:m").withZoneUTC());
Expand Down Expand Up @@ -62,4 +71,29 @@ public static String transformForSolr(String dateString, String pid, String data
public static String transformForSolr(String dateString) {
return JodaAdapter.transformForSolr(dateString, null, null);
}

/**
* Adds another parser to the end of the list of parser to try.
*
* @param format
* A format to attempt to parse, as accepted by
* DateTimeFormat.forPattern().
*/
public static void addDateParser(String format) {
parsers.add(DateTimeFormat.forPattern(format));
}

/**
* Adds another parser at the given offset.
*
* @param position
* The at which to attempt to put this parser. Other parsers will be
* pushed back in the ordering.
* @param format
* A format to attempt to parse, as accepted by
* DateTimeFormat.forPattern().
*/
public static void addDateParser(int position, String format) {
parsers.add(position, DateTimeFormat.forPattern(format));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
import junit.framework.TestCase;

public class JodaAdapterTest extends TestCase {
protected void setUp() throws Exception {
super.setUp();
JodaAdapter.resetParsers();
}

public void testMDY() {
String source = "07/22/2013";
String dest = "2013-07-22T00:00:00.000Z";
Expand Down Expand Up @@ -41,4 +46,24 @@ public void testISODateTimeWithOffset() {
public void testUnparsable() {
assertEquals("", JodaAdapter.transformForSolr("2222-22-22"));
}

public void testAddFormat() {
String source = "00™2013™22™07™+00:00™00";
String format = "H™y™d™M™ZZ™m";
String dest = "2013-07-22T00:00:00.000Z";

assertEquals("", JodaAdapter.transformForSolr(source));
JodaAdapter.addDateParser(format);
assertEquals(dest, JodaAdapter.transformForSolr(source));
}

public void testAddFormatAtPosition() {
String source = "00™2013™22™07™+00:00™00";
String format = "H™y™d™M™ZZ™m";
String dest = "2013-07-22T00:00:00.000Z";

assertEquals("", JodaAdapter.transformForSolr(source));
JodaAdapter.addDateParser(0, format);
assertEquals(dest, JodaAdapter.transformForSolr(source));
}
}

0 comments on commit 4e71d6f

Please sign in to comment.