Source code: com/jabberwookie/ns/jabber/Chunk.java
1 /*
2 * Chunk.java
3 *
4 * Created on April 21, 2003, 6:20 PM
5 * Copyright (c) 2003, Sean M. Meiners, sean@jabberwookie.com
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * * Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright notice,
14 * this list of conditions and the following disclaimer in the documentation
15 * and/or other materials provided with the distribution.
16 * * Neither the name of JabberWookie nor the names of its contributors may be used
17 * to endorse or promote products derived from this software without specific
18 * prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
24 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
27 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 package com.jabberwookie.ns.jabber;
33
34 import com.jabberwookie.ns.jabber.Const;
35
36 import com.ssttr.xml.XMLElement;
37
38 /**
39 * This is the most basic element of a Jabber stream. It represents
40 * the elements that are common to IQ, Presence, and Message blocks.
41 * @author smeiners
42 */
43 public class Chunk
44 extends XMLElement
45 implements Const
46 {
47 private static int id = 0;
48
49 private int typeInt = -1;
50
51 public static class Type
52 {
53 public static final int MESSAGE = 1;
54 public static final int PRESENCE = 2;
55 public static final int IQ = 3;
56 }
57
58 public static final String typeIntToString(int type)
59 {
60 switch (type)
61 {
62 case 1: return Const.MESSAGE;
63 case 2: return Const.PRESENCE;
64 case 3: return Const.IQ;
65 default: return "invalid";
66 }
67 }
68
69 /** Creates a new instance of Chunk */
70 public Chunk(String tag)
71 {
72 super(tag);
73 setId(++id);
74 }
75
76 public Chunk(int chunkType)
77 {
78 super( typeIntToString(chunkType) );
79 typeInt = chunkType;
80 setId(++id);
81 }
82
83 public Chunk (int chunkType, String to)
84 {
85 this(chunkType);
86 setTo(to);
87 setId(++id);
88 }
89
90 public Chunk (int chunkType, String to, String type)
91 {
92 this(chunkType,to);
93 setType(type);
94 setId(++id);
95 }
96
97 public int getTypeInt()
98 { return typeInt; }
99
100 public void setType(String type)
101 {
102 setAttribute(TYPE,type);
103 }
104
105 public String getType()
106 {
107 return getAttribute(TYPE);
108 }
109
110 public void setId(String id)
111 {
112 if( id != null )
113 setAttribute(ID,id);
114 }
115
116 public void setId(int id)
117 {
118 setAttribute(ID,Integer.toHexString(id));
119 }
120
121 public String getId()
122 {
123 return getAttribute(ID);
124 }
125
126 public void setTo(String jid)
127 {
128 setAttribute(TO, jid);
129 }
130
131 public String getTo()
132 {
133 return getAttribute(TO);
134 }
135
136 public void setFrom(String jid)
137 {
138 setAttribute(FROM, jid);
139 }
140
141 public String getFrom()
142 {
143 return getAttribute(FROM);
144 }
145
146 public Error getError()
147 {
148 XMLElement error = getChild(Const.ERROR);
149 if( error != null )
150 return new Error(error);
151
152 return null;
153 }
154 }