parent
f69cd55970
commit
cd75cb30b2
7 changed files with 144 additions and 2 deletions
@ -0,0 +1,10 @@ |
||||
package org.jivesoftware.smack.initializer; |
||||
|
||||
public final class SmackCoreInitializer extends UrlInitializer { |
||||
|
||||
@Override |
||||
protected String getProvidersUri() { |
||||
return "classpath:org.jivesoftware.smack/smack.providers"; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,43 @@ |
||||
/** |
||||
* |
||||
* Copyright 2018 Paul Schaub. |
||||
* |
||||
* 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.packet; |
||||
|
||||
import org.jivesoftware.smack.util.XmlStringBuilder; |
||||
|
||||
/** |
||||
* BodyElement that should only be used if the body is used as a child element of an element which is not a message. |
||||
* The difference of this class to {@link Message.Body} is, that a BodyElement when serialized into xml, will |
||||
* annotate the namespace "jabber:client". |
||||
*/ |
||||
public class BodyElement extends Message.Body { |
||||
|
||||
public BodyElement(String language, String message) { |
||||
super(language, message); |
||||
} |
||||
|
||||
@Override |
||||
public XmlStringBuilder toXML() { |
||||
XmlStringBuilder xml = new XmlStringBuilder(); |
||||
xml.halfOpenElement(getElementName()) |
||||
.xmlnsAttribute(getNamespace()) |
||||
.xmllangAttribute(getLanguage()) |
||||
.rightAngleBracket(); |
||||
xml.escape(getMessage()); |
||||
xml.closeElement(getElementName()); |
||||
return xml; |
||||
} |
||||
} |
@ -0,0 +1,39 @@ |
||||
/** |
||||
* |
||||
* Copyright © 2018 Paul Schaub |
||||
* |
||||
* 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.provider; |
||||
|
||||
import org.jivesoftware.smack.packet.BodyElement; |
||||
import org.jivesoftware.smack.packet.Stanza; |
||||
import org.jivesoftware.smack.util.ParserUtils; |
||||
|
||||
import org.xmlpull.v1.XmlPullParser; |
||||
|
||||
public class BodyProvider extends ExtensionElementProvider<BodyElement> { |
||||
|
||||
public static final BodyProvider TEST_INSTANCE = new BodyProvider(); |
||||
|
||||
@Override |
||||
public BodyElement parse(XmlPullParser parser, int initialDepth) throws Exception { |
||||
String xmlLang = ParserUtils.getXmlLang(parser); |
||||
if (xmlLang == null) { |
||||
xmlLang = Stanza.getDefaultLanguage(); |
||||
} |
||||
|
||||
String body = parser.nextText(); |
||||
return new BodyElement(xmlLang, body); |
||||
} |
||||
} |
@ -0,0 +1,11 @@ |
||||
<?xml version="1.0"?> |
||||
<!-- Providers for Smack --> |
||||
<smackProviders> |
||||
|
||||
<extensionProvider> |
||||
<elementName>body</elementName> |
||||
<namespace>jabber:client</namespace> |
||||
<className>org.jivesoftware.smack.provider.BodyProvider</className> |
||||
</extensionProvider> |
||||
|
||||
</smackProviders> |
@ -0,0 +1,38 @@ |
||||
/** |
||||
* |
||||
* Copyright © 2018 Paul Schaub |
||||
* |
||||
* 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.provider; |
||||
|
||||
import static junit.framework.TestCase.assertEquals; |
||||
|
||||
import org.jivesoftware.smack.packet.Message; |
||||
import org.jivesoftware.smack.test.util.SmackTestSuite; |
||||
import org.jivesoftware.smack.test.util.TestUtils; |
||||
|
||||
import org.junit.Test; |
||||
import org.xmlpull.v1.XmlPullParser; |
||||
|
||||
public class BodyProviderTest extends SmackTestSuite { |
||||
|
||||
@Test |
||||
public void bodyProviderTest() throws Exception { |
||||
String xml = "<body xmlns='jabber:client' xml:lang='en'>This is a message.</body>"; |
||||
XmlPullParser parser = TestUtils.getParser(xml); |
||||
Message.Body body = BodyProvider.TEST_INSTANCE.parse(parser); |
||||
assertEquals("This is a message.", body.getMessage()); |
||||
assertEquals("en", body.getLanguage()); |
||||
} |
||||
} |
Loading…
Reference in new issue