Search in sources :

Example 41 with Metacard

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);
    }
}
Also used : Metacard(ddf.catalog.data.Metacard) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream)

Example 42 with Metacard

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;
}
Also used : Metacard(ddf.catalog.data.Metacard) AttributeImpl(ddf.catalog.data.impl.AttributeImpl)

Example 43 with 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"));
}
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 44 with Metacard

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));
}
Also used : TimeZone(java.util.TimeZone) Metacard(ddf.catalog.data.Metacard) InputStream(java.io.InputStream) Test(org.junit.Test)

Example 45 with Metacard

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));
}
Also used : Metacard(ddf.catalog.data.Metacard) InputStream(java.io.InputStream) Test(org.junit.Test)

Aggregations

Metacard (ddf.catalog.data.Metacard)746 Test (org.junit.Test)470 ArrayList (java.util.ArrayList)206 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)149 InputStream (java.io.InputStream)136 HashMap (java.util.HashMap)129 AttributeImpl (ddf.catalog.data.impl.AttributeImpl)115 Result (ddf.catalog.data.Result)109 Serializable (java.io.Serializable)100 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)90 QueryRequest (ddf.catalog.operation.QueryRequest)84 QueryImpl (ddf.catalog.operation.impl.QueryImpl)80 QueryResponse (ddf.catalog.operation.QueryResponse)78 SourceResponse (ddf.catalog.operation.SourceResponse)76 IOException (java.io.IOException)75 List (java.util.List)74 Map (java.util.Map)67 Filter (org.opengis.filter.Filter)67 CreateResponse (ddf.catalog.operation.CreateResponse)66 HashSet (java.util.HashSet)65