1 /** 2 * $Revision$ 3 * $Date$ 4 * 5 * Copyright 2003-2007 Jive Software. 6 * 7 * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); 8 * you may not use this file except in compliance with the License. 9 * You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, software 14 * distributed under the License is distributed on an "AS IS" BASIS, 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 * See the License for the specific language governing permissions and 17 * limitations under the License. 18 */ 19 20 package org.jivesoftware.smackx.workgroup.ext.notes; 21 22 import org.jivesoftware.smack.packet.IQ; 23 import org.jivesoftware.smack.provider.IQProvider; 24 import org.xmlpull.v1.XmlPullParser; 25 26 /** 27 * IQ packet for retrieving and adding Chat Notes. 28 */ 29 public class ChatNotes extends IQ { 30 31 /** 32 * Element name of the packet extension. 33 */ 34 public static final String ELEMENT_NAME = "chat-notes"; 35 36 /** 37 * Namespace of the packet extension. 38 */ 39 public static final String NAMESPACE = "http://jivesoftware.com/protocol/workgroup"; 40 41 42 private String sessionID; 43 private String notes; 44 45 public String getSessionID() { 46 return sessionID; 47 } 48 49 public void setSessionID(String sessionID) { 50 this.sessionID = sessionID; 51 } 52 53 public String getNotes() { 54 return notes; 55 } 56 57 public void setNotes(String notes) { 58 this.notes = notes; 59 } 60 61 public String getChildElementXML() { 62 StringBuilder buf = new StringBuilder(); 63 64 buf.append("<").append(ELEMENT_NAME).append(" xmlns=\"").append(NAMESPACE).append("\">"); 65 buf.append("<sessionID>").append(getSessionID()).append("</sessionID>"); 66 67 if (getNotes() != null) { 68 buf.append("<notes>").append(getNotes()).append("</notes>"); 69 } 70 buf.append("</").append(ELEMENT_NAME).append("> "); 71 72 return buf.toString(); 73 } 74 75 /** 76 * An IQProvider for ChatNotes packets. 77 * 78 * @author Derek DeMoro 79 */ 80 public static class Provider implements IQProvider { 81 82 public Provider() { 83 super(); 84 } 85 86 public IQ parseIQ(XmlPullParser parser) throws Exception { 87 ChatNotes chatNotes = new ChatNotes(); 88 89 boolean done = false; 90 while (!done) { 91 int eventType = parser.next(); 92 if (eventType == XmlPullParser.START_TAG) { 93 if (parser.getName().equals("sessionID")) { 94 chatNotes.setSessionID(parser.nextText()); 95 } 96 else if (parser.getName().equals("text")) { 97 String note = parser.nextText(); 98 note = note.replaceAll("\\\\n", "\n"); 99 chatNotes.setNotes(note); 100 } 101 } 102 else if (eventType == XmlPullParser.END_TAG) { 103 if (parser.getName().equals(ELEMENT_NAME)) { 104 done = true; 105 } 106 } 107 } 108 109 return chatNotes; 110 } 111 } 112 113 /** 114 * Replaces all instances of oldString with newString in string. 115 * 116 * @param string the String to search to perform replacements on 117 * @param oldString the String that should be replaced by newString 118 * @param newString the String that will replace all instances of oldString 119 * @return a String will all instances of oldString replaced by newString 120 */ 121 public static final String replace(String string, String oldString, String newString) { 122 if (string == null) { 123 return null; 124 } 125 // If the newString is null or zero length, just return the string since there's nothing 126 // to replace. 127 if (newString == null) { 128 return string; 129 } 130 int i = 0; 131 // Make sure that oldString appears at least once before doing any processing. 132 if ((i = string.indexOf(oldString, i)) >= 0) { 133 // Use char []'s, as they are more efficient to deal with. 134 char[] string2 = string.toCharArray(); 135 char[] newString2 = newString.toCharArray(); 136 int oLength = oldString.length(); 137 StringBuilder buf = new StringBuilder(string2.length); 138 buf.append(string2, 0, i).append(newString2); 139 i += oLength; 140 int j = i; 141 // Replace all remaining instances of oldString with newString. 142 while ((i = string.indexOf(oldString, i)) > 0) { 143 buf.append(string2, j, i - j).append(newString2); 144 i += oLength; 145 j = i; 146 } 147 buf.append(string2, j, string2.length - j); 148 return buf.toString(); 149 } 150 return string; 151 } 152 } 153 154 155 156