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