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 org.jivesoftware.smack.packet.IQ; 021import org.jivesoftware.smack.provider.IQProvider; 022import org.jivesoftware.smack.util.XmlStringBuilder; 023import org.xmlpull.v1.XmlPullParser; 024 025/** 026 * IQ packet for retrieving and adding Chat Notes. 027 */ 028public class ChatNotes extends IQ { 029 030 /** 031 * Element name of the packet extension. 032 */ 033 public static final String ELEMENT_NAME = "chat-notes"; 034 035 /** 036 * Namespace of the packet extension. 037 */ 038 public static final String NAMESPACE = "http://jivesoftware.com/protocol/workgroup"; 039 040 041 private String sessionID; 042 private String notes; 043 044 public String getSessionID() { 045 return sessionID; 046 } 047 048 public void setSessionID(String sessionID) { 049 this.sessionID = sessionID; 050 } 051 052 public String getNotes() { 053 return notes; 054 } 055 056 public void setNotes(String notes) { 057 this.notes = notes; 058 } 059 060 public String getChildElementXML() { 061 XmlStringBuilder buf = new XmlStringBuilder(); 062 063 buf.append("<").append(ELEMENT_NAME).append(" xmlns=\"").append(NAMESPACE).append("\">"); 064 buf.append("<sessionID>").append(getSessionID()).append("</sessionID>"); 065 066 if (getNotes() != null) { 067 buf.element("notes", getNotes()); 068 } 069 buf.append("</").append(ELEMENT_NAME).append("> "); 070 071 return buf.toString(); 072 } 073 074 /** 075 * An IQProvider for ChatNotes packets. 076 * 077 * @author Derek DeMoro 078 */ 079 public static class Provider implements IQProvider { 080 081 public Provider() { 082 super(); 083 } 084 085 public IQ parseIQ(XmlPullParser parser) throws Exception { 086 ChatNotes chatNotes = new ChatNotes(); 087 088 boolean done = false; 089 while (!done) { 090 int eventType = parser.next(); 091 if (eventType == XmlPullParser.START_TAG) { 092 if (parser.getName().equals("sessionID")) { 093 chatNotes.setSessionID(parser.nextText()); 094 } 095 else if (parser.getName().equals("text")) { 096 String note = parser.nextText(); 097 note = note.replaceAll("\\\\n", "\n"); 098 chatNotes.setNotes(note); 099 } 100 } 101 else if (eventType == XmlPullParser.END_TAG) { 102 if (parser.getName().equals(ELEMENT_NAME)) { 103 done = true; 104 } 105 } 106 } 107 108 return chatNotes; 109 } 110 } 111 112 /** 113 * Replaces all instances of oldString with newString in string. 114 * 115 * @param string the String to search to perform replacements on 116 * @param oldString the String that should be replaced by newString 117 * @param newString the String that will replace all instances of oldString 118 * @return a String will all instances of oldString replaced by newString 119 */ 120 public static final String replace(String string, String oldString, String newString) { 121 if (string == null) { 122 return null; 123 } 124 // If the newString is null or zero length, just return the string since there's nothing 125 // to replace. 126 if (newString == null) { 127 return string; 128 } 129 int i = 0; 130 // Make sure that oldString appears at least once before doing any processing. 131 if ((i = string.indexOf(oldString, i)) >= 0) { 132 // Use char []'s, as they are more efficient to deal with. 133 char[] string2 = string.toCharArray(); 134 char[] newString2 = newString.toCharArray(); 135 int oLength = oldString.length(); 136 StringBuilder buf = new StringBuilder(string2.length); 137 buf.append(string2, 0, i).append(newString2); 138 i += oLength; 139 int j = i; 140 // Replace all remaining instances of oldString with newString. 141 while ((i = string.indexOf(oldString, i)) > 0) { 142 buf.append(string2, j, i - j).append(newString2); 143 i += oLength; 144 j = i; 145 } 146 buf.append(string2, j, string2.length - j); 147 return buf.toString(); 148 } 149 return string; 150 } 151} 152 153 154