]> sjero.net Git - linphone/blob - p2pproxy/src/org/linphone/p2pproxy/core/P2pUserProfileAdvertisement.java
remove mediastreamer2 and add it as a submodule instead.
[linphone] / p2pproxy / src / org / linphone / p2pproxy / core / P2pUserProfileAdvertisement.java
1 /*
2 p2pproxy
3 Copyright (C) 2007  Jehan Monnier ()
4
5 P2pUserProfileAdvertisement.java - .
6
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; either version 2
10 of the License, or (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20 */
21 package org.linphone.p2pproxy.core;
22
23
24 import java.io.Serializable;
25 import java.net.URI;
26 import java.net.URISyntaxException;
27 import java.util.Enumeration;
28
29 import org.apache.log4j.Logger;
30
31
32
33
34 import net.jxta.document.Advertisement;
35 import net.jxta.document.AdvertisementFactory;
36 import net.jxta.document.Attributable;
37 import net.jxta.document.Document;
38 import net.jxta.document.Element;
39 import net.jxta.document.ExtendableAdvertisement;
40 import net.jxta.document.MimeMediaType;
41 import net.jxta.document.StructuredDocument;
42 import net.jxta.document.StructuredDocumentFactory;
43 import net.jxta.document.TextElement;
44 import net.jxta.id.ID;
45 import net.jxta.id.IDFactory;
46
47 /**
48  * derivated from jxta Advertisement tutorial
49  * <pre>
50  * &lt;?xml version="1.0"?>
51  * &lt;!DOCTYPE jxta:System>
52  * &lt;jxta:System xmlns:jxta="http://jxta.org">
53  *   &lt;id>id&lt;/id>
54  *   &lt;user-name>endpoint user name&lt;/name>
55  * &lt;/jxta:System>
56  * </pre>
57  */
58 public class P2pUserProfileAdvertisement extends ExtendableAdvertisement implements Comparable, Cloneable, Serializable {
59    /**
60     * Instantiator
61     */
62    public static class Instantiator implements AdvertisementFactory.Instantiator {
63
64       /**
65        * Returns the identifying type of this Advertisement.
66        *
67        * @return String the type of advertisement
68        */
69       public String getAdvertisementType() {
70          return P2pUserProfileAdvertisement.getAdvertisementType();
71       }
72
73       /**
74        * Constructs an instance of <CODE>Advertisement</CODE> matching the
75        * type specified by the <CODE>advertisementType</CODE> parameter.
76        *
77        * @return The instance of <CODE>Advertisement</CODE> or null if it
78        *         could not be created.
79        */
80       public Advertisement newInstance() {
81          return new P2pUserProfileAdvertisement();
82       }
83
84       /**
85        * Constructs an instance of <CODE>Advertisement</CODE> matching the
86        * type specified by the <CODE>advertisementType</CODE> parameter.
87        *
88        * @param root Specifies a portion of a StructuredDocument which will
89        *             be converted into an Advertisement.
90        * @return The instance of <CODE>Advertisement</CODE> or null if it
91        *         could not be created.
92        */
93       public Advertisement newInstance(net.jxta.document.Element root) {
94          return new P2pUserProfileAdvertisement(root);
95       }
96    }
97    private ID mId ;;
98    private String mName;
99    public final static String USER_NAME_TAG = "user-name";
100    private final static String mIdTag = "ID";
101    private final static String[] mIndexs = {USER_NAME_TAG};
102    private final static Logger mLog = Logger.getLogger(P2pUserProfileAdvertisement.class);
103    /**
104     * 
105     */
106    public P2pUserProfileAdvertisement(Element root) {
107
108       TextElement doc = (TextElement) root;
109
110       if (!getAdvertisementType().equals(doc.getName())) {
111          throw new IllegalArgumentException("Could not construct : " + getClass().getName() + "from doc containing a " + doc.getName());
112       }
113       initialize(doc);
114
115    }
116    public P2pUserProfileAdvertisement() {
117
118       // TODO Auto-generated constructor stub
119    }
120    /* (non-Javadoc)
121     * @see net.jxta.document.ExtendableAdvertisement#getDocument(net.jxta.document.MimeMediaType)
122     */
123    @Override
124    public Document getDocument(MimeMediaType asMimeType) {
125
126       StructuredDocument adv = StructuredDocumentFactory.newStructuredDocument(asMimeType,
127             getAdvertisementType());
128       if (adv instanceof Attributable) {
129          ((Attributable) adv).addAttribute("xmlns:jxta", "http://jxta.org");
130       }
131       Element e;
132       e = adv.createElement(mIdTag, getID().toString());
133       adv.appendChild(e);
134       e = adv.createElement(USER_NAME_TAG, getUserName().trim());
135       adv.appendChild(e);
136       return adv;
137    }
138
139    @Override
140    public ID getID() {
141       return mId;
142    }
143
144    @Override
145    public String[] getIndexFields() {
146       return mIndexs;
147    }
148    public static String getAdvertisementType() {
149       return "jxta:p2p-proxy-user-profile";
150    }
151    /* (non-Javadoc)
152     * @see net.jxta.document.Advertisement#toString()
153     */
154    @Override
155    public String toString() {
156       // TODO Auto-generated method stub
157       return super.toString();
158    }
159    public int compareTo(Object other) {
160       return getID().toString().compareTo(other.toString());
161    }
162    /**
163     * Intialize a System advertisement from a portion of a structured document.
164     *
165     * @param root document root
166     */
167    protected void initialize(Element root) {
168       if (!TextElement.class.isInstance(root)) {
169          throw new IllegalArgumentException(getClass().getName() +
170                " only supports TextElement");
171       }
172       TextElement doc = (TextElement) root;
173       if (!doc.getName().equals(getAdvertisementType())) {
174          throw new IllegalArgumentException("Could not construct : "
175                + getClass().getName() + "from doc containing a " +
176                doc.getName());
177       }
178       Enumeration elements = doc.getChildren();
179       while (elements.hasMoreElements()) {
180          TextElement elem = (TextElement) elements.nextElement();
181          if (!handleElement(elem)) {
182             mLog.warn("Unhandleded element \'" + elem.getName() + "\' in " +  doc.getName());
183          }
184       }
185    }
186    /**
187     * Process an individual element from the document.
188     *
189     * @param elem the element to be processed.
190     * @return true if the element was recognized, otherwise false.
191     */
192    protected boolean handleElement(TextElement elem) {
193       if (elem.getName().equals(mIdTag)) {
194          try {
195             URI id = new URI(elem.getTextValue());
196             setID(IDFactory.fromURI(id));
197          } catch (URISyntaxException badID) {
198             throw new IllegalArgumentException("unknown ID format in advertisement: " +
199                   elem.getTextValue());
200          }
201          catch (ClassCastException badID) {
202             throw new IllegalArgumentException("Id is not a known id type: " +
203                   elem.getTextValue());
204          }
205          return true;
206       }
207       if (elem.getName().equals(USER_NAME_TAG)) {
208          setUserName(elem.getTextValue());
209          return true;
210       }
211       return false;
212    }
213    public void setID(ID id) {
214       mId = id;
215    }
216    @Override
217    public String getBaseAdvType() {
218       // TODO Auto-generated method stub
219       return null;
220    }
221    /**
222     * @return Returns the mName.
223     */
224    public String getUserName() {
225       return mName;
226    }
227    /**
228     * @param name The mName to set.
229     */
230    public void setUserName(String name) {
231       mName = name;
232    }
233    /* (non-Javadoc)
234     * @see java.lang.Object#equals(java.lang.Object)
235     */
236    @Override
237    public boolean equals(Object obj) {
238
239       if (this == obj) {
240           return true;
241       }
242       if (obj instanceof P2pUserProfileAdvertisement) {
243          P2pUserProfileAdvertisement adv = (P2pUserProfileAdvertisement) obj;
244           return getID().equals(adv.getID());
245       }
246
247       return false;
248   
249    }
250
251 }