List of usage examples for org.w3c.dom Element getClass
@HotSpotIntrinsicCandidate public final native Class<?> getClass();
From source file:org.openmrs.module.drawing.obs.handler.DrawingHandler.java
public void saveAnnotation(Obs obs, ImageAnnotation annotation, boolean delete) { try {//from www . j a v a 2s . c o m log.info("drawing: Saving annotation for obs " + obs.getObsId()); File metadataFile = getComplexMetadataFile(obs); log.info("drawing: Using file " + metadataFile.getCanonicalPath()); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document xmldoc; Element annotationsParent; int newId = 0; if (metadataFile.exists()) { xmldoc = builder.parse(metadataFile); annotationsParent = (Element) xmldoc.getElementsByTagName("Annotations").item(0); NodeList annotationNodeList = xmldoc.getElementsByTagName("Annotation"); for (int i = 0; i < annotationNodeList.getLength(); i++) { NamedNodeMap attributes = annotationNodeList.item(i).getAttributes(); String idString = attributes.getNamedItem("id").getNodeValue(); int existingId = Integer.parseInt(idString); if (existingId == annotation.getId() && !(annotation.getStatus() == Status.UNCHANGED)) { annotationsParent.removeChild(annotationNodeList.item(i)); break; } if (existingId >= newId) newId = existingId + 1; } } else { metadataFile.createNewFile(); DOMImplementation domImpl = builder.getDOMImplementation(); xmldoc = domImpl.createDocument(null, "ImageMetadata", null); Element root = xmldoc.getDocumentElement(); annotationsParent = xmldoc.createElementNS(null, "Annotations"); root.appendChild(annotationsParent); } if (!delete && annotation.getStatus() != Status.UNCHANGED) { if (annotation.getId() >= 0) newId = annotation.getId(); Element e = xmldoc.createElementNS(null, "Annotation"); Node n = xmldoc.createTextNode(annotation.getText()); e.setAttributeNS(null, "id", newId + ""); e.setAttributeNS(null, "xcoordinate", annotation.getLocation().getX() + ""); e.setAttributeNS(null, "ycoordinate", annotation.getLocation().getY() + ""); e.setAttributeNS(null, "userid", annotation.getUser().getUserId() + ""); e.setAttributeNS(null, "date", annotation.getDate().getTime() + ""); e.appendChild(n); annotationsParent.appendChild(e); } Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF8"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(new DOMSource(xmldoc), new StreamResult(metadataFile)); log.info("drawing: Saving annotation complete"); } catch (Exception e) { log.error("drawing: Error saving image metadata: " + e.getClass() + " " + e.getMessage()); } }