Skip to content

Commit 5b9720b

Browse files
committed
Keep whitespace in plain text email messagebodys
Fixes OfficeDev#198
1 parent 03db6b8 commit 5b9720b

2 files changed

Lines changed: 36 additions & 10 deletions

File tree

src/main/java/microsoft/exchange/webservices/data/EwsXmlReader.java

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,17 @@ private void internalReadElement(String namespacePrefix, String localName,
147147
*/
148148
public void read() throws ServiceXmlDeserializationException,
149149
XMLStreamException {
150+
read(false);
151+
}
152+
153+
/**
154+
* Reads the specified node type.
155+
*
156+
* @throws ServiceXmlDeserializationException the service xml deserialization exception
157+
* @throws javax.xml.stream.XMLStreamException the xML stream exception
158+
*/
159+
private void read(boolean keepWhiteSpace) throws ServiceXmlDeserializationException,
160+
XMLStreamException {
150161
// The caller to EwsXmlReader.Read expects
151162
// that there's another node to
152163
// read. Throw an exception if not true.
@@ -158,9 +169,10 @@ public void read() throws ServiceXmlDeserializationException,
158169
XMLEvent event = xmlReader.nextEvent();
159170
if (event.getEventType() == XMLStreamConstants.CHARACTERS) {
160171
Characters characters = (Characters) event;
161-
if (characters.isIgnorableWhiteSpace()
162-
|| characters.isWhiteSpace()) {
163-
continue;
172+
if (!keepWhiteSpace)
173+
if (characters.isIgnorableWhiteSpace()
174+
|| characters.isWhiteSpace()) {
175+
continue;
164176
}
165177
}
166178
this.prevEvent = this.presentEvent;
@@ -389,18 +401,31 @@ public <T> T readElementValue(Class<T> cls) throws Exception {
389401
*/
390402
public String readValue() throws XMLStreamException,
391403
ServiceXmlDeserializationException {
404+
return readValue(false);
405+
}
406+
/**
407+
* Reads the value. Should return content element or text node as string
408+
* Present event must be START ELEMENT. After executing this function
409+
* Present event will be set on END ELEMENT
410+
*
411+
* @return String
412+
* @throws javax.xml.stream.XMLStreamException the xML stream exception
413+
* @throws ServiceXmlDeserializationException the service xml deserialization exception
414+
*/
415+
public String readValue(boolean keepWhiteSpace) throws XMLStreamException,
416+
ServiceXmlDeserializationException {
392417
String errMsg = String.format("Could not read value from %s.",
393418
XmlNodeType.getString(this.presentEvent.getEventType()));
394419
if (this.presentEvent.isStartElement()) {
395420
// Go to next event and check for Characters event
396-
this.read();
421+
this.read(keepWhiteSpace);
397422
if (this.presentEvent.isCharacters()) {
398423
StringBuffer elementValue = new StringBuffer();
399424
do {
400425
if (this.getNodeType().nodeType == XmlNodeType.CHARACTERS) {
401426
Characters characters = (Characters) this.presentEvent;
402-
if (!characters.isIgnorableWhiteSpace()
403-
&& !characters.isWhiteSpace()) {
427+
if (keepWhiteSpace || (!characters.isIgnorableWhiteSpace()
428+
&& !characters.isWhiteSpace())) {
404429
if (characters.getData().length() != 0) {
405430
elementValue.append(characters.getData());
406431
}
@@ -427,11 +452,11 @@ public String readValue() throws XMLStreamException,
427452
StringBuffer data = new StringBuffer(this.presentEvent
428453
.asCharacters().getData());
429454
do {
430-
this.read();
455+
this.read(keepWhiteSpace);
431456
if (this.getNodeType().nodeType == XmlNodeType.CHARACTERS) {
432457
Characters characters = (Characters) this.presentEvent;
433-
if (!characters.isIgnorableWhiteSpace()
434-
&& !characters.isWhiteSpace()) {
458+
if (keepWhiteSpace || (!characters.isIgnorableWhiteSpace()
459+
&& !characters.isWhiteSpace())) {
435460
if (characters.getData().length() != 0) {
436461
data.append(characters.getData());
437462
}

src/main/java/microsoft/exchange/webservices/data/MessageBody.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ protected void readAttributesFromXml(EwsServiceXmlReader reader)
100100
@Override
101101
protected void readTextValueFromXml(EwsServiceXmlReader reader)
102102
throws XMLStreamException, ServiceXmlDeserializationException {
103-
this.text = reader.readValue();
103+
boolean keepWhiteSpace = this.getBodyType() == BodyType.Text;
104+
this.text = reader.readValue(keepWhiteSpace);
104105
}
105106

106107
/**

0 commit comments

Comments
 (0)