/** * 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()); }
return Unchecked.wrap(() -> { try (InputStream in = source.openBufferedStream()) { XMLStreamReader xmlReader = xmlInputFactory().createXMLStreamReader(in); try { HashMap<String, XmlElement> refs = new HashMap<>(); XmlElement root = parse(xmlReader, refAttrName, refs); return new XmlFile(root, refs); } finally { xmlReader.close();
public void test_equalsHashCodeToString() { ByteSource source = ByteSource.wrap(SAMPLE.getBytes(StandardCharsets.UTF_8)); XmlFile test = XmlFile.of(source); XmlFile test2 = XmlFile.of(source); assertFalse(test.equals(null)); assertFalse(test.equals(ANOTHER_TYPE)); assertEquals(test, test); assertEquals(test, test2); assertEquals(test.hashCode(), test2.hashCode()); assertEquals(test.toString(), test2.toString()); }
/** * Parses the specified source as an XML file to an in-memory DOM-like structure. * <p> * This parses the specified byte source expecting an XML file format. * The resulting instance can be queried for the root element. * * @param source the XML source data * @return the parsed file * @throws UncheckedIOException if an IO exception occurs * @throws IllegalArgumentException if the file cannot be parsed */ public static XmlFile of(ByteSource source) { return of(source, ""); }
try { String elementName = parseElementName(reader); ImmutableMap<String, String> attrs = parseAttributes(reader); childBuilder.add(parse(reader, refAttr, refs)); break;
public void test_of_ByteSource_badEnd() { ByteSource source = ByteSource.wrap(SAMPLE_BAD_END.getBytes(StandardCharsets.UTF_8)); assertThrowsIllegalArg(() -> XmlFile.of(source)); }
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_mismatchedTags() { ByteSource source = ByteSource.wrap(SAMPLE_MISMATCHED_TAGS.getBytes(StandardCharsets.UTF_8)); assertThrowsIllegalArg(() -> XmlFile.of(source)); }
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_of_ByteSource_ioException() { ByteSource source = Files.asByteSource(new File("/oh-dear-no-such-file")); assertThrows(() -> XmlFile.of(source), UncheckedIOException.class); }
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()); }
public void test_of_ByteSource_parsedReferences_ioException() { ByteSource source = Files.asByteSource(new File("/oh-dear-no-such-file")); assertThrows(() -> XmlFile.of(source, "key"), UncheckedIOException.class); }