Search in sources :

Example 16 with DatatypeFactory

use of javax.xml.datatype.DatatypeFactory 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 17 with DatatypeFactory

use of javax.xml.datatype.DatatypeFactory in project ddf by codice.

the class PredicateTest method testTemporalNullOperation.

@Test
public void testTemporalNullOperation() throws Exception {
    String methodName = "testTemporalNullOperation";
    LOGGER.debug("***************  START: {}  *****************", methodName);
    MockQuery query = new MockQuery();
    DatatypeFactory df = DatatypeFactory.newInstance();
    XMLGregorianCalendar start = df.newXMLGregorianCalendarDate(2011, 10, 25, 0);
    XMLGregorianCalendar end = df.newXMLGregorianCalendarDate(2011, 10, 27, 0);
    query.addTemporalFilter(start, end, Metacard.EFFECTIVE);
    SubscriptionFilterVisitor visitor = new SubscriptionFilterVisitor();
    Predicate pred = (Predicate) query.getFilter().accept(visitor, null);
    LOGGER.debug("Resulting Predicate: {}", pred);
    Filter filter = query.getFilter();
    FilterTransformer transform = new FilterTransformer();
    transform.setIndentation(2);
    String filterXml = transform.transform(filter);
    LOGGER.debug(filterXml);
    // input that passes temporal
    LOGGER.debug("\npass temporal.\n");
    MetacardImpl metacard = new MetacardImpl();
    metacard.setCreatedDate(new Date());
    metacard.setExpirationDate(new Date());
    metacard.setModifiedDate(new Date());
    metacard.setMetadata(TestDataLibrary.getCatAndDogEntry());
    XMLGregorianCalendar cal = df.newXMLGregorianCalendarDate(2011, 10, 26, 0);
    Date effectiveDate = cal.toGregorianCalendar().getTime();
    metacard.setEffectiveDate(effectiveDate);
    HashMap<String, Object> properties = new HashMap<>();
    properties.put(PubSubConstants.HEADER_OPERATION_KEY, null);
    Map<String, Object> contextualMap = constructContextualMap(metacard);
    properties.put(PubSubConstants.HEADER_CONTEXTUAL_KEY, contextualMap);
    properties.put(PubSubConstants.HEADER_ENTRY_KEY, metacard);
    Event testEvent = new Event("topic", properties);
    boolean b = pred.matches(testEvent);
    assertTrue(b);
    LOGGER.debug("***************  END: {}  *****************", methodName);
}
Also used : DatatypeFactory(javax.xml.datatype.DatatypeFactory) SubscriptionFilterVisitor(ddf.catalog.pubsub.internal.SubscriptionFilterVisitor) HashMap(java.util.HashMap) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Date(java.util.Date) GeospatialPredicate(ddf.catalog.pubsub.predicate.GeospatialPredicate) ContentTypePredicate(ddf.catalog.pubsub.predicate.ContentTypePredicate) Predicate(ddf.catalog.pubsub.predicate.Predicate) XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) Filter(org.opengis.filter.Filter) Event(org.osgi.service.event.Event) FilterTransformer(org.geotools.filter.FilterTransformer) Test(org.junit.Test)

Example 18 with DatatypeFactory

use of javax.xml.datatype.DatatypeFactory in project ddf by codice.

the class PredicateTest method testTemporalNullMetadata.

