示例#1
0
  /** {@inheritDoc} */
  @Override
  public Document getDocument(MimeMediaType asMimeType) {
    StructuredDocument adv =
        StructuredDocumentFactory.newStructuredDocument(asMimeType, getAdvertisementType());

    if (adv instanceof XMLDocument) {
      XMLDocument xmlDoc = (XMLDocument) adv;

      xmlDoc.addAttribute("xmlns:jxta", "http://jxta.org");
      xmlDoc.addAttribute("xml:space", "preserve");
    }

    Element e;

    e = adv.createElement(typeTag, Integer.toString(getDiscoveryType()));
    adv.appendChild(e);

    int threshold = getThreshold();

    if (threshold < 0) {
      throw new IllegalStateException("threshold must be >= 0");
    }
    e = adv.createElement(thresholdTag, Integer.toString(threshold));
    adv.appendChild(e);

    PeerAdvertisement peerAdv = getPeerAdvertisement();

    if ((peerAdv != null)) {
      e = adv.createElement(peerAdvTag, peerAdv.toString());
      adv.appendChild(e);
    }

    String attr = getAttr();

    if ((attr != null) && (attr.length() > 0)) {
      e = adv.createElement(queryAttrTag, attr.trim());
      adv.appendChild(e);

      String value = getValue();

      if ((value != null) && (value.length() > 0)) {
        e = adv.createElement(queryValueTag, value.trim());
        adv.appendChild(e);
      } else {
        if (threshold < 0) {
          throw new IllegalStateException("Attribute specified, but no value was specified.");
        }
      }
    }
    return adv;
  }
示例#2
0
  /**
   * Return a string representation of this advertisement. The string will contain the advertisement
   * pretty-print formatted as a UTF-8 encoded XML Document.
   *
   * @return A String containing the advertisement.
   */
  @Override
  public String toString() {
    XMLDocument doc = (XMLDocument) getDocument(MimeMediaType.XMLUTF8);

    // Force pretty printing
    doc.addAttribute("xml:space", "default");

    if (this.xmlSignatureInfoElement == null || this.xmlSignatureElement == null) {
      // Not signature available.
    } else {
      StructuredDocumentUtils.copyElements(doc, doc.getRoot(), this.xmlSignatureInfoElement);
      StructuredDocumentUtils.copyElements(doc, doc.getRoot(), this.xmlSignatureElement);
    }

    return doc.toString();
  }