Skip to content

Commit 7b4331a

Browse files
committed
Merge pull request OfficeDev#202 from superelchi/master
Keep whitespace in plain text email messagebodys
2 parents 0b12da6 + 25b4ac8 commit 7b4331a

2 files changed

Lines changed: 51 additions & 10 deletions

File tree

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

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,19 @@ private void internalReadElement(String namespacePrefix, String localName,
159159
*/
160160
public void read() throws ServiceXmlDeserializationException,
161161
XMLStreamException {
162+
read(false);
163+
}
164+
165+
/**
166+
* Reads the specified node type.
167+
*
168+
* @param keepWhiteSpace Do not remove whitespace characters if true
169+
*
170+
* @throws ServiceXmlDeserializationException the service xml deserialization exception
171+
* @throws javax.xml.stream.XMLStreamException the xML stream exception
172+
*/
173+
private void read(boolean keepWhiteSpace) throws ServiceXmlDeserializationException,
174+
XMLStreamException {
162175
// The caller to EwsXmlReader.Read expects
163176
// that there's another node to
164177
// read. Throw an exception if not true.
@@ -170,9 +183,10 @@ public void read() throws ServiceXmlDeserializationException,
170183
XMLEvent event = xmlReader.nextEvent();
171184
if (event.getEventType() == XMLStreamConstants.CHARACTERS) {
172185
Characters characters = (Characters) event;
173-
if (characters.isIgnorableWhiteSpace()
174-
|| characters.isWhiteSpace()) {
175-
continue;
186+
if (!keepWhiteSpace)
187+
if (characters.isIgnorableWhiteSpace()
188+
|| characters.isWhiteSpace()) {
189+
continue;
176190
}
177191
}
178192
this.prevEvent = this.presentEvent;
@@ -401,18 +415,32 @@ public <T> T readElementValue(Class<T> cls) throws Exception {
401415
*/
402416
public String readValue() throws XMLStreamException,
403417
ServiceXmlDeserializationException {
418+
return readValue(false);
419+
}
420+
/**
421+
* Reads the value. Should return content element or text node as string
422+
* Present event must be START ELEMENT. After executing this function
423+
* Present event will be set on END ELEMENT
424+
*
425+
* @param keepWhiteSpace Do not remove whitespace characters if true
426+
* @return String
427+
* @throws javax.xml.stream.XMLStreamException the xML stream exception
428+
* @throws ServiceXmlDeserializationException the service xml deserialization exception
429+
*/
430+
public String readValue(boolean keepWhiteSpace) throws XMLStreamException,
431+
ServiceXmlDeserializationException {
404432
String errMsg = String.format("Could not read value from %s.",
405433
XmlNodeType.getString(this.presentEvent.getEventType()));
406434
if (this.presentEvent.isStartElement()) {
407435
// Go to next event and check for Characters event
408-
this.read();
436+
this.read(keepWhiteSpace);
409437
if (this.presentEvent.isCharacters()) {
410438
StringBuffer elementValue = new StringBuffer();
411439
do {
412440
if (this.getNodeType().nodeType == XmlNodeType.CHARACTERS) {
413441
Characters characters = (Characters) this.presentEvent;
414-
if (!characters.isIgnorableWhiteSpace()
415-
&& !characters.isWhiteSpace()) {
442+
if (keepWhiteSpace || (!characters.isIgnorableWhiteSpace()
443+
&& !characters.isWhiteSpace())) {
416444
if (characters.getData().length() != 0) {
417445
elementValue.append(characters.getData());
418446
}
@@ -439,11 +467,11 @@ public String readValue() throws XMLStreamException,
439467
StringBuffer data = new StringBuffer(this.presentEvent
440468
.asCharacters().getData());
441469
do {
442-
this.read();
470+
this.read(keepWhiteSpace);
443471
if (this.getNodeType().nodeType == XmlNodeType.CHARACTERS) {
444472
Characters characters = (Characters) this.presentEvent;
445-
if (!characters.isIgnorableWhiteSpace()
446-
&& !characters.isWhiteSpace()) {
473+
if (keepWhiteSpace || (!characters.isIgnorableWhiteSpace()
474+
&& !characters.isWhiteSpace())) {
447475
if (characters.getData().length() != 0) {
448476
data.append(characters.getData());
449477
}

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,18 @@
2222
*/
2323
package microsoft.exchange.webservices.data;
2424

25+
import org.apache.commons.logging.Log;
26+
import org.apache.commons.logging.LogFactory;
27+
2528
import javax.xml.stream.XMLStreamException;
2629

2730
/**
2831
* Represents the body of a message.
2932
*/
3033
public final class MessageBody extends ComplexProperty {
3134

35+
private static final Log log = LogFactory.getLog(MessageBody.class);
36+
3237
/**
3338
* The body type.
3439
*/
@@ -112,7 +117,15 @@ protected void readAttributesFromXml(EwsServiceXmlReader reader)
112117
@Override
113118
protected void readTextValueFromXml(EwsServiceXmlReader reader)
114119
throws XMLStreamException, ServiceXmlDeserializationException {
115-
this.text = reader.readValue();
120+
if (log.isDebugEnabled()) {
121+
log.debug("Reading text value from XML. BodyType = " + this.getBodyType() +
122+
", keepWhiteSpace = " +
123+
((this.getBodyType() == BodyType.Text) ? "true." : "false."));
124+
}
125+
this.text = reader.readValue(this.getBodyType() == BodyType.Text);
126+
if (log.isDebugEnabled()) {
127+
log.debug("Text value read:\n---\n" + this.text + "\n---");
128+
}
116129
}
117130

118131
/**

0 commit comments

Comments
 (0)