📄 advertisementtutorial_java.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0135)http://platform.jxta.org/source/browse/*checkout*/platform/binding/java/tutorials/src/tutorial/advertisement/AdvertisementTutorial.java -->
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<META content="MSHTML 6.00.2900.3059" name=GENERATOR></HEAD>
<BODY><PRE>/*
* Copyright (c) 2006 Sun Microsystems, Inc. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Sun Microsystems, Inc. for Project JXTA."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact Project JXTA at http://www.jxta.org.
*
* 5. Products derived from this software may not be called "JXTA",
* nor may "JXTA" appear in their name, without prior written
* permission of Sun.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL SUN MICROSYSTEMS OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of Project JXTA. For more
* information on Project JXTA, please see
* <http://www.jxta.org/>.
*
* This license is based on the BSD license adopted by the Apache Foundation.
*
* $Id: AdvertisementTutorial.java,v 1.1 2007/03/21 21:58:23 hamada Exp $
*/
package tutorial.advertisement;
import net.jxta.document.*;
import net.jxta.id.ID;
import net.jxta.id.IDFactory;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.net.InetAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.util.logging.Logger;
/**
* Simple Advertisement Tutorial creates a advertisment describing a system
* <p/>
* <pre>
* &lt;?xml version="1.0"?>
* &lt;!DOCTYPE jxta:System>
* &lt;jxta:System xmlns:jxta="http://jxta.org">
* &lt;id>id&lt;/id>
* &lt;name>Device Name&lt;/name>
* &lt;ip>ip address&lt;/ip>
* &lt;hwarch>x86&lt;/hwarch>
* &lt;hwvendor>Sun MicroSystems&lt;/hwvendor>
* &lt;OSName>&lt;/OSName>
* &lt;OSVer>&lt;/OSVer>
* &lt;osarch>&lt;/osarch>
* &lt;sw>&lt;/sw>
* &lt;/jxta:System>
* </pre>
*/
public class AdvertisementTutorial extends Advertisement
implements Comparable, Cloneable, Serializable {
private String hwarch;
private String hwvendor;
private ID id = ID.nullID;
private String ip;
private String name;
private String osname;
private String osversion;
private String osarch;
private String inventory;
private final static Logger LOG =
Logger.getLogger(AdvertisementTutorial.class.getName());
private final static String OSNameTag = "OSName";
private final static String OSVersionTag = "OSVer";
private final static String OSarchTag = "osarch";
private final static String hwarchTag = "hwarch";
private final static String hwvendorTag = "hwvendor";
private final static String idTag = "ID";
private final static String ipTag = "ip";
private final static String nameTag = "name";
private final static String swTag = "sw";
/**
* Indexable fields. Advertisements must define the indexables, in order
* to properly index and retrieve these advertisements locally and on the
* network
*/
private final static String[] fields = {idTag, nameTag, hwarchTag};
/**
* Default Constructor
*/
public AdvertisementTutorial() {
}
/**
* Construct from a StructuredDocument
*
* @param root Root element
*/
public AdvertisementTutorial(Element root) {
TextElement doc = (TextElement) root;
if (!getAdvertisementType().equals(doc.getName())) {
throw new IllegalArgumentException("Could not construct : " +
getClass().getName() + "from doc containing a " + doc.getName());
}
initialize(doc);
}
/**
* Construct a doc from InputStream
*
* @param stream the underlying input stream.
* @throws IOException if an I/O error occurs.
*/
public AdvertisementTutorial(InputStream stream) throws IOException {
StructuredTextDocument doc = (StructuredTextDocument)
StructuredDocumentFactory
.newStructuredDocument(MimeMediaType.XMLUTF8, stream);
initialize(doc);
}
/**
* Sets the hWArch attribute of the AdvertisementTutorial object
*
* @param hwarch The new hWArch value
*/
public void setHWArch(String hwarch) {
this.hwarch = hwarch;
}
/**
* Sets the OSArch attribute of the AdvertisementTutorial object
*
* @param osarch The new hWArch value
*/
public void setOSArch(String osarch) {
this.osarch = osarch;
}
/**
* Sets the hWVendor attribute of the AdvertisementTutorial object
*
* @param hwvendor The new hWVendor value
*/
public void setHWVendor(String hwvendor) {
this.hwvendor = hwvendor;
}
/**
* sets the unique id
*
* @param id The id
*/
public void setID(ID id) {
this.id = (id == null ? null : id);
}
/**
* Sets the iP attribute of the AdvertisementTutorial object
*
* @param ip The new iP value
*/
public void setIP(String ip) {
this.ip = ip;
}
/**
* Sets the name attribute of the AdvertisementTutorial object
*
* @param name The new name value
*/
public void setName(String name) {
this.name = name;
}
/**
* Sets the oSName attribute of the AdvertisementTutorial object
*
* @param osname The new oSName value
*/
public void setOSName(String osname) {
this.osname = osname;
}
/**
* Sets the oSVersion attribute of the AdvertisementTutorial object
*
* @param osversion The new oSVersion value
*/
public void setOSVersion(String osversion) {
this.osversion = osversion;
}
/**
* Sets the SWInventory attribute of the AdvertisementTutorial object
*
* @param inventory the software inventory of the system
*/
public void setSWInventory(String inventory) {
this.inventory = inventory;
}
/**
* {@inheritDoc}
*
* @param asMimeType Document encoding
* @return The document value
*/
@Override
public Document getDocument(MimeMediaType asMimeType) {
StructuredDocument adv = StructuredDocumentFactory.newStructuredDocument(asMimeType,
getAdvertisementType());
if (adv instanceof Attributable) {
((Attributable) adv).addAttribute("xmlns:jxta", "http://jxta.org");
}
Element e;
e = adv.createElement(idTag, getID().toString());
adv.appendChild(e);
e = adv.createElement(nameTag, getName().trim());
adv.appendChild(e);
e = adv.createElement(OSNameTag, getOSName().trim());
adv.appendChild(e);
e = adv.createElement(OSVersionTag, getOSVersion().trim());
adv.appendChild(e);
e = adv.createElement(OSarchTag, getOSArch().trim());
adv.appendChild(e);
e = adv.createElement(ipTag, getIP().trim());
adv.appendChild(e);
e = adv.createElement(hwarchTag, getHWArch().trim());
adv.appendChild(e);
e = adv.createElement(hwvendorTag, getHWVendor().trim());
adv.appendChild(e);
e = adv.createElement(swTag, getSWInventory().trim());
adv.appendChild(e);
return adv;
}
/**
* Gets the hWArch attribute of the AdvertisementTutorial object
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -