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);
}
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"));
}
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();
}
}
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()));
}
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;
}
Aggregations