// Helper method for XML.hasSimpleContent() boolean hasChildElement() { org.w3c.dom.NodeList nodes = this.dom.getChildNodes(); for (int i = 0; i < nodes.getLength(); i++) { if (nodes.item(i).getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) return true; } return false; }
int getChildIndex() { if (this.isAttributeType()) return -1; if (parent() == null) return -1; org.w3c.dom.NodeList siblings = this.dom.getParentNode().getChildNodes(); for (int i = 0; i < siblings.getLength(); i++) { if (siblings.item(i) == dom) { return i; } } // Either the parent is -1 or one of the this node's parent's children is this node. throw new RuntimeException("Unreachable."); }
/** updates the XMl with hashcode for the files */ 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 = 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; }
@Override public void onChange(String resource) { try { LOG.debug("Reading " + resource); HashMap<CommandExecutor.Method, Stage> newexecutorsMap = new HashMap<CommandExecutor.Method, Stage>(); Document document = getResourceLoader().getDocument(resource); Map<Stage, Integer> totals = new EnumMap<Stage, Integer>(Stage.class); if (document != null) { org.w3c.dom.NodeList ellist = document.getDocumentElement().getChildNodes(); Stage prevStage = Stage.RECOGNIZER; for (int i = 0; i <= ellist.getLength(); i++) { if (ellist.item(i) instanceof Element) { Element el = (Element) ellist.item(i); if (el.getTagName().equals("localhost")) { int max = Integer.parseInt(el.getAttribute("max_simultaneous_transcoders")); Stage s = Stage.valueOf(el.getAttribute("stage").toUpperCase()); Integer t = totals.get(s); if (t == null) t = 0; t += max; totals.put(s, t); for (int j = 1; j <= max; j++) { newexecutorsMap.put(new CommandExecutor.Method(), s); } } else if (el.getTagName().equals("server")) { int max = Integer.parseInt(el.getAttribute("max_simultaneous_transcoders")); Stage s = Stage.valueOf(el.getAttribute("stage").toUpperCase()); Integer t = totals.get(s); if (t == null) t = 0; t += max; totals.put(s, t); String host = el.getAttribute("host"); int port = Integer.parseInt(el.getAttribute("port")); for (int j = 1; j <= max; j++) { newexecutorsMap.put(new CommandExecutor.Method(host, port), s); } } } } for (Map.Entry<Stage, Integer> e : totals.entrySet()) { threadPools.get(e.getKey()).setCorePoolSize(e.getValue()); threadPools.get(e.getKey()).setMaximumPoolSize(e.getValue()); } } else { LOG.warn("No " + resource); } synchronized (executorsMap) { executorsMap.clear(); executorsMap.putAll(newexecutorsMap); } LOG.service( "Reading of configuration file " + resource + " successfull. Executors " + executorsMap + ". Max simultaneous transcoders: " + totals); } catch (Exception e) { LOG.error( e.getClass() + " " + e.getMessage() + " In " + resource + " Executors now " + executorsMap + " (not changed)", e); } }