Exemple #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;
  }