AbstractParser.java

  1package eu.siacs.conversations.parser;
  2
  3import java.text.ParseException;
  4import java.text.SimpleDateFormat;
  5import java.util.Locale;
  6
  7import eu.siacs.conversations.entities.Account;
  8import eu.siacs.conversations.entities.Contact;
  9import eu.siacs.conversations.entities.Conversation;
 10import eu.siacs.conversations.entities.MucOptions;
 11import eu.siacs.conversations.services.XmppConnectionService;
 12import eu.siacs.conversations.xml.Element;
 13import eu.siacs.conversations.xmpp.jid.InvalidJidException;
 14import eu.siacs.conversations.xmpp.jid.Jid;
 15
 16public abstract class AbstractParser {
 17
 18	protected XmppConnectionService mXmppConnectionService;
 19
 20	protected AbstractParser(XmppConnectionService service) {
 21		this.mXmppConnectionService = service;
 22	}
 23
 24	public static Long parseTimestamp(Element element, Long d) {
 25		Element delay = element.findChild("delay","urn:xmpp:delay");
 26		if (delay != null) {
 27			String stamp = delay.getAttribute("stamp");
 28			if (stamp != null) {
 29				try {
 30					return AbstractParser.parseTimestamp(delay.getAttribute("stamp"));
 31				} catch (ParseException e) {
 32					return d;
 33				}
 34			}
 35		}
 36		return d;
 37	}
 38
 39	public static long parseTimestamp(Element element) {
 40		return parseTimestamp(element, System.currentTimeMillis());
 41	}
 42
 43	public static long parseTimestamp(String timestamp) throws ParseException {
 44		timestamp = timestamp.replace("Z", "+0000");
 45		SimpleDateFormat dateFormat;
 46		long ms;
 47		if (timestamp.charAt(19) == '.' && timestamp.length() >= 25) {
 48			String millis = timestamp.substring(19,timestamp.length() - 5);
 49			try {
 50				double fractions = Double.parseDouble("0" + millis);
 51				ms = Math.round(1000 * fractions);
 52			} catch (NumberFormatException e) {
 53				ms = 0;
 54			}
 55		} else {
 56			ms = 0;
 57		}
 58		timestamp = timestamp.substring(0,19)+timestamp.substring(timestamp.length() -5,timestamp.length());
 59		dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ",Locale.US);
 60		return Math.min(dateFormat.parse(timestamp).getTime()+ms, System.currentTimeMillis());
 61	}
 62
 63	protected void updateLastseen(final Account account, final Jid from) {
 64		final Contact contact = account.getRoster().getContact(from);
 65		contact.setLastResource(from.isBareJid() ? "" : from.getResourcepart());
 66	}
 67
 68	protected String avatarData(Element items) {
 69		Element item = items.findChild("item");
 70		if (item == null) {
 71			return null;
 72		}
 73		return item.findChildContent("data", "urn:xmpp:avatar:data");
 74	}
 75
 76	public static MucOptions.User parseItem(Conversation conference, Element item) {
 77		final String local = conference.getJid().getLocalpart();
 78		final String domain = conference.getJid().getDomainpart();
 79		String affiliation = item.getAttribute("affiliation");
 80		String role = item.getAttribute("role");
 81		String nick = item.getAttribute("nick");
 82		Jid fullJid;
 83		try {
 84			fullJid = nick != null ? Jid.fromParts(local, domain, nick) : null;
 85		} catch (InvalidJidException e) {
 86			fullJid = null;
 87		}
 88		Jid realJid = item.getAttributeAsJid("jid");
 89		MucOptions.User user = new MucOptions.User(conference.getMucOptions(), nick == null ? null : fullJid);
 90		user.setRealJid(realJid);
 91		user.setAffiliation(affiliation);
 92		user.setRole(role);
 93		return user;
 94	}
 95
 96	public static String extractErrorMessage(Element packet) {
 97		final Element error = packet.findChild("error");
 98		if (error != null && error.getChildren().size() > 0) {
 99			final String text = error.findChildContent("text");
100			if (text != null && !text.trim().isEmpty()) {
101				return text;
102			} else {
103				return error.getChildren().get(0).getName().replace("-"," ");
104			}
105		} else {
106			return null;
107		}
108	}
109}