Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

extra conversion flexibility for Dates.

Signed-off-by: gburgett <gordon.burgett@gmail.com>
  • Loading branch information...
commit 40ce0b197aa462971b959e698d7928c44e31f18f 1 parent 237d41d
@gburgett authored
View
30 java/XFlat/src/org/xflatdb/xflat/convert/converters/JDOMConverters.java
@@ -15,6 +15,7 @@
*/
package org.xflatdb.xflat.convert.converters;
+import java.util.Date;
import org.jdom2.*;
import org.xflatdb.xflat.convert.ConversionService;
import org.xflatdb.xflat.convert.Converter;
@@ -40,9 +41,10 @@ public static void registerTo(ConversionService service){
service.addConverter(String.class, Text.class, StringToTextConverter);
service.addConverter(String.class, CDATA.class, StringToCdataConverter);
- service.addConverter(String.class, Content.class, StringToCdataConverter);
+ service.addConverter(String.class, Content.class, StringToTextConverter);
registerNumbers(service);
+ registerDates(service);
}
//<editor-fold desc="string" >
@@ -156,5 +158,29 @@ private static void registerNumbers(ConversionService service){
= new TwoStepConverter<>(StringConverters.ObjectToStringConverter, StringToTextConverter);
- //</editor-fold
+ //</editor-fold>
+
+ //<editor-fold desc="dates">
+
+ private static void registerDates(ConversionService service){
+ service.addConverter(Content.class, Date.class, ContentToDateConverter);
+ service.addConverter(Element.class, Date.class, ContentToDateConverter);
+ service.addConverter(Text.class, Date.class, ContentToDateConverter);
+ service.addConverter(CDATA.class, Date.class, ContentToDateConverter);
+ service.addConverter(Attribute.class, Date.class, AttributeToDateConverter);
+
+ service.addConverter(Date.class, Content.class, DateToTextConverter);
+ service.addConverter(Date.class, Text.class, DateToTextConverter);
+ }
+
+ public static final Converter<Content, Date> ContentToDateConverter
+ = new TwoStepConverter<>(ContentToStringConverter, StringConverters.StringToDateConverter);
+
+ public static final Converter<Attribute, Date> AttributeToDateConverter
+ = new TwoStepConverter<>(AttributeToStringConverter, StringConverters.StringToDateConverter);
+
+ public static final Converter<Date, Text> DateToTextConverter
+ = new TwoStepConverter<>(StringConverters.DateToStringConverter, StringToTextConverter);
+
+ //</editor-fold>
}
View
39 java/XFlat/src/org/xflatdb/xflat/convert/converters/StringConverters.java
@@ -70,6 +70,20 @@ public Boolean convert(String source) {
public static final Converter<String, Long> StringToLongConverter = new Converter<String, Long>() {
@Override
public Long convert(String source) throws ConversionException {
+ //is it all numbers? Longs can also represent dates.
+ for(int i = 0; i < source.length(); i++){
+ if(!Character.isDigit(source.codePointAt(i))){
+
+ //it's a date format string or nothing
+ try{
+ return format.get().parse(source).getTime();
+ }catch(ParseException ex){
+ //could also be represented as a long
+ throw new ConversionException("error parsing date", ex);
+ }
+ }
+ }
+
try{
return Long.valueOf(source);
}catch(NumberFormatException ex){
@@ -121,10 +135,27 @@ public String convert(Object source) {
public static final Converter<String, Date> StringToDateConverter = new Converter<String, Date>(){
@Override
public Date convert(String source) throws ConversionException {
- try{
- return format.get().parse(source);
- }catch(ParseException ex){
- throw new ConversionException("error parsing date", ex);
+ //first, is it all digits? Dates can be represented as longs.
+ for(int i = 0; i < source.length(); i++){
+ if(!Character.isDigit(source.codePointAt(i))){
+
+ //it's a date format string or nothing
+
+ try{
+ return format.get().parse(source);
+ }catch(ParseException ex){
+ //could also be represented as a long
+ throw new ConversionException("error parsing date", ex);
+ }
+ }
+ }
+
+ //it's all digits - it's a long or nothing
+ try{
+ return new Date(Long.parseLong(source));
+ }
+ catch(NumberFormatException ex2){
+ throw new ConversionException("error parsing date", ex2);
}
}
};
View
4 java/XFlat/src/org/xflatdb/xflat/query/XPathQuery.java
@@ -623,9 +623,9 @@ protected boolean matchesSafely(Element item) {
@Override
public void describeTo(Description description) {
- description.appendText("value at ")
+ description.appendText("a value at ")
.appendText(this.selector.getExpression())
- .appendText(" that ")
+ .appendText(" that is ")
.appendDescriptionOf(this.subMatcher);
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.