private Element makeTerm(Document doc, TablesRevisionProblem problem, Field field)
      throws RenderException {
    assert field != null;

    Element element = doc.createElement("term");

    if (field.equals(problem.getFraction())) {
      Element child = doc.createElement("numerator");
      child.setTextContent("1");
      element.appendChild(child);

      child = doc.createElement("denominator");
      child.setTextContent(String.valueOf(problem.getValue(field)));
      if (field.equals(problem.getBlank())) {
        setBlank(element);
      }
      element.appendChild(child);

    } else {
      if (field.equals(problem.getBlank())) {
        setBlank(element);
      }

      element.setTextContent(String.valueOf(problem.getValue(field)));
    }

    return element;
  }
  private Element makeRHS(Document doc, TablesRevisionProblem problem) throws RenderException {

    Element element = doc.createElement("answer");
    element.setTextContent(String.valueOf(problem.getAnswer()));

    if (Field.ANSWER.equals(problem.getBlank())) {
      setBlank(element);
    }

    return element;
  }
  @Override
  protected Element renderProblem(
      DOMRenderContext context, Document doc, TablesRevisionProblem problem)
      throws RenderException {
    if (problem == null) {
      return null;
    }

    if (doc == null) {
      throw new NullPointerException("Document is null");
    }

    Element lhs = makeLHS(doc, problem);
    Element rhs = makeRHS(doc, problem);

    Element element = doc.createElement(DOMRenderContext.TAG_PROBLEM);
    element.setAttribute("type", TablesRevision.EXERCISE_ID);

    if (problem.getFlip()) {
      element.appendChild(rhs);
      element.appendChild(doc.createElement("equals"));
      element.appendChild(lhs);
    } else {
      element.appendChild(lhs);
      element.appendChild(doc.createElement("equals"));
      element.appendChild(rhs);
    }

    return element;
  }
  private Element makeLHS(Document doc, TablesRevisionProblem problem) throws RenderException {

    Element element = doc.createElement(problem.isDivide() ? "divide" : "multiply");

    element.appendChild(makeTerm(doc, problem, Field.TERM1));
    element.appendChild(makeTerm(doc, problem, Field.TERM2));

    return element;
  }