List of usage examples for org.w3c.dom Node setNodeValue
public void setNodeValue(String nodeValue) throws DOMException;
From source file:org.kuali.coeus.propdev.impl.budget.subaward.PropDevPropDevBudgetSubAwardServiceImpl.java
/** * updates the XMl with hashcode for the files *///from w w w. j ava2 s. c o m protected BudgetSubAwards updateXML(byte xmlContents[], Map fileMap, BudgetSubAwards budgetSubAwardBean, Budget budget) throws Exception { javax.xml.parsers.DocumentBuilderFactory domParserFactory = javax.xml.parsers.DocumentBuilderFactory .newInstance(); javax.xml.parsers.DocumentBuilder domParser = domParserFactory.newDocumentBuilder(); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xmlContents); org.w3c.dom.Document document = domParser.parse(byteArrayInputStream); byteArrayInputStream.close(); String namespace = null; String formName = null; if (document != null) { Node node; Element element = document.getDocumentElement(); NamedNodeMap map = element.getAttributes(); String namespaceHolder = element.getNodeName().substring(0, element.getNodeName().indexOf(':')); node = map.getNamedItem("xmlns:" + namespaceHolder); namespace = node.getNodeValue(); FormMappingInfo formMappingInfo = formMappingService.getFormInfo(namespace); formName = formMappingInfo.getFormName(); budgetSubAwardBean.setNamespace(namespace); budgetSubAwardBean.setFormName(formName); } String xpathEmptyNodes = "//*[not(node()) and local-name(.) != 'FileLocation' and local-name(.) != 'HashValue']"; String xpathOtherPers = "//*[local-name(.)='ProjectRole' and local-name(../../.)='OtherPersonnel' and count(../NumberOfPersonnel)=0]"; removeAllEmptyNodes(document, xpathEmptyNodes, 0); removeAllEmptyNodes(document, xpathOtherPers, 1); removeAllEmptyNodes(document, xpathEmptyNodes, 0); changeDataTypeForNumberOfOtherPersons(document); List<String> fedNonFedSubAwardForms = getFedNonFedSubawardForms(); NodeList budgetYearList = XPathAPI.selectNodeList(document, "//*[local-name(.) = 'BudgetYear']"); for (int i = 0; i < budgetYearList.getLength(); i++) { Node bgtYearNode = budgetYearList.item(i); String period = getValue(XPathAPI.selectSingleNode(bgtYearNode, "BudgetPeriod")); if (fedNonFedSubAwardForms.contains(namespace)) { Element newBudgetYearElement = copyElementToName((Element) bgtYearNode, bgtYearNode.getNodeName()); bgtYearNode.getParentNode().replaceChild(newBudgetYearElement, bgtYearNode); } else { Element newBudgetYearElement = copyElementToName((Element) bgtYearNode, bgtYearNode.getNodeName() + period); bgtYearNode.getParentNode().replaceChild(newBudgetYearElement, bgtYearNode); } } Node oldroot = document.removeChild(document.getDocumentElement()); Node newroot = document.appendChild(document.createElement("Forms")); newroot.appendChild(oldroot); org.w3c.dom.NodeList lstFileName = document.getElementsByTagName("att:FileName"); org.w3c.dom.NodeList lstFileLocation = document.getElementsByTagName("att:FileLocation"); org.w3c.dom.NodeList lstMimeType = document.getElementsByTagName("att:MimeType"); org.w3c.dom.NodeList lstHashValue = document.getElementsByTagName("glob:HashValue"); org.w3c.dom.Node fileNode, hashNode, mimeTypeNode; org.w3c.dom.NamedNodeMap fileNodeMap, hashNodeMap; String fileName; byte fileBytes[]; String contentId; List attachmentList = new ArrayList(); for (int index = 0; index < lstFileName.getLength(); index++) { fileNode = lstFileName.item(index); Node fileNameNode = fileNode.getFirstChild(); fileName = fileNameNode.getNodeValue(); fileBytes = (byte[]) fileMap.get(fileName); if (fileBytes == null) { throw new RuntimeException("FileName mismatch in XML and PDF extracted file"); } String hashVal = grantApplicationHashService.computeAttachmentHash(fileBytes); hashNode = lstHashValue.item(index); hashNodeMap = hashNode.getAttributes(); Node temp = document.createTextNode(hashVal); hashNode.appendChild(temp); hashNode = hashNodeMap.getNamedItem("glob:hashAlgorithm"); hashNode.setNodeValue(InfastructureConstants.HASH_ALGORITHM); fileNode = lstFileLocation.item(index); fileNodeMap = fileNode.getAttributes(); fileNode = fileNodeMap.getNamedItem("att:href"); contentId = fileNode.getNodeValue(); String encodedContentId = cleanContentId(contentId); fileNode.setNodeValue(encodedContentId); mimeTypeNode = lstMimeType.item(0); String contentType = mimeTypeNode.getFirstChild().getNodeValue(); BudgetSubAwardAttachment budgetSubAwardAttachmentBean = new BudgetSubAwardAttachment(); budgetSubAwardAttachmentBean.setData(fileBytes); budgetSubAwardAttachmentBean.setName(encodedContentId); budgetSubAwardAttachmentBean.setType(contentType); budgetSubAwardAttachmentBean.setBudgetSubAward(budgetSubAwardBean); attachmentList.add(budgetSubAwardAttachmentBean); } budgetSubAwardBean.setBudgetSubAwardAttachments(attachmentList); javax.xml.transform.Transformer transformer = javax.xml.transform.TransformerFactory.newInstance() .newTransformer(); transformer.setOutputProperty(javax.xml.transform.OutputKeys.INDENT, "yes"); ByteArrayOutputStream bos = new ByteArrayOutputStream(); javax.xml.transform.stream.StreamResult result = new javax.xml.transform.stream.StreamResult(bos); javax.xml.transform.dom.DOMSource source = new javax.xml.transform.dom.DOMSource(document); transformer.transform(source, result); budgetSubAwardBean.setSubAwardXmlFileData(new String(bos.toByteArray())); bos.close(); return budgetSubAwardBean; }
From source file:org.kuali.coeus.propdev.impl.budget.subaward.PropDevPropDevBudgetSubAwardServiceImpl.java
private void setValue(Node node, String value) { Node child = null; for (child = node.getFirstChild(); child != null; child = child.getNextSibling()) { if (child.getNodeType() == Node.TEXT_NODE) { child.setNodeValue(value); break; }//from w w w . j av a 2 s . c o m } }
From source file:org.kuali.kra.proposaldevelopment.budget.service.impl.BudgetSubAwardServiceImpl.java
/** * updates the XMl with hashcode for the files *///from w w w . ja v a 2 s. c o m protected BudgetSubAwards updateXML(byte xmlContents[], Map fileMap, BudgetSubAwards budgetSubAwardBean) throws Exception { javax.xml.parsers.DocumentBuilderFactory domParserFactory = javax.xml.parsers.DocumentBuilderFactory .newInstance(); javax.xml.parsers.DocumentBuilder domParser = domParserFactory.newDocumentBuilder(); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xmlContents); org.w3c.dom.Document document = domParser.parse(byteArrayInputStream); byteArrayInputStream.close(); String namespace = null; if (document != null) { Node node; String formName; Element element = document.getDocumentElement(); NamedNodeMap map = element.getAttributes(); String namespaceHolder = element.getNodeName().substring(0, element.getNodeName().indexOf(':')); node = map.getNamedItem("xmlns:" + namespaceHolder); namespace = node.getNodeValue(); FormMappingInfo formMappingInfo = new FormMappingLoader().getFormInfo(namespace); formName = formMappingInfo.getFormName(); budgetSubAwardBean.setNamespace(namespace); budgetSubAwardBean.setFormName(formName); } String xpathEmptyNodes = "//*[not(node()) and local-name(.) != 'FileLocation' and local-name(.) != 'HashValue']"; String xpathOtherPers = "//*[local-name(.)='ProjectRole' and local-name(../../.)='OtherPersonnel' and count(../NumberOfPersonnel)=0]"; removeAllEmptyNodes(document, xpathEmptyNodes, 0); removeAllEmptyNodes(document, xpathOtherPers, 1); removeAllEmptyNodes(document, xpathEmptyNodes, 0); changeDataTypeForNumberOfOtherPersons(document); List<String> fedNonFedSubAwardForms = getFedNonFedSubawardForms(); NodeList budgetYearList = XPathAPI.selectNodeList(document, "//*[local-name(.) = 'BudgetYear']"); for (int i = 0; i < budgetYearList.getLength(); i++) { Node bgtYearNode = budgetYearList.item(i); String period = getValue(XPathAPI.selectSingleNode(bgtYearNode, "BudgetPeriod")); if (fedNonFedSubAwardForms.contains(namespace)) { Element newBudgetYearElement = copyElementToName((Element) bgtYearNode, bgtYearNode.getNodeName()); bgtYearNode.getParentNode().replaceChild(newBudgetYearElement, bgtYearNode); } else { Element newBudgetYearElement = copyElementToName((Element) bgtYearNode, bgtYearNode.getNodeName() + period); bgtYearNode.getParentNode().replaceChild(newBudgetYearElement, bgtYearNode); } } Node oldroot = document.removeChild(document.getDocumentElement()); Node newroot = document.appendChild(document.createElement("Forms")); newroot.appendChild(oldroot); org.w3c.dom.NodeList lstFileName = document.getElementsByTagName("att:FileName"); org.w3c.dom.NodeList lstFileLocation = document.getElementsByTagName("att:FileLocation"); org.w3c.dom.NodeList lstMimeType = document.getElementsByTagName("att:MimeType"); org.w3c.dom.NodeList lstHashValue = document.getElementsByTagName("glob:HashValue"); if ((lstFileName.getLength() != lstFileLocation.getLength()) || (lstFileLocation.getLength() != lstHashValue.getLength())) { // throw new RuntimeException("Tag occurances mismatch in XML File"); } org.w3c.dom.Node fileNode, hashNode, mimeTypeNode; org.w3c.dom.NamedNodeMap fileNodeMap, hashNodeMap; String fileName; byte fileBytes[]; String contentId; List attachmentList = new ArrayList(); for (int index = 0; index < lstFileName.getLength(); index++) { fileNode = lstFileName.item(index); Node fileNameNode = fileNode.getFirstChild(); fileName = fileNameNode.getNodeValue(); fileBytes = (byte[]) fileMap.get(fileName); if (fileBytes == null) { throw new RuntimeException("FileName mismatch in XML and PDF extracted file"); } String hashVal = GrantApplicationHash.computeAttachmentHash(fileBytes); hashNode = lstHashValue.item(index); hashNodeMap = hashNode.getAttributes(); Node temp = document.createTextNode(hashVal); hashNode.appendChild(temp); hashNode = hashNodeMap.getNamedItem("glob:hashAlgorithm"); hashNode.setNodeValue(S2SConstants.HASH_ALGORITHM); fileNode = lstFileLocation.item(index); fileNodeMap = fileNode.getAttributes(); fileNode = fileNodeMap.getNamedItem("att:href"); contentId = fileNode.getNodeValue(); String encodedContentId = cleanContentId(contentId); fileNode.setNodeValue(encodedContentId); mimeTypeNode = lstMimeType.item(0); String contentType = mimeTypeNode.getFirstChild().getNodeValue(); BudgetSubAwardAttachment budgetSubAwardAttachmentBean = new BudgetSubAwardAttachment(); budgetSubAwardAttachmentBean.setAttachment(fileBytes); budgetSubAwardAttachmentBean.setContentId(encodedContentId); budgetSubAwardAttachmentBean.setContentType(contentType); budgetSubAwardAttachmentBean.setBudgetId(budgetSubAwardBean.getBudgetId()); budgetSubAwardAttachmentBean.setSubAwardNumber(budgetSubAwardBean.getSubAwardNumber()); attachmentList.add(budgetSubAwardAttachmentBean); } budgetSubAwardBean.setBudgetSubAwardAttachments(attachmentList); javax.xml.transform.Transformer transformer = javax.xml.transform.TransformerFactory.newInstance() .newTransformer(); transformer.setOutputProperty(javax.xml.transform.OutputKeys.INDENT, "yes"); ByteArrayOutputStream bos = new ByteArrayOutputStream(); javax.xml.transform.stream.StreamResult result = new javax.xml.transform.stream.StreamResult(bos); javax.xml.transform.dom.DOMSource source = new javax.xml.transform.dom.DOMSource(document); transformer.transform(source, result); budgetSubAwardBean.setSubAwardXmlFileData(new String(bos.toByteArray())); bos.close(); return budgetSubAwardBean; }
From source file:org.kuali.rice.core.api.util.xml.XmlHelper.java
private static void trimElement(Node node) throws SAXException, IOException, ParserConfigurationException { if (node.hasChildNodes()) { NodeList children = node.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); if (child != null) { trimElement(child);// w w w . ja va 2 s . c om } } } else { if (node.getNodeType() == Node.TEXT_NODE) { String text = node.getNodeValue(); text = StringUtils.isEmpty(text) ? "" : text.trim(); node.setNodeValue(text); } } }
From source file:org.nimbustools.messaging.gt4_0_elastic.rpc.RequestEngine.java
protected void handleCreatedOrExpires(Node node, String name) { final Node firstChild = node.getFirstChild(); final String firstChildNodeValue = firstChild.getNodeValue(); if (firstChildNodeValue == null) { logger.warn("Timestamp with null value for " + name); } else {// www .ja v a 2s . c o m final String newValue = this.chop(firstChildNodeValue); if (newValue != null) { firstChild.setNodeValue(newValue); } } }
From source file:org.omegat.filters.TestFilterBase.java
/** * Remove version and toolname, then compare. *//*ww w. j a v a2 s . c o m*/ protected void compareTMX(File f1, File f2) throws Exception { XPathExpression exprVersion = XPathFactory.newInstance().newXPath() .compile("/tmx/header/@creationtoolversion"); XPathExpression exprTool = XPathFactory.newInstance().newXPath().compile("/tmx/header/@creationtool"); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder builder = factory.newDocumentBuilder(); builder.setEntityResolver(TMXReader2.TMX_DTD_RESOLVER); Document doc1 = builder.parse(f1); Document doc2 = builder.parse(f2); Node n; n = (Node) exprVersion.evaluate(doc1, XPathConstants.NODE); n.setNodeValue(""); n = (Node) exprVersion.evaluate(doc2, XPathConstants.NODE); n.setNodeValue(""); n = (Node) exprTool.evaluate(doc1, XPathConstants.NODE); n.setNodeValue(""); n = (Node) exprTool.evaluate(doc2, XPathConstants.NODE); n.setNodeValue(""); assertXMLEqual(doc1, doc2); }
From source file:org.opencastproject.mediapackage.MediaPackageBuilderImpl.java
/** * Rewrite the url elements using the serializer. Attention: This method modifies the given DOM! *//* w ww .j a va2 s. co m*/ private static void rewriteUrls(Node xml, MediaPackageSerializer serializer) throws XPathExpressionException, URISyntaxException { XPath xPath = XPathFactory.newInstance().newXPath(); NodeList nodes = (NodeList) xPath.evaluate("//*[local-name() = 'url']", xml, XPathConstants.NODESET); for (int i = 0; i < nodes.getLength(); i++) { Node uri = nodes.item(i).getFirstChild(); if (uri != null) uri.setNodeValue(serializer.resolvePath(uri.getNodeValue()).toString()); } }
From source file:org.openestate.io.core.XmlUtils.java
/** * Recursively remove any comments and unnecessary white spaces from a * {@link Node} and its children./*from w ww . j av a 2 s . c o m*/ * * @param node * the node to clean */ public static void clean(Node node) { NodeList childNodes = node.getChildNodes(); for (int n = childNodes.getLength() - 1; n >= 0; n--) { Node child = childNodes.item(n); short nodeType = child.getNodeType(); if (nodeType == Node.ELEMENT_NODE) { XmlUtils.clean(child); } else if (nodeType == Node.COMMENT_NODE) { node.removeChild(child); } else if (nodeType == Node.TEXT_NODE) { String value = StringUtils.trimToNull(child.getNodeValue()); if (value == null) node.removeChild(child); else child.setNodeValue(value); } } }
From source file:org.openmrs.projectbuendia.webservices.rest.XmlTestUtil.java
/** * Converts an XML document into a string, applying indentation. First all elements have * their text//w w w .j a v a2 s. c o m * content trimmed, just for simplicity. */ static String toIndentedString(Document doc) throws TransformerException { for (Element element : toElementIterable(doc.getElementsByTagName("*"))) { for (Node node : toIterable(element.getChildNodes())) { if (node.getNodeType() == Node.TEXT_NODE) { node.setNodeValue(node.getNodeValue().trim()); } } } Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); StringWriter outStream = new StringWriter(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(outStream); transformer.transform(source, result); return outStream.toString(); }
From source file:org.opensingular.internal.lib.commons.xml.MElement.java
/** * Atualiza o Node (Element ou atributo) j exisitente. Se o Node no for * localizado, ento adiciona se o valor for diferente de null. * * @param xPath Caminho do Node a ser atualizado ou criado * @param value Novo valor do Node. Se for null, ento o valor limpo. Se o * element j existir e valor for null, ento transforma a tag em * empty, mas a mantm no XML. * @return O Node alterado ou criado, ou null se no for possvel atualizar * o valor do mesmo.//from w w w.j a v a 2s . co m */ public final Node updateNode(String xPath, String value) { Node n = getNode(xPath); if ((n == null) && !StringUtils.isEmpty(value)) { return addElement(xPath, value); } else if (n instanceof Element) { Node filho = n.getFirstChild(); if (filho == null) { if (!StringUtils.isEmpty(value)) { Document d = n.getOwnerDocument(); Text txt = d.createTextNode(value); n.appendChild(txt); } } else if (XmlUtil.isNodeTypeText(filho)) { if (!StringUtils.isEmpty(value)) { filho.setNodeValue(value); } else { n.removeChild(filho); } } else { return null; } } else if (n instanceof Attr) { //No h como saber quem o pai (n.getParentNode() retorna null) if (value == null) { return addElement(xPath, ""); //Fora a remoo do atributo } return addElement(xPath, value); } else { return null; } return n; }