public void noTrades() { XmlElement rootEl = XmlElement.ofChildren("dataDocument", ImmutableList.of()); List<Trade> trades = FpmlDocumentParser.of(FpmlPartySelector.any()).parseTrades(rootEl, ImmutableMap.of()); assertEquals(trades.size(), 0); }
/** * Obtains an instance of the parser, based on the specified selector and plugins. * <p> * The FpML parser has a number of plugin points that can be controlled: * <ul> * <li>the {@linkplain FpmlPartySelector party selector} * <li>the {@linkplain FpmlTradeInfoParserPlugin trade info parser} * <li>the {@linkplain FpmlParserPlugin trade parsers} * <li>the {@linkplain ReferenceData reference data} * </ul> * * @param ourPartySelector the selector used to find "our" party within the set of parties in the FpML document * @param tradeInfoParser the trade info parser * @param tradeParsers the map of trade parsers, keyed by the FpML element name * @param refData the reference data to use * @return the document parser */ public static FpmlDocumentParser of( FpmlPartySelector ourPartySelector, FpmlTradeInfoParserPlugin tradeInfoParser, Map<String, FpmlParserPlugin> tradeParsers, ReferenceData refData) { return new FpmlDocumentParser(ourPartySelector, tradeInfoParser, tradeParsers, refData); }
/** * Parses FpML from the specified source, extracting the trades. * <p> * This parses the specified byte source which must be an XML document. * <p> * Sometimes, the FpML document is embedded in a non-FpML wrapper. * This method will intelligently find the FpML document at the root or within one or two levels * of wrapper by searching for an element that contains both {@code <trade>} and {@code <party>}. * * @param source the source of the FpML XML document * @return the parsed trades * @throws RuntimeException if a parse error occurred */ public List<Trade> parseTrades(ByteSource source) { XmlFile xmlFile = XmlFile.of(source, FpmlDocument.ID); XmlElement root = findFpmlRoot(xmlFile.getRoot()); return parseTrades(root, xmlFile.getReferences()); }
/** * Obtains an instance of the parser, based on the specified selector. * <p> * The FpML parser has a number of plugin points that can be controlled: * <ul> * <li>the {@linkplain FpmlPartySelector party selector} * <li>the {@linkplain FpmlTradeInfoParserPlugin trade info parser} * <li>the {@linkplain FpmlParserPlugin trade parsers} * <li>the {@linkplain ReferenceData reference data} * </ul> * This method uses the {@linkplain FpmlTradeInfoParserPlugin#standard() standard} * trade info parser, the trade parsers registered in {@link FpmlParserPlugin} * configuration and the {@linkplain ReferenceData#standard() standard} reference data. * * @param ourPartySelector the selector used to find "our" party within the set of parties in the FpML document * @return the document parser */ public static FpmlDocumentParser of(FpmlPartySelector ourPartySelector) { return of(ourPartySelector, FpmlTradeInfoParserPlugin.standard()); }
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(); }
/** * Obtains an instance of the parser, based on the specified selector and plugins. * <p> * The FpML parser has a number of plugin points that can be controlled: * <ul> * <li>the {@linkplain FpmlPartySelector party selector} * <li>the {@linkplain FpmlTradeInfoParserPlugin trade info parser} * <li>the {@linkplain FpmlParserPlugin trade parsers} * <li>the {@linkplain ReferenceData reference data} * </ul> * This method uses the {@linkplain ReferenceData#standard() standard} reference data. * * @param ourPartySelector the selector used to find "our" party within the set of parties in the FpML document * @param tradeInfoParser the trade info parser * @param tradeParsers the map of trade parsers, keyed by the FpML element name * @return the document parser */ public static FpmlDocumentParser of( FpmlPartySelector ourPartySelector, FpmlTradeInfoParserPlugin tradeInfoParser, Map<String, FpmlParserPlugin> tradeParsers) { return of(ourPartySelector, tradeInfoParser, tradeParsers, ReferenceData.standard()); }
@Test(dataProvider = "parse") public void parse(String location) { ByteSource resource = ResourceLocator.of(location).getByteSource(); List<Trade> trades = FpmlDocumentParser.of(FpmlPartySelector.matching("Party2")).parseTrades(resource); assertEquals(trades.size(), 1); }
/** * Obtains an instance of the parser, based on the specified selector and trade info plugin. * <p> * The FpML parser has a number of plugin points that can be controlled: * <ul> * <li>the {@linkplain FpmlPartySelector party selector} * <li>the {@linkplain FpmlTradeInfoParserPlugin trade info parser} * <li>the {@linkplain FpmlParserPlugin trade parsers} * <li>the {@linkplain ReferenceData reference data} * </ul> * This method uses the trade parsers registered in {@link FpmlParserPlugin} configuration * and the {@linkplain ReferenceData#standard() standard} reference data. * * @param ourPartySelector the selector used to find "our" party within the set of parties in the FpML document * @param tradeInfoParser the trade info parser * @return the document parser */ public static FpmlDocumentParser of( FpmlPartySelector ourPartySelector, FpmlTradeInfoParserPlugin tradeInfoParser) { return of(ourPartySelector, tradeInfoParser, FpmlParserPlugin.extendedEnum().lookupAllNormalized()); }
public void badSelector() { String location = "classpath:com/opengamma/strata/loader/fpml/ird-ex08-fra.xml"; ByteSource resource = ResourceLocator.of(location).getByteSource(); FpmlDocumentParser parser = FpmlDocumentParser.of(allParties -> ImmutableList.of("rubbish")); assertThrows( () -> parser.parseTrades(resource), FpmlParseException.class, "Selector returned an ID .*"); }
public void fra_interpolated() { String location = "classpath:com/opengamma/strata/loader/fpml/ird-ex08-fra-interpolated.xml"; ByteSource resource = ResourceLocator.of(location).getByteSource(); List<Trade> trades = FpmlDocumentParser.of(FpmlPartySelector.matching("Party1")).parseTrades(resource); assertFra(trades, true); }
public void fra_wrapper1() { String location = "classpath:com/opengamma/strata/loader/fpml/ird-ex08-fra-wrapper1.xml"; ByteSource resource = ResourceLocator.of(location).getByteSource(); List<Trade> trades = FpmlDocumentParser.of(FpmlPartySelector.matching("Party2")).parseTrades(resource); assertFra(trades, false); }
public void fra() { String location = "classpath:com/opengamma/strata/loader/fpml/ird-ex08-fra.xml"; ByteSource resource = ResourceLocator.of(location).getByteSource(); List<Trade> trades = FpmlDocumentParser.of(FpmlPartySelector.matching("Party2")).parseTrades(resource); assertFra(trades, false); }
public void fra_namespace() { String location = "classpath:com/opengamma/strata/loader/fpml/ird-ex08-fra-namespace.xml"; ByteSource resource = ResourceLocator.of(location).getByteSource(); List<Trade> trades = FpmlDocumentParser.of(FpmlPartySelector.matching("Party2")).parseTrades(resource); assertFra(trades, false); }
public void fra_wrapper2() { String location = "classpath:com/opengamma/strata/loader/fpml/ird-ex08-fra-wrapper2.xml"; ByteSource resource = ResourceLocator.of(location).getByteSource(); List<Trade> trades = FpmlDocumentParser.of(FpmlPartySelector.matching("Party2")).parseTrades(resource); assertFra(trades, false); }
public void fra_wrapper_clearingStatus() { String location = "classpath:com/opengamma/strata/loader/fpml/ird-ex08-fra-wrapper-clearing-status.xml"; ByteSource resource = ResourceLocator.of(location).getByteSource(); List<Trade> trades = FpmlDocumentParser.of(FpmlPartySelector.matching("Party2")).parseTrades(resource); assertFra(trades, false); }
public void notFpml() { String location = "classpath:com/opengamma/strata/loader/fpml/not-fpml.xml"; ByteSource resource = ResourceLocator.of(location).getByteSource(); FpmlDocumentParser parser = FpmlDocumentParser.of(FpmlPartySelector.any()); assertThrows( () -> parser.parseTrades(resource), FpmlParseException.class, "Unable to find FpML root element.*"); }
public void unknownProduct() { XmlElement tradeDateEl = XmlElement.ofContent("tradeDate", "2000-06-30"); XmlElement tradeHeaderEl = XmlElement.ofChildren("tradeHeader", ImmutableList.of(tradeDateEl)); XmlElement unknownEl = XmlElement.ofChildren("unknown", ImmutableList.of()); XmlElement tradeEl = XmlElement.ofChildren("trade", ImmutableList.of(tradeHeaderEl, unknownEl)); XmlElement rootEl = XmlElement.ofChildren("dataDocument", ImmutableList.of(tradeEl)); FpmlDocumentParser parser = FpmlDocumentParser.of(FpmlPartySelector.any()); assertThrows( () -> parser.parseTrades(rootEl, ImmutableMap.of()), FpmlParseException.class, ".*unknown.*"); }
public void fxSpot() { String location = "classpath:com/opengamma/strata/loader/fpml/fx-ex01-fx-spot.xml"; ByteSource resource = ResourceLocator.of(location).getByteSource(); List<Trade> trades = FpmlDocumentParser.of(FpmlPartySelector.matching("Party1")).parseTrades(resource); assertEquals(trades.size(), 1); Trade trade = trades.get(0); assertEquals(trade.getClass(), FxSingleTrade.class); FxSingleTrade fxTrade = (FxSingleTrade) trade; assertEquals(fxTrade.getInfo().getTradeDate(), Optional.of(date(2001, 10, 23))); FxSingle fx = fxTrade.getProduct(); assertEquals(fx.getBaseCurrencyPayment(), Payment.of(GBP, 10000000, date(2001, 10, 25))); assertEquals(fx.getCounterCurrencyPayment(), Payment.of(USD, -14800000, date(2001, 10, 25))); }
public void fxForward_splitDate() { String location = "classpath:com/opengamma/strata/loader/fpml/fx-ex03-fx-fwd-split-date.xml"; ByteSource resource = ResourceLocator.of(location).getByteSource(); List<Trade> trades = FpmlDocumentParser.of(FpmlPartySelector.matching("Party1")).parseTrades(resource); assertEquals(trades.size(), 1); Trade trade = trades.get(0); assertEquals(trade.getClass(), FxSingleTrade.class); FxSingleTrade fxTrade = (FxSingleTrade) trade; assertEquals(fxTrade.getInfo().getTradeDate(), Optional.of(date(2001, 11, 19))); FxSingle fx = fxTrade.getProduct(); assertEquals(fx.getBaseCurrencyPayment(), Payment.of(EUR, 10000000, date(2001, 12, 21))); assertEquals(fx.getCounterCurrencyPayment(), Payment.of(USD, -9175000, date(2001, 12, 22))); }