use of ddf.catalog.transformer.api.PrintWriter in project ddf by codice.
the class MetacardMarshallerImpl method marshal.
@Override
public String marshal(Metacard metacard, Map<String, Serializable> arguments) throws XmlPullParserException, IOException, CatalogTransformerException {
PrintWriter writer = this.writerProvider.build(Metacard.class);
if (arguments != null && arguments.get(OMIT_XML_DECL) != null) {
Boolean omitXmlDec = Boolean.valueOf(String.valueOf(arguments.get(OMIT_XML_DECL)));
if (omitXmlDec == null || !omitXmlDec) {
writer.setRawValue(XML_DECL);
}
}
writer.startNode("metacard");
for (Map.Entry<String, String> nsRow : NAMESPACE_MAP.entrySet()) {
writer.addAttribute(nsRow.getKey(), nsRow.getValue());
}
if (metacard.getId() != null) {
writer.addAttribute(GML_PREFIX + ":id", metacard.getId());
}
writer.startNode("type");
if (StringUtils.isBlank(metacard.getMetacardType().getName())) {
writer.setValue(MetacardType.DEFAULT_METACARD_TYPE_NAME);
} else {
writer.setValue(metacard.getMetacardType().getName());
}
// type
writer.endNode();
if (StringUtils.isNotBlank(metacard.getSourceId())) {
writer.startNode("source");
writer.setValue(metacard.getSourceId());
// source
writer.endNode();
}
// if multi-threading, cannot abstract XmlPullParser creation to class member.
// xmlPullParser used only for geometry
XmlPullParser xmlPullParser = XppFactory.createDefaultParser();
Set<AttributeDescriptor> attributeDescriptors = metacard.getMetacardType().getAttributeDescriptors();
for (AttributeDescriptor attributeDescriptor : attributeDescriptors) {
String attributeName = attributeDescriptor.getName();
if (attributeName.equals("id")) {
continue;
}
Attribute attribute = metacard.getAttribute(attributeName);
if (attribute != null && attribute.getValue() != null) {
AttributeType.AttributeFormat format = attributeDescriptor.getType().getAttributeFormat();
writeAttributeToXml(writer, xmlPullParser, attribute, format);
}
}
// metacard
writer.endNode();
return writer.makeString();
}
use of ddf.catalog.transformer.api.PrintWriter in project ddf by codice.
the class XmlResponseQueueTransformer method transform.
@Override
public BinaryContent transform(SourceResponse response, Map<String, Serializable> args) throws CatalogTransformerException {
try {
PrintWriter writer = printWriterProvider.build(Metacard.class);
writer.setRawValue("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n");
writer.startNode("metacards");
for (Map.Entry<String, String> nsRow : NAMESPACE_MAP.entrySet()) {
writer.addAttribute(nsRow.getKey(), nsRow.getValue());
}
if (response.getResults() != null && !response.getResults().isEmpty()) {
StringWriter metacardContent = fjp.invoke(new MetacardForkTask(ImmutableList.copyOf(response.getResults()), fjp, geometryTransformer, threshold, metacardMarshaller));
writer.setRawValue(metacardContent.getBuffer().toString());
}
// metacards
writer.endNode();
ByteArrayInputStream bais = new ByteArrayInputStream(writer.makeString().getBytes(StandardCharsets.UTF_8));
return new BinaryContentImpl(bais, mimeType);
} catch (Exception e) {
LOGGER.info("Failed Query response transformation", e);
throw new CatalogTransformerException("Failed Query response transformation");
}
}
use of ddf.catalog.transformer.api.PrintWriter in project ddf by codice.
the class TestEscapingPrintWriter method testUndefinedCharacter.
@Test
public void testUndefinedCharacter() throws CatalogTransformerException {
String input = new String(Character.toChars(55296));
String expected = "�";
StringWriter stringWriter = new StringWriter(8);
PrintWriter escapingPrintWriter = new EscapingPrintWriter(stringWriter);
escapingPrintWriter.setValue(input);
escapingPrintWriter.flush();
String output = stringWriter.toString();
assertEquals(expected, output);
}
use of ddf.catalog.transformer.api.PrintWriter in project ddf by codice.
the class TestEscapingPrintWriter method testControlCharacters.
@Test
public void testControlCharacters() throws CatalogTransformerException {
String input = "\0 \t \n \r";
String expected = "� \t \n 
";
StringWriter stringWriter = new StringWriter(8);
PrintWriter escapingPrintWriter = new EscapingPrintWriter(stringWriter);
escapingPrintWriter.setValue(input);
escapingPrintWriter.flush();
String output = stringWriter.toString();
assertEquals(expected, output);
}
use of ddf.catalog.transformer.api.PrintWriter in project ddf by codice.
the class TestCswQueryResponseTransformer method testMarshalAcknowledgementWithFailedTransforms.
@Test
public void testMarshalAcknowledgementWithFailedTransforms() throws WebApplicationException, IOException, JAXBException, CatalogTransformerException {
GetRecordsType query = new GetRecordsType();
query.setResultType(ResultType.RESULTS);
query.setMaxRecords(BigInteger.valueOf(6));
query.setStartPosition(BigInteger.valueOf(0));
SourceResponse sourceResponse = createSourceResponse(query, 6);
Map<String, Serializable> args = new HashMap<>();
args.put(CswConstants.RESULT_TYPE_PARAMETER, ResultType.RESULTS);
args.put(CswConstants.GET_RECORDS, query);
PrintWriter printWriter = getSimplePrintWriter();
MetacardTransformer mockMetacardTransformer = mock(MetacardTransformer.class);
final AtomicLong atomicLong = new AtomicLong(0);
when(mockMetacardTransformer.transform(any(Metacard.class), anyMap())).then(invocationOnMock -> {
if (atomicLong.incrementAndGet() == 2) {
throw new CatalogTransformerException("");
}
Metacard metacard = (Metacard) invocationOnMock.getArguments()[0];
BinaryContentImpl bci = new BinaryContentImpl(IOUtils.toInputStream(metacard.getId() + ","), new MimeType("application/xml"));
return bci;
});
when(mockPrintWriterProvider.build((Class<Metacard>) notNull())).thenReturn(printWriter);
when(mockTransformerManager.getTransformerBySchema(anyString())).thenReturn(mockMetacardTransformer);
CswQueryResponseTransformer cswQueryResponseTransformer = new CswQueryResponseTransformer(mockTransformerManager, mockPrintWriterProvider);
cswQueryResponseTransformer.init();
BinaryContent content = cswQueryResponseTransformer.transform(sourceResponse, args);
cswQueryResponseTransformer.destroy();
String xml = new String(content.getByteArray());
assertThat(xml, containsString(CswQueryResponseTransformer.NUMBER_OF_RECORDS_MATCHED_ATTRIBUTE + " 6"));
assertThat(xml, containsString(CswQueryResponseTransformer.NUMBER_OF_RECORDS_RETURNED_ATTRIBUTE + " 5"));
assertThat(xml, containsString(CswQueryResponseTransformer.NEXT_RECORD_ATTRIBUTE + " 0"));
}
Aggregations