1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-10-31 22:15:59 +01:00

Make XHTMLText use XmlStringBuidler

also define all element and attribute names as constants.
This commit is contained in:
Florian Schmaus 2014-07-05 14:19:31 +02:00
parent 8526f8ab29
commit d8d88d9abd
6 changed files with 185 additions and 206 deletions

View file

@ -47,6 +47,8 @@ import java.util.*;
*/
public class Message extends Packet {
public static final String BODY = "body";
private Type type = Type.normal;
private String thread = null;
private String language;
@ -440,9 +442,9 @@ public class Message extends Packet {
// Skip the default language
if(body.equals(defaultBody))
continue;
buf.halfOpenElement("body").xmllangAttribute(body.getLanguage()).rightAngelBracket();
buf.halfOpenElement(BODY).xmllangAttribute(body.getLanguage()).rightAngelBracket();
buf.escape(body.getMessage());
buf.closeElement("body");
buf.closeElement(BODY);
}
buf.optElement("thread", thread);
// Append the error subpacket if the message type is an error.

View file

@ -185,7 +185,7 @@ public class PacketParserUtils {
message.addSubject(xmlLang, subject);
}
}
else if (elementName.equals("body")) {
else if (elementName.equals(Message.BODY)) {
String xmlLang = getLanguageAttribute(parser);
if (xmlLang == null) {
xmlLang = defaultLanguage;

View file

@ -37,8 +37,6 @@ import java.util.List;
* @author Gaston Dombiak
*/
public class XHTMLManager {
private final static String namespace = "http://jabber.org/protocol/xhtml-im";
// Enable the XHTML support on every established connection
// The ServiceDiscoveryManager class should have been already initialized
static {
@ -57,7 +55,7 @@ public class XHTMLManager {
* @return an Iterator for the bodies in the message or null if none.
*/
public static List<String> getBodies(Message message) {
XHTMLExtension xhtmlExtension = (XHTMLExtension) message.getExtension("html", namespace);
XHTMLExtension xhtmlExtension = (XHTMLExtension) message.getExtension(XHTMLExtension.ELEMENT, XHTMLExtension.NAMESPACE);
if (xhtmlExtension != null)
return xhtmlExtension.getBodies();
else
@ -71,7 +69,7 @@ public class XHTMLManager {
* @param body the string to add as an XHTML body to the message
*/
public static void addBody(Message message, String body) {
XHTMLExtension xhtmlExtension = (XHTMLExtension) message.getExtension("html", namespace);
XHTMLExtension xhtmlExtension = (XHTMLExtension) message.getExtension(XHTMLExtension.ELEMENT, XHTMLExtension.NAMESPACE);
if (xhtmlExtension == null) {
// Create an XHTMLExtension and add it to the message
xhtmlExtension = new XHTMLExtension();
@ -88,7 +86,7 @@ public class XHTMLManager {
* @return a boolean indicating whether the message is an XHTML message
*/
public static boolean isXHTMLMessage(Message message) {
return message.getExtension("html", namespace) != null;
return message.getExtension(XHTMLExtension.ELEMENT, XHTMLExtension.NAMESPACE) != null;
}
/**
@ -105,10 +103,10 @@ public class XHTMLManager {
return;
if (enabled) {
ServiceDiscoveryManager.getInstanceFor(connection).addFeature(namespace);
ServiceDiscoveryManager.getInstanceFor(connection).addFeature(XHTMLExtension.NAMESPACE);
}
else {
ServiceDiscoveryManager.getInstanceFor(connection).removeFeature(namespace);
ServiceDiscoveryManager.getInstanceFor(connection).removeFeature(XHTMLExtension.NAMESPACE);
}
}
@ -119,7 +117,7 @@ public class XHTMLManager {
* @return a boolean indicating if the XHTML support is enabled for the given connection
*/
public static boolean isServiceEnabled(XMPPConnection connection) {
return ServiceDiscoveryManager.getInstanceFor(connection).includesFeature(namespace);
return ServiceDiscoveryManager.getInstanceFor(connection).includesFeature(XHTMLExtension.NAMESPACE);
}
/**
@ -134,6 +132,6 @@ public class XHTMLManager {
*/
public static boolean isServiceEnabled(XMPPConnection connection, String userID)
throws NoResponseException, XMPPErrorException, NotConnectedException {
return ServiceDiscoveryManager.getInstanceFor(connection).supportsFeature(userID, namespace);
return ServiceDiscoveryManager.getInstanceFor(connection).supportsFeature(userID, XHTMLExtension.NAMESPACE);
}
}

View file

@ -17,7 +17,8 @@
package org.jivesoftware.smackx.xhtmlim;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.util.XmlStringBuilder;
/**
* An XHTMLText represents formatted text. This class also helps to build valid
@ -27,9 +28,9 @@ import org.jivesoftware.smack.util.StringUtils;
*/
public class XHTMLText {
private static final String NAMESPACE = "http://www.w3.org/1999/xhtml";
public static final String NAMESPACE = "http://www.w3.org/1999/xhtml";
private StringBuilder text = new StringBuilder(30);
private final XmlStringBuilder text = new XmlStringBuilder();
/**
* Creates a new XHTMLText with body tag params.
@ -41,58 +42,53 @@ public class XHTMLText {
appendOpenBodyTag(style, lang);
}
public static final String A = "a";
public static final String HREF = "href";
public static final String STYLE = "style";
/**
* Appends a tag that indicates that an anchor section begins.
*
* @param href indicates the URL being linked to
* @param style the XHTML style of the anchor
*/
public void appendOpenAnchorTag(String href, String style) {
StringBuilder sb = new StringBuilder("<a");
if (href != null) {
sb.append(" href=\"");
sb.append(href);
sb.append("\"");
}
if (style != null) {
sb.append(" style=\"");
sb.append(style);
sb.append("\"");
}
sb.append(">");
text.append(sb.toString());
public XHTMLText appendOpenAnchorTag(String href, String style) {
text.halfOpenElement(A);
text.optAttribute(HREF, href);
text.optAttribute(STYLE, style);
text.rightAngelBracket();
return this;
}
/**
* Appends a tag that indicates that an anchor section ends.
*
*/
public void appendCloseAnchorTag() {
text.append("</a>");
public XHTMLText appendCloseAnchorTag() {
text.closeElement(A);
return this;
}
public static final String BLOCKQUOTE = "blockquote";
/**
* Appends a tag that indicates that a blockquote section begins.
*
* @param style the XHTML style of the blockquote
*/
public void appendOpenBlockQuoteTag(String style) {
StringBuilder sb = new StringBuilder("<blockquote");
if (style != null) {
sb.append(" style=\"");
sb.append(style);
sb.append("\"");
}
sb.append(">");
text.append(sb.toString());
public XHTMLText appendOpenBlockQuoteTag(String style) {
text.halfOpenElement(BLOCKQUOTE);
text.optAttribute(STYLE, style);
text.rightAngelBracket();
return this;
}
/**
* Appends a tag that indicates that a blockquote section ends.
*
*/
public void appendCloseBlockQuoteTag() {
text.append("</blockquote>");
public XHTMLText appendCloseBlockQuoteTag() {
text.closeElement(BLOCKQUOTE);
return this;
}
/**
@ -101,114 +97,113 @@ public class XHTMLText {
* @param style the XHTML style of the body
* @param lang the language of the body
*/
private void appendOpenBodyTag(String style, String lang) {
StringBuilder sb = new StringBuilder("<body xmlns=\"" + NAMESPACE + "\"");
if (style != null) {
sb.append(" style=\"");
sb.append(style);
sb.append("\"");
}
if (lang != null) {
sb.append(" xml:lang=\"");
sb.append(lang);
sb.append("\"");
}
sb.append(">");
text.append(sb.toString());
private XHTMLText appendOpenBodyTag(String style, String lang) {
text.halfOpenElement(Message.BODY);
text.xmlnsAttribute(NAMESPACE);
text.optElement(STYLE, style);
text.xmllangAttribute(lang);
text.rightAngelBracket();
return this;
}
/**
* Appends a tag that indicates that a body section ends.
*
*/
private String closeBodyTag() {
return "</body>";
public XHTMLText appendCloseBodyTag() {
text.closeElement(Message.BODY);
return this;
}
public static final String BR = "br";
public static final String CITE = "cite";
public static final String CODE = "code";
/**
* Appends a tag that inserts a single carriage return.
*
*/
public void appendBrTag() {
text.append("<br/>");
public XHTMLText appendBrTag() {
text.closeElement(BR);
return this;
}
/**
* Appends a tag that indicates a reference to work, such as a book, report or web site.
*
*/
public void appendOpenCiteTag() {
text.append("<cite>");
public XHTMLText appendOpenCiteTag() {
text.openElement(CITE);
return this;
}
/**
* Appends a tag that indicates text that is the code for a program.
*
*/
public void appendOpenCodeTag() {
text.append("<code>");
public XHTMLText appendOpenCodeTag() {
text.openElement(CODE);
return this;
}
/**
* Appends a tag that indicates end of text that is the code for a program.
*
*/
public void appendCloseCodeTag() {
text.append("</code>");
public XHTMLText appendCloseCodeTag() {
text.closeElement(CODE);
return this;
}
public static final String EM = "em";
/**
* Appends a tag that indicates emphasis.
*
*/
public void appendOpenEmTag() {
text.append("<em>");
public XHTMLText appendOpenEmTag() {
text.openElement(EM);
return this;
}
/**
* Appends a tag that indicates end of emphasis.
*
*/
public void appendCloseEmTag() {
text.append("</em>");
public XHTMLText appendCloseEmTag() {
text.closeElement(EM);
return this;
}
public static final String H = "h";
/**
* Appends a tag that indicates a header, a title of a section of the message.
*
* @param level the level of the Header. It should be a value between 1 and 3
* @param level the level of the Header. It must be a value between 1 and 3
* @param style the XHTML style of the blockquote
*/
public void appendOpenHeaderTag(int level, String style) {
public XHTMLText appendOpenHeaderTag(int level, String style) {
if (level > 3 || level < 1) {
return;
throw new IllegalArgumentException("Level must be between 1 and 3");
}
StringBuilder sb = new StringBuilder("<h");
sb.append(level);
if (style != null) {
sb.append(" style=\"");
sb.append(style);
sb.append("\"");
}
sb.append(">");
text.append(sb.toString());
text.halfOpenElement(H + Integer.toString(level));
text.optAttribute(STYLE, style);
text.rightAngelBracket();
return this;
}
/**
* Appends a tag that indicates that a header section ends.
*
* @param level the level of the Header. It should be a value between 1 and 3
* @param level the level of the Header. It must be a value between 1 and 3
*/
public void appendCloseHeaderTag(int level) {
public XHTMLText appendCloseHeaderTag(int level) {
if (level > 3 || level < 1) {
return;
throw new IllegalArgumentException("Level must be between 1 and 3");
}
StringBuilder sb = new StringBuilder("</h");
sb.append(level);
sb.append(">");
text.append(sb.toString());
text.closeElement(H + Integer.toBinaryString(level));
return this;
}
public static final String IMG = "img";
/**
* Appends a tag that indicates an image.
*
@ -218,51 +213,30 @@ public class XHTMLText {
* @param src where to get the picture
* @param width how wide is the picture
*/
public void appendImageTag(String align, String alt, String height, String src, String width) {
StringBuilder sb = new StringBuilder("<img");
if (align != null) {
sb.append(" align=\"");
sb.append(align);
sb.append("\"");
}
if (alt != null) {
sb.append(" alt=\"");
sb.append(alt);
sb.append("\"");
}
if (height != null) {
sb.append(" height=\"");
sb.append(height);
sb.append("\"");
}
if (src != null) {
sb.append(" src=\"");
sb.append(src);
sb.append("\"");
}
if (width != null) {
sb.append(" width=\"");
sb.append(width);
sb.append("\"");
}
sb.append(">");
text.append(sb.toString());
public XHTMLText appendImageTag(String align, String alt, String height, String src, String width) {
text.halfOpenElement(IMG);
text.optAttribute("align", align);
text.optAttribute("alt", alt);
text.optAttribute("height", height);
text.optAttribute("src", src);
text.optAttribute("width", width);
text.rightAngelBracket();
return this;
}
public static final String LI = "li";
public static final String OL = "ol";
/**
* Appends a tag that indicates the start of a new line item within a list.
*
* @param style the style of the line item
*/
public void appendLineItemTag(String style) {
StringBuilder sb = new StringBuilder("<li");
if (style != null) {
sb.append(" style=\"");
sb.append(style);
sb.append("\"");
}
sb.append(">");
text.append(sb.toString());
public XHTMLText appendLineItemTag(String style) {
text.halfOpenElement(LI);
text.optAttribute(STYLE, style);
text.rightAngelBracket();
return this;
}
/**
@ -271,65 +245,59 @@ public class XHTMLText {
*
* @param style the style of the ordered list
*/
public void appendOpenOrderedListTag(String style) {
StringBuilder sb = new StringBuilder("<ol");
if (style != null) {
sb.append(" style=\"");
sb.append(style);
sb.append("\"");
}
sb.append(">");
text.append(sb.toString());
public XHTMLText appendOpenOrderedListTag(String style) {
text.halfOpenElement(OL);
text.optAttribute(STYLE, style);
text.rightAngelBracket();
return this;
}
/**
* Appends a tag that indicates that an ordered list section ends.
*
*/
public void appendCloseOrderedListTag() {
text.append("</ol>");
public XHTMLText appendCloseOrderedListTag() {
text.closeElement(OL);
return this;
}
public static final String UL = "ul";
/**
* Appends a tag that creates an unordered list. The unordered part means that the items
* in the list are not in any particular order.
*
* @param style the style of the unordered list
*/
public void appendOpenUnorderedListTag(String style) {
StringBuilder sb = new StringBuilder("<ul");
if (style != null) {
sb.append(" style=\"");
sb.append(style);
sb.append("\"");
}
sb.append(">");
text.append(sb.toString());
public XHTMLText appendOpenUnorderedListTag(String style) {
text.halfOpenElement(UL);
text.optAttribute(STYLE, style);
text.rightAngelBracket();
return this;
}
/**
* Appends a tag that indicates that an unordered list section ends.
*
*/
public void appendCloseUnorderedListTag() {
text.append("</ul>");
public XHTMLText appendCloseUnorderedListTag() {
text.closeElement(UL);
return this;
}
public static final String P = "p";
/**
* Appends a tag that indicates the start of a new paragraph. This is usually rendered
* with two carriage returns, producing a single blank line in between the two paragraphs.
*
* @param style the style of the paragraph
*/
public void appendOpenParagraphTag(String style) {
StringBuilder sb = new StringBuilder("<p");
if (style != null) {
sb.append(" style=\"");
sb.append(style);
sb.append("\"");
}
sb.append(">");
text.append(sb.toString());
public XHTMLText appendOpenParagraphTag(String style) {
text.halfOpenElement(P);
text.optAttribute(STYLE, style);
text.rightAngelBracket();
return this;
}
/**
@ -337,72 +305,75 @@ public class XHTMLText {
* with two carriage returns, producing a single blank line in between the two paragraphs.
*
*/
public void appendCloseParagraphTag() {
text.append("</p>");
public XHTMLText appendCloseParagraphTag() {
text.closeElement(P);
return this;
}
public static final String Q = "q";
/**
* Appends a tag that indicates that an inlined quote section begins.
*
* @param style the style of the inlined quote
*/
public void appendOpenInlinedQuoteTag(String style) {
StringBuilder sb = new StringBuilder("<q");
if (style != null) {
sb.append(" style=\"");
sb.append(style);
sb.append("\"");
}
sb.append(">");
text.append(sb.toString());
public XHTMLText appendOpenInlinedQuoteTag(String style) {
text.halfOpenElement(Q);
text.optAttribute(STYLE, style);
text.rightAngelBracket();
return this;
}
/**
* Appends a tag that indicates that an inlined quote section ends.
*
*/
public void appendCloseInlinedQuoteTag() {
text.append("</q>");
public XHTMLText appendCloseInlinedQuoteTag() {
text.closeElement(Q);
return this;
}
public static final String SPAN = "span";
/**
* Appends a tag that allows to set the fonts for a span of text.
*
* @param style the style for a span of text
*/
public void appendOpenSpanTag(String style) {
StringBuilder sb = new StringBuilder("<span");
if (style != null) {
sb.append(" style=\"");
sb.append(style);
sb.append("\"");
}
sb.append(">");
text.append(sb.toString());
public XHTMLText appendOpenSpanTag(String style) {
text.halfOpenElement(SPAN);
text.optAttribute(STYLE, style);
text.rightAngelBracket();
return this;
}
/**
* Appends a tag that indicates that a span section ends.
*
*/
public void appendCloseSpanTag() {
text.append("</span>");
public XHTMLText appendCloseSpanTag() {
text.closeElement(SPAN);
return this;
}
public static final String STRONG = "strong";
/**
* Appends a tag that indicates text which should be more forceful than surrounding text.
*
*/
public void appendOpenStrongTag() {
text.append("<strong>");
public XHTMLText appendOpenStrongTag() {
text.openElement(STRONG);
return this;
}
/**
* Appends a tag that indicates that a strong section ends.
*
*/
public void appendCloseStrongTag() {
text.append("</strong>");
public XHTMLText appendCloseStrongTag() {
text.closeElement(STRONG);
return this;
}
/**
@ -410,8 +381,9 @@ public class XHTMLText {
*
* @param textToAppend the text to append
*/
public void append(String textToAppend) {
text.append(StringUtils.escapeForXML(textToAppend));
public XHTMLText append(String textToAppend) {
text.escape(textToAppend);
return this;
}
/**
@ -422,7 +394,11 @@ public class XHTMLText {
* @return the text of the XHTMLText
*/
public String toString() {
return text.toString().concat(closeBodyTag());
appendCloseBodyTag();
return text.toString();
}
public XmlStringBuilder toXML() {
return text;
}
}

View file

@ -18,6 +18,7 @@
package org.jivesoftware.smackx.xhtmlim.packet;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.util.XmlStringBuilder;
import java.util.ArrayList;
import java.util.Collections;
@ -38,6 +39,9 @@ import java.util.List;
*/
public class XHTMLExtension implements PacketExtension {
public static final String ELEMENT = "html";
public static final String NAMESPACE = "http://jabber.org/protocol/xhtml-im";
private List<String> bodies = new ArrayList<String>();
/**
@ -47,7 +51,7 @@ public class XHTMLExtension implements PacketExtension {
* @return the XML element name of the packet extension.
*/
public String getElementName() {
return "html";
return ELEMENT;
}
/**
@ -57,7 +61,7 @@ public class XHTMLExtension implements PacketExtension {
* @return the XML namespace of the packet extension.
*/
public String getNamespace() {
return "http://jabber.org/protocol/xhtml-im";
return NAMESPACE;
}
/**
@ -76,16 +80,16 @@ public class XHTMLExtension implements PacketExtension {
* </pre>
*
*/
public String toXML() {
StringBuilder buf = new StringBuilder();
buf.append("<").append(getElementName()).append(" xmlns=\"").append(getNamespace()).append(
"\">");
@Override
public XmlStringBuilder toXML() {
XmlStringBuilder xml = new XmlStringBuilder(this);
xml.rightAngelBracket();
// Loop through all the bodies and append them to the string buffer
for (String body : getBodies()) {
buf.append(body);
xml.append(body);
}
buf.append("</").append(getElementName()).append(">");
return buf.toString();
xml.closeElement(this);
return xml;
}
/**

View file

@ -16,6 +16,7 @@
*/
package org.jivesoftware.smackx.xhtmlim.provider;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.provider.PacketExtensionProvider;
import org.jivesoftware.smack.util.StringUtils;
@ -31,8 +32,6 @@ import java.io.IOException;
* @author Vyacheslav Blinov
*/
public class XHTMLExtensionProvider implements PacketExtensionProvider {
public static final String BODY_ELEMENT = "body";
@Override
public PacketExtension parseExtension(XmlPullParser parser) throws IOException, XmlPullParserException {
XHTMLExtension xhtmlExtension = new XHTMLExtension();
@ -47,7 +46,7 @@ public class XHTMLExtensionProvider implements PacketExtensionProvider {
int eventType = parser.next();
if (eventType == XmlPullParser.START_TAG) {
boolean appendNamespace = false;
if (BODY_ELEMENT.equals(parser.getName())) {
if (Message.BODY.equals(parser.getName())) {
buffer = new StringBuilder();
tagDepth = parser.getDepth();
appendNamespace = true;
@ -67,7 +66,7 @@ public class XHTMLExtensionProvider implements PacketExtensionProvider {
// handle self-closing tags by our own means
appendEndTag(buffer, parser, tagStarted);
tagStarted = false;
if (BODY_ELEMENT.equals(name) && parser.getDepth() <= tagDepth) {
if (Message.BODY.equals(name) && parser.getDepth() <= tagDepth) {
xhtmlExtension.addBody(buffer.toString());
}
}