Search in sources :

Example 1 with SaxEventHandler

use of org.codice.ddf.transformer.xml.streaming.SaxEventHandler in project ddf by codice.

the class TestXmlInputTransformer method testDescribableGettersSetters.

/*
     * The methods in tested in this Test are either simple getters/setters or NOOPS
     * If any of them ever get implemented, they need to have better, more descriptive tests written.
     */
@Test
public void testDescribableGettersSetters() throws SAXException {
    SaxEventHandlerFactory factory = new XmlSaxEventHandlerFactoryImpl();
    assertThat(factory.getDescription(), is(notNullValue()));
    assertThat(factory.getId(), is(notNullValue()));
    assertThat(factory.getOrganization(), is(notNullValue()));
    assertThat(factory.getTitle(), is(notNullValue()));
    assertThat(factory.getVersion(), is(notNullValue()));
    SaxEventHandler handler = factory.getNewSaxEventHandler();
    handler.setDocumentLocator(null);
    handler.endDocument();
    handler.startPrefixMapping(null, null);
    handler.endPrefixMapping(null);
    handler.ignorableWhitespace(null, 0, 0);
    handler.processingInstruction(null, null);
    handler.skippedEntity(null);
    factory = new GmlHandlerFactory();
    assertThat(factory.getDescription(), is(notNullValue()));
    assertThat(factory.getId(), is(notNullValue()));
    assertThat(factory.getOrganization(), is(notNullValue()));
    assertThat(factory.getTitle(), is(notNullValue()));
    assertThat(factory.getVersion(), is(notNullValue()));
    handler = factory.getNewSaxEventHandler();
    handler.setDocumentLocator(null);
    handler.endDocument();
    handler.endPrefixMapping(null);
    handler.ignorableWhitespace(null, 0, 0);
    handler.processingInstruction(null, null);
    handler.skippedEntity(null);
}
Also used : SaxEventHandlerFactory(org.codice.ddf.transformer.xml.streaming.SaxEventHandlerFactory) SaxEventHandler(org.codice.ddf.transformer.xml.streaming.SaxEventHandler) Test(org.junit.Test)

Example 2 with SaxEventHandler

use of org.codice.ddf.transformer.xml.streaming.SaxEventHandler in project ddf by codice.

the class TestGenericXmlLib method testExceptionHappyHandler.

@Test
public void testExceptionHappyHandler() throws SAXException, FileNotFoundException, CatalogTransformerException {
    InputStream inputStream = new FileInputStream("src/test/resources/metacard2.xml");
    SaxEventHandlerFactory saxEventHandlerFactory = mock(SaxEventHandlerFactory.class);
    when(saxEventHandlerFactory.getId()).thenReturn("test");
    SaxEventHandler handler = getNewHandler();
    when(saxEventHandlerFactory.getNewSaxEventHandler()).thenReturn(handler);
    XmlInputTransformer xmlInputTransformer = new XmlInputTransformer();
    xmlInputTransformer.setSaxEventHandlerConfiguration(Collections.singletonList("test"));
    xmlInputTransformer.setSaxEventHandlerFactories(Collections.singletonList(saxEventHandlerFactory));
    Metacard metacard = null;
    try {
        metacard = xmlInputTransformer.transform(inputStream, "test");
    } catch (IOException e) {
        fail();
    }
    assertThat(metacard.getAttribute(Metacard.METADATA).getValue(), notNullValue());
    assertThat(metacard.getAttribute(Metacard.ID).getValue(), is("test"));
}
Also used : Metacard(ddf.catalog.data.Metacard) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) SaxEventHandlerFactory(org.codice.ddf.transformer.xml.streaming.SaxEventHandlerFactory) SaxEventHandler(org.codice.ddf.transformer.xml.streaming.SaxEventHandler) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 3 with SaxEventHandler

use of org.codice.ddf.transformer.xml.streaming.SaxEventHandler in project ddf by codice.

the class TestGenericXmlLib method testBadInputTransform.

