📄 contentadvertisementimpl.java
字号:
* @param is the InputStream containing the advertisement
* @exception IllegalArgumentException if the advertisement was invalid
* @exception IOException if there was an I/O error
*/
public ContentAdvertisementImpl(InputStream is) throws IOException {
registerAdvertisement();
StructuredTextDocument doc = (StructuredTextDocument)
StructuredDocumentFactory.newStructuredDocument(
new MimeMediaType("text", "xml"), is);
init(doc);
}
/**
* Creates a new ContentAdvertisement from the specified TextElement.
* @exception IllegalArgumentException if the TextElement was missing data
* or contained invalid data
* @exception NullPointerException if te is null
* @see ContentMetadataFactory
*/
public ContentAdvertisementImpl(TextElement te) {
registerAdvertisement();
init(te);
}
// reads advertisement from specified TextElement
private void init(TextElement root) {
Vector metadataElems = new Vector();
if (!root.getName().equals(getAdvertisementType())) {
throw new IllegalArgumentException(
"Not a ContentAdvertisement element: " + root.getName());
}
Enumeration e = root.getChildren();
while (e.hasMoreElements()) {
TextElement te = (TextElement)e.nextElement();
String s = te.getName();
String t = te.getTextValue();
if ((t != null)||(te.getChildren() !=null)) {
if (s.equals(EN_NAME)) {
name = t;
} else
if (s.equals(EN_CID)) {
try {
cid = new ContentIdImpl(t);
} catch (IllegalArgumentException ex) {
throw new IllegalArgumentException(
"Invalid content id: " + t);
}
} else
if (s.equals(EN_LENGTH)) {
try {
length = Long.parseLong(t);
} catch (NumberFormatException ex) {
throw new IllegalArgumentException(
"Invalid content length: " + t);
}
} else
if (s.equals(EN_TYPE)) {
type = t;
} else
if (s.equals(EN_DESCRIPTION)) {
description = t;
} else
if (s.equals(EN_METADATA)) {
metadataElems.addElement(ContentMetadataFactory
.newInstance(te));
} else
if (s.equals(EN_ADDRESS)) {
address = t;
}
} else
if (s.equals(EN_METADATA)) {
//a metadata element may have a null value if all of its
//information is stored in sub-elements
metadataElems.addElement(ContentMetadataFactory
.newInstance(te));
}
}
if (name == null) {
throw new IllegalArgumentException(
"Missing required 'name' element");
}
if (cid == null) {
throw new IllegalArgumentException(
"Missing required 'cid' element");
}
if(metadataElems.size() != 0) {
metadata = new ContentMetadata[metadataElems.size()];
metadataElems.toArray(metadata);
}
}
/**
* Returns the advertisement type.
*/
public static String getAdvertisementType() {
return "jxta:ContentAdvertisement";
}
/**
* Returns the name of the content.
*/
public String getName() {
return name;
}
/**
* Returns the content id.
*/
public ContentId getContentId() {
return cid;
}
/**
* Returns the length of the content, or -1 if unknown
*/
public long getLength() {
return length;
}
/**
* Returns the mime type of the content, or null if unknown
*/
public String getType() {
return type;
}
/**
* Returns the description of the content or null if none.
*/
public String getDescription() {
return description;
}
/**
* Returns the metadata describing this content, or null if not specified.
*/
public ContentMetadata[] getMetadata() {
return metadata;
}
/**
* Returns the peer id or null if none.
*/
public String getAddress() {
return address;
}
/**
* Sets the address of the peer that holds the content.
*/
public void setAddress(String address) {
this.address = address;
}
/**
* Returns the document representation of this advertisement.
*/
public Document getDocument(MimeMediaType mtype) {
StructuredDocument doc = (StructuredDocument)
StructuredDocumentFactory.newStructuredDocument(
mtype, getAdvertisementType());
appendElements(doc, doc);
return doc;
}
/**
* Appends this content advertisement to the specified structured
* document element.
*/
public void appendDocument(StructuredDocument doc, Element el) {
Element e = doc.createElement(getAdvertisementType());
el.appendChild(e);
appendElements(doc, e);
}
// append elements to specified document element
private void appendElements(StructuredDocument doc, Element e) {
e.appendChild(doc.createElement(EN_NAME, name));
e.appendChild(doc.createElement(EN_CID, cid.toString()));
if (length != -1) {
e.appendChild(doc.createElement(EN_LENGTH, Long.toString(length)));
}
if (type != null) {
e.appendChild(doc.createElement(EN_TYPE, type));
}
if (description != null) {
e.appendChild(doc.createElement(EN_DESCRIPTION, description));
}
if (metadata != null) {
for(int a = 0; a < metadata.length; a++) {
//insert metadata element into doc
metadata[a].appendDocument(doc, doc);
}
}
if (address != null) {
e.appendChild(doc.createElement(EN_ADDRESS, address));
}
}
public boolean equals(Object adv) {
if (LOG.isEnabledFor(Level.DEBUG))
LOG.debug("ContentAdvertismentImpl.equals("+adv.toString()+") "+toString());
if (adv instanceof ContentAdvertisementImpl) {
return toString().equals(((ContentAdvertisementImpl)adv).toString());
}
return false;
}
/**
* Returns a string representation of this advertisement.
*/
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("{name=");
sb.append(name);
sb.append(";cid=");
sb.append(cid.toString());
if (length != -1) {
sb.append(";length=");
sb.append(length);
}
if (type != null) {
sb.append(";type=");
sb.append(type);
}
if (description != null) {
sb.append(";description=");
sb.append(description);
}
if (metadata != null) {
sb.append(";metadata=(");
for(int a = 0; a < metadata.length; a++)
sb.append(metadata[a] + "|");
sb.setCharAt(sb.length() - 1, ')');
}
if (address != null) {
sb.append(";address=");
sb.append(address);
}
sb.append('}');
return sb.toString();
}
public static void registerAdvertisement () {
if (!isRegistered) {
// make sure that the AdvertisementFactory knows about ContentAdvertisement.
AdvertisementFactory.Instantiator instantiator = new Instantiator();
AdvertisementFactory.registerAdvertisementInstance (getAdvertisementType(),
instantiator);
isRegistered = true;
}
}
public String [] getIndexFields() {
return fields;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -