use of javax.xml.datatype.DatatypeConfigurationException in project GeoGig by boundlessgeo.
the class ParseTimestamp method _call.
/**
* Parses a string with a timestamp
*
* @return a Long with the timestamp represented by the specified string
*/
@Override
protected Long _call() {
Preconditions.checkState(string != null, "String has not been set.");
try {
// see if it is a timestamp in milisecs
Long milis = new Long(string);
return milis;
} catch (NumberFormatException e) {
}
SimpleDateFormat formatter;
final Platform platform = platform();
if (string.equals("yesterday")) {
// from current time
try {
formatter = new SimpleDateFormat("dd/MM/yyyy");
Date today = new Date(platform.currentTimeMillis());
long todayOnlyDate = formatter.parse(formatter.format(today)).getTime();
long millisecsInOneDay = 60 * 60 * 24 * 1000;
long yesterday = todayOnlyDate - millisecsInOneDay;
return yesterday;
} catch (ParseException e) {
// shouldn't reach this
}
}
if (string.equals("today")) {
try {
formatter = new SimpleDateFormat("dd/MM/yyyy");
Date today = new Date(platform.currentTimeMillis());
long todayOnlyDate = formatter.parse(formatter.format(today)).getTime();
return todayOnlyDate;
} catch (ParseException e) {
// shouldn't reach this
}
}
// parse it as a git-like time reference
String[] tokens = string.split("\\.");
if (tokens.length % 2 != 0) {
if (tokens[tokens.length - 1].toLowerCase().equals("ago")) {
long currentTime = platform.currentTimeMillis();
int i;
for (i = 0; i < tokens.length - 1; i++) {
try {
double number = Double.parseDouble(tokens[i]);
i++;
String s = tokens[i].toLowerCase();
if (s.endsWith("s")) {
s = s.substring(0, s.length() - 1);
}
if (units.containsKey(s)) {
currentTime -= units.get(s) * number;
} else {
break;
}
} catch (Exception e) {
break;
}
}
if (i == tokens.length - 1) {
return currentTime;
}
}
}
// finally, try to parse it as a Date object
try {
long time = javax.xml.datatype.DatatypeFactory.newInstance().newXMLGregorianCalendar(string).toGregorianCalendar().getTimeInMillis();
return time;
} catch (DatatypeConfigurationException e) {
} catch (IllegalArgumentException e) {
}
throw new IllegalArgumentException("Invalid timestamp string: " + string);
}
use of javax.xml.datatype.DatatypeConfigurationException in project xades4j by luisgoncalves.
the class ToXmlCompleteRevocRefsConverter method convertIntoObjectTree.
@Override
public void convertIntoObjectTree(PropertyDataObject propData, XmlUnsignedPropertiesType xmlProps, Document doc) {
CompleteRevocationRefsData complRevocRefsData = (CompleteRevocationRefsData) propData;
// Only CRL refs are supported.
XmlCRLRefsType xmlCRLRefs = new XmlCRLRefsType();
List<XmlCRLRefType> xmlCRLRefsList = xmlCRLRefs.getCRLRef();
try {
for (CRLRef crlRef : complRevocRefsData.getCrlRefs()) {
XmlCRLIdentifierType xmlCrlId = new XmlCRLIdentifierType();
xmlCrlId.setIssueTime(DatatypeFactory.newInstance().newXMLGregorianCalendar(crlRef.issueTime));
xmlCrlId.setIssuer(crlRef.issuerDN);
// May be null.
xmlCrlId.setNumber(crlRef.serialNumber);
XmlDigestAlgAndValueType xmlDigest = new XmlDigestAlgAndValueType();
XmlDigestMethodType xmlDigestMethod = new XmlDigestMethodType();
xmlDigestMethod.setAlgorithm(crlRef.digestAlgUri);
xmlDigest.setDigestValue(crlRef.digestValue);
xmlDigest.setDigestMethod(xmlDigestMethod);
XmlCRLRefType xmlCrlRef = new XmlCRLRefType();
xmlCrlRef.setCRLIdentifier(xmlCrlId);
xmlCrlRef.setDigestAlgAndValue(xmlDigest);
xmlCRLRefsList.add(xmlCrlRef);
}
} catch (DatatypeConfigurationException ex) {
throw new UnsupportedOperationException(ex.getMessage(), ex);
}
XmlCompleteRevocationRefsType xmlComplRevocRefs = new XmlCompleteRevocationRefsType();
// Only CRL refs are supported.
xmlComplRevocRefs.setCRLRefs(xmlCRLRefs);
xmlProps.getUnsignedSignatureProperties().setCompleteRevocationRefs(xmlComplRevocRefs);
}
use of javax.xml.datatype.DatatypeConfigurationException in project sis by apache.
the class UniversalTimeAdapter method marshal.
/**
* Converts the date to the object to be marshalled in a XML file or stream.
* JAXB calls automatically this method at marshalling time.
*
* @param value the {@code java.util} date value, or {@code null}.
* @return the XML date, or {@code null}.
*/
@Override
public XMLGregorianCalendar marshal(final Date value) {
if (value != null) {
final GregorianCalendar calendar = new GregorianCalendar(UTC, Locale.ROOT);
calendar.setTime(value);
try {
final XMLGregorianCalendar gc = getDatatypeFactory().newXMLGregorianCalendar(calendar);
if (gc.getMillisecond() == 0) {
gc.setMillisecond(DatatypeConstants.FIELD_UNDEFINED);
}
return gc;
} catch (DatatypeConfigurationException e) {
Context.warningOccured(Context.current(), XmlAdapter.class, "marshal", e, true);
}
}
return null;
}
use of javax.xml.datatype.DatatypeConfigurationException in project incubator-rya by apache.
the class DateTimeTtlValueConverter method convert.
@Override
public void convert(String ttl, String startTime) {
try {
long start_l, stop_l;
long ttl_l = Long.parseLong(ttl);
stop_l = System.currentTimeMillis();
if (startTime != null)
stop_l = Long.parseLong(startTime);
start_l = stop_l - ttl_l;
GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance();
cal.setTimeZone(getTimeZone());
cal.setTimeInMillis(start_l);
DatatypeFactory factory = DatatypeFactory.newInstance();
start = vf.createLiteral(factory.newXMLGregorianCalendar(cal));
cal.setTimeInMillis(stop_l);
stop = vf.createLiteral(factory.newXMLGregorianCalendar(cal));
} catch (DatatypeConfigurationException e) {
throw new RuntimeException("Exception occurred creating DataTypeFactory", e);
}
}
use of javax.xml.datatype.DatatypeConfigurationException in project TranskribusCore by Transkribus.
the class JaxbUtils method getXmlCalendar.
public static XMLGregorianCalendar getXmlCalendar(Date date) {
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(date);
XMLGregorianCalendar xmlCal = null;
try {
xmlCal = DatatypeFactory.newInstance().newXMLGregorianCalendar(cal);
} catch (DatatypeConfigurationException e) {
logger.error("DatatypeFactory: implementation is not available or cannot be instantiated. CreateDate will be null!", e);
}
return xmlCal;
}
Aggregations