Search in sources :

Example 36 with Attribute

use of ddf.catalog.data.Attribute in project ddf by codice.

the class IntegrationTest method testInputAndOutput.

@Test
public void testInputAndOutput() throws CatalogTransformerException, IOException {
    Parser parser = new XmlParser();
    InputTransformer inputTransformer = new XmlInputTransformer(parser);
    MetacardMarshaller metacardMarshaller = new MetacardMarshallerImpl(parser, new PrintWriterProviderImpl());
    MetacardTransformer outputTransformer = new XmlMetacardTransformer(metacardMarshaller);
    InputStream input = getClass().getResourceAsStream("/extensibleMetacard.xml");
    Metacard metacard = inputTransformer.transform(input);
    LOGGER.info("Attributes: ");
    for (AttributeDescriptor descriptor : metacard.getMetacardType().getAttributeDescriptors()) {
        Attribute attribute = metacard.getAttribute(descriptor.getName());
        LOGGER.info("\t" + descriptor.getName() + ": " + ((attribute == null) ? attribute : attribute.getValue()));
    }
    BinaryContent output = outputTransformer.transform(metacard, mockArguments);
    String outputString = new String(output.getByteArray());
    // TODO test equivalence with XMLUnit.
    LOGGER.info(outputString);
}
Also used : XmlParser(org.codice.ddf.parser.xml.XmlParser) MetacardTransformer(ddf.catalog.transform.MetacardTransformer) XmlMetacardTransformer(ddf.catalog.transformer.xml.XmlMetacardTransformer) Attribute(ddf.catalog.data.Attribute) MetacardMarshaller(ddf.catalog.transformer.api.MetacardMarshaller) InputStream(java.io.InputStream) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) XmlMetacardTransformer(ddf.catalog.transformer.xml.XmlMetacardTransformer) XmlInputTransformer(ddf.catalog.transformer.xml.XmlInputTransformer) InputTransformer(ddf.catalog.transform.InputTransformer) BinaryContent(ddf.catalog.data.BinaryContent) Parser(org.codice.ddf.parser.Parser) XmlParser(org.codice.ddf.parser.xml.XmlParser) MetacardMarshallerImpl(ddf.catalog.transformer.xml.MetacardMarshallerImpl) Metacard(ddf.catalog.data.Metacard) PrintWriterProviderImpl(ddf.catalog.transformer.xml.PrintWriterProviderImpl) XmlInputTransformer(ddf.catalog.transformer.xml.XmlInputTransformer) Test(org.junit.Test)

Example 37 with Attribute

use of ddf.catalog.data.Attribute in project ddf by codice.

the class TestXmlInputTransformer method testTransformWithExtensibleMetacardType.

@Test
public void testTransformWithExtensibleMetacardType() throws IOException, CatalogTransformerException {
    List<MetacardType> metacardTypes = new ArrayList<MetacardType>(1);
    MetacardType extensibleType = new MetacardTypeImpl("extensible.metacard", BasicTypes.BASIC_METACARD.getAttributeDescriptors());
    metacardTypes.add(extensibleType);
    xit.setMetacardTypes(metacardTypes);
    Metacard metacard = xit.transform(new FileInputStream("src/test/resources/extensibleMetacard.xml"));
    LOGGER.info("ID: {}", metacard.getId());
    LOGGER.info("Type: {}", metacard.getMetacardType().getName());
    LOGGER.info("Source: {}", metacard.getSourceId());
    LOGGER.info("Attributes: ");
    for (AttributeDescriptor descriptor : metacard.getMetacardType().getAttributeDescriptors()) {
        Attribute attribute = metacard.getAttribute(descriptor.getName());
        LOGGER.info("\t" + descriptor.getName() + ": " + ((attribute == null) ? attribute : attribute.getValue()));
    }
}
Also used : Metacard(ddf.catalog.data.Metacard) Attribute(ddf.catalog.data.Attribute) ArrayList(java.util.ArrayList) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) MetacardTypeImpl(ddf.catalog.data.impl.MetacardTypeImpl) MetacardType(ddf.catalog.data.MetacardType) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 38 with Attribute

use of ddf.catalog.data.Attribute in project ddf by codice.

the class MetacardImpl method writeObject.

/**
     * Serializes this {@link MetacardImpl} instance.
     *
     * @param stream the {@link ObjectOutputStream} that contains the object to be serialized
     * @throws IOException
     * @serialData First, all non-transient fields are written out by the default Java serialization
     * implementation ( {@link ObjectOutputStream#defaultWriteObject()}) . Next, the
     * {@link MetacardType} is written out as a {@link MetacardTypeImpl}. Then the
     * <i>number</i> of {@code Attribute} objects is written as an {@code int}. After
     * the number of objects, each {@code Attribute} object is written out.
     * <p>
     * <p>
     * The MetacardType object is written out as a {@link MetacardTypeImpl} object
     * because {@link MetacardTypeImpl} is a class that is part of the DDF API and is
     * guaranteed to be on the classpath when this object is deserialized. Secondly, the
     * {@link MetacardTypeImpl} has a trusted serialization implementation where the
     * object's logical representation is serialized.
     * </p>
     */