@Test
public void testTemporalNullMetadata() throws Exception {
    String methodName = "testTemporalNullMetadata";
    LOGGER.debug("***************  START: {}  *****************", methodName);
    MockQuery query = new MockQuery();
    DatatypeFactory df = DatatypeFactory.newInstance();
    XMLGregorianCalendar start = df.newXMLGregorianCalendarDate(2011, 10, 25, 0);
    XMLGregorianCalendar end = df.newXMLGregorianCalendarDate(2011, 10, 27, 0);
    query.addTemporalFilter(start, end, Metacard.EFFECTIVE);
    SubscriptionFilterVisitor visitor = new SubscriptionFilterVisitor();
    Predicate pred = (Predicate) query.getFilter().accept(visitor, null);
    LOGGER.debug("Resulting Predicate: {}", pred);
    Filter filter = query.getFilter();
    FilterTransformer transform = new FilterTransformer();
    transform.setIndentation(2);
    String filterXml = transform.transform(filter);
    LOGGER.debug(filterXml);
    // input that passes temporal
    LOGGER.debug("\npass temporal.\n");
    MetacardImpl metacard = new MetacardImpl();
    metacard.setCreatedDate(new Date());
    metacard.setExpirationDate(new Date());
    metacard.setModifiedDate(new Date());
    XMLGregorianCalendar cal = df.newXMLGregorianCalendarDate(2011, 10, 26, 0);
    Date effectiveDate = cal.toGregorianCalendar().getTime();
    metacard.setEffectiveDate(effectiveDate);
    HashMap<String, Object> properties = new HashMap<>();
    properties.put(PubSubConstants.HEADER_OPERATION_KEY, PubSubConstants.CREATE);
    // no contextual map containing indexed contextual data
    properties.put(PubSubConstants.HEADER_ENTRY_KEY, metacard);
    Event testEvent = new Event("topic", properties);
    boolean b = pred.matches(testEvent);
    assertTrue(b);
    LOGGER.debug("***************  END: {}  *****************", methodName);
}
Also used : DatatypeFactory(javax.xml.datatype.DatatypeFactory) SubscriptionFilterVisitor(ddf.catalog.pubsub.internal.SubscriptionFilterVisitor) HashMap(java.util.HashMap) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Date(java.util.Date) GeospatialPredicate(ddf.catalog.pubsub.predicate.GeospatialPredicate) ContentTypePredicate(ddf.catalog.pubsub.predicate.ContentTypePredicate) Predicate(ddf.catalog.pubsub.predicate.Predicate) XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) Filter(org.opengis.filter.Filter) Event(org.osgi.service.event.Event) FilterTransformer(org.geotools.filter.FilterTransformer) Test(org.junit.Test)

Example 19 with DatatypeFactory

use of javax.xml.datatype.DatatypeFactory in project ddf by codice.

the class PredicateTest method testMultipleCriteriaWithContentTypes.

