use of ddf.catalog.data.Metacard in project ddf by codice.
the class XmlInputTransformer method transform.
/**
* Takes in an XML {@link InputStream} and returns a populated {@link Metacard}
* The Metacard is populated with all attributes that have been parsed by the {@link SaxEventHandler}s
* declared in {@link XmlInputTransformer#saxEventHandlerConfiguration}s
*
* @param inputStream an XML input stream to be turned into a Metacard
* @return a populated Metacard
* @throws CatalogTransformerException
* @throws IOException
*/
public Metacard transform(InputStream inputStream) throws CatalogTransformerException {
if (inputStream == null) {
throw new CatalogTransformerException();
}
/*
* Create the necessary new SaxEventHandlerDelegate
*/
SaxEventHandlerDelegate delegate = create();
/*
* Split the input stream, so that we can use it for parsing as well as read it into the Metacard.METADATA attribute
*/
try (OutputStream baos = new ByteArrayOutputStream();
OutputStream outputStream = new BufferedOutputStream(baos);
InputStream teeInputStream = new BufferedInputStream(delegate.getMetadataStream(inputStream, outputStream))) {
/*
* Read the input stream into the metacard - where all the magic happens
*/
Metacard metacard = delegate.read(teeInputStream).getMetacard(id);
/*
* Read the metadata from the split input stream and set it on the Metacard.METADATA attribute.
* However, if the metadata is null or empty, throw an exception - we can't return a metacard
* with no metadata
*/
outputStream.flush();
String metadata = baos.toString();
if (metadata.isEmpty()) {
throw new CatalogTransformerException("Metadata is empty from output stream. Could not properly parse metacard.");
}
metacard.setAttribute(new AttributeImpl(Metacard.METADATA, metadata));
return metacard;
} catch (IOException e) {
LOGGER.debug("IO Exception during parsing", e);
throw new CatalogTransformerException("Could not finish transforming metacard because of IOException", e);
}
}
use of ddf.catalog.data.Metacard in project ddf by codice.
the class XmlInputTransformer method transform.
/**
* Takes in an XML {@link InputStream} and an ID and returns a populated {@link Metacard}
* The Metacard is populated with all attributes that have been parsed by the {@link SaxEventHandler}s
* declared in {@link XmlInputTransformer#saxEventHandlerConfiguration}s and with the specific ID
*
* @param inputStream an XML input stream to be turned into a Metacard.
* @param id the attribute value for the {@link Metacard#ID} attribute that should be set in
* the generated {@link Metacard}
* @return a populated Metacard
* @throws CatalogTransformerException
* @throws IOException
*/
public Metacard transform(InputStream inputStream, String id) throws CatalogTransformerException, IOException {
Metacard metacard = transform(inputStream);
metacard.setAttribute(new AttributeImpl(Metacard.ID, id));
return metacard;
}
use of ddf.catalog.data.Metacard 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 ddf.catalog.data.Metacard in project ddf by codice.
the class TikaInputTransformerTest method testOpenOffice.
@Test
public void testOpenOffice() throws Exception {
InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream("testOpenOffice2.odt");
/*
* The dates in testOpenOffice2.odt do not contain timezones. If no timezone is specified,
* the Tika input transformer assumes the local time zone. Set the system timezone to UTC
* so we can do assertions.
*/
TimeZone defaultTimeZone = TimeZone.getDefault();
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
Metacard metacard = transform(stream);
assertNotNull(metacard);
assertThat(metacard.getTitle(), is("Test OpenOffice2 Document"));
assertThat(convertDate(metacard.getCreatedDate()), is("2007-09-14 11:06:08 UTC"));
assertThat(convertDate(metacard.getModifiedDate()), is("2013-02-13 06:52:10 UTC"));
assertNotNull(metacard.getMetadata());
assertThat(metacard.getMetadata(), containsString("This is a sample Open Office document, written in NeoOffice 2.2.1"));
assertThat(metacard.getContentTypeName(), is("application/vnd.oasis.opendocument.text"));
// Reset timezone back to local time zone.
TimeZone.setDefault(defaultTimeZone);
assertThat(metacard.getAttribute(Core.DATATYPE).getValue(), is(DOCUMENT));
}
use of ddf.catalog.data.Metacard in project ddf by codice.
the class TikaInputTransformerTest method testXlsx.
@Test
public void testXlsx() throws Exception {
InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream("testEXCEL.xlsx");
Metacard metacard = transform(stream);
assertNotNull(metacard);
assertThat(metacard.getTitle(), is("Simple Excel document"));
assertThat(convertDate(metacard.getCreatedDate()), is("2007-10-01 16:13:56 UTC"));
assertThat(convertDate(metacard.getModifiedDate()), is("2008-12-11 16:02:17 UTC"));
assertNotNull(metacard.getMetadata());
assertThat(metacard.getMetadata(), containsString("Sample Excel Worksheet - Numbers and their Squares"));
assertThat(metacard.getContentTypeName(), is("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"));
assertThat(metacard.getAttribute(Core.DATATYPE).getValue(), is(DOCUMENT));
}
Aggregations