// Walk the tree from a specified element, // inserting data from a DicomObject where required. private static String getElementText(Element element, DicomObject dicomObject) { if (dicomTag(element)) return getDicomElementText(element, dicomObject); if (element.getTagName().equals("block")) return getBlockText(element, dicomObject); StringBuffer sb = new StringBuffer(); sb.append("<" + element.getTagName()); NamedNodeMap attributes = element.getAttributes(); Attr attr; for (int i = 0; i < attributes.getLength(); i++) { attr = (Attr) attributes.item(i); String attrValue = attr.getValue().trim(); if (dicomTag(attrValue)) { attrValue = getDicomElementText(attrValue, dicomObject); } sb.append(" " + attr.getName() + "=\"" + attrValue + "\""); } sb.append(">"); if (element.getTagName().equals("table")) sb.append(getTableText(element, dicomObject)); else if (element.getTagName().equals("publication-date")) sb.append(StringUtil.getDate()); else sb.append(getChildrenText(element, dicomObject)); sb.append("</" + element.getTagName() + ">"); return sb.toString(); }
// Return all the text from the child nodes of an element, // inserting data from the DicomObject where required. private static String getChildrenText(Element element, DicomObject dicomObject) { StringBuffer sb = new StringBuffer(); Node node; NodeList nodeList = element.getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { node = nodeList.item(i); if (node.getNodeType() == Node.TEXT_NODE) { sb.append(node.getNodeValue()); } else if (node.getNodeType() == Node.ELEMENT_NODE) { sb.append(getElementText((Element) node, dicomObject)); } } return sb.toString(); }
// Handle HTML table elements specially, automatically // inserting the DICOM element name and the value for each child element. private static String getTableText(Element element, DicomObject dicomObject) { StringBuffer sb = new StringBuffer(); Node node; Element elem; NodeList nodeList = element.getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { node = nodeList.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { elem = (Element) node; if (dicomTag(elem)) { sb.append("\n<tr>"); sb.append("<td width=\"200\">" + getDicomElementName(elem) + "</td>"); sb.append("<td>" + getDicomElementText(elem, dicomObject) + "</td>"); sb.append("</tr>"); } else sb.append(getElementText(elem, dicomObject)); } } return sb.toString(); }
// Process a block element, inserting its contents before the element itself, // allowing multiple instances of the element to be created, one for each DicomObject. private static String getBlockText(Element element, DicomObject dicomObject) { StringBuffer sb = new StringBuffer(); sb.append(getChildrenText(element, dicomObject)); sb.append(XmlUtil.toString(element)); return sb.toString(); }