Ejemplo n.º 1
0
 @Test
 public void query2() {
   Folder c1 = db.createFolder("/c1");
   c1.documents().build(Name.create(db, "original")).elem("test").end("test").commit();
   XMLDocument doc = c1.documents().get("original").xml();
   doc.query().single("/test");
 }
Ejemplo n.º 2
0
 @Test
 public void query3() {
   Folder c1 = db.createFolder("/c1");
   XMLDocument doc =
       c1.documents().build(Name.create(db, "original")).elem("test").end("test").commit();
   assertEquals(1, doc.query().all("/test").size());
 }
Ejemplo n.º 3
0
 @Test
 public void nameAndPathFromLoad() {
   XMLDocument doc =
       db.createFolder("/top").documents().load(Name.create(db, "foo"), Source.xml("<root/>"));
   assertEquals("foo", doc.name());
   assertEquals("/top/foo", doc.path());
 }
Ejemplo n.º 4
0
 @Test
 public void delete1() {
   Folder c1 = db.createFolder("/c1");
   XMLDocument doc =
       c1.documents().build(Name.create(db, "original")).elem("test").end("test").commit();
   doc.delete();
   assertEquals(0, c1.documents().size());
 }
Ejemplo n.º 5
0
 @Test
 public void writeToOutputStream() throws IOException {
   XMLDocument doc =
       db.createFolder("/top").documents().load(Name.create(db, "foo"), Source.xml("<root/>"));
   ByteArrayOutputStream out = new ByteArrayOutputStream();
   doc.write(out);
   out.close();
   assertEquals("<root/>", out.toString());
 }
Ejemplo n.º 6
0
 @Test
 public void lengthFromCreate() {
   XMLDocument doc =
       db.createFolder("/top")
           .documents()
           .build(Name.create(db, "foo"))
           .elem("root")
           .end("root")
           .commit();
   assertThat(doc.length(), Matchers.greaterThan(0L));
 }
Ejemplo n.º 7
0
 @Test
 public void contentsAsStringFromCreate() {
   XMLDocument doc =
       db.createFolder("/top")
           .documents()
           .build(Name.create(db, "foo"))
           .elem("root")
           .end("root")
           .commit();
   assertEquals("<root/>", doc.contentsAsString());
 }
Ejemplo n.º 8
0
 /**
  * This method returns the original document appended with an XML signature element, if it exists.
  * Before an advertisement is signed, the method returns the original document. After the
  * advertisement is signed successfully, the method returns the original document appended with a
  * valid XML signature element. For a newly discovered advertisement, the method may return the
  * original document with or without an XML signature element attached, depending on whether the
  * original publisher has signed it or not. If there is an XML signature attached, the verify()
  * method must return true before you can trust the advertisement.
  *
  * <p>When the XML signature element is not attached, the method is equivalent to
  * getDocument(MimeMediaType.XMLUTF8).
  *
  * <p>The abstract method getDocument(MimeMediaType asMimeType) is used only for the subclass to
  * implement the advertisement. The getSignedDocument() is used whenever you want to get the
  * document out of the advertisement.
  *
  * @return The document with an XML signature, if it exists.
  */
 public final synchronized Document getSignedDocument() {
   if (this.xmlSignatureInfoElement == null || this.xmlSignatureElement == null) {
     return this.getDocument(MimeMediaType.XMLUTF8);
   } else {
     XMLDocument tempDoc = (XMLDocument) this.getDocument(MimeMediaType.XMLUTF8);
     StructuredDocumentUtils.copyElements(
         tempDoc, tempDoc.getRoot(), this.xmlSignatureInfoElement);
     StructuredDocumentUtils.copyElements(tempDoc, tempDoc.getRoot(), this.xmlSignatureElement);
     return tempDoc;
   }
 }
Ejemplo n.º 9
0
 @Test
 public void copy1() {
   Folder c1 = db.createFolder("/c1"), c2 = db.createFolder("/c2");
   XMLDocument original =
       c1.documents().build(Name.create(db, "original")).elem("test").end("test").commit();
   XMLDocument copy = original.copy(c2, Name.keepCreate(db));
   assertEquals(1, c1.documents().size());
   c1.query().single("/test");
   assertEquals(1, c2.documents().size());
   c2.query().single("/test");
   copy.query().single("/test");
 }
Ejemplo n.º 10
0
 @Test
 public void nameAndPathFromCreate() {
   XMLDocument doc =
       db.createFolder("/top")
           .documents()
           .build(Name.create(db, "foo"))
           .elem("root")
           .end("root")
           .commit();
   assertEquals("foo", doc.name());
   assertEquals("/top/foo", doc.path());
 }
Ejemplo n.º 11
0
 @Test
 public void move1() {
   Folder c1 = db.createFolder("/c1"), c2 = db.createFolder("/c2");
   XMLDocument doc =
       c1.documents().build(Name.create(db, "original")).elem("test").end("test").commit();
   doc.move(c2, Name.keepCreate(db));
   assertEquals(0, c1.documents().size());
   assertFalse(c1.query().exists("/test"));
   assertEquals(1, c2.documents().size());
   c2.query().single("/test");
   doc.query().single("/test");
   assertEquals("/c2/original", doc.path());
 }
