Skip to content

Commit

Permalink
DROOLS-1437 Avoid stax-utils dependency by performing manual indentat…
Browse files Browse the repository at this point in the history
…ion (apache#37)
  • Loading branch information
tarilabs authored and etirelli committed Feb 13, 2017
1 parent 14a1d3c commit a8e36f2
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 30 deletions.
10 changes: 0 additions & 10 deletions kie-dmn/kie-dmn-backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,6 @@
<artifactId>xmlunit-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.java.dev.stax-utils</groupId>
<artifactId>stax-utils</artifactId>
<exclusions>
<exclusion>
<groupId>com.bea.xml</groupId>
<artifactId>jsr173-ri</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@


public class CustomStaxReader extends StaxReader {

/**
* ATTENTION use read-only methods do not mutate
* ATTENTION this is intercepted during XStream StaxDriver creation as there is no proper API to inherit.
* Do not mutate reference - mutating this reference would not sort any effect on the actual underlying StaxReader
*/
private XMLStreamReader in;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,24 @@
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

import com.thoughtworks.xstream.io.StreamException;
import com.thoughtworks.xstream.io.naming.NameCoder;
import com.thoughtworks.xstream.io.xml.QNameMap;
import com.thoughtworks.xstream.io.xml.StaxWriter;


public class CustomStaxWriter extends StaxWriter {
/**
* ATTENTION use read-only methods do not mutate
* ATTENTION this is intercepted during XStream StaxDriver creation as there is no proper API to inherit.
* Do not mutate reference - mutating this reference would not sort any effect on the actual underlying StaxWriter
*/
private XMLStreamWriter out;

private int tagDepth = 0;
private static enum Op {
START_NODE, END_NODE, VALUE;
}
private Op lastOp = null;

public CustomStaxWriter(QNameMap qnameMap, XMLStreamWriter out, boolean writeStartEndDocument, boolean repairingNamespace, NameCoder nameCoder) throws XMLStreamException {
super(qnameMap, out, writeStartEndDocument, repairingNamespace, nameCoder);
this.out = out;
Expand All @@ -26,4 +33,50 @@ public void writeNamespace(String prefix, String uri) throws XMLStreamException
public void setDefaultNamespace(String uri) throws XMLStreamException {
out.setDefaultNamespace(uri);
}

@Override
public void endNode() {
if ( this.lastOp == Op.END_NODE ) {
try {
out.writeCharacters( System.lineSeparator() );
for ( int i = 0; i < (tagDepth-1); i++ ) { out.writeCharacters(" "); }
} catch (XMLStreamException e) {
throw new StreamException(e);
}
}
super.endNode();
--this.tagDepth;

this.lastOp = Op.END_NODE;

if ( this.tagDepth == 0 ) {
// closed last element before EOF
try {
out.writeCharacters( System.lineSeparator() );
} catch (XMLStreamException e) {
throw new StreamException(e);
}
}
}

@Override
public void startNode(String arg0) {
try {
out.writeCharacters( System.lineSeparator() );
for ( int i = 0; i < tagDepth; i++ ) { out.writeCharacters(" "); }
} catch (XMLStreamException e) {
throw new StreamException(e);
}
super.startNode(arg0);
++this.tagDepth;

this.lastOp = Op.START_NODE;
}

@Override
public void setValue(String text) {
super.setValue(text);

this.lastOp = Op.VALUE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@
import com.thoughtworks.xstream.io.xml.AbstractPullReader;
import com.thoughtworks.xstream.io.xml.QNameMap;
import com.thoughtworks.xstream.io.xml.StaxDriver;
import com.thoughtworks.xstream.io.xml.StaxReader;
import com.thoughtworks.xstream.io.xml.StaxWriter;

import javanet.staxutils.StaxUtilsXMLOutputFactory;

import org.kie.dmn.feel.model.v1_1.Artifact;
import org.kie.dmn.feel.model.v1_1.Association;
import org.kie.dmn.feel.model.v1_1.AuthorityRequirement;
Expand Down Expand Up @@ -71,7 +68,6 @@
import java.io.*;

import javax.xml.namespace.QName;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;
Expand All @@ -95,18 +91,6 @@ public class XStreamMarshaller
// qmap.registerMapping(new javax.xml.namespace.QName("http://www.omg.org/spec/FEEL/20140401", "feel", "feel"), "dddecision"); // FIXME still not sure how to output non-used ns like 'feel:'

staxDriver = new StaxDriver() {

private XMLOutputFactory outputFactory = null;

public XMLOutputFactory getOutputFactory() {
if (outputFactory == null) {
XMLOutputFactory factory = new StaxUtilsXMLOutputFactory(super.getOutputFactory());
factory.setProperty(StaxUtilsXMLOutputFactory.INDENTING, Boolean.TRUE);
outputFactory = factory;
}
return outputFactory;
}

public AbstractPullReader createStaxReader(XMLStreamReader in) {
return new CustomStaxReader(getQnameMap(), in);
}
Expand Down

0 comments on commit a8e36f2

Please sign in to comment.