private void translate(Element parent) {
// Step 1:
// Recurse through all elements and either
// translate them or remove them.
for (int i = 0; i < parent.getContentSize(); i++) {
Content decedent = parent.getContent(i);
if (decedent instanceof org.jdom.Text) {
} else if (decedent instanceof Element) {
Element element = (Element) decedent;
String name = element.getName();
// First all the DRI elements, allow them to pass.
if ("p".equals(name)) {
// Paragraphs are tricky, it may be either an HTML
// or DRI <p> element. However, while HTML will allow
// <p> to nest DRI does not, thus first we need to
// check if this is at the block level, if it is then
// we need remove it.
if (parent.isRootElement()) {
// The paragraph is not nested, so translate it to
// a DRI <p>
moveAttribute(element, "class", "rend");
limitAttributes(element, "id", "n", "rend");
translate(element);
} else {
// The paragraph is nested which is not allowed in
// DRI, so remove it.
removeContent(element);
}
} else if ("h1".equals(name) || "h2".equals(name)
|| "h3".equals(name) || "h4".equals(name)
|| "h5".equals(name)) {
// The HTML <H1> tag is translated into the DRI
// <p rend="heading"> tag.
if (parent.isRootElement()) {
limitAttributes(element);
element.setName("p");
element.setAttribute("rend", "heading");
translate(element);
} else {
// DRI paragraphs can not be nested.
removeContent(element);
}
} else if ("a".equals(name)) {
// The HTML <a> tag is translated into the DRI
// <xref> tag.
moveAttribute(element, "href", "target");
limitAttributes(element, "target");
element.setName("xref");
translate(element);
} else if ("ol".equals(name)) {
// the HTML tag <ol> its translated into the DRI
// <list> tag
// <list type="ordered" n="list_part_one"
// id="css.submit.LicenseAgreement.list.list_part_one">
moveAttribute(element, "class", "rend");
limitAttributes(element, "id", "n", "rend");
element.setName("list");
element.setAttribute("type", "ordered");
translate(element);
} else if ("li".equals(name)) {
// the HTML tag <li> its translated into the DRI
// <item> tag
moveAttribute(element, "class", "rend");
limitAttributes(element, "id", "n", "rend");
element.setName("item");
translate(element);
} else if ("b".equals(name)) {
// The HTML <b> tag is translated to a highlight
// element with a rend of bold.
limitAttributes(element);
element.setName("hi");
element.setAttribute("rend", "bold");
translate(element);
} else if ("i".equals(name)) {
// The HTML <i> tag is translated to a highlight
// element with a rend of italic.
limitAttributes(element);
element.setName("hi");
element.setAttribute("rend", "italic");
translate(element);
} else if ("u".equals(name)) {
// The HTML <u> tag is translated to a highlight
// element with a rend of underline.
limitAttributes(element);
element.setName("hi");
element.setAttribute("rend", "underline");
translate(element);
} else if ("img".equals(name)) {
// The HTML <img> element is translated into a DRI figure
moveAttribute(element, "src", "source");
limitAttributes(element, "source");
element.setName("figure");
translate(element);
}
// Next all the DRI elements that we allow to pass through.
else if ("hi".equals(name)) {
limitAttributes(element, "rend");
translate(element);
} else if ("xref".equals(name)) {
limitAttributes(element, "target");
translate(element);
} else if ("figure".equals(name)) {
limitAttributes(element, "rend", "source", "target");
translate(element);
} else {
removeContent(decedent);
}
} else {
removeContent(decedent);
}
}
// Step 2:
// Ensure that all top level elements are encapusalted inside
// a block level element (i.e. a paragraph)
if (parent.isRootElement()) {
List<Content> removed = new ArrayList<Content>();
for (int i = 0; i < parent.getContentSize(); i++) {
Content current = parent.getContent(i);
if ((current instanceof Element)
&& ("p".equals(((Element) current).getName()))) {
// A paragraph is being open, combine anything up to this
// point into a paragraph.