Search in sources :

Example 11 with DatatypeConfigurationException

use of javax.xml.datatype.DatatypeConfigurationException in project jabref by JabRef.

the class BibTeXMLExportFormat method parse.

/**
     * Generic method that gets an instance of an entry type (article, book, booklet ...). It also
     * gets one bibEntry. Then the method checks all fields of the entry and then for all fields the method
     * uses the set method of the entry type with the fieldname. So for example if a bib entry has the field
     * author and the value for it is "Max Mustermann" and the given type is an article, then this method
     * will invoke <Code>article.setAuthor("Max Mustermann")</Code>. <br>
     * <br>
     * The second part of this method is that the entry type will be set to the entry. So e.g., if the type is
     * article then <Code>entry.setArticle(article)</Code> will be invoked.
     *
     * @param entryType The type parameterized type of the entry.
     * @param bibEntry  The bib entry, which fields will be set to the entryType.
     * @param entry     The bibtexml entry. The entryType will be set to this entry.
     */
private <T> void parse(T entryType, BibEntry bibEntry, Entry entry) {
    List<Method> declaredSetMethods = getListOfSetMethods(entryType);
    Map<String, String> fieldMap = bibEntry.getFieldMap();
    for (Map.Entry<String, String> entryField : fieldMap.entrySet()) {
        String value = entryField.getValue();
        String key = entryField.getKey();
        for (Method method : declaredSetMethods) {
            String methodNameWithoutSet = method.getName().replace("set", "").toLowerCase(ENGLISH);
            try {
                if ("year".equals(key) && key.equals(methodNameWithoutSet)) {
                    try {
                        XMLGregorianCalendar calendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(value);
                        method.invoke(entryType, calendar);
                    } catch (DatatypeConfigurationException e) {
                        LOGGER.error("A configuration error occured");
                    }
                    break;
                } else if ("number".equals(key) && key.equals(methodNameWithoutSet)) {
                    method.invoke(entryType, new BigInteger(value));
                    break;
                } else if (key.equals(methodNameWithoutSet)) {
                    method.invoke(entryType, value);
                    break;
                }
            } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
                LOGGER.error("Could not invoke method", e);
            }
        }
        //set the entryType to the entry
        List<Method> entryMethods = getListOfSetMethods(entry);
        for (Method method : entryMethods) {
            String methodWithoutSet = method.getName().replace("set", "");
            String simpleClassName = entryType.getClass().getSimpleName();
            if (methodWithoutSet.equals(simpleClassName)) {
                try {
                    method.invoke(entry, entryType);
                } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
                    LOGGER.warn("Could not set the type to the entry");
                }
            }
        }
    }
}
Also used : Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) DatatypeConfigurationException(javax.xml.datatype.DatatypeConfigurationException) BigInteger(java.math.BigInteger) Map(java.util.Map)

Example 12 with DatatypeConfigurationException

use of javax.xml.datatype.DatatypeConfigurationException in project jena by apache.

the class FunctionsSQL method toSQLdatetimeString.

/** Turn the lexical form of an XSD date into what SQL believes in */
public static String toSQLdatetimeString(String lex) {
    try {
        DatatypeFactory f = DatatypeFactory.newInstance();
        XMLGregorianCalendar cal = f.newXMLGregorianCalendar(lex);
        long millis = cal.toGregorianCalendar().getTimeInMillis();
        Timestamp timestamp = new Timestamp(millis);
        return timestamp.toString();
    } catch (DatatypeConfigurationException e) {
        LoggerFactory.getLogger(SQLUtils.class).warn("Failed to convert " + lex, e);
        return "0000-00-00 00:00:00";
    }
}
Also used : XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) DatatypeConfigurationException(javax.xml.datatype.DatatypeConfigurationException) DatatypeFactory(javax.xml.datatype.DatatypeFactory) Timestamp(java.sql.Timestamp)

Example 13 with DatatypeConfigurationException

use of javax.xml.datatype.DatatypeConfigurationException in project jena by apache.

the class NodeValue method getDatatypeFactory.

