ChatNotes.java

  1. /**
  2.  *
  3.  * Copyright 2003-2007 Jive Software.
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.jivesoftware.smackx.workgroup.ext.notes;

  18. import java.io.IOException;

  19. import org.jivesoftware.smack.packet.IQ;
  20. import org.jivesoftware.smack.provider.IQProvider;
  21. import org.xmlpull.v1.XmlPullParser;
  22. import org.xmlpull.v1.XmlPullParserException;

  23. /**
  24.  * IQ packet for retrieving and adding Chat Notes.
  25.  */
  26. public class ChatNotes extends IQ {

  27.     /**
  28.      * Element name of the packet extension.
  29.      */
  30.     public static final String ELEMENT_NAME = "chat-notes";

  31.     /**
  32.      * Namespace of the packet extension.
  33.      */
  34.     public static final String NAMESPACE = "http://jivesoftware.com/protocol/workgroup";

  35.     public ChatNotes() {
  36.         super(ELEMENT_NAME, NAMESPACE);
  37.     }

  38.     private String sessionID;
  39.     private String notes;

  40.     public String getSessionID() {
  41.         return sessionID;
  42.     }

  43.     public void setSessionID(String sessionID) {
  44.         this.sessionID = sessionID;
  45.     }

  46.     public String getNotes() {
  47.         return notes;
  48.     }

  49.     public void setNotes(String notes) {
  50.         this.notes = notes;
  51.     }

  52.     @Override
  53.     protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) {
  54.         buf.rightAngleBracket();

  55.         buf.append("<sessionID>").append(getSessionID()).append("</sessionID>");

  56.         if (getNotes() != null) {
  57.             buf.element("notes", getNotes());
  58.         }

  59.         return buf;
  60.     }

  61.     /**
  62.      * An IQProvider for ChatNotes packets.
  63.      *
  64.      * @author Derek DeMoro
  65.      */
  66.     public static class Provider extends IQProvider<ChatNotes> {

  67.         @Override
  68.         public ChatNotes parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException {
  69.             ChatNotes chatNotes = new ChatNotes();

  70.             boolean done = false;
  71.             while (!done) {
  72.                 int eventType = parser.next();
  73.                 if (eventType == XmlPullParser.START_TAG) {
  74.                     if (parser.getName().equals("sessionID")) {
  75.                         chatNotes.setSessionID(parser.nextText());
  76.                     }
  77.                     else if (parser.getName().equals("text")) {
  78.                         String note = parser.nextText();
  79.                         note = note.replaceAll("\\\\n", "\n");
  80.                         chatNotes.setNotes(note);
  81.                     }
  82.                 }
  83.                 else if (eventType == XmlPullParser.END_TAG) {
  84.                     if (parser.getName().equals(ELEMENT_NAME)) {
  85.                         done = true;
  86.                     }
  87.                 }
  88.             }

  89.             return chatNotes;
  90.         }
  91.     }

  92.     /**
  93.      * Replaces all instances of oldString with newString in string.
  94.      *
  95.      * @param string    the String to search to perform replacements on
  96.      * @param oldString the String that should be replaced by newString
  97.      * @param newString the String that will replace all instances of oldString
  98.      * @return a String will all instances of oldString replaced by newString
  99.      */
  100.     public static final String replace(String string, String oldString, String newString) {
  101.         if (string == null) {
  102.             return null;
  103.         }
  104.         // If the newString is null or zero length, just return the string since there's nothing
  105.         // to replace.
  106.         if (newString == null) {
  107.             return string;
  108.         }
  109.         int i = 0;
  110.         // Make sure that oldString appears at least once before doing any processing.
  111.         if ((i = string.indexOf(oldString, i)) >= 0) {
  112.             // Use char []'s, as they are more efficient to deal with.
  113.             char[] string2 = string.toCharArray();
  114.             char[] newString2 = newString.toCharArray();
  115.             int oLength = oldString.length();
  116.             StringBuilder buf = new StringBuilder(string2.length);
  117.             buf.append(string2, 0, i).append(newString2);
  118.             i += oLength;
  119.             int j = i;
  120.             // Replace all remaining instances of oldString with newString.
  121.             while ((i = string.indexOf(oldString, i)) > 0) {
  122.                 buf.append(string2, j, i - j).append(newString2);
  123.                 i += oLength;
  124.                 j = i;
  125.             }
  126.             buf.append(string2, j, string2.length - j);
  127.             return buf.toString();
  128.         }
  129.         return string;
  130.     }
  131. }