/** * {@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; }
/** * 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; }
/** * {@inheritDoc} * * @param encodeAs Description of the Parameter * @return The document value */ public Document getDocument(MimeMediaType encodeAs) { StructuredDocument adv = (StructuredDocument) super.getDocument(encodeAs); if (hasALoop()) { throw new IllegalStateException("I won't write a doc for a route with a loop"); } PeerID pid = getDestPeerID(); AccessPointAdvertisement dest = getDest(); if ((null != pid) && (null != dest) && (null != dest.getPeerID())) { if (!pid.equals(dest.getPeerID())) { throw new IllegalStateException( "Destination peer id and destination access point adv don't refer to the same peer"); } } // HACK Backwards Compatibility if ((null == pid) && (null != dest)) { pid = dest.getPeerID(); } if (pid != null) { Element e0 = adv.createElement(DEST_PID_TAG, pid.toString()); adv.appendChild(e0); } if (dest != null) { // create the copy without the PID AccessPointAdvertisement ap = (AccessPointAdvertisement) AdvertisementFactory.newAdvertisement( AccessPointAdvertisement.getAdvertisementType()); ap.setEndpointAddresses(dest.getVectorEndpointAddresses()); StructuredTextDocument xptDoc = (StructuredTextDocument) ap.getDocument(encodeAs); Element e1 = adv.createElement("Dst"); adv.appendChild(e1); StructuredDocumentUtils.copyElements(adv, e1, xptDoc); } // only include hops if we have some if (getHops().hasMoreElements()) { Element e2 = adv.createElement("Hops"); adv.appendChild(e2); for (Enumeration e = getHops(); e.hasMoreElements(); ) { AccessPointAdvertisement hop = (AccessPointAdvertisement) e.nextElement(); if (hop != null) { StructuredTextDocument xptDoc = (StructuredTextDocument) hop.getDocument(encodeAs); StructuredDocumentUtils.copyElements(adv, e2, xptDoc); } } } return adv; }