PrettyPrint XML in custom serializer doesn't seem to work?
See original GitHub issueHello there!
I am using jackson
on a project and greatly enjoy it, so first off thanks.
However, I’m running in to an odd issue with Pretty Printing XML:
<?xml version="1.0" encoding="UTF-8"?><bom serialNumber="urn:uuid:3e671687-395b-41f5-a30f-a58921a69b79" version="1" xmlns="http://cyclonedx.org/schema/bom/1.1">
<components>
<component type="framework" bom-ref="pkg:maven/org.example.acme/web-framework@1.0.0">
<group>org.example.acme</group>
<name>web-framework</name>
<version>1.0.0</version>
<purl>pkg:maven/org.example.acme/web-framework@1.0.0</purl>
</component>
<component type="library" bom-ref="pkg:maven/org.example.acme/persistence@3.1.0">
<group>org.example.acme</group>
<name>persistence</name>
<version>3.1.0</version>
<purl>pkg:maven/org.example.acme/persistence@3.1.0</purl>
</component>
<component type="library" bom-ref="pkg:maven/org.example.acme/common-util@3.0.0">
<group>com.example.acme</group>
<name>common-util</name>
<version>3.0.0</version>
<purl>pkg:maven/org.example.acme/common-util@3.0.0</purl>
</component>
</components><dg:dependencies xmlns:dg="http://cyclonedx.org/schema/ext/dependency-graph/1.0"><dg:dependency ref="pkg:maven/org.example.acme/web-framework@1.0.0"><dg:dependency ref="pkg:maven/org.example.acme/common-util@3.0.0"/><dg:dependency ref="pkg:maven/org.example.acme/common-persistence@3.0.0"/></dg:dependency><dg:dependency ref="pkg:maven/org.example.acme/persistence@3.1.0"><dg:dependency ref="pkg:maven/org.example.acme/common-util@3.0.0"/></dg:dependency><dg:dependency ref="pkg:maven/org.example.acme/common-util@3.0.0"/></dg:dependencies>
</bom>
The dg:dependencies
section is all done using a custom serializer, for a number of reasons.
Inside of there, I generally construct things like so:
@Override
public void serialize(
final List<Dependency> dependencies, final JsonGenerator generator, final SerializerProvider provider)
throws IOException
{
if (generator instanceof ToXmlGenerator) {
final ToXmlGenerator toXmlGenerator = (ToXmlGenerator) generator;
final XMLStreamWriter staxWriter = toXmlGenerator.getStaxWriter();
try {
if (dependencies != null && !dependencies.isEmpty()) {
if (useNamespace) {
staxWriter.writeStartElement(NAMESPACE_PREFIX, "dependencies", NAMESPACE_URI);
} else {
staxWriter.writeStartElement("dependencies");
}
toXmlGenerator.writeStartArray();
for (Dependency dependency : dependencies) {
writeDependency(dependency, staxWriter);
}
toXmlGenerator.writeEndArray();
staxWriter.writeEndElement();
}
}
catch (XMLStreamException ex) {
throw new IOException(ex);
}
I’ve been using the staxWriter to output, because I need control over the namespace, and had some issues that I honestly can’t remember at this point, but staxWriter was the only way to get around them.
No matter what I do, I can’t seem to get the <dg:dependencies
section to pretty print, but the rest of the document does. Am I holding this thing wrong 😃
The project and PR I am working on at the moment are here, if seeing more code is useful: https://github.com/CycloneDX/cyclonedx-core-java/pull/84
Cheers!
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (4 by maintainers)
Top GitHub Comments
That’s the spirit! Looking forward to contributions!
Also very happy that you were able to make things work. Working with XML is not always easy (with or without Jackson) so getting to work right is a good feeling.
Correct: the issue is that only calls via
ToXmlGenerator
will be pretty-printed: there is no standard Stax (or Stax2) way to indent XML content (this is a generally difficult problem for arbitrary XML content; without schema it is not possible to know definitely where whitespace is meaningful and where not). Theoretically you can simply use Stax XmlStreamWriter calls directly, but that may be problematic wrt synchronizing actual indentation depth and so on.As to namespaces; XML format backend forces use of repairing writer so you should be able to force certain bindings for namespaces. So perhaps you can just call
writeNamespace()
to establish Namespace URI/prefix binding, if that is needed?