private NodeList getValueNodes(Node operation) { List<Node> valueNL = getChildNodes(operation, "value"); if (valueNL.isEmpty()) { return null; } return valueNL.get(0).getChildNodes(); }
/** * Constructs an instance of <code>Attribute</code>. * * @param name A String representing <code>AttributeName</code> (the name of the attribute). * @param nameSpace A String representing the namespace in which <code>AttributeName</code> * elements are interpreted. * @param values A List of DOM element representing the <code>AttributeValue</code> object. * @exception SAMLException if there is an error in the sender or in the element definition. */ public Attribute(String name, String nameSpace, List values) throws SAMLException { super(name, nameSpace); if (values == null || values.isEmpty()) { if (SAMLUtilsCommon.debug.messageEnabled()) { SAMLUtilsCommon.debug.message("Attribute: AttributeValue is" + "required."); } throw new SAMLRequesterException(SAMLUtilsCommon.bundle.getString("nullInput")); } if (_attributeValue == null) { _attributeValue = new ArrayList(); } // Make sure this is a list of AttributeValue Iterator iter = values.iterator(); String tag = null; while (iter.hasNext()) { tag = ((Element) iter.next()).getLocalName(); if ((tag == null) || (!tag.equals("AttributeValue"))) { if (SAMLUtilsCommon.debug.messageEnabled()) { SAMLUtilsCommon.debug.message("AttributeValue: wrong input."); } throw new SAMLRequesterException(SAMLUtilsCommon.bundle.getString("wrongInput")); } } _attributeValue = values; }
/** Given an input string, level, and optionally a tag length, find a matching prefix. */ private PrefixMatch findPrefixMatch( String input, TagLengthList tagLength, LevelTypeList level_type) { List<PrefixMatch> match_list = new ArrayList<PrefixMatch>(); PrefixTree<PrefixMatch> tree = prefix_tree_map.get(level_type); assert tree != null; List<PrefixMatch> list = tree.search(input); if (!list.isEmpty()) { if (tagLength == null) match_list.addAll(list); else { for (PrefixMatch match : list) if (match.getScheme().getTagLength() == tagLength) match_list.add(match); } } if (match_list.isEmpty()) throw new TDTException("No schemes or levels matched the input value"); else if (match_list.size() > 1) throw new TDTException("More than one scheme/level matched the input value"); else return match_list.get(0); }
@Override protected void fillVoiceXmlDocument( Document document, Element formElement, VoiceXmlDialogueContext dialogueContext) throws VoiceXmlDocumentRenderingException { List<String> submitNameList = new ArrayList<String>(); VariableList submitVariableList = mSubmitParameters; if (submitVariableList != null) { addVariables(formElement, submitVariableList); for (Entry<String, String> entry : mSubmitParameters) { submitNameList.add(entry.getKey()); } } Element subdialogueElement = DomUtils.appendNewElement(formElement, SUBDIALOG_ELEMENT); subdialogueElement.setAttribute(NAME_ATTRIBUTE, SUBDIALOGUE_FORM_ITEM_NAME); subdialogueElement.setAttribute(SRC_ATTRIBUTE, mUri); if (!submitNameList.isEmpty()) { subdialogueElement.setAttribute(NAME_LIST_ATTRIBUTE, StringUtils.join(submitNameList, " ")); } for (Parameter parameter : mParameters) { Element paramElement = DomUtils.appendNewElement(subdialogueElement, PARAM_ELEMENT); paramElement.setAttribute(NAME_ATTRIBUTE, parameter.getName()); setAttribute(paramElement, VALUE_ATTRIBUTE, parameter.getValue()); setAttribute(paramElement, EXPR_ATTRIBUTE, parameter.getExpression()); } SubmitMethod submitMethod = mMethod; if (submitMethod != null) { subdialogueElement.setAttribute(METHOD_ATTRIBUTE, submitMethod.name()); } DocumentFetchConfiguration fetchConfiguration = mFetchConfiguration; if (fetchConfiguration != null) { applyFetchAudio(subdialogueElement, fetchConfiguration.getFetchAudio()); applyRessourceFetchConfiguration(subdialogueElement, fetchConfiguration); } Element filledElement = DomUtils.appendNewElement(subdialogueElement, FILLED_ELEMENT); createVarElement( filledElement, SUBDIALOGUE_RESULT_VARIABLE_NAME, "dialog." + SUBDIALOGUE_FORM_ITEM_NAME); if (mPostDialogueScript != null) { createScript(filledElement, mPostDialogueScript); } createScript( filledElement, RIVR_SCOPE_OBJECT + ".addValueResult(" + SUBDIALOGUE_RESULT_VARIABLE_NAME + ");"); createGotoSubmit(filledElement); }
private void processCopy(Node operation) { List<Node> fromNode = getChildNodes(operation, "from"); List<Node> toNode = getChildNodes(operation, "to"); if (fromNode.isEmpty() || toNode.isEmpty()) { return; } String path = fromNode.get(0).getTextContent(); String fromPath = (new File(path).isAbsolute()) ? path : absolutePath(path); String toPath = absolutePath(toNode.get(0).getTextContent()); Boolean copyContents = fromPath.endsWith(File.separator + "*"); File from = new File((copyContents) ? fromPath.substring(0, fromPath.length() - 2) : fromPath); File to = new File(toPath); if (!from.exists()) { Utils.onError(new Error.FileNotFound(from.getPath())); return; } try { if (copyContents) { FileUtils.forceMkdir(to); FileUtils.copyDirectory(from, to); } else if (from.isDirectory()) { FileUtils.forceMkdir(to); FileUtils.copyDirectoryToDirectory(from, to); } else { if (to.isDirectory()) { FileUtils.forceMkdir(to); FileUtils.copyFileToDirectory(from, to); } else { File toDir = new File(Utils.getParentDir(to)); FileUtils.forceMkdir(toDir); FileUtils.copyFile(from, to); } } } catch (IOException ex) { Utils.onError(new Error.FileCopy(from.getPath(), to.getPath())); } }
private void processXml(Node operation) throws ParserConfigurationException, TransformerConfigurationException { List<Node> targets = getChildNodes(operation, "target"); List<Node> appendOpNodes = getChildNodes(operation, "append"); List<Node> setOpNodes = getChildNodes(operation, "set"); List<Node> replaceOpNodes = getChildNodes(operation, "replace"); List<Node> removeOpNodes = getChildNodes(operation, "remove"); if (targets.isEmpty()) { return; } for (int t = 0; t < targets.size(); t++) { File target = new File(absolutePath(targets.get(t).getTextContent())); if (!target.exists()) { Utils.onError(new Error.FileNotFound(target.getPath())); return; } DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc = null; try { doc = db.parse(target); } catch (Exception ex) { Utils.onError(new Error.FileParse(target.getPath())); return; } for (int i = 0; i < removeOpNodes.size(); i++) removeXmlEntry(doc, removeOpNodes.get(i)); for (int i = 0; i < replaceOpNodes.size(); i++) replaceXmlEntry(doc, replaceOpNodes.get(i)); for (int i = 0; i < setOpNodes.size(); i++) setXmlEntry(doc, setOpNodes.get(i)); for (int i = 0; i < appendOpNodes.size(); i++) appendXmlEntry(doc, appendOpNodes.get(i)); try { OutputFormat format = new OutputFormat(doc); format.setOmitXMLDeclaration(true); format.setLineWidth(65); format.setIndenting(true); format.setIndent(4); Writer out = new FileWriter(target); XMLSerializer serializer = new XMLSerializer(out, format); serializer.serialize(doc); } catch (IOException e) { Utils.onError(new Error.WriteXmlConfig(target.getPath())); } } }
private NodeList findNodes(Document doc, Node operation) { List<Node> xpaths = getChildNodes(operation, "xpath"); if (xpaths.isEmpty()) { return null; } String xpathExpression = xpaths.get(0).getTextContent(); if (xpathExpression == null) { return null; } XPathFactory xPathfactory = XPathFactory.newInstance(); XPath xpath = xPathfactory.newXPath(); NodeList nl = null; try { XPathExpression expr = xpath.compile(xpathExpression); nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET); } catch (XPathExpressionException ex) { Utils.onError(new Error.WrongXpathExpression(xpathExpression)); } return nl; }
/** * Constructs an attribute element from an existing XML block. * * @param element representing a DOM tree element. * @exception SAMLException if there is an error in the sender or in the element definition. */ public Attribute(Element element) throws SAMLException { // make sure that the input xml block is not null if (element == null) { SAMLUtilsCommon.debug.message("Attribute: Input is null."); throw new SAMLRequesterException(SAMLUtilsCommon.bundle.getString("nullInput")); } // Make sure this is an Attribute. String tag = null; tag = element.getLocalName(); if ((tag == null) || (!tag.equals("Attribute"))) { SAMLUtilsCommon.debug.message("Attribute: wrong input"); throw new SAMLRequesterException(SAMLUtilsCommon.bundle.getString("wrongInput")); } int i = 0; // handle attributes NamedNodeMap atts = element.getAttributes(); int attrCount = atts.getLength(); for (i = 0; i < attrCount; i++) { Node att = atts.item(i); if (att.getNodeType() == Node.ATTRIBUTE_NODE) { String attName = att.getLocalName(); if (attName == null) { attName = att.getNodeName(); } if (attName == null || attName.length() == 0) { if (SAMLUtilsCommon.debug.messageEnabled()) { SAMLUtilsCommon.debug.message("Attribute:" + "Attribute Name is either null or empty."); } continue; // throw new SAMLRequesterException( // SAMLUtilsCommon.bundle.getString("nullInput")); } if (attName.equals("AttributeName")) { this._attributeName = ((Attr) att).getValue().trim(); } else if (attName.equals("AttributeNamespace")) { this._attributeNameSpace = ((Attr) att).getValue().trim(); } } } // AttributeName is required if (_attributeName == null || _attributeName.length() == 0) { if (SAMLUtilsCommon.debug.messageEnabled()) { SAMLUtilsCommon.debug.message("Attribute: " + "AttributeName is required attribute"); } throw new SAMLRequesterException(SAMLUtilsCommon.bundle.getString("missingAttribute")); } // AttributeNamespace is required if (_attributeNameSpace == null || _attributeNameSpace.length() == 0) { if (SAMLUtilsCommon.debug.messageEnabled()) { SAMLUtilsCommon.debug.message("Attribute: " + "AttributeNamespace is required attribute"); } throw new SAMLRequesterException(SAMLUtilsCommon.bundle.getString("missingAttribute")); } // handle the children of Attribute element NodeList nodes = element.getChildNodes(); int nodeCount = nodes.getLength(); if (nodeCount > 0) { for (i = 0; i < nodeCount; i++) { Node currentNode = nodes.item(i); if (currentNode.getNodeType() == Node.ELEMENT_NODE) { String tagName = currentNode.getLocalName(); String tagNS = currentNode.getNamespaceURI(); if ((tagName == null) || tagName.length() == 0 || tagNS == null || tagNS.length() == 0) { if (SAMLUtilsCommon.debug.messageEnabled()) { SAMLUtilsCommon.debug.message( "Attribute:" + " The tag name or tag namespace of child" + " element is either null or empty."); } throw new SAMLRequesterException(SAMLUtilsCommon.bundle.getString("nullInput")); } if (tagName.equals("AttributeValue") && tagNS.equals(SAMLConstants.assertionSAMLNameSpaceURI)) { if (_attributeValue == null) { _attributeValue = new ArrayList(); } if (!(_attributeValue.add((Element) currentNode))) { if (SAMLUtilsCommon.debug.messageEnabled()) { SAMLUtilsCommon.debug.message( "Attribute: failed to " + "add to the attribute value list."); } throw new SAMLRequesterException(SAMLUtilsCommon.bundle.getString("addListError")); } } else { if (SAMLUtilsCommon.debug.messageEnabled()) { SAMLUtilsCommon.debug.message("Attribute:" + "wrong element:" + tagName); } throw new SAMLRequesterException(SAMLUtilsCommon.bundle.getString("wrongInput")); } } // end of if (currentNode.getNodeType() == Node.ELEMENT_NODE) } // end of for loop } // end of if (nodeCount > 0) if (_attributeValue == null || _attributeValue.isEmpty()) { if (SAMLUtilsCommon.debug.messageEnabled()) { SAMLUtilsCommon.debug.message( "Attribute: " + "should contain at least one AttributeValue."); } throw new SAMLRequesterException(SAMLUtilsCommon.bundle.getString("missingElement")); } }
private void processTxt(Node operation) { List<Node> targets = getChildNodes(operation, "target"); List<Node> optionNodes = getChildNodes(operation, "opt"); List<Node> separatorNode = getChildNodes(operation, "separator"); if (targets.isEmpty() || optionNodes.isEmpty()) { return; } String defaultSeparator = "="; String globalSeparator = defaultSeparator; if (!separatorNode.isEmpty()) { globalSeparator = separatorNode.get(0).getTextContent(); if (globalSeparator.length() != 1) { globalSeparator = defaultSeparator; } } Map<String, String> options = new HashMap<String, String>(); Map<String, String> processedOptions = new HashMap<String, String>(); for (int i = 0; i < optionNodes.size(); i++) { Node option = optionNodes.get(i); String name = option.getAttributes().getNamedItem("name").getNodeValue(); String value = option.getTextContent(); if (options.containsKey(name)) { options.remove(name); } options.put(name, value); } for (int t = 0; t < targets.size(); t++) { File target = new File(absolutePath(targets.get(t).getTextContent())); File tmpFile = new File(Utils.timestamp()); BufferedWriter bw = null; BufferedReader br = null; try { Node separatorAttr = targets.get(t).getAttributes().getNamedItem("separator"); String separator = (separatorAttr == null) ? globalSeparator : separatorAttr.getNodeValue(); if (separator.length() != 1) { separator = globalSeparator; } bw = new BufferedWriter(new FileWriter(tmpFile)); if (target.exists()) { br = new BufferedReader(new FileReader(target)); for (String line; (line = br.readLine()) != null; ) { String[] parts = line.split(separator); if (parts.length < 2) { bw.write(line); bw.newLine(); continue; } String optName = parts[0].trim(); if (options.containsKey(optName)) { String optValue = options.get(optName); bw.write(optName + " " + separator + " " + optValue); bw.newLine(); processedOptions.put(optName, optValue); options.remove(optName); } else if (processedOptions.containsKey(optName)) { bw.write(optName + " " + separator + " " + processedOptions.get(optName)); bw.newLine(); } else { bw.write(line); bw.newLine(); } } br.close(); } for (Map.Entry<String, String> entry : options.entrySet()) { bw.write(entry.getKey() + " " + separator + " " + entry.getValue()); bw.newLine(); } bw.close(); FileUtils.copyFile(tmpFile, target); FileUtils.forceDelete(tmpFile); } catch (IOException ex) { Utils.onError(new Error.WriteTxtConfig(target.getPath())); } } }
public void generateSubAwardLineItems(BudgetSubAwards subAward, Budget budget) { BudgetDecimal amountChargeFA = new BudgetDecimal(25000); String directLtCostElement = getParameterService() .getParameterValueAsString( BudgetDocument.class, Constants.SUBCONTRACTOR_DIRECT_LT_25K_PARAM); String directGtCostElement = getParameterService() .getParameterValueAsString( BudgetDocument.class, Constants.SUBCONTRACTOR_DIRECT_GT_25K_PARAM); String inDirectLtCostElement = getParameterService() .getParameterValueAsString( BudgetDocument.class, Constants.SUBCONTRACTOR_F_AND_A_LT_25K_PARAM); String inDirectGtCostElement = getParameterService() .getParameterValueAsString( BudgetDocument.class, Constants.SUBCONTRACTOR_F_AND_A_GT_25K_PARAM); for (BudgetSubAwardPeriodDetail detail : subAward.getBudgetSubAwardPeriodDetails()) { BudgetPeriod budgetPeriod = findBudgetPeriod(detail, budget); List<BudgetLineItem> currentLineItems = findSubAwardLineItems(budgetPeriod, subAward.getSubAwardNumber()); // zero out existing line items before recalculating for (BudgetLineItem item : currentLineItems) { item.setDirectCost(BudgetDecimal.ZERO); item.setCostSharingAmount(BudgetDecimal.ZERO); item.setSubAwardNumber(subAward.getSubAwardNumber()); item.setLineItemDescription(subAward.getOrganizationName()); } if (BudgetDecimal.returnZeroIfNull(detail.getDirectCost()).isNonZero() || hasBeenChanged(detail, true)) { BudgetDecimal ltValue = lesserValue(detail.getDirectCost(), amountChargeFA); BudgetDecimal gtValue = detail.getDirectCost().subtract(ltValue); if (ltValue.isNonZero()) { BudgetLineItem lt = findOrCreateLineItem( currentLineItems, detail, subAward, budgetPeriod, directLtCostElement); lt.setLineItemCost(ltValue); } if (gtValue.isNonZero()) { BudgetLineItem gt = findOrCreateLineItem( currentLineItems, detail, subAward, budgetPeriod, directGtCostElement); gt.setLineItemCost(gtValue); } amountChargeFA = amountChargeFA.subtract(ltValue); } if (BudgetDecimal.returnZeroIfNull(detail.getIndirectCost()).isNonZero() || hasBeenChanged(detail, false)) { BudgetDecimal ltValue = lesserValue(detail.getIndirectCost(), amountChargeFA); BudgetDecimal gtValue = detail.getIndirectCost().subtract(ltValue); if (ltValue.isNonZero()) { BudgetLineItem lt = findOrCreateLineItem( currentLineItems, detail, subAward, budgetPeriod, inDirectLtCostElement); lt.setLineItemCost(ltValue); } if (gtValue.isNonZero()) { BudgetLineItem gt = findOrCreateLineItem( currentLineItems, detail, subAward, budgetPeriod, inDirectGtCostElement); gt.setLineItemCost(gtValue); } amountChargeFA = amountChargeFA.subtract(ltValue); } Collections.sort( currentLineItems, new Comparator<BudgetLineItem>() { public int compare(BudgetLineItem arg0, BudgetLineItem arg1) { return arg0.getLineItemNumber().compareTo(arg1.getLineItemNumber()); } }); Iterator<BudgetLineItem> iter = currentLineItems.iterator(); while (iter.hasNext()) { BudgetLineItem lineItem = iter.next(); if (BudgetDecimal.returnZeroIfNull(lineItem.getLineItemCost()).isZero()) { budgetPeriod.getBudgetLineItems().remove(lineItem); iter.remove(); } else { if (!budgetPeriod.getBudgetLineItems().contains(lineItem)) { budgetPeriod.getBudgetLineItems().add(lineItem); } } } if (!currentLineItems.isEmpty() && BudgetDecimal.returnZeroIfNull(detail.getCostShare()).isNonZero()) { currentLineItems.get(0).setCostSharingAmount(detail.getCostShare()); } } }