/** Private helper function to encode the subjects */
  private void encodeSubject(Subject subject, PrintStream out, Indenter indenter) {
    char[] indent = indenter.makeString().toCharArray();
    out.print(indent);
    out.append("<Subject SubjectCategory=\"").append(subject.getCategory().toString()).append('"');

    List subjectAttrs = subject.getAttributesAsList();

    if (subjectAttrs.size() == 0) {
      // there's nothing in this Subject, so just close the tag
      out.println("/>");
    } else {
      // there's content, so fill it in
      out.println('>');

      encodeAttributes(subjectAttrs, out, indenter);

      out.print(indent);
      out.println("</Subject>");
    }
  }
  /* (non-Javadoc)
   * @see org.fcrepo.server.security.impl.RequestCtx#encode(java.io.OutputStream, org.jboss.security.xacml.sunxacml.Indenter, java.lang.String)
   */
  @Override
  public void encode(OutputStream output, Indenter indenter, String nsURI) {

    // Make a PrintStream for a nicer printing interface
    PrintStream out = new PrintStream(output);

    // Prepare the indentation string
    char[] topIndent = indenter.makeString().toCharArray();
    out.print(topIndent);
    if (nsURI != null) {
      out.append("<Request xmlns=\"").append(nsURI).println("\">");
    } else {
      out.println("<Request>");
    }
    // go in one more for next-level elements...
    indenter.in();
    char[] indent = indenter.makeString().toCharArray();

    // ...and go in again for everything else
    indenter.in();

    // first off, go through all subjects
    Iterator it = subjects.iterator();
    while (it.hasNext()) {
      Subject subject = (Subject) (it.next());
      encodeSubject(subject, out, indenter);
    }

    // next do the resource
    if ((resource.size() != 0) || (resourceContent != null)) {
      out.print(indent);
      out.println("<Resource>");
      if (resourceContent != null)
        out.append(indenter.makeString())
            .append("<ResourceContent>")
            .append(resourceContent)
            .println("</ResourceContent>");
      encodeAttributes(resource, out, indenter);
      out.print(indent);
      out.println("</Resource>");
    } else {
      out.print(indent);
      out.println("<Resource/>");
    }

    // now the action
    if (action.size() != 0) {
      out.print(indent);
      out.println("<Action>");
      encodeAttributes(action, out, indenter);
      out.print(indent);
      out.println("</Action>");
    } else {
      out.print(indent);
      out.println("<Action/>");
    }

    // Bug ID:1745062
    out.print(indent);
    out.println("<Environment>");
    // finally the environment, if there are any attrs
    if (environment.size() != 0) {
      encodeAttributes(environment, out, indenter);
    }
    out.print(indent);
    out.println("</Environment>");

    // we're back to the top
    indenter.out();
    indenter.out();

    out.print(topIndent);
    out.println("</Request>");
  }