private void writeObject(ObjectOutputStream stream) throws IOException {
    /*
         * defaultWriteObject() is invoked for greater flexibility and compatibility. See the
         * *Serialization Note* in MetacardImpl's class Javadoc.
         */
    stream.defaultWriteObject();
    /*
         * Cannot allow unknown implementations of MetacardType to be serialized. Must convert them
         * to our implementation to guarantee it is serializing the logical representation and not
         * the physical representation.
         */
    if (type instanceof MetacardTypeImpl) {
        stream.writeObject(type);
    } else {
        MetacardTypeImpl mt = new MetacardTypeImpl(type.getName(), type.getAttributeDescriptors());
        stream.writeObject(mt);
    }
    if (map != null) {
        stream.writeInt(map.size());
        for (Attribute attribute : this.map.values()) {
            stream.writeObject(attribute);
        }
    } else {
        if (wrappedMetacard != null && wrappedMetacard.getMetacardType() != null) {
            MetacardType metacardType = wrappedMetacard.getMetacardType();
            List<Attribute> attributes = new ArrayList<Attribute>();
            if (metacardType.getAttributeDescriptors() == null) {
                // no descriptors, means no attributes can be defined.
                // no attributes defined, means no attributes written to
                // disk
                stream.writeInt(0);
            } else {
                for (AttributeDescriptor ad : metacardType.getAttributeDescriptors()) {
                    Attribute attribute = wrappedMetacard.getAttribute(ad.getName());
                    if (attribute != null) {
                        attributes.add(attribute);
                    }
                }
                // Must loop again because the size of the attributes list
                // is not known until list has been fully populated.
                stream.writeInt(attributes.size());
                for (Attribute attribute : attributes) {
                    stream.writeObject(attribute);
                }
            }
        }
    }
}
Also used : Attribute(ddf.catalog.data.Attribute) ArrayList(java.util.ArrayList) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) MetacardType(ddf.catalog.data.MetacardType)

Example 39 with Attribute

use of ddf.catalog.data.Attribute in project ddf by codice.

the class MetacardImpl method requestData.

/**
     * The brains of the operation -- does the interaction with the map or the wrapped metacard.
     *
     * @param <T>           the type of the Attribute value expected
     * @param attributeName the name of the {@link Attribute} to retrieve
     * @param returnType    the class that the value of the {@link ddf.catalog.data.AttributeType} is expected to be bound to
     * @return the value of the requested {@link Attribute} name
     */
protected <T> T requestData(String attributeName, Class<T> returnType) {
    Attribute attribute = getAttribute(attributeName);
    if (attribute == null) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Attribute {} was not found, returning null", attributeName);
        }
        return null;
    }
    Serializable data = attribute.getValue();
    if (data == null) {
        return null;
    }
    if (returnType.isAssignableFrom(data.getClass())) {
        return returnType.cast(data);
    } else {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug(data.getClass().toString() + " can not be assigned to " + returnType.toString());
        }
    }
    return null;
}
Also used : Serializable(java.io.Serializable) Attribute(ddf.catalog.data.Attribute)

Example 40 with Attribute

use of ddf.catalog.data.Attribute in project ddf by codice.

the class MetacardImpl method readObject.

/**
     * Deserializes this {@link MetacardImpl}'s instance.
     *
     * @param stream the {@link ObjectInputStream} that contains the bytes of the object
     * @throws IOException
     * @throws ClassNotFoundException
     */
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
    /*
         * defaultReadObject() is invoked for greater flexibility and compatibility. See the
         * *Serialization Note* in MetacardImpl's class Javadoc.
         */
    stream.defaultReadObject();
    map = new HashMap<String, Attribute>();
    wrappedMetacard = null;
    type = (MetacardType) stream.readObject();
    if (type == null) {
        throw new InvalidObjectException(MetacardType.class.getName() + " instance cannot be null.");
    }
    int numElements = stream.readInt();
    for (int i = 0; i < numElements; i++) {
        Attribute attribute = (Attribute) stream.readObject();
        if (attribute != null) {
            AttributeDescriptor attributeDescriptor = getMetacardType().getAttributeDescriptor(attribute.getName());
            if (attributeDescriptor != null && attribute.getValue() != null) {
                attributeDescriptor.getType().getAttributeFormat();
                attributeDescriptor.getType().getClass();
            }
        }
        setAttribute(attribute);
    }
}
Also used : Attribute(ddf.catalog.data.Attribute) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) InvalidObjectException(java.io.InvalidObjectException)

Aggregations

Attribute (ddf.catalog.data.Attribute)103 Metacard (ddf.catalog.data.Metacard)39 Test (org.junit.Test)37 ArrayList (java.util.ArrayList)30 AttributeImpl (ddf.catalog.data.impl.AttributeImpl)29 AttributeDescriptor (ddf.catalog.data.AttributeDescriptor)26 Serializable (java.io.Serializable)23 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)15 HashMap (java.util.HashMap)15 Result (ddf.catalog.data.Result)14 List (java.util.List)14 MetacardType (ddf.catalog.data.MetacardType)11 QueryResponse (ddf.catalog.operation.QueryResponse)11 Date (java.util.Date)11 Map (java.util.Map)11 HashSet (java.util.HashSet)10 Optional (java.util.Optional)8 Set (java.util.Set)8 Filter (org.opengis.filter.Filter)8 UpdateRequestImpl (ddf.catalog.operation.impl.UpdateRequestImpl)7