/** * Validates that the scheme attribute is known. * * @param baseEl the FpML element to parse * @param schemeAttr the scheme attribute name * @param schemeValues the scheme attribute values that are accepted * @throws FpmlParseException if the scheme does not match */ public void validateScheme(XmlElement baseEl, String schemeAttr, String... schemeValues) { if (baseEl.getAttributes().containsKey(schemeAttr)) { String scheme = baseEl.getAttribute(schemeAttr); for (String schemeValue : schemeValues) { if (scheme.startsWith(schemeValue)) { return; } } throw new FpmlParseException("Unknown '" + schemeAttr + "' attribute value: " + scheme); } }
/** * Looks up an element by href/id reference. * * @param hrefEl the element containing the href/id * @return the matched element * @throws FpmlParseException if the reference is not found */ // lookup an element via href/id reference public XmlElement lookupReference(XmlElement hrefEl) { String hrefId = hrefEl.getAttribute(HREF); XmlElement el = references.get(hrefId); if (el == null) { throw new FpmlParseException(Messages.format("Document reference not found: href='{}'", hrefId)); } return el; }
public void test_ofChildren_one() { XmlElement test = XmlElement.ofChildren("test", ATTR_MAP, CHILD_LIST_ONE); assertEquals(test.getName(), "test"); assertEquals(test.getAttributes(), ATTR_MAP); assertEquals(test.hasContent(), false); assertEquals(test.getContent(), ""); assertEquals(test.getChildren(), CHILD_LIST_ONE); assertEquals(test.getAttribute("key"), "value"); assertEquals(test.findAttribute("key"), Optional.of("value")); assertEquals(test.findAttribute("none"), Optional.empty()); assertEquals(test.getChild(0), LEAF1); assertEquals(test.getChild("leaf1"), LEAF1); assertEquals(test.findChild("leaf1"), Optional.of(LEAF1)); assertEquals(test.getChildren("leaf1"), ImmutableList.of(LEAF1)); assertEquals(test.toString(), "<test key=\"value\" og=\"strata\">" + System.lineSeparator() + " <leaf1 ... />" + System.lineSeparator() + "</test>"); }
private static ImmutableListMultimap<String, String> parseParties(XmlElement root) { ListMultimap<String, String> parties = ArrayListMultimap.create(); for (XmlElement child : root.getChildren("party")) { parties.putAll(child.getAttribute(ID), findPartyIds(child)); } return ImmutableListMultimap.copyOf(parties); }
public void test_ofChildren_empty() { XmlElement test = XmlElement.ofChildren("test", CHILD_LIST_EMPTY); assertEquals(test.getName(), "test"); assertEquals(test.getAttributes(), ATTR_MAP_EMPTY); assertEquals(test.hasContent(), false); assertEquals(test.getContent(), ""); assertEquals(test.getChildren(), CHILD_LIST_EMPTY); assertThrowsIllegalArg(() -> test.getAttribute("notFound")); assertThrows(() -> test.getChild(0), IndexOutOfBoundsException.class); assertThrowsIllegalArg(() -> test.getChild("notFound")); assertEquals(test.findChild("notFound"), Optional.empty()); assertEquals(test.getChildren("notFound"), ImmutableList.of()); assertEquals(test.toString(), "<test></test>"); }
private ListMultimap<String, StandardId> parseAllTradeIds(XmlElement tradeHeaderEl) { // look through each partyTradeIdentifier ListMultimap<String, StandardId> allIds = ArrayListMultimap.create(); List<XmlElement> partyTradeIdentifierEls = tradeHeaderEl.getChildren("partyTradeIdentifier"); for (XmlElement partyTradeIdentifierEl : partyTradeIdentifierEls) { Optional<XmlElement> partyRefOptEl = partyTradeIdentifierEl.findChild("partyReference"); if (partyRefOptEl.isPresent() && partyRefOptEl.get().findAttribute(HREF).isPresent()) { String partyHref = partyRefOptEl.get().findAttribute(HREF).get(); // try to find a tradeId, either in versionedTradeId or as a direct child Optional<XmlElement> vtradeIdOptEl = partyTradeIdentifierEl.findChild("versionedTradeId"); Optional<XmlElement> tradeIdOptEl = vtradeIdOptEl .map(vt -> Optional.of(vt.getChild("tradeId"))) .orElse(partyTradeIdentifierEl.findChild("tradeId")); if (tradeIdOptEl.isPresent() && tradeIdOptEl.get().findAttribute("tradeIdScheme").isPresent()) { XmlElement tradeIdEl = tradeIdOptEl.get(); String scheme = tradeIdEl.getAttribute("tradeIdScheme"); // ignore if there is an empty scheme or value if (!scheme.isEmpty() && !tradeIdEl.getContent().isEmpty()) { allIds.put(partyHref, StandardId.of(StandardId.encodeScheme(scheme), tradeIdEl.getContent())); } } } } return allIds; }
public void test_ofChildren_multi() { XmlElement test = XmlElement.ofChildren("test", ATTR_MAP, CHILD_LIST_MULTI); assertEquals(test.getName(), "test"); assertEquals(test.getAttributes(), ATTR_MAP); assertEquals(test.getAttribute("key"), "value"); assertEquals(test.hasContent(), false); assertEquals(test.getContent(), ""); assertEquals(test.getChildren(), CHILD_LIST_MULTI); assertEquals(test.getAttribute("key"), "value"); assertEquals(test.getChild(0), LEAF1); assertEquals(test.getChild(1), LEAF2A); assertEquals(test.getChild(2), LEAF2B); assertEquals(test.getChild("leaf1"), LEAF1); assertThrowsIllegalArg(() -> test.getChild("leaf2")); assertEquals(test.findChild("leaf1"), Optional.of(LEAF1)); assertThrowsIllegalArg(() -> test.findChild("leaf2")); assertEquals(test.getChildren("leaf1"), ImmutableList.of(LEAF1)); assertEquals(test.getChildren("leaf2"), ImmutableList.of(LEAF2A, LEAF2B)); }
/** * Converts an FpML 'BuyerSeller.model' to a {@code BuySell}. * <p> * The {@link TradeInfo} builder is updated with the counterparty. * * @param baseEl the FpML payer receiver model element * @param tradeInfoBuilder the builder of the trade info * @return the pay/receive flag * @throws RuntimeException if unable to parse */ public BuySell parseBuyerSeller(XmlElement baseEl, TradeInfoBuilder tradeInfoBuilder) { String buyerPartyReference = baseEl.getChild("buyerPartyReference").getAttribute(FpmlDocument.HREF); String sellerPartyReference = baseEl.getChild("sellerPartyReference").getAttribute(FpmlDocument.HREF); if (ourPartyHrefIds.isEmpty() || ourPartyHrefIds.contains(buyerPartyReference)) { tradeInfoBuilder.counterparty(StandardId.of(FPML_PARTY_SCHEME, parties.get(sellerPartyReference).get(0))); return BuySell.BUY; } else if (ourPartyHrefIds.contains(sellerPartyReference)) { tradeInfoBuilder.counterparty(StandardId.of(FPML_PARTY_SCHEME, parties.get(buyerPartyReference).get(0))); return BuySell.SELL; } else { throw new FpmlParseException(Messages.format( "Neither buyerPartyReference nor sellerPartyReference contain our party ID: {}", ourPartyHrefIds)); } }
public void test_ofContent() { XmlElement test = XmlElement.ofContent("test", ATTR_MAP_EMPTY, "hello"); assertEquals(test.getName(), "test"); assertEquals(test.getAttributes(), ATTR_MAP_EMPTY); assertEquals(test.hasContent(), true); assertEquals(test.getContent(), "hello"); assertEquals(test.getChildren(), CHILD_LIST_EMPTY); assertThrowsIllegalArg(() -> test.getAttribute("notFound")); assertThrows(() -> test.getChild(0), IndexOutOfBoundsException.class); assertThrowsIllegalArg(() -> test.getChild("notFound")); assertEquals(test.findChild("notFound"), Optional.empty()); assertEquals(test.getChildren("notFound"), ImmutableList.of()); assertEquals(test.toString(), "<test>hello</test>"); }
String payerPartyReference = baseEl.getChild("payerPartyReference").getAttribute(HREF); String receiverPartyReference = baseEl.getChild("receiverPartyReference").getAttribute(HREF); Object currentCounterparty = tradeInfoBuilder.build().getCounterparty().orElse(null);
public void test_ofContent_empty() { XmlElement test = XmlElement.ofContent("test", ""); assertEquals(test.getName(), "test"); assertEquals(test.getAttributes(), ATTR_MAP_EMPTY); assertEquals(test.hasContent(), false); assertEquals(test.getContent(), ""); assertEquals(test.getChildren(), CHILD_LIST_EMPTY); assertThrowsIllegalArg(() -> test.getAttribute("notFound")); assertThrows(() -> test.getChild(0), IndexOutOfBoundsException.class); assertThrowsIllegalArg(() -> test.getChild("notFound")); assertEquals(test.findChild("notFound"), Optional.empty()); assertEquals(test.getChildren("notFound"), ImmutableList.of()); assertEquals(test.toString(), "<test></test>"); }
} else { throw new FpmlParseException( "Unable to resolve 'dateRelativeTo' to a date: " + baseEl.getChild("dateRelativeTo").getAttribute(HREF));