public Document nextTableRow(String atts) {

    // peek at current cell - stay with it IF
    //  + it's the first cell in the row
    //  + the current cursor points at the first child (a block)
    //  + the block pointed by cursor is empty
    Element cell = peek("table-cell", "nextTableRow() is not applicable outside enclosing table");
    if (cell.getPreviousSibling() == null
        && cursor == cell.getFirstChild()
        && !cursor.hasChildNodes()) {
      attributes((Element) cell.getParentNode(), atts);
      return this;
    }

    // pop to table
    pop("table", "nextTableRow() is not applicable outside enclosing table");
    Element table = cursor;

    // last child is already table-body?
    if (table.getLastChild().getNodeName().equals("table-body")) {
      cursor = (Element) table.getLastChild();
    } else {
      push("table-body");
    }

    // add row
    push("table-row", atts);

    // add cell
    push("table-cell", "border=" + table.getAttribute("border"));
    push("block");

    // done
    return this;
  }
Example #2
0
  @Test
  public void testAddRemoveChild() throws Exception {
    //		builderFactory = DocumentBuilderFactory.newInstance();
    builderFactory.setNamespaceAware(true);

    String xml =
        "<?xml version=\"1.0\"?>"
            + "<root>"
            + "<item id=\"a\"/>"
            + "<child id=\"1\"/>"
            + "<item id=\"b\"/>"
            + "<child id=\"2\"/>"
            + "</root>";

    DocumentBuilder builder = builderFactory.newDocumentBuilder();
    Document doc = builder.parse(new ByteArrayInputStream(xml.getBytes()));
    Element root = doc.getDocumentElement();

    Element newElem = doc.createElement("tail");
    newElem.setAttribute("id", "3");
    Node appended = root.appendChild(newElem);

    Assert.assertEquals("added element", "tail", root.getLastChild().getNodeName());
    Assert.assertEquals("added attribute", "3", ((Element) root.getLastChild()).getAttribute("id"));

    root.setAttribute("id", "root");
    Assert.assertEquals("root attribute set", "root", root.getAttribute("id"));
    root.removeAttribute("id");
    Assert.assertEquals("root attribute remove", null, root.getAttribute("id"));

    root.removeChild(appended);
    Assert.assertEquals("removed element", "child", root.getLastChild().getNodeName());
    Assert.assertEquals("removed element", "2", ((Element) root.getLastChild()).getAttribute("id"));
  }