@Test(expected = CatalogTransformerException.class)
public void testBadInputTransform() throws FileNotFoundException, CatalogTransformerException {
    InputStream inputStream = new FileInputStream("src/test/resources/metacard3InvalidXml.xml");
    SaxEventHandlerFactory saxEventHandlerFactory = mock(SaxEventHandlerFactory.class);
    when(saxEventHandlerFactory.getId()).thenReturn("test");
    SaxEventHandler handler = getNewHandler();
    when(saxEventHandlerFactory.getNewSaxEventHandler()).thenReturn(handler);
    XmlInputTransformer xmlInputTransformer = new XmlInputTransformer();
    xmlInputTransformer.setSaxEventHandlerConfiguration(Collections.singletonList("test"));
    xmlInputTransformer.setSaxEventHandlerFactories(Collections.singletonList(saxEventHandlerFactory));
    try {
        xmlInputTransformer.transform(inputStream, "test");
    } catch (IOException e) {
        fail();
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) SaxEventHandlerFactory(org.codice.ddf.transformer.xml.streaming.SaxEventHandlerFactory) SaxEventHandler(org.codice.ddf.transformer.xml.streaming.SaxEventHandler) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 4 with SaxEventHandler

use of org.codice.ddf.transformer.xml.streaming.SaxEventHandler in project ddf by codice.

the class TestGenericXmlLib method testNoConfigTransform.

@Test
public void testNoConfigTransform() throws IOException, CatalogTransformerException {
    SaxEventHandlerFactory saxEventHandlerFactory = mock(SaxEventHandlerFactory.class);
    when(saxEventHandlerFactory.getId()).thenReturn("test");
    SaxEventHandler handler = getNewHandler();
    when(saxEventHandlerFactory.getNewSaxEventHandler()).thenReturn(handler);
    XmlInputTransformer xmlInputTransformer = new XmlInputTransformer();
    xmlInputTransformer.setSaxEventHandlerFactories(Collections.singletonList(saxEventHandlerFactory));
    xmlInputTransformer.setSaxEventHandlerConfiguration(Collections.singletonList("test"));
    SaxEventHandlerDelegate delegate = xmlInputTransformer.create();
    MetacardType metacardType = delegate.getMetacardType(xmlInputTransformer.getId());
    assertThat(metacardType.getAttributeDescriptors(), is(BasicTypes.BASIC_METACARD.getAttributeDescriptors()));
}
Also used : SaxEventHandlerFactory(org.codice.ddf.transformer.xml.streaming.SaxEventHandlerFactory) SaxEventHandler(org.codice.ddf.transformer.xml.streaming.SaxEventHandler) MetacardType(ddf.catalog.data.MetacardType) Test(org.junit.Test)

Example 5 with SaxEventHandler

use of org.codice.ddf.transformer.xml.streaming.SaxEventHandler in project ddf by codice.

the class InputTransformerErrorHandler method getMetacard.

public Metacard getMetacard(String id) {
    MetacardType metacardType = getMetacardType(id);
    if (metacardType == null) {
        metacardType = BasicTypes.BASIC_METACARD;
        LOGGER.debug("No metacard type found. Defaulting to Basic Metacard.");
    }
    /*
         * Create a new MetacardImpl with the proper MetacardType
         */
    Metacard metacard = new MetacardImpl(metacardType);
    /*
             * If any major errors occur during parsing, print them out and add them to the metacard as validation errors
             */
    InputTransformerErrorHandler inputTransformerErrorHandler = (InputTransformerErrorHandler) parser.getErrorHandler();
    if (inputTransformerErrorHandler != null) {
        String parseWarningsErrors = inputTransformerErrorHandler.getParseWarningsErrors();
        if (StringUtils.isNotBlank(parseWarningsErrors)) {
            LOGGER.debug(parseWarningsErrors);
            List<Serializable> warningsAndErrors = new ArrayList<>();
            warningsAndErrors.add(parseWarningsErrors);
            if (metacard.getAttribute(Validation.VALIDATION_ERRORS) != null) {
                List<Serializable> values = metacard.getAttribute(Validation.VALIDATION_ERRORS).getValues();
                if (values != null) {
                    warningsAndErrors.addAll(values);
                }
            }
            metacard.setAttribute(new AttributeImpl(Validation.VALIDATION_ERRORS, Collections.unmodifiableList(warningsAndErrors)));
        }
    }
    /*
         * Populate metacard with all attributes constructed in SaxEventHandlers during parsing
         */
    Map<String, Boolean> multiValuedMap = saxEventHandlerUtils.getMultiValuedNameMap(metacardType.getAttributeDescriptors());
    for (SaxEventHandler eventHandler : eventHandlers) {
        List<Attribute> attributes = eventHandler.getAttributes();
        for (Attribute attribute : attributes) {
            /*
                 * If metacard already has values in the attribute, skip the attribute,
                 * instead of simply overwriting the existing values.
                 */
            if (metacard.getAttribute(attribute.getName()) == null) {
                metacard.setAttribute(attribute);
            } else if (MapUtils.getBoolean(multiValuedMap, attribute.getName(), false)) {
                metacard.getAttribute(attribute.getName()).getValues().addAll(attribute.getValues());
            }
        }
    }
    return metacard;
}
Also used : Serializable(java.io.Serializable) Attribute(ddf.catalog.data.Attribute) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) ArrayList(java.util.ArrayList) SaxEventHandler(org.codice.ddf.transformer.xml.streaming.SaxEventHandler) MetacardType(ddf.catalog.data.MetacardType) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Metacard(ddf.catalog.data.Metacard)

Aggregations

SaxEventHandler (org.codice.ddf.transformer.xml.streaming.SaxEventHandler)8 SaxEventHandlerFactory (org.codice.ddf.transformer.xml.streaming.SaxEventHandlerFactory)6 Test (org.junit.Test)6 IOException (java.io.IOException)4 Metacard (ddf.catalog.data.Metacard)3 FileInputStream (java.io.FileInputStream)3 InputStream (java.io.InputStream)3 Attribute (ddf.catalog.data.Attribute)2 MetacardType (ddf.catalog.data.MetacardType)2 AttributeImpl (ddf.catalog.data.impl.AttributeImpl)2 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)1 Serializable (java.io.Serializable)1 ArrayList (java.util.ArrayList)1