@Test
public void testMultipleCriteriaWithContentTypes() throws Exception {
    String methodName = "testMultipleCriteriaWithContentTypes";
    LOGGER.debug("***************  START: {}  *****************", methodName);
    MockQuery query = new MockQuery();
    DatatypeFactory df = DatatypeFactory.newInstance();
    XMLGregorianCalendar start = df.newXMLGregorianCalendarDate(2011, 10, 25, 0);
    XMLGregorianCalendar end = df.newXMLGregorianCalendarDate(2011, 10, 27, 0);
    query.addTemporalFilter(start, end, Metacard.EFFECTIVE);
    // create content type criteria
    String version1 = "version1";
    String type1 = "type1";
    List<MockTypeVersionsExtension> extensions = new ArrayList<>();
    MockTypeVersionsExtension ext1 = new MockTypeVersionsExtension();
    List<String> ext1Versions = ext1.getVersions();
    ext1Versions.add(version1);
    ext1.setExtensionTypeName(type1);
    extensions.add(ext1);
    query.addTypeFilter(extensions);
    SubscriptionFilterVisitor visitor = new SubscriptionFilterVisitor();
    Predicate pred = (Predicate) query.getFilter().accept(visitor, null);
    LOGGER.debug("Resulting Predicate: {}", pred);
    Filter filter = query.getFilter();
    FilterTransformer transform = new FilterTransformer();
    transform.setIndentation(2);
    String filterXml = transform.transform(filter);
    LOGGER.debug(filterXml);
    // input that passes both temporal and content type
    LOGGER.debug("\npass temporal and pass content type.\n");
    MetacardImpl metacard = new MetacardImpl();
    metacard.setCreatedDate(new Date());
    metacard.setExpirationDate(new Date());
    metacard.setModifiedDate(new Date());
    metacard.setMetadata(TestDataLibrary.getCatAndDogEntry());
    XMLGregorianCalendar cal = df.newXMLGregorianCalendarDate(2011, 10, 26, 0);
    Date effectiveDate = cal.toGregorianCalendar().getTime();
    metacard.setEffectiveDate(effectiveDate);
    HashMap<String, Object> properties = new HashMap<>();
    properties.put(PubSubConstants.HEADER_OPERATION_KEY, PubSubConstants.CREATE);
    Map<String, Object> contextualMap = constructContextualMap(metacard);
    properties.put(PubSubConstants.HEADER_CONTEXTUAL_KEY, contextualMap);
    // Above Pulled from PubSubProviderImpl
    properties.put(PubSubConstants.HEADER_CONTENT_TYPE_KEY, type1 + "," + version1);
    properties.put(PubSubConstants.HEADER_ENTRY_KEY, metacard);
    Event testEvent = new Event("topic", properties);
    boolean b = pred.matches(testEvent);
    assertTrue(b);
    // input that fails both temporal and content type
    LOGGER.debug("\nfail temporal.  fail content type.\n");
    // time out of
    XMLGregorianCalendar cal1 = df.newXMLGregorianCalendarDate(2012, 10, 30, 0);
    // range
    Date effectiveDate1 = cal1.toGregorianCalendar().getTime();
    metacard.setEffectiveDate(effectiveDate1);
    LOGGER.debug("metacard date: {}", metacard.getEffectiveDate());
    properties.clear();
    properties.put(PubSubConstants.HEADER_OPERATION_KEY, PubSubConstants.CREATE);
    properties.put(PubSubConstants.HEADER_CONTEXTUAL_KEY, contextualMap);
    properties.put(PubSubConstants.HEADER_CONTENT_TYPE_KEY, "invalid_type" + "," + version1);
    properties.put(PubSubConstants.HEADER_ENTRY_KEY, metacard);
    testEvent = new Event("topic", properties);
    assertFalse(pred.matches(testEvent));
    // input that passes temporal and fails content type
    LOGGER.debug("\npass temporal.  fail content type\n");
    // time in
    XMLGregorianCalendar cal2 = df.newXMLGregorianCalendarDate(2011, 10, 26, 0);
    // range
    Date effectiveDate2 = cal2.toGregorianCalendar().getTime();
    metacard.setEffectiveDate(effectiveDate2);
    LOGGER.debug("metacard date: {}", metacard.getEffectiveDate());
    properties.clear();
    properties.put(PubSubConstants.HEADER_OPERATION_KEY, PubSubConstants.CREATE);
    properties.put(PubSubConstants.HEADER_CONTEXTUAL_KEY, contextualMap);
    properties.put(PubSubConstants.HEADER_CONTENT_TYPE_KEY, "invalid_type" + "," + version1);
    properties.put(PubSubConstants.HEADER_ENTRY_KEY, metacard);
    testEvent = new Event("topic", properties);
    assertFalse(pred.matches(testEvent));
    // input that fails temporal and passes content type
    LOGGER.debug("\nfail temporal.  pass content type\n");
    // time out of
    XMLGregorianCalendar cal3 = df.newXMLGregorianCalendarDate(2012, 10, 26, 0);
    // range
    Date effectiveDate3 = cal3.toGregorianCalendar().getTime();
    metacard.setEffectiveDate(effectiveDate3);
    LOGGER.debug("metacard date: {}", metacard.getEffectiveDate());
    properties.clear();
    properties.put(PubSubConstants.HEADER_OPERATION_KEY, PubSubConstants.CREATE);
    properties.put(PubSubConstants.HEADER_CONTEXTUAL_KEY, contextualMap);
    properties.put(PubSubConstants.HEADER_CONTENT_TYPE_KEY, type1 + "," + version1);
    properties.put(PubSubConstants.HEADER_ENTRY_KEY, metacard);
    testEvent = new Event("topic", properties);
    assertFalse(pred.matches(testEvent));
    // multiple content types
    LOGGER.debug("\nTesting multiple content types.\n");
    String type2 = "type2";
    String version2 = "version2";
    MockTypeVersionsExtension ext2 = new MockTypeVersionsExtension();
    List<String> ext2Versions = ext2.getVersions();
    ext2Versions.add(version2);
    ext2.setExtensionTypeName(type2);
    extensions.add(ext2);
    // No version
    String type3 = "type3";
    MockTypeVersionsExtension ext3 = new MockTypeVersionsExtension();
    ext3.setExtensionTypeName(type3);
    extensions.add(ext3);
    MockQuery query2 = new MockQuery();
    query2.addTemporalFilter(start, end, Metacard.EFFECTIVE);
    query2.addTypeFilter(extensions);
    SubscriptionFilterVisitor visitor1 = new SubscriptionFilterVisitor();
    Predicate pred1 = (Predicate) query2.getFilter().accept(visitor1, null);
    LOGGER.debug("resulting predicate: " + pred1);
    // Create metacard for input
    // time and contentType match
    // time in
    XMLGregorianCalendar cal4 = df.newXMLGregorianCalendarDate(2011, 10, 26, 0);
    // range
    Date effectiveDate4 = cal4.toGregorianCalendar().getTime();
    metacard.setEffectiveDate(effectiveDate4);
    LOGGER.debug("metacard date: {}", metacard.getEffectiveDate());
    properties.clear();
    properties.put(PubSubConstants.HEADER_OPERATION_KEY, PubSubConstants.CREATE);
    properties.put(PubSubConstants.HEADER_CONTEXTUAL_KEY, contextualMap);
    properties.put(PubSubConstants.HEADER_CONTENT_TYPE_KEY, type1 + "," + version1);
    properties.put(PubSubConstants.HEADER_ENTRY_KEY, metacard);
    testEvent = new Event("topic", properties);
    assertTrue(pred1.matches(testEvent));
    // time and contentType match against content type 3 with any version
    // time in
    XMLGregorianCalendar cal5 = df.newXMLGregorianCalendarDate(2011, 10, 26, 0);
    // range
    Date effectiveDate5 = cal5.toGregorianCalendar().getTime();
    metacard.setEffectiveDate(effectiveDate5);
    LOGGER.debug("metacard date: {}", metacard.getEffectiveDate());
    properties.clear();
    properties.put(PubSubConstants.HEADER_OPERATION_KEY, PubSubConstants.CREATE);
    properties.put(PubSubConstants.HEADER_CONTEXTUAL_KEY, contextualMap);
    properties.put(PubSubConstants.HEADER_CONTENT_TYPE_KEY, type3 + "," + "random_version");
    properties.put(PubSubConstants.HEADER_ENTRY_KEY, metacard);
    testEvent = new Event("topic", properties);
    assertTrue(pred1.matches(testEvent));
    // time matches and contentType matches type2
    // time in
    XMLGregorianCalendar cal6 = df.newXMLGregorianCalendarDate(2011, 10, 26, 0);
    // range
    Date effectiveDate6 = cal6.toGregorianCalendar().getTime();
    metacard.setEffectiveDate(effectiveDate6);
    LOGGER.debug("metacard date: {}", metacard.getEffectiveDate());
    properties.clear();
    properties.put(PubSubConstants.HEADER_OPERATION_KEY, PubSubConstants.CREATE);
    properties.put(PubSubConstants.HEADER_CONTEXTUAL_KEY, contextualMap);
    properties.put(PubSubConstants.HEADER_CONTENT_TYPE_KEY, type2 + "," + version2);
    properties.put(PubSubConstants.HEADER_ENTRY_KEY, metacard);
    testEvent = new Event("topic", properties);
    assertTrue(pred1.matches(testEvent));
    // time matches and content type doesn't match
    // time in
    XMLGregorianCalendar cal7 = df.newXMLGregorianCalendarDate(2011, 10, 26, 0);
    // range
    Date effectiveDate7 = cal7.toGregorianCalendar().getTime();
    metacard.setEffectiveDate(effectiveDate7);
    LOGGER.debug("metacard date: {}", metacard.getEffectiveDate());
    properties.clear();
    properties.put(PubSubConstants.HEADER_OPERATION_KEY, PubSubConstants.CREATE);
    properties.put(PubSubConstants.HEADER_CONTEXTUAL_KEY, contextualMap);
    properties.put(PubSubConstants.HEADER_CONTENT_TYPE_KEY, type2 + "," + version1);
    properties.put(PubSubConstants.HEADER_ENTRY_KEY, metacard);
    testEvent = new Event("topic", properties);
    assertFalse(pred1.matches(testEvent));
    LOGGER.debug("***************  END: {}  *****************", methodName);
}
Also used : DatatypeFactory(javax.xml.datatype.DatatypeFactory) SubscriptionFilterVisitor(ddf.catalog.pubsub.internal.SubscriptionFilterVisitor) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Date(java.util.Date) GeospatialPredicate(ddf.catalog.pubsub.predicate.GeospatialPredicate) ContentTypePredicate(ddf.catalog.pubsub.predicate.ContentTypePredicate) Predicate(ddf.catalog.pubsub.predicate.Predicate) XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) Filter(org.opengis.filter.Filter) Event(org.osgi.service.event.Event) FilterTransformer(org.geotools.filter.FilterTransformer) Test(org.junit.Test)