Ejemplo n.º 12
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;
  }
Ejemplo n.º 13
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();
  }
Ejemplo n.º 14
0
    void define(Node self, Document doc) throws ThinklabException {

      if (attrs != null)
        for (Pair<String, String> a : attrs) {
          Attr attr = doc.createAttribute(a.getFirst());
          attr.setValue(a.getSecond());
          ((Element) self).setAttributeNode(attr);
        }

      for (Object o : contents) {

        if (o instanceof String) {
          String text = (String) o;
          XMLDocument.setTextContent(doc, self, text);
        } else if (o instanceof Collection<?>) {
          for (Iterator<?> it = ((Collection<?>) o).iterator(); it.hasNext(); ) {
            Object no = it.next();
            if (no instanceof XmlNode) {
              self.appendChild(((XmlNode) no).create(self, doc));
            } else if (no instanceof Polylist) {
              self.appendChild(((Polylist) no).createXmlNode().create(self, doc));
            } else {
              throw new ThinklabValidationException(
                  "XML.node: collections must be of XmlNode or Polylist");
            }
          }
        } else if (o instanceof XmlNode) {
          self.appendChild(((XmlNode) o).create(self, doc));
        } else if (o instanceof Polylist) {
          self.appendChild(((Polylist) o).createXmlNode().create(self, doc));
        }
      }
    }
Ejemplo n.º 15
0
    Node create(Node parent, Document doc) throws ThinklabException {

      Node ret = doc.createElement(tag);

      if (attrs != null)
        for (Pair<String, String> a : attrs) {
          Attr attr = doc.createAttribute(a.getFirst());
          attr.setValue(a.getSecond());
          ((Element) ret).setAttributeNode(attr);
        }

      for (Object o : contents) {

        if (o instanceof String) {
          String text = (String) o;
          XMLDocument.setTextContent(doc, ret, text);
        } else if (o instanceof Collection<?>) {
          for (Iterator<?> it = ((Collection<?>) o).iterator(); it.hasNext(); ) {
            Object no = it.next();
            if (!(no instanceof XmlNode)) {
              throw new ThinklabValidationException("XML.node: collections must be of XmlNode");
            }
            ret.appendChild(((XmlNode) no).create(ret, doc));
          }
        } else if (o instanceof XmlNode) {
          ret.appendChild(((XmlNode) o).create(ret, doc));
        }
      }

      return ret;
    }
Ejemplo n.º 16
0
  public static XMLDocument document(Object... objects) throws ThinklabException {

    XmlNode root = null;
    ArrayList<String> namespaces = null;

    for (Object o : objects) {
      if (o instanceof String) {

        /*
         * namespace
         */
        if (namespaces == null) namespaces = new ArrayList<String>();

        // namespaces.add((String)o);

      } else if (o instanceof XmlNode) {

        /*
         * must be only root node
         */
        if (root != null)
          throw new ThinklabValidationException("XML document: non-unique root node");
        root = (XmlNode) o;
      }
    }

    if (root == null) throw new ThinklabValidationException("XML.document: no root node specified");

    XMLDocument doc = new XMLDocument(root.tag);

    if (namespaces != null) {
      for (String ns : namespaces) {
        String[] nss = ns.split("=");
        if (nss.length != 2)
          throw new ThinklabValidationException(
              "XML.document: bad namespace specification: must be name=uri: " + ns);
        doc.addNamespace(nss[0], nss[1]);
      }
    }

    root.define(doc.root(), doc.dom);

    return doc;
  }
Ejemplo n.º 17
0
 @Test
 public void delete2() {
   Folder c1 = db.createFolder("/c1");
   XMLDocument doc1 =
       c1.documents()
           .build(Name.create(db, "doc1"))
           .elem("test")
           .attr("xml:id", "a")
           .end("test")
           .commit();
   XMLDocument doc2 =
       c1.documents()
           .build(Name.create(db, "doc2"))
           .elem("test2")
           .attr("xml:id", "b")
           .end("test2")
           .commit();
   doc1.delete();
   doc2.delete();
   assertEquals(0, c1.documents().size());
 }
Ejemplo n.º 18
0
 @Test
 public void contentsAsStringFromLoad() {
   XMLDocument doc =
       db.createFolder("/top").documents().load(Name.create(db, "foo"), Source.xml("<root/>"));
   assertEquals("<root/>", doc.contentsAsString());
 }
Ejemplo n.º 19
0
 @Test
 public void lengthFromLoad() {
   XMLDocument doc =
       db.createFolder("/top").documents().load(Name.create(db, "foo"), Source.xml("<root/>"));
   assertThat(doc.length(), Matchers.greaterThan(0L));
 }