1
0
mirror of https://github.com/moparisthebest/davmail synced 2024-12-13 11:12:22 -05:00

Refactor XMLStreamUtil

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@1392 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2010-08-28 20:21:31 +00:00
parent 0529be03a1
commit 4962684d0f
8 changed files with 138 additions and 109 deletions

View File

@ -36,7 +36,6 @@ import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.util.URIUtil;
import org.apache.log4j.Logger;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
@ -1375,12 +1374,10 @@ public class CaldavConnection extends AbstractConnection {
protected void parseXmlBody() throws IOException {
XMLStreamReader streamReader = null;
try {
XMLInputFactory inputFactory = XMLStreamUtil.getXmlInputFactory();
streamReader = inputFactory.createXMLStreamReader(new StringReader(body));
streamReader = XMLStreamUtil.createXMLStreamReader(body);
while (streamReader.hasNext()) {
int event = streamReader.next();
if (event == XMLStreamConstants.START_ELEMENT) {
streamReader.nextTag();
if (XMLStreamUtil.isStartTag(streamReader)) {
String tagLocalName = streamReader.getLocalName();
if ("prop".equals(tagLocalName)) {
handleProp(streamReader);
@ -1420,21 +1417,18 @@ public class CaldavConnection extends AbstractConnection {
public void handleCompFilter(XMLStreamReader reader) throws XMLStreamException {
while (reader.hasNext() && !isEndTag(reader, "comp-filter")) {
int event = reader.next();
if (event == XMLStreamConstants.START_ELEMENT) {
String tagLocalName = reader.getLocalName();
if ("time-range".equals(tagLocalName)) {
timeRangeStart = reader.getAttributeValue(null, "start");
timeRangeEnd = reader.getAttributeValue(null, "end");
}
reader.nextTag();
if (XMLStreamUtil.isStartTag(reader, "time-range")) {
timeRangeStart = reader.getAttributeValue(null, "start");
timeRangeEnd = reader.getAttributeValue(null, "end");
}
}
}
public void handleProp(XMLStreamReader reader) throws XMLStreamException {
while (reader.hasNext() && !isEndTag(reader, "prop")) {
int event = reader.next();
if (event == XMLStreamConstants.START_ELEMENT) {
reader.nextTag();
if (XMLStreamUtil.isStartTag(reader)) {
String tagLocalName = reader.getLocalName();
String tagText = null;
if (reader.hasText()) {

View File

@ -22,8 +22,10 @@ import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;
@ -122,4 +124,60 @@ public final class XMLStreamUtil {
return (reader.getEventType() == XMLStreamConstants.START_ELEMENT) && (reader.getLocalName().equals(tagLocalName));
}
/**
* Test if reader is on a start tag.
*
* @param reader xml stream reader
* @return true if reader is on a start tag
*/
public static boolean isStartTag(XMLStreamReader reader) {
return (reader.getEventType() == XMLStreamConstants.START_ELEMENT);
}
/**
* Test if reader is on an end tag named tagLocalName.
*
* @param reader xml stream reader
* @param tagLocalName tag local name
* @return true if reader is on an end tag named tagLocalName
*/
public static boolean isEndTag(XMLStreamReader reader, String tagLocalName) {
return (reader.getEventType() == XMLStreamConstants.END_ELEMENT) && (reader.getLocalName().equals(tagLocalName));
}
/**
* Create XML stream reader for byte array
*
* @param xmlContent xml content as byte array
* @return XML stream reader
* @throws XMLStreamException on error
*/
public static XMLStreamReader createXMLStreamReader(byte[] xmlContent) throws XMLStreamException {
return createXMLStreamReader(new ByteArrayInputStream(xmlContent));
}
/**
* Create XML stream reader for string
*
* @param xmlContent xml content as string
* @return XML stream reader
* @throws XMLStreamException on error
*/
public static XMLStreamReader createXMLStreamReader(String xmlContent) throws XMLStreamException {
XMLInputFactory xmlInputFactory = XMLStreamUtil.getXmlInputFactory();
return xmlInputFactory.createXMLStreamReader(new StringReader(xmlContent));
}
/**
* Create XML stream reader for inputStream
*
* @param inputStream xml content inputStream
* @return XML stream reader
* @throws XMLStreamException on error
*/
public static XMLStreamReader createXMLStreamReader(InputStream inputStream) throws XMLStreamException {
XMLInputFactory xmlInputFactory = XMLStreamUtil.getXmlInputFactory();
return xmlInputFactory.createXMLStreamReader(inputStream);
}
}

View File

@ -53,7 +53,6 @@ import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimePart;
import javax.mail.util.SharedByteArrayInputStream;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import java.io.*;
@ -1981,10 +1980,9 @@ public class DavExchangeSession extends ExchangeSession {
String timezoneName = null;
XMLStreamReader reader;
try {
XMLInputFactory xmlInputFactory = XMLStreamUtil.getXmlInputFactory();
reader = xmlInputFactory.createXMLStreamReader(new ByteArrayInputStream(roamingdictionary));
reader = XMLStreamUtil.createXMLStreamReader(roamingdictionary);
while (reader.hasNext()) {
reader.next();
reader.nextTag();
if (XMLStreamUtil.isStartTag(reader, "e")
&& "18-timezone".equals( XMLStreamUtil.getAttributeValue(reader, "k"))) {
String value = XMLStreamUtil.getAttributeValue(reader, "v");

View File

@ -27,8 +27,6 @@ import org.apache.jackrabbit.webdav.property.DefaultDavProperty;
import org.apache.jackrabbit.webdav.xml.Namespace;
import org.apache.log4j.Logger;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import java.io.*;
@ -166,14 +164,6 @@ public class ExchangePropPatchMethod extends PostMethod {
return "PROPPATCH";
}
protected boolean isStartTag(XMLStreamReader reader, String tagLocalName) {
return (reader.getEventType() == XMLStreamConstants.START_ELEMENT) && (reader.getLocalName().equals(tagLocalName));
}
protected boolean isEndTag(XMLStreamReader reader, String tagLocalName) {
return (reader.getEventType() == XMLStreamConstants.END_ELEMENT) && (reader.getLocalName().equals(tagLocalName));
}
List<MultiStatusResponse> responses;
@Override
@ -183,8 +173,7 @@ public class ExchangePropPatchMethod extends PostMethod {
responses = new ArrayList<MultiStatusResponse>();
XMLStreamReader reader;
try {
XMLInputFactory xmlInputFactory = XMLStreamUtil.getXmlInputFactory();
reader = xmlInputFactory.createXMLStreamReader(new FilterInputStream(getResponseBodyAsStream()) {
reader = XMLStreamUtil.createXMLStreamReader(new FilterInputStream(getResponseBodyAsStream()) {
final byte[] lastbytes = new byte[3];
@Override
@ -206,8 +195,8 @@ public class ExchangePropPatchMethod extends PostMethod {
});
while (reader.hasNext()) {
reader.next();
if (isStartTag(reader, "response")) {
reader.nextTag();
if (XMLStreamUtil.isStartTag(reader, "response")) {
handleResponse(reader);
}
}
@ -223,9 +212,9 @@ public class ExchangePropPatchMethod extends PostMethod {
protected void handleResponse(XMLStreamReader reader) throws XMLStreamException {
String href = null;
String responseStatus = "";
while (reader.hasNext() && !isEndTag(reader, "response")) {
int event = reader.next();
if (event == XMLStreamConstants.START_ELEMENT) {
while (reader.hasNext() && !XMLStreamUtil.isEndTag(reader, "response")) {
reader.nextTag();
if (XMLStreamUtil.isStartTag(reader)) {
String tagLocalName = reader.getLocalName();
if ("href".equals(tagLocalName)) {
href = reader.getElementText();
@ -243,9 +232,9 @@ public class ExchangePropPatchMethod extends PostMethod {
protected void handlePropstat(XMLStreamReader reader, MultiStatusResponse multiStatusResponse) throws XMLStreamException {
int propstatStatus = 0;
while (reader.hasNext() && !isEndTag(reader, "propstat")) {
int event = reader.next();
if (event == XMLStreamConstants.START_ELEMENT) {
while (reader.hasNext() && !XMLStreamUtil.isEndTag(reader, "propstat")) {
reader.nextTag();
if (XMLStreamUtil.isStartTag(reader)) {
String tagLocalName = reader.getLocalName();
if ("status".equals(tagLocalName)) {
if ("HTTP/1.1 200 OK".equals(reader.getElementText())) {
@ -263,9 +252,9 @@ public class ExchangePropPatchMethod extends PostMethod {
protected void handleProperty(XMLStreamReader reader, MultiStatusResponse multiStatusResponse) throws XMLStreamException {
while (reader.hasNext() && !isEndTag(reader, "prop")){
int event = reader.nextTag();
if (event == XMLStreamConstants.START_ELEMENT) {
while (reader.hasNext() && !XMLStreamUtil.isEndTag(reader, "prop")){
reader.nextTag();
if (XMLStreamUtil.isStartTag(reader)) {
String tagLocalName = reader.getLocalName();
Namespace namespace = Namespace.getNamespace(reader.getNamespaceURI());
multiStatusResponse.add(new DefaultDavProperty(tagLocalName, reader.getElementText(), namespace));

View File

@ -29,7 +29,6 @@ import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.log4j.Logger;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
@ -661,29 +660,17 @@ public abstract class EWSMethod extends PostMethod {
) {
errorDetail = result;
}
if (isStartTag(reader, "faultstring")) {
if (XMLStreamUtil.isStartTag(reader, "faultstring")) {
errorDetail = reader.getElementText();
}
}
protected boolean isStartTag(XMLStreamReader reader) {
return (reader.getEventType() == XMLStreamConstants.START_ELEMENT);
}
protected boolean isStartTag(XMLStreamReader reader, String tagLocalName) {
return (reader.getEventType() == XMLStreamConstants.START_ELEMENT) && (reader.getLocalName().equals(tagLocalName));
}
protected boolean isEndTag(XMLStreamReader reader, String tagLocalName) {
return (reader.getEventType() == XMLStreamConstants.END_ELEMENT) && (reader.getLocalName().equals(tagLocalName));
}
protected Item handleItem(XMLStreamReader reader) throws XMLStreamException {
Item responseItem = new Item();
responseItem.type = reader.getLocalName();
while (reader.hasNext() && !isEndTag(reader, responseItem.type)) {
int event = reader.next();
if (event == XMLStreamConstants.START_ELEMENT) {
while (reader.hasNext() && !XMLStreamUtil.isEndTag(reader, responseItem.type)) {
reader.nextTag();
if (XMLStreamUtil.isStartTag(reader)) {
String tagLocalName = reader.getLocalName();
String value = null;
if ("ExtendedProperty".equals(tagLocalName)) {
@ -712,9 +699,9 @@ public abstract class EWSMethod extends PostMethod {
protected List<FileAttachment> handleAttachments(XMLStreamReader reader) throws XMLStreamException {
List<FileAttachment> attachments = new ArrayList<FileAttachment>();
while (reader.hasNext() && !(isEndTag(reader, "Attachments"))) {
int event = reader.next();
if (event == XMLStreamConstants.START_ELEMENT) {
while (reader.hasNext() && !(XMLStreamUtil.isEndTag(reader, "Attachments"))) {
reader.nextTag();
if (XMLStreamUtil.isStartTag(reader)) {
String tagLocalName = reader.getLocalName();
if ("FileAttachment".equals(tagLocalName)) {
attachments.add(handleFileAttachment(reader));
@ -726,9 +713,9 @@ public abstract class EWSMethod extends PostMethod {
protected FileAttachment handleFileAttachment(XMLStreamReader reader) throws XMLStreamException {
FileAttachment fileAttachment = new FileAttachment();
while (reader.hasNext() && !(isEndTag(reader, "FileAttachment"))) {
int event = reader.next();
if (event == XMLStreamConstants.START_ELEMENT) {
while (reader.hasNext() && !(XMLStreamUtil.isEndTag(reader, "FileAttachment"))) {
reader.nextTag();
if (XMLStreamUtil.isStartTag(reader)) {
String tagLocalName = reader.getLocalName();
if ("AttachmentId".equals(tagLocalName)) {
fileAttachment.attachmentId = getAttributeValue(reader, "Id");
@ -751,9 +738,9 @@ public abstract class EWSMethod extends PostMethod {
protected void addExtendedPropertyValue(XMLStreamReader reader, Item item) throws XMLStreamException {
String propertyTag = null;
String propertyValue = null;
while (reader.hasNext() && !(isEndTag(reader, "ExtendedProperty"))) {
reader.next();
if (reader.getEventType() == XMLStreamConstants.START_ELEMENT) {
while (reader.hasNext() && !(XMLStreamUtil.isEndTag(reader, "ExtendedProperty"))) {
reader.nextTag();
if (XMLStreamUtil.isStartTag(reader)) {
String tagLocalName = reader.getLocalName();
if ("ExtendedFieldURI".equals(tagLocalName)) {
propertyTag = getAttributeValue(reader, "PropertyTag");
@ -768,9 +755,9 @@ public abstract class EWSMethod extends PostMethod {
propertyValue = reader.getElementText();
} else if ("Values".equals(tagLocalName)) {
StringBuilder buffer = new StringBuilder();
while (reader.hasNext() && !(isEndTag(reader, "Values"))) {
reader.next();
if (reader.getEventType() == XMLStreamConstants.START_ELEMENT) {
while (reader.hasNext() && !(XMLStreamUtil.isEndTag(reader, "Values"))) {
reader.nextTag();
if (XMLStreamUtil.isStartTag(reader)) {
if (buffer.length() > 0) {
buffer.append(',');
@ -819,19 +806,18 @@ public abstract class EWSMethod extends PostMethod {
responseItems = new ArrayList<Item>();
XMLStreamReader reader;
try {
XMLInputFactory xmlInputFactory = XMLStreamUtil.getXmlInputFactory();
reader = xmlInputFactory.createXMLStreamReader(getResponseBodyAsStream());
reader = XMLStreamUtil.createXMLStreamReader(getResponseBodyAsStream());
while (reader.hasNext()) {
reader.next();
reader.nextTag();
handleErrors(reader);
if (serverVersion == null && isStartTag(reader, "ServerVersionInfo")) {
if (serverVersion == null && XMLStreamUtil.isStartTag(reader, "ServerVersionInfo")) {
String majorVersion = getAttributeValue(reader, "MajorVersion");
if ("14".equals(majorVersion)) {
serverVersion = "Exchange2010";
} else {
serverVersion = "Exchange2007_SP1";
}
} else if (isStartTag(reader, responseCollectionName)) {
} else if (XMLStreamUtil.isStartTag(reader, responseCollectionName)) {
handleItems(reader);
} else {
handleCustom(reader);
@ -855,9 +841,9 @@ public abstract class EWSMethod extends PostMethod {
}
private void handleItems(XMLStreamReader reader) throws XMLStreamException {
while (reader.hasNext() && !isEndTag(reader, responseCollectionName)) {
reader.next();
if (isStartTag(reader)) {
while (reader.hasNext() && !XMLStreamUtil.isEndTag(reader, responseCollectionName)) {
reader.nextTag();
if (XMLStreamUtil.isStartTag(reader)) {
responseItems.add(handleItem(reader));
}
}

View File

@ -18,6 +18,8 @@
*/
package davmail.exchange.ews;
import davmail.exchange.XMLStreamUtil;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import java.io.IOException;
@ -97,7 +99,7 @@ public class GetUserAvailabilityMethod extends EWSMethod {
@Override
protected void handleCustom(XMLStreamReader reader) throws XMLStreamException {
if (isStartTag(reader, "MergedFreeBusy")) {
if (XMLStreamUtil.isStartTag(reader, "MergedFreeBusy")) {
this.mergedFreeBusy = reader.getElementText();
}
}

View File

@ -18,7 +18,8 @@
*/
package davmail.exchange.ews;
import javax.xml.stream.XMLStreamConstants;
import davmail.exchange.XMLStreamUtil;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import java.io.IOException;
@ -48,16 +49,16 @@ public class GetUserConfigurationMethod extends EWSMethod {
@Override
protected void handleCustom(XMLStreamReader reader) throws XMLStreamException {
if (isStartTag(reader, "UserConfiguration")) {
if (XMLStreamUtil.isStartTag(reader, "UserConfiguration")) {
responseItems.add(handleUserConfiguration(reader));
}
}
private Item handleUserConfiguration(XMLStreamReader reader) throws XMLStreamException {
Item responseItem = new Item();
while (reader.hasNext() && !(isEndTag(reader, "UserConfiguration"))) {
int event = reader.next();
if (event == XMLStreamConstants.START_ELEMENT) {
while (reader.hasNext() && !(XMLStreamUtil.isEndTag(reader, "UserConfiguration"))) {
reader.nextTag();
if (XMLStreamUtil.isStartTag(reader)) {
String tagLocalName = reader.getLocalName();
if ("DictionaryEntry".equals(tagLocalName)) {
handleDictionaryEntry(reader, responseItem);
@ -69,9 +70,9 @@ public class GetUserConfigurationMethod extends EWSMethod {
private void handleDictionaryEntry(XMLStreamReader reader, Item responseItem) throws XMLStreamException {
String key = null;
while (reader.hasNext() && !(isEndTag(reader, "DictionaryEntry"))) {
int event = reader.next();
if (event == XMLStreamConstants.START_ELEMENT) {
while (reader.hasNext() && !(XMLStreamUtil.isEndTag(reader, "DictionaryEntry"))) {
reader.nextTag();
if (XMLStreamUtil.isStartTag(reader)) {
String tagLocalName = reader.getLocalName();
if ("Value".equals(tagLocalName)) {
if (key == null) {

View File

@ -18,7 +18,8 @@
*/
package davmail.exchange.ews;
import javax.xml.stream.XMLStreamConstants;
import davmail.exchange.XMLStreamUtil;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
@ -45,12 +46,12 @@ public class ResolveNamesMethod extends EWSMethod {
Item responseItem = new Item();
responseItem.type = "Contact";
// skip to Contact
while (reader.hasNext() && !isStartTag(reader, "Resolution")) {
reader.next();
while (reader.hasNext() && !XMLStreamUtil.isStartTag(reader, "Resolution")) {
reader.nextTag();
}
while (reader.hasNext() && !isEndTag(reader, "Resolution")) {
int event = reader.next();
if (event == XMLStreamConstants.START_ELEMENT) {
while (reader.hasNext() && !XMLStreamUtil.isEndTag(reader, "Resolution")) {
reader.nextTag();
if (XMLStreamUtil.isStartTag(reader)) {
String tagLocalName = reader.getLocalName();
if ("Mailbox".equals(tagLocalName)) {
handleMailbox(reader, responseItem);
@ -63,9 +64,9 @@ public class ResolveNamesMethod extends EWSMethod {
}
protected void handleMailbox(XMLStreamReader reader, Item responseItem) throws XMLStreamException {
while (reader.hasNext() && !isEndTag(reader, "Mailbox")) {
int event = reader.next();
if (event == XMLStreamConstants.START_ELEMENT) {
while (reader.hasNext() && !XMLStreamUtil.isEndTag(reader, "Mailbox")) {
reader.nextTag();
if (XMLStreamUtil.isStartTag(reader)) {
String tagLocalName = reader.getLocalName();
if ("Name".equals(tagLocalName)) {
responseItem.put(tagLocalName, reader.getElementText());
@ -75,9 +76,9 @@ public class ResolveNamesMethod extends EWSMethod {
}
protected void handleContact(XMLStreamReader reader, Item responseItem) throws XMLStreamException {
while (reader.hasNext() && !isEndTag(reader, "Contact")) {
int event = reader.next();
if (event == XMLStreamConstants.START_ELEMENT) {
while (reader.hasNext() && !XMLStreamUtil.isEndTag(reader, "Contact")) {
reader.nextTag();
if (XMLStreamUtil.isStartTag(reader)) {
String tagLocalName = reader.getLocalName();
if ("EmailAddresses".equals(tagLocalName)) {
handleEmailAddresses(reader, responseItem);
@ -93,9 +94,9 @@ public class ResolveNamesMethod extends EWSMethod {
}
protected void handlePhysicalAddresses(XMLStreamReader reader, Item responseItem) throws XMLStreamException {
while (reader.hasNext() && !isEndTag(reader, "PhysicalAddresses")) {
int event = reader.next();
if (event == XMLStreamConstants.START_ELEMENT) {
while (reader.hasNext() && !XMLStreamUtil.isEndTag(reader, "PhysicalAddresses")) {
reader.nextTag();
if (XMLStreamUtil.isStartTag(reader)) {
String tagLocalName = reader.getLocalName();
// TODO
}
@ -103,9 +104,9 @@ public class ResolveNamesMethod extends EWSMethod {
}
protected void handlePhoneNumbers(XMLStreamReader reader, Item responseItem) throws XMLStreamException {
while (reader.hasNext() && !isEndTag(reader, "PhoneNumbers")) {
int event = reader.next();
if (event == XMLStreamConstants.START_ELEMENT) {
while (reader.hasNext() && !XMLStreamUtil.isEndTag(reader, "PhoneNumbers")) {
reader.nextTag();
if (XMLStreamUtil.isStartTag(reader)) {
String tagLocalName = reader.getLocalName();
// TODO
}
@ -113,9 +114,9 @@ public class ResolveNamesMethod extends EWSMethod {
}
protected void handleEmailAddresses(XMLStreamReader reader, Item responseItem) throws XMLStreamException {
while (reader.hasNext() && !isEndTag(reader, "EmailAddresses")) {
int event = reader.next();
if (event == XMLStreamConstants.START_ELEMENT) {
while (reader.hasNext() && !XMLStreamUtil.isEndTag(reader, "EmailAddresses")) {
reader.nextTag();
if (XMLStreamUtil.isStartTag(reader)) {
String tagLocalName = reader.getLocalName();
if ("Entry".equals(tagLocalName)) {
String key = getAttributeValue(reader, "Key");