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.iqversion.packet;
019
020
021import org.jivesoftware.smack.packet.IQ;
022import org.jivesoftware.smack.util.StringUtils;
023
024/**
025 * A Version IQ packet, which is used by XMPP clients to discover version information
026 * about the software running at another entity's JID.<p>
027 *
028 * An example to discover the version of the server:
029 * <pre>
030 * // Request the version from the server.
031 * Version versionRequest = new Version();
032 * timeRequest.setType(IQ.Type.GET);
033 * timeRequest.setTo("example.com");
034 *
035 * // Create a packet collector to listen for a response.
036 * PacketCollector collector = con.createPacketCollector(
037 *                new PacketIDFilter(versionRequest.getPacketID()));
038 *
039 * con.sendPacket(versionRequest);
040 *
041 * // Wait up to 5 seconds for a result.
042 * IQ result = (IQ)collector.nextResult(5000);
043 * if (result != null && result.getType() == IQ.Type.RESULT) {
044 *     Version versionResult = (Version)result;
045 *     // Do something with result...
046 * }</pre><p>
047 *
048 * @author Gaston Dombiak
049 */
050public class Version extends IQ {
051    public static final String NAMESPACE = "jabber:iq:version";
052
053    private String name;
054    private String version;
055    private String os;
056
057    /**
058     * Creates a new Version object with given details.
059     *
060     * @param name The natural-language name of the software. This element is REQUIRED.
061     * @param version The specific version of the software. This element is REQUIRED.
062     * @param os The operating system of the queried entity. This element is OPTIONAL.
063     */
064    public Version(String name, String version, String os) {
065        this.setType(IQ.Type.RESULT);
066        this.name = name;
067        this.version = version;
068        this.os = os;
069    }
070
071    public Version(Version original) {
072        this(original.name, original.version, original.os);
073    }
074
075    /**
076     * Returns the natural-language name of the software. This property will always be
077     * present in a result.
078     *
079     * @return the natural-language name of the software.
080     */
081    public String getName() {
082        return name;
083    }
084
085    /**
086     * Sets the natural-language name of the software. This message should only be
087     * invoked when parsing the XML and setting the property to a Version instance.
088     *
089     * @param name the natural-language name of the software.
090     */
091    public void setName(String name) {
092        this.name = name;
093    }
094
095    /**
096     * Returns the specific version of the software. This property will always be
097     * present in a result.
098     *
099     * @return the specific version of the software.
100     */
101    public String getVersion() {
102        return version;
103    }
104
105    /**
106     * Sets the specific version of the software. This message should only be
107     * invoked when parsing the XML and setting the property to a Version instance.
108     *
109     * @param version the specific version of the software.
110     */
111    public void setVersion(String version) {
112        this.version = version;
113    }
114
115    /**
116     * Returns the operating system of the queried entity. This property will always be
117     * present in a result.
118     *
119     * @return the operating system of the queried entity.
120     */
121    public String getOs() {
122        return os;
123    }
124
125    /**
126     * Sets the operating system of the queried entity. This message should only be
127     * invoked when parsing the XML and setting the property to a Version instance.
128     *
129     * @param os operating system of the queried entity.
130     */
131    public void setOs(String os) {
132        this.os = os;
133    }
134
135    public String getChildElementXML() {
136        StringBuilder buf = new StringBuilder();
137        buf.append("<query xmlns=\"");
138        buf.append(Version.NAMESPACE);
139        buf.append("\">");
140        if (name != null) {
141            buf.append("<name>").append(StringUtils.escapeForXML(name)).append("</name>");
142        }
143        if (version != null) {
144            buf.append("<version>").append(StringUtils.escapeForXML(version)).append("</version>");
145        }
146        if (os != null) {
147            buf.append("<os>").append(StringUtils.escapeForXML(os)).append("</os>");
148        }
149        buf.append("</query>");
150        return buf.toString();
151    }
152}