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;
024import org.xmlpull.v1.XmlPullParser;
025import org.xmlpull.v1.XmlPullParserException;
026
027/**
028 * IQ stanza(/packet) for retrieving and adding Chat Notes.
029 */
030public class ChatNotes extends IQ {
031
032    /**
033     * Element name of the stanza(/packet) extension.
034     */
035    public static final String ELEMENT_NAME = "chat-notes";
036
037    /**
038     * Namespace of the stanza(/packet) extension.
039     */
040    public static final String NAMESPACE = "http://jivesoftware.com/protocol/workgroup";
041
042    public ChatNotes() {
043        super(ELEMENT_NAME, NAMESPACE);
044    }
045
046    private String sessionID;
047    private String notes;
048
049    public String getSessionID() {
050        return sessionID;
051    }
052
053    public void setSessionID(String sessionID) {
054        this.sessionID = sessionID;
055    }
056
057    public String getNotes() {
058        return notes;
059    }
060
061    public void setNotes(String notes) {
062        this.notes = notes;
063    }
064
065    @Override
066    protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) {
067        buf.rightAngleBracket();
068
069        buf.append("<sessionID>").append(getSessionID()).append("</sessionID>");
070
071        if (getNotes() != null) {
072            buf.element("notes", getNotes());
073        }
074
075        return buf;
076    }
077
078    /**
079     * An IQProvider for ChatNotes packets.
080     *
081     * @author Derek DeMoro
082     */
083    public static class Provider extends IQProvider<ChatNotes> {
084
085        @Override
086        public ChatNotes parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException {
087            ChatNotes chatNotes = new ChatNotes();
088
089            boolean done = false;
090            while (!done) {
091                int eventType = parser.next();
092                if (eventType == XmlPullParser.START_TAG) {
093                    if (parser.getName().equals("sessionID")) {
094                        chatNotes.setSessionID(parser.nextText());
095                    }
096                    else if (parser.getName().equals("text")) {
097                        String note = parser.nextText();
098                        note = note.replaceAll("\\\\n", "\n");
099                        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