From 9ef99fd67612268414c6848aed352b93a7a94494 Mon Sep 17 00:00:00 2001 From: Enrique Zamudio Date: Fri, 1 Jul 2016 08:16:14 -0500 Subject: [PATCH] read config from a Reader --- .../com/solab/iso8583/parse/ConfigParser.java | 28 ++++++++++++++----- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/solab/iso8583/parse/ConfigParser.java b/src/main/java/com/solab/iso8583/parse/ConfigParser.java index 80ff6a5..9e040ae 100644 --- a/src/main/java/com/solab/iso8583/parse/ConfigParser.java +++ b/src/main/java/com/solab/iso8583/parse/ConfigParser.java @@ -20,6 +20,7 @@ package com.solab.iso8583.parse; import java.io.IOException; import java.io.InputStream; +import java.io.Reader; import java.net.URL; import java.text.ParseException; import java.util.ArrayList; @@ -84,7 +85,7 @@ public class ConfigParser { try (InputStream ins = loader.getResourceAsStream(path)) { if (ins != null) { log.debug("ISO8583 Parsing config from classpath file {}", path); - parse(mfact, ins); + parse(mfact, new InputSource(ins)); } else { log.error("ISO8583 File not found in classpath: {}", path); } @@ -96,11 +97,18 @@ public class ConfigParser { public static MessageFactory createFromUrl(URL url) throws IOException { MessageFactory mfact = new MessageFactory<>(); try (InputStream stream = url.openStream()) { - parse(mfact, stream); + parse(mfact, new InputSource(stream)); } return mfact; } + /** Creates a messageFactory from the XML contained in the specified Reader. */ + public static MessageFactory createFromReader(Reader reader) throws IOException { + MessageFactory mfact = new MessageFactory<>(); + parse(mfact, new InputSource(reader)); + return mfact; + } + protected static void parseHeaders( final NodeList nodes, final MessageFactory mfact) throws IOException { ArrayList refs = null; @@ -376,9 +384,9 @@ public class ConfigParser { /** Reads the XML from the stream and configures the message factory with its values. * @param mfact The message factory to be configured with the values read from the XML. - * @param stream The InputStream containing the XML configuration. */ + * @param source The InputSource containing the XML configuration. */ protected static void parse( - MessageFactory mfact, InputStream stream) throws IOException { + MessageFactory mfact, InputSource source) throws IOException { final DocumentBuilderFactory docfact = DocumentBuilderFactory.newInstance(); DocumentBuilder docb = null; Document doc = null; @@ -399,7 +407,7 @@ public class ConfigParser { return null; } }); - doc = docb.parse(stream); + doc = docb.parse(source); } catch (ParserConfigurationException | SAXException ex) { log.error("ISO8583 Cannot parse XML configuration", ex); return; @@ -428,7 +436,7 @@ public class ConfigParser { public static void configureFromUrl( MessageFactory mfact, URL url) throws IOException { try (InputStream stream = url.openStream()) { - parse(mfact, stream); + parse(mfact, new InputSource(stream)); } } @@ -440,13 +448,19 @@ public class ConfigParser { try (InputStream ins = mfact.getClass().getClassLoader().getResourceAsStream(path)) { if (ins != null) { log.debug("ISO8583 Parsing config from classpath file {}", path); - parse(mfact, ins); + parse(mfact, new InputSource(ins)); } else { log.warn("ISO8583 File not found in classpath: {}", path); } } } + /** Configures a MessageFactory using the XML data obtained from the specified Reader. */ + public static void configureFromReader(MessageFactory mfact, Reader reader) + throws IOException { + parse(mfact, new InputSource(reader)); + } + /** Parses a message type expressed as a hex string and returns the integer number. * For example, "0200" or "200" return the number 512 (0x200) */ private static int parseType(String type) throws IOException {