3 Copyright (C) 2007 Jehan Monnier ()
5 P2pUserProfileAdvertisement.java - .
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.
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.
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.
21 package org.linphone.p2pproxy.core;
24 import java.io.Serializable;
26 import java.net.URISyntaxException;
27 import java.util.Enumeration;
29 import org.apache.log4j.Logger;
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;
48 * derivated from jxta Advertisement tutorial
50 * <?xml version="1.0"?>
51 * <!DOCTYPE jxta:System>
52 * <jxta:System xmlns:jxta="http://jxta.org">
54 * <user-name>endpoint user name</name>
58 public class P2pUserProfileAdvertisement extends ExtendableAdvertisement implements Comparable, Cloneable, Serializable {
62 public static class Instantiator implements AdvertisementFactory.Instantiator {
65 * Returns the identifying type of this Advertisement.
67 * @return String the type of advertisement
69 public String getAdvertisementType() {
70 return P2pUserProfileAdvertisement.getAdvertisementType();
74 * Constructs an instance of <CODE>Advertisement</CODE> matching the
75 * type specified by the <CODE>advertisementType</CODE> parameter.
77 * @return The instance of <CODE>Advertisement</CODE> or null if it
78 * could not be created.
80 public Advertisement newInstance() {
81 return new P2pUserProfileAdvertisement();
85 * Constructs an instance of <CODE>Advertisement</CODE> matching the
86 * type specified by the <CODE>advertisementType</CODE> parameter.
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.
93 public Advertisement newInstance(net.jxta.document.Element root) {
94 return new P2pUserProfileAdvertisement(root);
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);
106 public P2pUserProfileAdvertisement(Element root) {
108 TextElement doc = (TextElement) root;
110 if (!getAdvertisementType().equals(doc.getName())) {
111 throw new IllegalArgumentException("Could not construct : " + getClass().getName() + "from doc containing a " + doc.getName());
116 public P2pUserProfileAdvertisement() {
118 // TODO Auto-generated constructor stub
121 * @see net.jxta.document.ExtendableAdvertisement#getDocument(net.jxta.document.MimeMediaType)
124 public Document getDocument(MimeMediaType asMimeType) {
126 StructuredDocument adv = StructuredDocumentFactory.newStructuredDocument(asMimeType,
127 getAdvertisementType());
128 if (adv instanceof Attributable) {
129 ((Attributable) adv).addAttribute("xmlns:jxta", "http://jxta.org");
132 e = adv.createElement(mIdTag, getID().toString());
134 e = adv.createElement(USER_NAME_TAG, getUserName().trim());
145 public String[] getIndexFields() {
148 public static String getAdvertisementType() {
149 return "jxta:p2p-proxy-user-profile";
152 * @see net.jxta.document.Advertisement#toString()
155 public String toString() {
156 // TODO Auto-generated method stub
157 return super.toString();
159 public int compareTo(Object other) {
160 return getID().toString().compareTo(other.toString());
163 * Intialize a System advertisement from a portion of a structured document.
165 * @param root document root
167 protected void initialize(Element root) {
168 if (!TextElement.class.isInstance(root)) {
169 throw new IllegalArgumentException(getClass().getName() +
170 " only supports TextElement");
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 " +
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());
187 * Process an individual element from the document.
189 * @param elem the element to be processed.
190 * @return true if the element was recognized, otherwise false.
192 protected boolean handleElement(TextElement elem) {
193 if (elem.getName().equals(mIdTag)) {
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());
201 catch (ClassCastException badID) {
202 throw new IllegalArgumentException("Id is not a known id type: " +
203 elem.getTextValue());
207 if (elem.getName().equals(USER_NAME_TAG)) {
208 setUserName(elem.getTextValue());
213 public void setID(ID id) {
217 public String getBaseAdvType() {
218 // TODO Auto-generated method stub
222 * @return Returns the mName.
224 public String getUserName() {
228 * @param name The mName to set.
230 public void setUserName(String name) {
234 * @see java.lang.Object#equals(java.lang.Object)
237 public boolean equals(Object obj) {
242 if (obj instanceof P2pUserProfileAdvertisement) {
243 P2pUserProfileAdvertisement adv = (P2pUserProfileAdvertisement) obj;
244 return getID().equals(adv.getID());