Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#1707] Fix indent of DataWriter and adding test case #1740

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading