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