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.packet.IqData;
  21. import org.jivesoftware.smack.packet.XmlEnvironment;
  22. import org.jivesoftware.smack.provider.IqProvider;
  23. import org.jivesoftware.smack.xml.XmlPullParser;
  24. import org.jivesoftware.smack.xml.XmlPullParserException;

  25. /**
  26.  * IQ stanza for retrieving and adding Chat Notes.
  27.  */
  28. public class ChatNotes extends IQ {

  29.     /**
  30.      * Element name of the stanza extension.
  31.      */
  32.     public static final String ELEMENT_NAME = "chat-notes";

  33.     /**
  34.      * Namespace of the stanza extension.
  35.      */
  36.     public static final String NAMESPACE = "http://jivesoftware.com/protocol/workgroup";

  37.     public ChatNotes() {
  38.         super(ELEMENT_NAME, NAMESPACE);
  39.     }

  40.     private String sessionID;
  41.     private String notes;

  42.     public String getSessionID() {
  43.         return sessionID;
  44.     }

  45.     public void setSessionID(String sessionID) {
  46.         this.sessionID = sessionID;
  47.     }

  48.     public String getNotes() {
  49.         return notes;
  50.     }

  51.     public void setNotes(String notes) {
  52.         this.notes = notes;
  53.     }

  54.     @Override
  55.     protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) {
  56.         buf.rightAngleBracket();

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

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

  61.         return buf;
  62.     }

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

  69.         @Override
  70.         public ChatNotes parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException {
  71.             ChatNotes chatNotes = new ChatNotes();

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

  91.             return chatNotes;
  92.         }
  93.     }

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