/**
     * Get a datatype factory using the correct classloader
     * 
     * See JENA-328. DatatypeFactory.newInstance() clashes with OSGi
     * This is clearly crazy, but DatatypeFactory is missing a very obvious
     * method newInstance(Classloader). The method that was added is very
     * hard to use correctly, as we shall see...
     */
private static DatatypeFactory getDatatypeFactory() throws DatatypeConfigurationException {
    // Step 1. Try the system property
    String dtfClass = System.getProperty(DatatypeFactory.DATATYPEFACTORY_PROPERTY);
    try {
        File jaxpPropFile = new File(System.getProperty("java.home") + File.separator + "lib" + File.separator + "jaxp.properties");
        // Step 2. Otherwise, try property in jaxp.properties
        if (dtfClass == null && jaxpPropFile.exists() && jaxpPropFile.canRead()) {
            Properties jaxp = new Properties();
            try (InputStream in = new FileInputStream(jaxpPropFile)) {
                jaxp.load(in);
                dtfClass = jaxp.getProperty(DatatypeFactory.DATATYPEFACTORY_PROPERTY);
            } catch (Exception e) {
                log.warn("Issue loading jaxp.properties", e);
            }
        }
    }// File.exists and File.canRead may throw  SecurityException (probably AccessControlException)
     catch (SecurityException ex) {
        log.warn("Security exception try to get jaxp.properties: " + ex.getMessage());
    }
    // and loading org.apache.xerces.jaxp.datatype.DatatypeFactoryImpl
    if (dtfClass == null) {
        ClassLoader cl = NodeValue.class.getClassLoader();
        Iterator<DatatypeFactory> factoryIterator = ServiceLoader.load(DatatypeFactory.class, cl).iterator();
        if (factoryIterator.hasNext())
            return factoryIterator.next();
    }
    // from the JDK rt.jar version of javax.xml.datatype.DatatypeFactory.
    if (dtfClass == null)
        dtfClass = DatatypeFactory.DATATYPEFACTORY_IMPLEMENTATION_CLASS;
    return DatatypeFactory.newInstance(dtfClass, NodeValue.class.getClassLoader());
}
Also used : DatatypeFactory(javax.xml.datatype.DatatypeFactory) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Properties(java.util.Properties) File(java.io.File) FileInputStream(java.io.FileInputStream) ARQInternalErrorException(org.apache.jena.sparql.ARQInternalErrorException) DatatypeConfigurationException(javax.xml.datatype.DatatypeConfigurationException) DatatypeFormatException(org.apache.jena.datatypes.DatatypeFormatException)

Example 14 with DatatypeConfigurationException

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);
}
Also used : Platform(org.locationtech.geogig.api.Platform) Date(java.util.Date) DatatypeConfigurationException(javax.xml.datatype.DatatypeConfigurationException) ParseException(java.text.ParseException) DatatypeConfigurationException(javax.xml.datatype.DatatypeConfigurationException) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

DatatypeConfigurationException (javax.xml.datatype.DatatypeConfigurationException)14 GregorianCalendar (java.util.GregorianCalendar)8 XMLGregorianCalendar (javax.xml.datatype.XMLGregorianCalendar)8 DatatypeFactory (javax.xml.datatype.DatatypeFactory)7 Date (java.util.Date)5 IOException (java.io.IOException)2 BigInteger (java.math.BigInteger)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 JAXBElement (javax.xml.bind.JAXBElement)2 JAXBException (javax.xml.bind.JAXBException)2 Marshaller (javax.xml.bind.Marshaller)2 QName (javax.xml.namespace.QName)2 MXBooleanType (com.ibm.maximo.MXBooleanType)1 MXDateTimeType (com.ibm.maximo.MXDateTimeType)1 MXStringType (com.ibm.maximo.MXStringType)1 FreeBusyResponseType (com.microsoft.schemas.exchange.services._2006.messages.FreeBusyResponseType)1 GetUserAvailabilityRequestType (com.microsoft.schemas.exchange.services._2006.messages.GetUserAvailabilityRequestType)1 GetUserAvailabilityResponseType (com.microsoft.schemas.exchange.services._2006.messages.GetUserAvailabilityResponseType)1 ArrayOfMailboxData (com.microsoft.schemas.exchange.services._2006.types.ArrayOfMailboxData)1