示例#1
0
  /**
   * {@inheritDoc}
   *
   * @param raw Description of the Parameter
   * @return Description of the Return Value
   */
  protected boolean handleElement(Element raw) {

    if (super.handleElement(raw)) {
      return true;
    }

    XMLElement elem = (XMLElement) raw;

    if (DEST_PID_TAG.equals(elem.getName())) {
      try {
        URI pID = new URI(elem.getTextValue());

        setDestPeerID((PeerID) IDFactory.fromURI(pID));
      } catch (URISyntaxException badID) {
        throw new IllegalArgumentException("Bad PeerID in advertisement");
      } catch (ClassCastException badID) {
        throw new IllegalArgumentException("ID in advertisement is not a peer id");
      }
      return true;
    }

    if (elem.getName().equals("Dst")) {
      for (Enumeration eachXpt = elem.getChildren(); eachXpt.hasMoreElements(); ) {
        TextElement aXpt = (TextElement) eachXpt.nextElement();

        AccessPointAdvertisement xptAdv =
            (AccessPointAdvertisement) AdvertisementFactory.newAdvertisement(aXpt);

        setDest(xptAdv);
      }
      return true;
    }

    if (elem.getName().equals("Hops")) {
      Vector hops = new Vector();

      for (Enumeration eachXpt = elem.getChildren(); eachXpt.hasMoreElements(); ) {
        TextElement aXpt = (TextElement) eachXpt.nextElement();

        AccessPointAdvertisement xptAdv =
            (AccessPointAdvertisement) AdvertisementFactory.newAdvertisement(aXpt);

        hops.addElement(xptAdv);
      }
      setHops(hops);
      return true;
    }

    return false;
  }
示例#2
0
  /**
   * Called by the XML implementation to signal the end of an XML entity.
   *
   * @param e The XML entity that ends.
   * @throws SAXException on any error
   */
  public void handleEndElement(XMLElement e) throws SAXException {

    try {
      switch (tokens.toToken(e.getName(), false)) {
        case TodoTokenTable.TOKEN_TO_DO:
        case TodoTokenTable.TOKEN_RESOLVEDCRITICS:
        case TodoTokenTable.TOKEN_TO_DO_LIST:
          // NOP
          break;

        case TodoTokenTable.TOKEN_TO_DO_ITEM:
          handleTodoItemEnd(e);
          break;

        case TodoTokenTable.TOKEN_HEADLINE:
          handleHeadline(e);
          break;

        case TodoTokenTable.TOKEN_DESCRIPTION:
          handleDescription(e);
          break;

        case TodoTokenTable.TOKEN_PRIORITY:
          handlePriority(e);
          break;

        case TodoTokenTable.TOKEN_MOREINFOURL:
          handleMoreInfoURL(e);
          break;

        case TodoTokenTable.TOKEN_ISSUE:
          handleIssueEnd(e);
          break;

        case TodoTokenTable.TOKEN_POSTER:
          handlePoster(e);
          break;

        case TodoTokenTable.TOKEN_OFFENDER:
          handleOffender(e);
          break;

        default:
          LOG.log(Level.WARNING, "WARNING: unknown end tag:" + e.getName());
          break;
      }
    } catch (Exception ex) {
      throw new SAXException(ex);
    }
  }
