Skip to content

Commit

Permalink
[#1707] Fix indent of DataWriter and adding test case
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentschoelens committed Sep 6, 2023
1 parent 57ebae1 commit d709c6e
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
Expand Down Expand Up @@ -218,7 +218,7 @@ public void startElement (String uri, String localName,
stateStack.push(SEEN_ELEMENT);
state = SEEN_NOTHING;
if (depth > 0) {
super.characters("\n");
super.characters("\n".toCharArray(), 0, 1);
}
doIndent();
super.startElement(uri, localName, qName, atts);
Expand Down Expand Up @@ -250,7 +250,7 @@ public void endElement (String uri, String localName, String qName)
{
depth--;
if (state == SEEN_ELEMENT) {
super.characters("\n");
super.characters("\n".toCharArray(), 0, 1);
doIndent();
}
super.endElement(uri, localName, qName);
Expand Down Expand Up @@ -314,8 +314,17 @@ public void endDocument() throws SAXException {
public void characters (char[] ch, int start, int length)
throws SAXException
{
state = SEEN_DATA;
super.characters(ch, start, length);
boolean hasContent = false;
for (int i = start; i < length; i++) {
if (!Character.isWhitespace(ch[i])) {
hasContent = true;
break;
}
}
if (hasContent) {
state = SEEN_DATA;
super.characters(ch, start, length);
}
}


Expand All @@ -338,7 +347,7 @@ private void doIndent ()
if (depth > 0) {
char[] ch = indentStep.toCharArray();
for( int i=0; i<depth; i++ )
characters(ch, 0, ch.length);
super.characters(ch, 0, ch.length);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
Expand All @@ -14,17 +14,20 @@
import org.glassfish.jaxb.runtime.v2.schemagen.xmlschema.JaxbDistribution;
import org.glassfish.jaxb.runtime.v2.schemagen.xmlschema.JaxbEnvironmentModel;
import org.junit.Assert;
import org.junit.ComparisonFailure;
import org.junit.Test;

import jakarta.xml.bind.annotation.XmlAccessType;
import jakarta.xml.bind.annotation.XmlAccessorType;
import jakarta.xml.bind.annotation.XmlAnyElement;
import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlElementWrapper;
import jakarta.xml.bind.annotation.XmlRootElement;
import jakarta.xml.bind.annotation.XmlSeeAlso;
import jakarta.xml.bind.annotation.XmlType;
import jakarta.xml.bind.annotation.XmlValue;
import javax.xml.XMLConstants;
import jakarta.xml.bind.Element;
import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBElement;
import jakarta.xml.bind.Marshaller;
Expand All @@ -40,6 +43,7 @@
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import junit.framework.TestCase;
import junit.textui.TestRunner;
Expand Down Expand Up @@ -84,6 +88,14 @@ static class Mapping {
A element2;
}

@XmlRootElement
static class Job {

@XmlAnyElement
private List<Element> content;

}

@Test
public void testMarshalSingleElement() throws Exception {
JAXBContext jc = JAXBContext.newInstance(Mapping.class);
Expand Down Expand Up @@ -164,6 +176,50 @@ public void testUnmarshalCollection() throws Exception {
}
}

@Test
public void testMarshallerJob() throws Exception {
JAXBContext jc = JAXBContext.newInstance(Job.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();

Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
String sourceXml = "<job type=\"NewMethod\">\n" +
"\n" +
"<foo id=\"1\">\n" +
"<bar>\n" +
"<baz>\n" +
"<value name=\"xxx\"/>\n" +
" </baz>\n" +
" </bar>\n" +
" </foo>\n" +
"\n" +
"</job>";

String expectedOutput = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" +
"<job>\n" +
" <foo id=\"1\">\n" +
" <bar>\n" +
" <baz>\n" +
" <value name=\"xxx\"/>\n" +
" </baz>\n" +
" </bar>\n" +
" </foo>\n" +
"</job>\n";

try {
JAXBElement<Job> job = unmarshaller.unmarshal(new StreamSource(new StringReader(sourceXml)), Job.class);

StringWriter sw = new StringWriter();
marshaller.marshal(job.getValue(), sw);
String output = sw.toString();
Assert.assertEquals(expectedOutput, output);
} catch (ComparisonFailure e) {
throw e;
} catch (Throwable t) {
Assert.fail("Exception is thrown " + t);
}
}

@Test
public void testXmlIDRefMarshal() throws Exception {

Expand Down

0 comments on commit d709c6e

Please sign in to comment.