001/* 002 * 003 * Copyright 2003-2007 Jive Software. 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.jivesoftware.smackx.workgroup.ext.notes; 019 020import java.io.IOException; 021 022import org.jivesoftware.smack.packet.IQ; 023import org.jivesoftware.smack.packet.IqData; 024import org.jivesoftware.smack.packet.XmlEnvironment; 025import org.jivesoftware.smack.provider.IqProvider; 026import org.jivesoftware.smack.xml.XmlPullParser; 027import org.jivesoftware.smack.xml.XmlPullParserException; 028 029import org.jxmpp.JxmppContext; 030 031/** 032 * IQ stanza for retrieving and adding Chat Notes. 033 */ 034public class ChatNotes extends IQ { 035 036 /** 037 * Element name of the stanza extension. 038 */ 039 public static final String ELEMENT_NAME = "chat-notes"; 040 041 /** 042 * Namespace of the stanza extension. 043 */ 044 public static final String NAMESPACE = "http://jivesoftware.com/protocol/workgroup"; 045 046 public ChatNotes() { 047 super(ELEMENT_NAME, NAMESPACE); 048 } 049 050 private String sessionID; 051 private String notes; 052 053 public String getSessionID() { 054 return sessionID; 055 } 056 057 public void setSessionID(String sessionID) { 058 this.sessionID = sessionID; 059 } 060 061 public String getNotes() { 062 return notes; 063 } 064 065 public void setNotes(String notes) { 066 this.notes = notes; 067 } 068 069 @Override 070 protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) { 071 buf.rightAngleBracket(); 072 073 buf.append("<sessionID>").append(getSessionID()).append("</sessionID>"); 074 075 if (getNotes() != null) { 076 buf.element("notes", getNotes()); 077 } 078 079 return buf; 080 } 081 082 /** 083 * An IQProvider for ChatNotes packets. 084 * 085 * @author Derek DeMoro 086 */ 087 public static class Provider extends IqProvider<ChatNotes> { 088 089 @Override 090 public ChatNotes parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment, JxmppContext jxmppContext) throws XmlPullParserException, IOException { 091 ChatNotes chatNotes = new ChatNotes(); 092 093 boolean done = false; 094 while (!done) { 095 XmlPullParser.Event eventType = parser.next(); 096 if (eventType == XmlPullParser.Event.START_ELEMENT) { 097 if (parser.getName().equals("sessionID")) { 098 chatNotes.setSessionID(parser.nextText()); 099 } 100 else if (parser.getName().equals("text")) { 101 String note = parser.nextText(); 102 note = note.replaceAll("\\\\n", "\n"); 103 chatNotes.setNotes(note); 104 } 105 } 106 else if (eventType == XmlPullParser.Event.END_ELEMENT) { 107 if (parser.getName().equals(ELEMENT_NAME)) { 108 done = true; 109 } 110 } 111 } 112 113 return chatNotes; 114 } 115 } 116 117 /** 118 * Replaces all instances of oldString with newString in string. 119 * 120 * @param string the String to search to perform replacements on 121 * @param oldString the String that should be replaced by newString 122 * @param newString the String that will replace all instances of oldString 123 * @return a String will all instances of oldString replaced by newString 124 */ 125 public static final String replace(String string, String oldString, String newString) { 126 if (string == null) { 127 return null; 128 } 129 // If the newString is null or zero length, just return the string since there's nothing 130 // to replace. 131 if (newString == null) { 132 return string; 133 } 134 int i = 0; 135 // Make sure that oldString appears at least once before doing any processing. 136 if ((i = string.indexOf(oldString, i)) >= 0) { 137 // Use char []'s, as they are more efficient to deal with. 138 char[] string2 = string.toCharArray(); 139 char[] newString2 = newString.toCharArray(); 140 int oLength = oldString.length(); 141 StringBuilder buf = new StringBuilder(string2.length); 142 buf.append(string2, 0, i).append(newString2); 143 i += oLength; 144 int j = i; 145 // Replace all remaining instances of oldString with newString. 146 while ((i = string.indexOf(oldString, i)) > 0) { 147 buf.append(string2, j, i - j).append(newString2); 148 i += oLength; 149 j = i; 150 } 151 buf.append(string2, j, string2.length - j); 152 return buf.toString(); 153 } 154 return string; 155 } 156} 157 158 159