示例#3
0
  /**
   * Process an individual element from the document during parse. Normally, implementations will
   * allow the base advertisments a chance to handle the element before attempting ot handle the
   * element themselves. ie.
   *
   * <p>
   *
   * <p>
   *
   * <pre><code>
   *  protected boolean handleElement(Element elem) {
   * <p/>
   *      if (super.handleElement()) {
   *           // it's been handled.
   *           return true;
   *           }
   * <p/>
   *      <i>... handle elements here ...</i>
   * <p/>
   *      // we don't know how to handle the element
   *      return false;
   *      }
   *  </code></pre>
   *
   * @param elem the element to be processed.
   * @return true if the element was recognized, otherwise false.
   */
  protected boolean handleElement(XMLElement elem) {

    String value = elem.getTextValue();

    if (null == value) {
      return false;
    }

    value = value.trim();

    if (0 == value.length()) {
      return false;
    }

    if (elem.getName().equals(typeTag)) {
      setDiscoveryType(Integer.parseInt(value));
      return true;
    }
    if (elem.getName().equals(thresholdTag)) {
      setThreshold(Integer.parseInt(value));
      return true;
    }
    if (elem.getName().equals(peerAdvTag)) {
      try {
        XMLDocument asDoc =
            (XMLDocument)
                StructuredDocumentFactory.newStructuredDocument(
                    MimeMediaType.XMLUTF8, new StringReader(value));
        PeerAdvertisement adv = (PeerAdvertisement) AdvertisementFactory.newAdvertisement(asDoc);
        setPeerAdvertisement(adv);
        return true;
      } catch (IOException failed) {
        IllegalArgumentException failure = new IllegalArgumentException("Bad Peer Advertisement");
        failure.initCause(failed);

        throw failure;
      }
    }
    if (elem.getName().equals(queryAttrTag)) {
      setAttr(value);
      return true;
    }
    if (elem.getName().equals(queryValueTag)) {
      setValue(value);
      return true;
    }

    // element was not handled
    return false;
  }
示例#4
0
  /**
   * Writes an XML element.
   *
   * @param xml the non-null XML element to write.
   * @param indent how many spaces to indent the element.
   */
  public void write(XMLElement xml, int indent) throws IOException {
    for (int i = 0; i < indent; i++) {
      this.writer.print(' ');
    }

    if (xml.getName() == null) {
      if (xml.getContent() != null) {
        this.writeEncoded(xml.getContent()); // aspect(xml.getContent(), "xml.getContent() - 111");
        this.writer.println();
      }
    } else {
      this.writer.print('<');
      this.writer.print(xml.getName()); // aspect(xml.getName(), "xml.getName() - 116");
      Enumeration _enum = xml._enumerateAttributeNames();

      while (_enum.hasMoreElements()) {
        String key = (String) _enum.nextElement();
        String value = xml.getAttribute(key);
        this.writer.print(" " + key + "=\""); // aspect(key, "key - 122");
        this.writeEncoded(value); // aspect(value, "value - 123");
        this.writer.print('"');
      }

      if ((xml.getContent() != null) && (xml.getContent().length() > 0)) {
        writer.print('>');
        this.writeEncoded(xml.getContent()); // aspect(xml.getContent(), "xml.getContent() - 130");
        writer.println("</" + xml.getName() + '>'); // aspect(xml.getName(), "xml.getName() - 131");
      } else if (xml.hasChildren()) {
        writer.println('>');
        _enum = xml._enumerateChildren();

        while (_enum.hasMoreElements()) {
          XMLElement child = (XMLElement) _enum.nextElement();
          this.write(child, indent + 4);
        }

        for (int i = 0; i < indent; i++) {
          this.writer.print(' ');
        }

        this.writer.println(
            "</" + xml.getName() + ">"); // aspect(xml.getName(), "xml.getName() - 138");
      } else {
        this.writer.println("/>");
      }
    }

    this.writer.flush();
  }
示例#5
0
  /**
   * Intialize a Discovery Query from a portion of a structured document.
   *
   * @param root document to intialize from
   */
  protected void initialize(Element root) {

    if (!XMLElement.class.isInstance(root)) {
      throw new IllegalArgumentException(getClass().getName() + " only supports XMLElement");
    }

    XMLElement doc = (XMLElement) root;

    if (!doc.getName().equals(getAdvertisementType())) {
      throw new IllegalArgumentException(
          "Could not construct : "
              + getClass().getName()
              + "from doc containing a "
              + doc.getName());
    }

    setDiscoveryType(-1); // force illegal value;

    Enumeration<XMLElement> elements = doc.getChildren();

    while (elements.hasMoreElements()) {
      XMLElement elem = elements.nextElement();

      if (!handleElement(elem)) {
        if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
          LOG.fine("Unhandled Element : " + elem.toString());
        }
      }
    }

    // sanity check time!

    if ((DiscoveryService.PEER != getDiscoveryType())
        && (DiscoveryService.GROUP != getDiscoveryType())
        && (DiscoveryService.ADV != getDiscoveryType())) {
      throw new IllegalArgumentException("Type is not one of the required values.");
    }
    if (getThreshold() < 0) {
      throw new IllegalArgumentException("Threshold must not be less than zero.");
    }
    if ((getDiscoveryType() != DiscoveryService.PEER) && (getThreshold() == 0)) {
      throw new IllegalArgumentException("Threshold may not be zero.");
    }
    if ((null == getAttr()) && (null != getValue())) {
      throw new IllegalArgumentException("Value specified without attribute.");
    }
  }
