Search in sources :

Example 26 with DatatypeConfigurationException

use of javax.xml.datatype.DatatypeConfigurationException in project iaf by ibissource.

the class IdinSenderTest method getDateTimestamp.

private XMLGregorianCalendar getDateTimestamp() {
    GregorianCalendar c = new GregorianCalendar();
    // EPOCH
    c.setTime(new Date(0));
    try {
        return DatatypeFactory.newInstance().newXMLGregorianCalendar(c);
    } catch (DatatypeConfigurationException e) {
        return null;
    }
}
Also used : DatatypeConfigurationException(javax.xml.datatype.DatatypeConfigurationException) GregorianCalendar(java.util.GregorianCalendar) XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) Date(java.util.Date)

Example 27 with DatatypeConfigurationException

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

the class NotificationXmlFormatter method format.

public String format(Notification aNotification) throws NotificationFormatException {
    ObjectHelper.notNull(jaxbContext, "jaxbContext");
    NotificationEventType jaxb;
    boolean wrap = false;
    if (aNotification instanceof AttributeChangeNotification) {
        AttributeChangeNotification ac = (AttributeChangeNotification) aNotification;
        jaxb = mObjectFactory.createAttributeChangeNotification().withAttributeName(ac.getAttributeName()).withAttributeType(ac.getAttributeType()).withNewValue(ac.getNewValue() == null ? null : String.valueOf(ac.getNewValue())).withOldValue(ac.getOldValue() == null ? null : String.valueOf(ac.getOldValue()));
    } else if (aNotification instanceof JMXConnectionNotification) {
        jaxb = mObjectFactory.createJMXConnectionNotification().withConnectionId(((JMXConnectionNotification) aNotification).getConnectionId());
    } else if (aNotification instanceof MBeanServerNotification) {
        jaxb = mObjectFactory.createMBeanServerNotification().withMBeanName(String.valueOf(((MBeanServerNotification) aNotification).getMBeanName()));
    } else if (aNotification instanceof MonitorNotification) {
        MonitorNotification mn = (MonitorNotification) aNotification;
        jaxb = mObjectFactory.createMonitorNotification().withDerivedGauge(String.valueOf(mn.getDerivedGauge())).withObservedAttribute(mn.getObservedAttribute()).withObservedObject(String.valueOf(mn.getObservedObject())).withTrigger(String.valueOf(mn.getTrigger()));
    } else if (aNotification instanceof RelationNotification) {
        RelationNotification rn = (RelationNotification) aNotification;
        jaxb = mObjectFactory.createRelationNotification().withObjectName(String.valueOf(rn.getObjectName())).withRelationId(rn.getRelationId()).withRelationTypeName(rn.getRelationTypeName()).withRoleName(rn.getRoleName());
        if (rn.getNewRoleValue() != null) {
            ObjectNamesType ont = toObjectNamesType(rn.getNewRoleValue());
            ((org.apache.camel.component.jmx.jaxb.RelationNotification) jaxb).withNewRoleValue(ont);
        }
        if (rn.getOldRoleValue() != null) {
            ObjectNamesType ont = toObjectNamesType(rn.getOldRoleValue());
            ((org.apache.camel.component.jmx.jaxb.RelationNotification) jaxb).withOldRoleValue(ont);
        }
        if (rn.getMBeansToUnregister() != null) {
            ObjectNamesType ont = toObjectNamesType(rn.getMBeansToUnregister());
            ((org.apache.camel.component.jmx.jaxb.RelationNotification) jaxb).withMBeansToUnregister(ont);
        }
    } else if (aNotification instanceof TimerNotification) {
        jaxb = mObjectFactory.createTimerNotification().withNotificationId(((TimerNotification) aNotification).getNotificationID());
    } else {
        jaxb = mObjectFactory.createNotificationEventType();
        wrap = true;
    }
    // add all of the common properties
    jaxb.withMessage(aNotification.getMessage()).withSequence(aNotification.getSequenceNumber()).withSource(String.valueOf(aNotification.getSource())).withTimestamp(aNotification.getTimeStamp()).withType(aNotification.getType());
    if (aNotification.getUserData() != null) {
        jaxb.withUserData(String.valueOf(aNotification.getUserData()));
    }
    try {
        DatatypeFactory df = getDatatypeFactory();
        Date date = new Date(aNotification.getTimeStamp());
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(date);
        jaxb.withDateTime(df.newXMLGregorianCalendar(gc));
        Object bean = wrap ? mObjectFactory.createNotificationEvent(jaxb) : jaxb;
        StringWriter sw = new StringWriter();
        // must create a new marshaller as its not thread safe
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.marshal(bean, sw);
        return sw.toString();
    } catch (JAXBException e) {
        throw new NotificationFormatException(e);
    } catch (DatatypeConfigurationException e) {
        throw new NotificationFormatException(e);
    }
}
Also used : MonitorNotification(javax.management.monitor.MonitorNotification) Marshaller(javax.xml.bind.Marshaller) AttributeChangeNotification(javax.management.AttributeChangeNotification) DatatypeFactory(javax.xml.datatype.DatatypeFactory) MBeanServerNotification(javax.management.MBeanServerNotification) JAXBException(javax.xml.bind.JAXBException) GregorianCalendar(java.util.GregorianCalendar) RelationNotification(javax.management.relation.RelationNotification) TimerNotification(javax.management.timer.TimerNotification) Date(java.util.Date) DatatypeConfigurationException(javax.xml.datatype.DatatypeConfigurationException) StringWriter(java.io.StringWriter) NotificationEventType(org.apache.camel.component.jmx.jaxb.NotificationEventType) ObjectNamesType(org.apache.camel.component.jmx.jaxb.ObjectNamesType) JMXConnectionNotification(javax.management.remote.JMXConnectionNotification)

Example 28 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 29 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 30 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)

Aggregations

DatatypeConfigurationException (javax.xml.datatype.DatatypeConfigurationException)56 XMLGregorianCalendar (javax.xml.datatype.XMLGregorianCalendar)35 GregorianCalendar (java.util.GregorianCalendar)33 DatatypeFactory (javax.xml.datatype.DatatypeFactory)20 Date (java.util.Date)18 BigInteger (java.math.BigInteger)6 ParseException (java.text.ParseException)6 JAXBException (javax.xml.bind.JAXBException)5 QName (javax.xml.namespace.QName)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 ConverterException (ch.ehi.ili2db.converter.ConverterException)2 BlackboxType (ch.interlis.ili2c.metamodel.BlackboxType)2 EnumerationType (ch.interlis.ili2c.metamodel.EnumerationType)2 NumericType (ch.interlis.ili2c.metamodel.NumericType)2 PrecisionDecimal (ch.interlis.ili2c.metamodel.PrecisionDecimal)2 IomObject (ch.interlis.iom.IomObject)2 ArrayOfString (com.marketo.mktows.ArrayOfString)2 LastUpdateAtSelector (com.marketo.mktows.LastUpdateAtSelector)2 File (java.io.File)2