All Downloads are FREE. Search and download functionalities are using the official Maven repository.

at.newmedialab.ldpath.model.transformers.DurationTransformer Maven / Gradle / Ivy

Go to download

Core Implementation of LD Path a simple path-based query language similar to XPath or SPARQL Property Paths that is particularly well-suited for querying and retrieving resources from the Linked Data Cloud by following RDF links between resources and servers.

There is a newer version: 0.9.7
Show newest version
package at.newmedialab.ldpath.model.transformers;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.Duration;

import at.newmedialab.ldpath.api.backend.RDFBackend;
import at.newmedialab.ldpath.api.transformers.NodeTransformer;

/**
 * Transforms (and validates) the lexical value of a node to
 * {@link Duration}.
 * @author Rupert Westenthaler
 * @param  the generic node type
 */
public class DurationTransformer implements NodeTransformer {

    /**
     * Lazy initialisation to avoid Exceptions if {@link DatatypeConfigurationException}
     * is thrown during initialisation of the Utility class.

* Do not access directly! Use {@link #getXmlDataTypeFactory()} instead. */ private static DatatypeFactory xmlDatatypeFactory; /** * Inits the {@link #xmlDatatypeFactory} if not already done.

* @return the XML datatype factory * @throws IllegalStateException if a {@link DatatypeConfigurationException} * is encountered during {@link DatatypeFactory#newInstance()} */ private static DatatypeFactory getXmlDataTypeFactory() throws IllegalStateException { if(xmlDatatypeFactory == null){ try { xmlDatatypeFactory = DatatypeFactory.newInstance(); } catch (DatatypeConfigurationException e) { throw new IllegalStateException("Unable to instantiate XML Datatype Factory!",e); } } return xmlDatatypeFactory; } @Override public Duration transform(RDFBackend backend, Node node) throws IllegalArgumentException { if(backend.isLiteral(node)) { return toDuration(backend.stringValue(node), false); } else { throw new IllegalArgumentException("cannot transform node of type "+ node.getClass().getCanonicalName()+" to byte"); } } private static Duration toDuration(String value, boolean nullAsZeroDuration) throws IllegalArgumentException,IllegalStateException{ if(value == null){ if(nullAsZeroDuration){ return getXmlDataTypeFactory().newDuration(0); } else { throw new IllegalArgumentException("The parsed value MUST NOT be NULL. Parse \"boolean nullAsZeroDuration=true\" to enable creation of zero lenght durations for NULL values!"); } } else { return getXmlDataTypeFactory().newDuration(value); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy