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