From d709c6e38de81427a8ee24dd0905b04b5d8cdbbb Mon Sep 17 00:00:00 2001 From: Laurent SCHOELENS <61973605+laurentschoelens@users.noreply.github.com> Date: Wed, 6 Sep 2023 21:50:37 +0200 Subject: [PATCH] =?UTF-8?q?[#1707]=C2=A0Fix=20indent=20of=20DataWriter=20a?= =?UTF-8?q?nd=20adding=20test=20case?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jaxb/core/marshaller/DataWriter.java | 21 +++++-- .../v2/schemagen/MarshallingAbstractTest.java | 58 ++++++++++++++++++- 2 files changed, 72 insertions(+), 7 deletions(-) diff --git a/jaxb-ri/core/src/main/java/org/glassfish/jaxb/core/marshaller/DataWriter.java b/jaxb-ri/core/src/main/java/org/glassfish/jaxb/core/marshaller/DataWriter.java index 65d6ea222..c3452155c 100644 --- a/jaxb-ri/core/src/main/java/org/glassfish/jaxb/core/marshaller/DataWriter.java +++ b/jaxb-ri/core/src/main/java/org/glassfish/jaxb/core/marshaller/DataWriter.java @@ -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 @@ -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); @@ -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); @@ -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); + } } @@ -338,7 +347,7 @@ private void doIndent () if (depth > 0) { char[] ch = indentStep.toCharArray(); for( int i=0; i content; + + } + @Test public void testMarshalSingleElement() throws Exception { JAXBContext jc = JAXBContext.newInstance(Mapping.class); @@ -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 = "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + " \n" + + " \n" + + " \n" + + "\n" + + ""; + + String expectedOutput = "\n" + + "\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + "\n"; + + try { + JAXBElement 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 {