Example #3
0
  // firstchild, lastchild, nextsibling, previoussibling, parent
  @Test
  public void testNavigate() throws Exception {
    String xml = "<?xml version=\"1.0\"?>" + "<root>" + "<a/>" + "<b/>" + "<c/>" + "</root>";

    DocumentBuilder builder = builderFactory.newDocumentBuilder();
    Document doc = builder.parse(new ByteArrayInputStream(xml.getBytes()));
    Element root = doc.getDocumentElement();
    Assert.assertEquals("a", ((Element) root.getFirstChild()).getTagName());
    Assert.assertEquals("c", ((Element) root.getLastChild()).getTagName());
    Assert.assertEquals("b", ((Element) root.getFirstChild().getNextSibling()).getTagName());
    Assert.assertEquals("b", ((Element) root.getLastChild().getPreviousSibling()).getTagName());
    Assert.assertEquals("root", ((Element) root.getFirstChild().getParentNode()).getTagName());
  }
  // the xml is different for these
  protected ArrayList<BGCOffenseSupplementBean> parseSupplements(Element e) {
    // XXX get right element first
    // e = offense
    // supp = offense/recordDetails/recordDetail/supplements (then each /supplement)
    Element eRecordDetails = (Element) e.getElementsByTagName("recordDetails").item(0);
    Element eRecordDetail = (Element) eRecordDetails.getElementsByTagName("recordDetail").item(0);
    Element eSupplements = (Element) eRecordDetail.getElementsByTagName("supplements").item(0);

    // HashMap<String,String> map = new HashMap<String,String>();
    ArrayList<BGCOffenseSupplementBean> list = new ArrayList<BGCOffenseSupplementBean>();
    NodeList nl = eSupplements.getElementsByTagName("supplement");
    for (int i = 0; i < nl.getLength(); i++) {
      Element eSupp = (Element) nl.item(i);
      Node eTitle = eSupp.getFirstChild();
      Node eValue = eSupp.getLastChild();
      if (eTitle.getNodeName().equals("displayTitle")
          && eValue.getNodeName().equals("displayValue")) {
        String sTitle = eTitle.getFirstChild().getNodeValue(); // getTextContent();
        String sValue = eValue.getFirstChild().getNodeValue(); // getTextContent();
        BGCOffenseSupplementBean bean = new BGCOffenseSupplementBean();
        bean.setDisplayTitle(sTitle);
        bean.setDisplayValue(sValue);
        // map.put(sTitle, sValue);
        list.add(bean);
      }
    }
    // return map;
    return list;
  }
  /**
   * Add section at specified depth. The depth is used to determine the font size and should in the
   * future be used for numbering in X.Y.Z format. 1 is the usual outermost section and maps to
   * {@link #FONT_XX_LARGE} by default. <a
   * href="http://www.w3.org/TR/REC-CSS2/fonts.html#font-styling">http://www.w3.org/TR/REC-CSS2/fonts.html#font-styling</a>
   * describes the meaning of logical font sizes in XSL/FO.
   *
   * @see #setSectionSizes
   */
  public Document startSection(String title, String id, int sectionDepth) {

    // check if
    if (id != null && id.startsWith("_"))
      throw new IllegalArgumentException("underscore is reserved for internal IDs");

    // return to the last block in flow
    pop("flow", "addSection() is not applicable outside document flow");
    cursor = (Element) cursor.getLastChild();

    // generate an id if necessary
    if (id == null || id.length() == 0) id = "toc" + toc.size();

    // start a new block
    String fontSize = getFontSize(sectionDepth);
    pop().push("block", "font-size=" + fontSize + "," + formatSection + ",id=" + id);

    // remember
    toc.add(new TOCEntry(id, title));

    // add the title
    addText(title);

    // create the following block
    nextParagraph();

    // done
    return this;
  }
  public TestReport runImpl() throws Exception {
    Handler h = new Handler();
    TestReport report = null;

    // cdata-sections == false
    Document doc = newSVGDoc();
    DOMConfiguration conf = ((AbstractDocument) doc).getDomConfig();
    conf.setParameter("cdata-sections", Boolean.FALSE);
    Element e = doc.getDocumentElement();
    e.appendChild(doc.createTextNode("abc"));
    e.appendChild(doc.createCDATASection("def"));
    e.appendChild(doc.createTextNode("ghi"));
    ((AbstractDocument) doc).normalizeDocument();
    if (!(e.getFirstChild().getNodeType() == Node.TEXT_NODE
        && e.getFirstChild().getNodeValue().equals("abcdefghi")
        && e.getFirstChild() == e.getLastChild())) {
      if (report == null) {
        report = reportError("Document.normalizeDocument test failed");
      }
      report.addDescriptionEntry("DOMConfiguration parameter", "cdata-sections == false");
    }

    // comments == false
    doc = newSVGDoc();
    conf = ((AbstractDocument) doc).getDomConfig();
    conf.setParameter("comments", Boolean.FALSE);
    e = doc.getDocumentElement();
    e.appendChild(doc.createTextNode("abc"));
    e.appendChild(doc.createComment("def"));
    e.appendChild(doc.createTextNode("ghi"));
    ((AbstractDocument) doc).normalizeDocument();
    if (!(e.getFirstChild().getNodeType() == Node.TEXT_NODE
        && e.getFirstChild().getNodeValue().equals("abcghi")
        && e.getFirstChild() == e.getLastChild())) {
      if (report == null) {
        report = reportError("Document.normalizeDocument test failed");
      }
      report.addDescriptionEntry("DOMConfiguration parameter", "comments == false");
    }

    // element-content-whitespace == false
    doc = newSVGDoc();
    conf = ((AbstractDocument) doc).getDomConfig();
    conf.setParameter("element-content-whitespace", Boolean.FALSE);
    e = doc.getDocumentElement();
    e.appendChild(doc.createTextNode("    "));
    e.appendChild(doc.createElementNS(SVG_NAMESPACE_URI, "g"));
    e.appendChild(doc.createTextNode("    "));
    ((AbstractDocument) doc).normalizeDocument();
    if (!(e.getFirstChild().getNodeType() == Node.ELEMENT_NODE
        && e.getFirstChild().getNodeName().equals("g")
        && e.getFirstChild() == e.getLastChild())) {
      if (report == null) {
        report = reportError("Document.normalizeDocument test failed");
      }
      report.addDescriptionEntry(
          "DOMConfiguration parameter", "element-content-whitespace == false");
    }

    // split-cdata-sections == true
    doc = newSVGDoc();
    conf = ((AbstractDocument) doc).getDomConfig();
    conf.setParameter("split-cdata-sections", Boolean.TRUE);
    conf.setParameter("error-handler", h);
    e = doc.getDocumentElement();
    e.appendChild(doc.createCDATASection("before ]]> after"));
    ((AbstractDocument) doc).normalizeDocument();
    if (!(e.getFirstChild().getNodeType() == Node.CDATA_SECTION_NODE
        && e.getFirstChild().getNodeValue().equals("before ]]")
        && e.getFirstChild().getNextSibling().getNodeType() == Node.CDATA_SECTION_NODE
        && e.getFirstChild().getNextSibling().getNodeValue().equals("> after")
        && e.getFirstChild().getNextSibling() == e.getLastChild()
        && h.get("cdata-sections-splitted") == 1)) {
      if (report == null) {
        report = reportError("Document.normalizeDocument test failed");
      }
      report.addDescriptionEntry("DOMConfiguration parameter", "split-cdata-sections == true");
    }

    // well-formed
    doc = newSVGDoc();
    ((AbstractDocument) doc).setStrictErrorChecking(false);
    conf = ((AbstractDocument) doc).getDomConfig();
    conf.setParameter("error-handler", h);
    e = doc.getDocumentElement();
    e.appendChild(doc.createComment("before -- after"));
    e.appendChild(doc.createComment("ends in a dash -"));
    e.setAttribute("*", "blah");
    e.appendChild(doc.createProcessingInstruction("abc", "def?>"));
    ((AbstractDocument) doc).normalizeDocument();
    if (!(h.get("wf-invalid-character-in-node-name") == 1 && h.get("wf-invalid-character") == 3)) {
      if (report == null) {
        report = reportError("Document.normalizeDocument test failed");
      }
      report.addDescriptionEntry("DOMConfiguration parameter", "well-formed == true");
    }

    // namespaces
    doc = newDoc();
    e = doc.createElementNS(null, "root");
    doc.appendChild(e);
    Element e2 = doc.createElementNS(null, "parent");
    e.appendChild(e2);
    e2.setAttributeNS(XMLNS_NAMESPACE_URI, "xmlns:ns", "http://www.example.org/ns1");
    e2.setAttributeNS(XMLNS_NAMESPACE_URI, "xmlns:bar", "http://www.example.org/ns2");
    Element e3 = doc.createElementNS("http://www.example.org/ns1", "ns:child1");
    e2.appendChild(e3);
    e3.setAttributeNS(XMLNS_NAMESPACE_URI, "xmlns:ns", "http://www.example.org/ns2");
    e3 = doc.createElementNS("http://www.example.org/ns2", "ns:child2");
    e2.appendChild(e3);
    ((AbstractDocument) doc).normalizeDocument();
    Attr a = e3.getAttributeNodeNS(XMLNS_NAMESPACE_URI, "ns");
    if (!(a != null
        && a.getNodeName().equals("xmlns:ns")
        && a.getNodeValue().equals("http://www.example.org/ns2"))) {
      if (report == null) {
        report = reportError("Document.normalizeDocument test failed");
      }
      report.addDescriptionEntry("DOMConfiguration parameter", "namespaces == true, test 1");
    }

    doc = newDoc();
    e = doc.createElementNS(null, "root");
    doc.appendChild(e);
    e2 = doc.createElementNS("http://www.example.org/ns1", "ns:child1");
    e.appendChild(e2);
    e2.setAttributeNS(XMLNS_NAMESPACE_URI, "xmlns:ns", "http://www.example.org/ns1");
    e3 = doc.createElementNS("http://www.example.org/ns1", "ns:child2");
    e2.appendChild(e3);
    e2 =
        (Element)
            ((AbstractDocument) doc).renameNode(e2, "http://www.example.org/ns2", "ns:child1");
    ((AbstractDocument) doc).normalizeDocument();
    a = e2.getAttributeNodeNS(XMLNS_NAMESPACE_URI, "ns");
    Attr a2 = e3.getAttributeNodeNS(XMLNS_NAMESPACE_URI, "ns");
    if (!(a != null
        && a.getNodeName().equals("xmlns:ns")
        && a.getNodeValue().equals("http://www.example.org/ns2")
        && a2 != null
        && a2.getNodeName().equals("xmlns:ns")
        && a2.getNodeValue().equals("http://www.example.org/ns1"))) {
      if (report == null) {
        report = reportError("Document.normalizeDocument test failed");
      }
      report.addDescriptionEntry("DOMConfiguration parameter", "namespaces == true, test 2");
    }

    doc = newDoc();
    e = doc.createElementNS(null, "root");
    doc.appendChild(e);
    e2 = doc.createElementNS("http://www.example.org/ns1", "child1");
    e.appendChild(e2);
    e2.setAttributeNS("http://www.example.org/ns2", "blah", "hi");
    ((AbstractDocument) doc).normalizeDocument();
    a = e2.getAttributeNodeNS(XMLNS_NAMESPACE_URI, "xmlns");
    a2 = e2.getAttributeNodeNS(XMLNS_NAMESPACE_URI, "NS1");
    if (!(a != null
        && a.getNodeValue().equals("http://www.example.org/ns1")
        && a2 != null
        && a2.getNodeValue().equals("http://www.example.org/ns2"))) {
      if (report == null) {
        report = reportError("Document.normalizeDocument test failed");
      }
      report.addDescriptionEntry("DOMConfiguration parameter", "namespaces == true, test 3");
    }

    // namespace-declarations == false
    doc = newDoc();
    e = doc.createElementNS(null, "ex:root");
    e.setAttributeNS(XMLNS_NAMESPACE_URI, "xmlns:ex", "http://www.example.org/ns1");
    conf = ((AbstractDocument) doc).getDomConfig();
    conf.setParameter("namespace-declarations", Boolean.FALSE);
    doc.appendChild(e);
    ((AbstractDocument) doc).normalizeDocument();
    if (!(e.getAttributeNodeNS(XMLNS_NAMESPACE_URI, "ex") == null)) {
      if (report == null) {
        report = reportError("Document.normalizeDocument test failed");
      }
      report.addDescriptionEntry("DOMConfiguration parameter", "namespace-declarations == false");
    }

    if (report == null) {
      return reportSuccess();
    }
    return report;
  }
  /** Builds the properties of the given class */
  void build(ClassDoc classDoc) {
    Element thead = getChild(classDoc.getPropertiesTable(), "thead");
    Element tr = getChild(thead, "tr");
    List<Element> header = children(tr, "td");
    if (header.size() < 1) {
      throw new RuntimeException(
          String.format("Expected at least 1 <td> in <thead>/<tr>, found: %s", header));
    }
    Map<String, Element> inheritedValueTitleMapping = new HashMap<String, Element>();
    List<Element> valueTitles = new ArrayList<Element>();
    for (int i = 1; i < header.size(); i++) {
      Element element = header.get(i);
      Element override = findChild(element, "overrides");
      if (override != null) {
        element.removeChild(override);
        inheritedValueTitleMapping.put(override.getTextContent(), element);
      }
      Node firstChild = element.getFirstChild();
      if (firstChild instanceof Text) {
        firstChild.setTextContent(firstChild.getTextContent().replaceFirst("^\\s+", ""));
      }
      Node lastChild = element.getLastChild();
      if (lastChild instanceof Text) {
        lastChild.setTextContent(lastChild.getTextContent().replaceFirst("\\s+$", ""));
      }
      valueTitles.add(element);
    }

    ClassDoc superClass = classDoc.getSuperClass();

    // adding the properties from the super class onto the inheriting class
    Map<String, PropertyDoc> props = new TreeMap<String, PropertyDoc>();
    if (superClass != null) {
      for (PropertyDoc propertyDoc : superClass.getClassProperties()) {
        Map<String, ExtraAttributeDoc> additionalValues =
            new LinkedHashMap<String, ExtraAttributeDoc>();
        for (ExtraAttributeDoc attributeDoc : propertyDoc.getAdditionalValues()) {
          String key = attributeDoc.getKey();
          if (inheritedValueTitleMapping.get(key) != null) {
            ExtraAttributeDoc newAttribute =
                new ExtraAttributeDoc(
                    inheritedValueTitleMapping.get(key), attributeDoc.getValueCell());
            additionalValues.put(newAttribute.getKey(), newAttribute);
          } else {
            additionalValues.put(key, attributeDoc);
          }
        }

        props.put(propertyDoc.getName(), propertyDoc.forClass(classDoc, additionalValues.values()));
      }
    }

    for (Element row : children(classDoc.getPropertiesTable(), "tr")) {
      List<Element> cells = children(row, "td");
      if (cells.size() != header.size()) {
        throw new RuntimeException(
            String.format("Expected %s <td> elements in <tr>, found: %s", header.size(), tr));
      }
      String propName = cells.get(0).getTextContent().trim();
      PropertyMetaData property = classDoc.getClassMetaData().findProperty(propName);
      if (property == null) {
        throw new RuntimeException(
            String.format(
                "No metadata for property '%s.%s'. Available properties: %s",
                classDoc.getName(), propName, classDoc.getClassMetaData().getPropertyNames()));
      }

      Map<String, ExtraAttributeDoc> additionalValues =
          new LinkedHashMap<String, ExtraAttributeDoc>();

      if (superClass != null) {
        PropertyDoc overriddenProp = props.get(propName);
        if (overriddenProp != null) {
          for (ExtraAttributeDoc attributeDoc : overriddenProp.getAdditionalValues()) {
            additionalValues.put(attributeDoc.getKey(), attributeDoc);
          }
        }
      }

      for (int i = 1; i < header.size(); i++) {
        if (cells.get(i).getFirstChild() == null) {
          continue;
        }
        ExtraAttributeDoc attributeDoc =
            new ExtraAttributeDoc(valueTitles.get(i - 1), cells.get(i));
        additionalValues.put(attributeDoc.getKey(), attributeDoc);
      }
      PropertyDoc propertyDoc =
          new PropertyDoc(
              property,
              javadocConverter.parse(property, listener).getDocbook(),
              new ArrayList<ExtraAttributeDoc>(additionalValues.values()));
      if (propertyDoc.getDescription() == null) {
        throw new RuntimeException(
            String.format(
                "Docbook content for '%s.%s' does not contain a description paragraph.",
                classDoc.getName(), propName));
      }

      props.put(propName, propertyDoc);
    }

    for (PropertyDoc propertyDoc : props.values()) {
      classDoc.addClassProperty(propertyDoc);
    }
  }