private static List<String> findPartyIds(XmlElement party) { ImmutableList.Builder<String> builder = ImmutableList.builder(); for (XmlElement child : party.getChildren("partyId")) { if (child.hasContent()) { builder.add(child.getContent()); } } return builder.build(); }
private static XmlElement getFpmlRoot(XmlElement el) { if (el.getChildren("party").size() > 0) { if (el.getChildren("trade").size() > 0) { return el; for (XmlElement child : el.getChildren()) { if (child.getChildren("trade").size() > 0) { List<XmlElement> fakeChildren = new ArrayList<>(); fakeChildren.addAll(el.getChildren("party")); fakeChildren.addAll(child.getChildren("trade")); XmlElement fakeRoot = XmlElement.ofChildren(el.getName(), el.getAttributes(), fakeChildren); return fakeRoot; for (XmlElement child : el.getChildren()) { for (XmlElement grandchild : child.getChildren()) { if (grandchild.getChildren("trade").size() > 0) { List<XmlElement> fakeChildren = new ArrayList<>(); fakeChildren.addAll(el.getChildren("party")); fakeChildren.addAll(grandchild.getChildren("trade")); XmlElement fakeRoot = XmlElement.ofChildren(el.getName(), el.getAttributes(), fakeChildren); return fakeRoot;
private Trade parseTrade(FpmlDocument document, XmlElement tradeEl) { // find which trade type it is by comparing children to known parsers for (Entry<String, FpmlParserPlugin> entry : tradeParsers.entrySet()) { Optional<XmlElement> productOptEl = tradeEl.findChild(entry.getKey()); if (productOptEl.isPresent()) { return entry.getValue().parseTrade(document, tradeEl); } } // failed to find a known trade type ImmutableSet<String> childNames = tradeEl.getChildren().stream().map(XmlElement::getName).collect(toImmutableSet()); throw new FpmlParseException("Unknown product type: " + childNames); }
private static XmlElement findFpmlRoot(XmlElement root) { XmlElement fpmlRoot = getFpmlRoot(root); if (fpmlRoot != null) { return fpmlRoot; } // try children of root element for (XmlElement el : root.getChildren()) { fpmlRoot = getFpmlRoot(el); if (fpmlRoot != null) { return fpmlRoot; } } // try grandchildren of root element for (XmlElement el1 : root.getChildren()) { for (XmlElement el2 : el1.getChildren()) { fpmlRoot = getFpmlRoot(el2); if (fpmlRoot != null) { return fpmlRoot; } } } throw new FpmlParseException("Unable to find FpML root element"); }
/** * Parses the FpML document extracting the trades. * <p> * This parses the specified FpML root element, using the map of references. * The FpML specification uses references to link one part of the XML to another. * For example, if one part of the XML has {@code <foo id="fooId">}, the references * map will contain an entry mapping "fooId" to the parsed element {@code <foo>}. * * @param fpmlRootEl the source of the FpML XML document * @param references the map of id/href to referenced element * @return the parsed trades * @throws RuntimeException if a parse error occurred */ public List<Trade> parseTrades( XmlElement fpmlRootEl, Map<String, XmlElement> references) { FpmlDocument document = new FpmlDocument(fpmlRootEl, references, ourPartySelector, tradeInfoParser, refData); List<XmlElement> tradeEls = document.getFpmlRoot().getChildren("trade"); ImmutableList.Builder<Trade> builder = ImmutableList.builder(); for (XmlElement tradeEl : tradeEls) { builder.add(parseTrade(document, tradeEl)); } return builder.build(); }
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); }
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; }
/** * Converts an FpML 'FloatingRateIndex' with multiple tenors to an {@code Index}. * * @param baseEl the FpML floating rate index element to parse * @return the index * @throws RuntimeException if unable to parse */ public List<Index> parseIndexes(XmlElement baseEl) { XmlElement indexEl = baseEl.getChild("floatingRateIndex"); validateScheme(indexEl, "floatingRateIndexScheme", "http://www.fpml.org/coding-scheme/floating-rate-index"); FloatingRateName floatingName = FloatingRateName.of(indexEl.getContent()); List<XmlElement> tenorEls = baseEl.getChildren("indexTenor"); if (tenorEls.isEmpty()) { return ImmutableList.of(floatingName.toOvernightIndex()); } else { return tenorEls.stream() .map(el -> floatingName.toIborIndex(parseIndexTenor(el))) .collect(toImmutableList()); } }
/** * Converts an FpML 'BusinessCentersOrReference.model' to a {@code HolidayCalendar}. * * @param baseEl the FpML business centers or reference element to parse * @return the holiday calendar * @throws RuntimeException if unable to parse */ public HolidayCalendarId parseBusinessCenters(XmlElement baseEl) { // FpML content: ('businessCentersReference' | 'businessCenters') // FpML 'businessCenters' content: ('businessCenter+') // Each 'businessCenter' is a location treated as a holiday calendar Optional<XmlElement> optionalBusinessCentersEl = baseEl.findChild("businessCenters"); XmlElement businessCentersEl = optionalBusinessCentersEl.orElseGet(() -> lookupReference(baseEl.getChild("businessCentersReference"))); HolidayCalendarId calendar = HolidayCalendarIds.NO_HOLIDAYS; for (XmlElement businessCenterEl : businessCentersEl.getChildren("businessCenter")) { calendar = calendar.combinedWith(parseBusinessCenter(businessCenterEl)); } return calendar; }
private ValueSchedule parseSchedule(XmlElement scheduleEl, double initialValue, ValueStepSequence seq, FpmlDocument document) { List<XmlElement> stepEls = scheduleEl.getChildren("step"); ImmutableList.Builder<ValueStep> stepBuilder = ImmutableList.builder(); for (XmlElement stepEl : stepEls) { LocalDate stepDate = document.parseDate(stepEl.getChild("stepDate")); double stepValue = document.parseDecimal(stepEl.getChild("stepValue")); stepBuilder.add(ValueStep.of(stepDate, ValueAdjustment.ofReplace(stepValue))); } return ValueSchedule.builder().initialValue(initialValue).steps(stepBuilder.build()).stepSequence(seq).build(); }
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 void checkStubForOvernightIndex(XmlElement baseEl, FpmlDocument document, OvernightIndex index) { document.validateNotPresent(baseEl, "stubAmount"); document.validateNotPresent(baseEl, "stubRate"); List<XmlElement> indicesEls = baseEl.getChildren("floatingRate"); if (indicesEls.size() == 1) { XmlElement indexEl = indicesEls.get(0); document.validateNotPresent(indexEl, "floatingRateMultiplierSchedule"); document.validateNotPresent(indexEl, "spreadSchedule"); document.validateNotPresent(indexEl, "rateTreatment"); document.validateNotPresent(indexEl, "capRateSchedule"); document.validateNotPresent(indexEl, "floorRateSchedule"); Index parsed = document.parseIndex(indexEl); if (parsed.equals(index)) { return; } throw new FpmlParseException("OvernightIndex swap cannot have a different index in the stub: " + baseEl); } throw new FpmlParseException("Unknown stub structure: " + baseEl); }
public void test_of_ByteSource() { ByteSource source = ByteSource.wrap(SAMPLE.getBytes(StandardCharsets.UTF_8)); XmlFile test = XmlFile.of(source); XmlElement root = test.getRoot(); assertEquals(root.getName(), "base"); assertEquals(root.getAttributes(), ATTR_MAP_EMPTY); assertEquals(root.getContent(), ""); assertEquals(root.getChildren().size(), 1); XmlElement child = root.getChild(0); assertEquals(child, XmlElement.ofChildren("test", ATTR_MAP, CHILD_LIST_MULTI)); assertEquals(test.getReferences(), ImmutableMap.of()); }
public void test_of_ByteSource_parsedReferences() { ByteSource source = ByteSource.wrap(SAMPLE.getBytes(StandardCharsets.UTF_8)); XmlFile test = XmlFile.of(source, "key"); XmlElement root = test.getRoot(); assertEquals(root.getName(), "base"); assertEquals(root.getAttributes(), ATTR_MAP_EMPTY); assertEquals(root.getContent(), ""); assertEquals(root.getChildren().size(), 1); XmlElement child = root.getChild(0); assertEquals(child, XmlElement.ofChildren("test", ATTR_MAP, CHILD_LIST_MULTI)); assertEquals(test.getReferences(), ImmutableMap.of("value", root.getChild(0))); }
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)); }
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>"); }
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>"); }
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>"); }
public void test_of_ByteSource_namespace() { ByteSource source = ByteSource.wrap(SAMPLE_NAMESPACE.getBytes(StandardCharsets.UTF_8)); XmlFile test = XmlFile.of(source); XmlElement root = test.getRoot(); assertEquals(root.getName(), "base"); assertEquals(root.getAttributes(), ImmutableMap.of()); assertEquals(root.getContent(), ""); assertEquals(root.getChildren().size(), 2); XmlElement child1 = root.getChild(0); assertEquals(child1.getName(), "p"); assertEquals(child1.getContent(), "Some text"); assertEquals(child1.getAttributes(), ImmutableMap.of()); XmlElement child2 = root.getChild(1); assertEquals(child2.getName(), "leaf1"); assertEquals(child2.getContent(), "leaf"); assertEquals(child2.getAttributes(), ImmutableMap.of("foo", "bla", "og", "strata")); assertEquals(test.getReferences(), ImmutableMap.of()); }
return IborRateStubCalculation.ofKnownAmount(document.parseCurrencyAmount(amountOptEl.get())); List<XmlElement> indicesEls = baseEl.getChildren("floatingRate"); if (indicesEls.size() == 1) { XmlElement indexEl = indicesEls.get(0);