mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2024-11-22 06:12:05 +01:00
Use CharSequence instead of String in parseContent()
and parseContentDepth(). This also means that the type of some fields changed from String to CharSequence. Also add Matcher for CharSequences.
This commit is contained in:
parent
54b18e3575
commit
f671b9e781
10 changed files with 80 additions and 34 deletions
|
@ -25,10 +25,10 @@ package org.jivesoftware.smack.parsing;
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class UnparsablePacket {
|
public class UnparsablePacket {
|
||||||
private final String content;
|
private final CharSequence content;
|
||||||
private final Exception e;
|
private final Exception e;
|
||||||
|
|
||||||
public UnparsablePacket(final String content, final Exception e) {
|
public UnparsablePacket(final CharSequence content, final Exception e) {
|
||||||
this.content = content;
|
this.content = content;
|
||||||
this.e = e;
|
this.e = e;
|
||||||
}
|
}
|
||||||
|
@ -46,7 +46,7 @@ public class UnparsablePacket {
|
||||||
*
|
*
|
||||||
* @return the raw stanza data
|
* @return the raw stanza data
|
||||||
*/
|
*/
|
||||||
public String getContent() {
|
public CharSequence getContent() {
|
||||||
return content;
|
return content;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -288,13 +288,13 @@ public class PacketParserUtils {
|
||||||
* @throws XmlPullParserException
|
* @throws XmlPullParserException
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public static String parseElement(XmlPullParser parser) throws XmlPullParserException, IOException {
|
public static CharSequence parseElement(XmlPullParser parser) throws XmlPullParserException, IOException {
|
||||||
assert(parser.getEventType() == XmlPullParser.START_TAG);
|
assert(parser.getEventType() == XmlPullParser.START_TAG);
|
||||||
return parseContentDepth(parser, parser.getDepth());
|
return parseContentDepth(parser, parser.getDepth());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the content of a element as string.
|
* Returns the content of a element.
|
||||||
* <p>
|
* <p>
|
||||||
* The parser must be positioned on the START_TAG of the element which content is going to get
|
* The parser must be positioned on the START_TAG of the element which content is going to get
|
||||||
* returned. If the current element is the empty element, then the empty string is returned. If
|
* returned. If the current element is the empty element, then the empty string is returned. If
|
||||||
|
@ -305,11 +305,11 @@ public class PacketParserUtils {
|
||||||
* Note that only the outermost namespace attributes ("xmlns") will be returned, not nested ones.
|
* Note that only the outermost namespace attributes ("xmlns") will be returned, not nested ones.
|
||||||
*
|
*
|
||||||
* @param parser the XML pull parser
|
* @param parser the XML pull parser
|
||||||
* @return the content of a tag as string
|
* @return the content of a tag
|
||||||
* @throws XmlPullParserException if parser encounters invalid XML
|
* @throws XmlPullParserException if parser encounters invalid XML
|
||||||
* @throws IOException if an IO error occurs
|
* @throws IOException if an IO error occurs
|
||||||
*/
|
*/
|
||||||
public static String parseContent(XmlPullParser parser)
|
public static CharSequence parseContent(XmlPullParser parser)
|
||||||
throws XmlPullParserException, IOException {
|
throws XmlPullParserException, IOException {
|
||||||
assert(parser.getEventType() == XmlPullParser.START_TAG);
|
assert(parser.getEventType() == XmlPullParser.START_TAG);
|
||||||
if (parser.isEmptyElementTag()) {
|
if (parser.isEmptyElementTag()) {
|
||||||
|
@ -338,7 +338,7 @@ public class PacketParserUtils {
|
||||||
* @throws XmlPullParserException
|
* @throws XmlPullParserException
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public static String parseContentDepth(XmlPullParser parser, int depth) throws XmlPullParserException, IOException {
|
public static CharSequence parseContentDepth(XmlPullParser parser, int depth) throws XmlPullParserException, IOException {
|
||||||
XmlStringBuilder xml = new XmlStringBuilder();
|
XmlStringBuilder xml = new XmlStringBuilder();
|
||||||
int event = parser.getEventType();
|
int event = parser.getEventType();
|
||||||
boolean isEmptyElement = false;
|
boolean isEmptyElement = false;
|
||||||
|
@ -389,7 +389,7 @@ public class PacketParserUtils {
|
||||||
}
|
}
|
||||||
event = parser.next();
|
event = parser.next();
|
||||||
}
|
}
|
||||||
return xml.toString();
|
return xml;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -995,15 +995,15 @@ public class PacketParserUtils {
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public static class UnparsedResultIQ extends IQ {
|
public static class UnparsedResultIQ extends IQ {
|
||||||
public UnparsedResultIQ(String content) {
|
public UnparsedResultIQ(CharSequence content) {
|
||||||
this.str = content;
|
this.content = content;
|
||||||
}
|
}
|
||||||
|
|
||||||
private final String str;
|
private final CharSequence content;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getChildElementXML() {
|
public CharSequence getChildElementXML() {
|
||||||
return this.str;
|
return this.content;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,8 +16,8 @@
|
||||||
*/
|
*/
|
||||||
package org.jivesoftware.smack.parsing;
|
package org.jivesoftware.smack.parsing;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertThat;
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.jivesoftware.smack.test.util.CharsequenceEquals.equalsCharSequence;
|
||||||
|
|
||||||
import org.jivesoftware.smack.SmackException;
|
import org.jivesoftware.smack.SmackException;
|
||||||
import org.jivesoftware.smack.packet.PacketExtension;
|
import org.jivesoftware.smack.packet.PacketExtension;
|
||||||
|
@ -63,14 +63,13 @@ public class ParsingExceptionTest {
|
||||||
EXTENSION2 +
|
EXTENSION2 +
|
||||||
"</message>");
|
"</message>");
|
||||||
int parserDepth = parser.getDepth();
|
int parserDepth = parser.getDepth();
|
||||||
String content = null;
|
CharSequence content = null;
|
||||||
try {
|
try {
|
||||||
PacketParserUtils.parseMessage(parser);
|
PacketParserUtils.parseMessage(parser);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
content = PacketParserUtils.parseContentDepth(parser, parserDepth);
|
content = PacketParserUtils.parseContentDepth(parser, parserDepth);
|
||||||
}
|
}
|
||||||
assertNotNull(content);
|
assertThat(MESSAGE_EXCEPTION_ELEMENT + EXTENSION2 + "</message>", equalsCharSequence(content));
|
||||||
assertEquals(MESSAGE_EXCEPTION_ELEMENT + EXTENSION2 + "</message>", content);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static class ThrowException implements PacketExtensionProvider {
|
static class ThrowException implements PacketExtensionProvider {
|
||||||
|
|
|
@ -0,0 +1,47 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Copyright © 2014 Florian Schmaus
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package org.jivesoftware.smack.test.util;
|
||||||
|
|
||||||
|
import org.hamcrest.Description;
|
||||||
|
import org.hamcrest.Factory;
|
||||||
|
import org.hamcrest.Matcher;
|
||||||
|
import org.hamcrest.TypeSafeMatcher;
|
||||||
|
|
||||||
|
public class CharsequenceEquals extends TypeSafeMatcher<CharSequence> {
|
||||||
|
|
||||||
|
private final String charSequenceString;
|
||||||
|
|
||||||
|
public CharsequenceEquals(CharSequence charSequence) {
|
||||||
|
charSequenceString = charSequence.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void describeTo(Description description) {
|
||||||
|
description.appendText("Does not match CharSequence ").appendValue(charSequenceString);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean matchesSafely(CharSequence item) {
|
||||||
|
String itemString = item.toString();
|
||||||
|
return charSequenceString.equals(itemString);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Factory
|
||||||
|
public static Matcher<CharSequence> equalsCharSequence(CharSequence charSequence) {
|
||||||
|
return new CharsequenceEquals(charSequence);
|
||||||
|
}
|
||||||
|
}
|
|
@ -786,7 +786,7 @@ public class PacketParserUtilsTest {
|
||||||
public void parseContentDepthTest() throws Exception {
|
public void parseContentDepthTest() throws Exception {
|
||||||
final String stanza = "<iq type='result' to='foo@bar.com' from='baz.com' id='42'/>";
|
final String stanza = "<iq type='result' to='foo@bar.com' from='baz.com' id='42'/>";
|
||||||
XmlPullParser parser = TestUtils.getParser(stanza, "iq");
|
XmlPullParser parser = TestUtils.getParser(stanza, "iq");
|
||||||
String content = PacketParserUtils.parseContent(parser);
|
CharSequence content = PacketParserUtils.parseContent(parser);
|
||||||
assertEquals("", content);
|
assertEquals("", content);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -26,9 +26,9 @@ import org.jivesoftware.smack.packet.PacketExtension;
|
||||||
*/
|
*/
|
||||||
public class SimplePayload implements PacketExtension
|
public class SimplePayload implements PacketExtension
|
||||||
{
|
{
|
||||||
private String elemName;
|
private final String elemName;
|
||||||
private String ns;
|
private final String ns;
|
||||||
private String payload;
|
private final CharSequence payload;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a <tt>SimplePayload</tt> object with the specified element name,
|
* Construct a <tt>SimplePayload</tt> object with the specified element name,
|
||||||
|
@ -38,7 +38,7 @@ public class SimplePayload implements PacketExtension
|
||||||
* @param namespace The namespace of the payload, null if there is none
|
* @param namespace The namespace of the payload, null if there is none
|
||||||
* @param xmlPayload The payload data
|
* @param xmlPayload The payload data
|
||||||
*/
|
*/
|
||||||
public SimplePayload(String elementName, String namespace, String xmlPayload)
|
public SimplePayload(String elementName, String namespace, CharSequence xmlPayload)
|
||||||
{
|
{
|
||||||
elemName = elementName;
|
elemName = elementName;
|
||||||
payload = xmlPayload;
|
payload = xmlPayload;
|
||||||
|
@ -55,7 +55,8 @@ public class SimplePayload implements PacketExtension
|
||||||
return ns;
|
return ns;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String toXML()
|
@Override
|
||||||
|
public CharSequence toXML()
|
||||||
{
|
{
|
||||||
return payload;
|
return payload;
|
||||||
}
|
}
|
||||||
|
|
|
@ -54,7 +54,7 @@ public class ItemProvider implements PacketExtensionProvider
|
||||||
|
|
||||||
if (ProviderManager.getExtensionProvider(payloadElemName, payloadNS) == null)
|
if (ProviderManager.getExtensionProvider(payloadElemName, payloadNS) == null)
|
||||||
{
|
{
|
||||||
String payloadText = PacketParserUtils.parseElement(parser);
|
CharSequence payloadText = PacketParserUtils.parseElement(parser);
|
||||||
return new PayloadItem<SimplePayload>(id, node, new SimplePayload(payloadElemName, payloadNS, payloadText));
|
return new PayloadItem<SimplePayload>(id, node, new SimplePayload(payloadElemName, payloadNS, payloadText));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
|
@ -148,7 +148,7 @@ public class ItemValidationTest extends InitExtensions {
|
||||||
SimplePayload payload = (SimplePayload) item.getPayload();
|
SimplePayload payload = (SimplePayload) item.getPayload();
|
||||||
assertEquals("foo", payload.getElementName());
|
assertEquals("foo", payload.getElementName());
|
||||||
assertEquals("smack:test", payload.getNamespace());
|
assertEquals("smack:test", payload.getNamespace());
|
||||||
assertXMLEqual(itemContent, payload.toXML());
|
assertXMLEqual(itemContent, payload.toXML().toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -195,7 +195,7 @@ public class ItemValidationTest extends InitExtensions {
|
||||||
SimplePayload payload = (SimplePayload) item.getPayload();
|
SimplePayload payload = (SimplePayload) item.getPayload();
|
||||||
assertEquals("entry", payload.getElementName());
|
assertEquals("entry", payload.getElementName());
|
||||||
assertEquals("http://www.w3.org/2005/Atom", payload.getNamespace());
|
assertEquals("http://www.w3.org/2005/Atom", payload.getNamespace());
|
||||||
assertXMLEqual(itemContent, payload.toXML());
|
assertXMLEqual(itemContent, payload.toXML().toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -231,6 +231,6 @@ public class ItemValidationTest extends InitExtensions {
|
||||||
assertEquals("testid1", item.getId());
|
assertEquals("testid1", item.getId());
|
||||||
assertTrue(item.getPayload() instanceof SimplePayload);
|
assertTrue(item.getPayload() instanceof SimplePayload);
|
||||||
|
|
||||||
assertXMLEqual(itemContent, ((SimplePayload)item.getPayload()).toXML());
|
assertXMLEqual(itemContent, ((SimplePayload)item.getPayload()).toXML().toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,6 +16,7 @@
|
||||||
*/
|
*/
|
||||||
package org.jivesoftware.smackx.xhtmlim.provider;
|
package org.jivesoftware.smackx.xhtmlim.provider;
|
||||||
|
|
||||||
|
import static org.jivesoftware.smack.test.util.CharsequenceEquals.equalsCharSequence;
|
||||||
import org.jivesoftware.smack.packet.PacketExtension;
|
import org.jivesoftware.smack.packet.PacketExtension;
|
||||||
import org.jivesoftware.smack.util.PacketParserUtils;
|
import org.jivesoftware.smack.util.PacketParserUtils;
|
||||||
import org.jivesoftware.smackx.xhtmlim.packet.XHTMLExtension;
|
import org.jivesoftware.smackx.xhtmlim.packet.XHTMLExtension;
|
||||||
|
@ -26,8 +27,6 @@ import org.xmlpull.v1.XmlPullParserException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import static org.hamcrest.CoreMatchers.instanceOf;
|
import static org.hamcrest.CoreMatchers.instanceOf;
|
||||||
import static org.hamcrest.CoreMatchers.is;
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.junit.Assert.assertThat;
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
|
|
||||||
|
@ -43,10 +42,10 @@ public class XHTMLExtensionProviderTest {
|
||||||
XHTMLExtensionProvider provider = new XHTMLExtensionProvider();
|
XHTMLExtensionProvider provider = new XHTMLExtensionProvider();
|
||||||
PacketExtension extension = provider.parseExtension(parser);
|
PacketExtension extension = provider.parseExtension(parser);
|
||||||
|
|
||||||
assertThat(extension, is(instanceOf(XHTMLExtension.class)));
|
assertThat(extension, instanceOf(XHTMLExtension.class));
|
||||||
XHTMLExtension attachmentsInfo = (XHTMLExtension) extension;
|
XHTMLExtension attachmentsInfo = (XHTMLExtension) extension;
|
||||||
|
|
||||||
assertEquals(sampleXhtml(), attachmentsInfo.getBodies().get(0));
|
assertThat(sampleXhtml(), equalsCharSequence(attachmentsInfo.getBodies().get(0)));
|
||||||
}
|
}
|
||||||
|
|
||||||
private String sampleXhtml() {
|
private String sampleXhtml() {
|
||||||
|
|
|
@ -982,7 +982,7 @@ public class XMPPTCPConnection extends AbstractXMPPConnection {
|
||||||
try {
|
try {
|
||||||
packet = PacketParserUtils.parseStanza(parser, XMPPTCPConnection.this);
|
packet = PacketParserUtils.parseStanza(parser, XMPPTCPConnection.this);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
String content = PacketParserUtils.parseContentDepth(parser, parserDepth);
|
CharSequence content = PacketParserUtils.parseContentDepth(parser, parserDepth);
|
||||||
UnparsablePacket message = new UnparsablePacket(content, e);
|
UnparsablePacket message = new UnparsablePacket(content, e);
|
||||||
if (callback != null) {
|
if (callback != null) {
|
||||||
callback.handleUnparsablePacket(message);
|
callback.handleUnparsablePacket(message);
|
||||||
|
|
Loading…
Reference in a new issue