Example 20 with DatatypeFactory

use of javax.xml.datatype.DatatypeFactory in project podam by devopsfolks.

the class XmlTypesExternalFactory method manufacturePojo.

@Override
public <T> T manufacturePojo(Class<T> pojoClass, Type... genericTypeArgs) {
    try {
        if (pojoClass.isAssignableFrom(XMLGregorianCalendar.class)) {
            DatatypeFactory factory = DatatypeFactory.newInstance();
            @SuppressWarnings("unchecked") T calendar = (T) factory.newXMLGregorianCalendar(new GregorianCalendar());
            LOG.info("Externally created XMLGregorianCalendar");
            return calendar;
        } else if (pojoClass.isAssignableFrom(Duration.class)) {
            DatatypeFactory factory = DatatypeFactory.newInstance();
            @SuppressWarnings("unchecked") T duration = (T) factory.newDuration(0L);
            LOG.info("Externally created Duration");
            return duration;
        }
    } catch (Exception e) {
        throw new IllegalStateException("Manufacturing failed", e);
    }
    return null;
}
Also used : DatatypeFactory(javax.xml.datatype.DatatypeFactory) GregorianCalendar(java.util.GregorianCalendar) XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) Duration(javax.xml.datatype.Duration)

Aggregations

DatatypeFactory (javax.xml.datatype.DatatypeFactory)22 XMLGregorianCalendar (javax.xml.datatype.XMLGregorianCalendar)17 GregorianCalendar (java.util.GregorianCalendar)10 DatatypeConfigurationException (javax.xml.datatype.DatatypeConfigurationException)9 Date (java.util.Date)7 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)5 SubscriptionFilterVisitor (ddf.catalog.pubsub.internal.SubscriptionFilterVisitor)5 ContentTypePredicate (ddf.catalog.pubsub.predicate.ContentTypePredicate)5 GeospatialPredicate (ddf.catalog.pubsub.predicate.GeospatialPredicate)5 Predicate (ddf.catalog.pubsub.predicate.Predicate)5 HashMap (java.util.HashMap)5 FilterTransformer (org.geotools.filter.FilterTransformer)5 Test (org.junit.Test)5 Filter (org.opengis.filter.Filter)5 Event (org.osgi.service.event.Event)5 ArrayList (java.util.ArrayList)3 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