示例#6
0
  /**
   * Private constructor. Use instantiator
   *
   * @param root Description of the Parameter
   */
  private RouteAdv(Element root) {
    if (!XMLElement.class.isInstance(root)) {
      throw new IllegalArgumentException(getClass().getName() + " only supports XLMElement");
    }

    XMLElement doc = (XMLElement) root;

    String doctype = doc.getName();

    String typedoctype = "";
    Attribute itsType = doc.getAttribute("type");

    if (null != itsType) {
      typedoctype = itsType.getValue();
    }

    if (!doctype.equals(getAdvertisementType()) && !getAdvertisementType().equals(typedoctype)) {
      throw new IllegalArgumentException(
          "Could not construct : "
              + getClass().getName()
              + "from doc containing a "
              + doc.getName());
    }

    Enumeration elements = doc.getChildren();

    while (elements.hasMoreElements()) {
      XMLElement elem = (XMLElement) elements.nextElement();

      if (!handleElement(elem)) {
        if (LOG.isEnabledFor(Level.DEBUG)) {
          LOG.debug("Unhandled Element: " + elem.toString());
        }
      }
    }

    // HACK Compatibility

    setDestPeerID(getDestPeerID());

    // Sanity Check!!!

    if (hasALoop()) {
      throw new IllegalArgumentException("Route contains a loop!");
    }
  }
示例#7
0
  /**
   * Called by the XML implementation to signal the start of an XML entity.
   *
   * @param e The entity being started.
   */
  public void handleStartElement(XMLElement e) {
    // cat.debug("NOTE: TodoParser handleStartTag:" + e.getName());

    try {
      switch (tokens.toToken(e.getName(), true)) {
        case TodoTokenTable.TOKEN_HEADLINE:
        case TodoTokenTable.TOKEN_DESCRIPTION:
        case TodoTokenTable.TOKEN_PRIORITY:
        case TodoTokenTable.TOKEN_MOREINFOURL:
        case TodoTokenTable.TOKEN_POSTER:
        case TodoTokenTable.TOKEN_OFFENDER:
          // NOP
          break;

        case TodoTokenTable.TOKEN_TO_DO:
          handleTodo(e);
          break;

        case TodoTokenTable.TOKEN_TO_DO_LIST:
          handleTodoList(e);
          break;

        case TodoTokenTable.TOKEN_TO_DO_ITEM:
          handleTodoItemStart(e);
          break;

        case TodoTokenTable.TOKEN_RESOLVEDCRITICS:
          handleResolvedCritics(e);
          break;

        case TodoTokenTable.TOKEN_ISSUE:
          handleIssueStart(e);
          break;

        default:
          LOG.log(Level.WARNING, "WARNING: unknown tag:" + e.getName());
          break;
      }
    } catch (Exception ex) {
      LOG.log(Level.SEVERE, "Exception in startelement", ex);
    }
  }
示例#8
0
  /**
   * This method is called when the end of an XML elemnt is encountered.
   *
   * @see #startElement
   * @param name the name of the element
   * @param nsPrefix the prefix used to identify the namespace
   * @param nsSystemID the system ID associated with the namespace
   */
  public void endElement(String name, String nsPrefix, String nsSystemID) {
    XMLElement elt = (XMLElement) this.stack.pop();

    if (elt.getChildrenCount() == 1) {
      XMLElement child = elt.getChildAtIndex(0);

      if (child.getName() == null) {
        elt.setContent(child.getContent());
        elt.removeChildAtIndex(0);
      }
    }
  }