private static void parseCurrencyNode(final XMLStreamReader reader) { Map<String, String> elementMap = new HashMap<>(); /* still at start of the element. Need to know when end is reached */ QName parsingElement = reader.getName(); try { while (reader.hasNext()) { int event = reader.next(); switch (event) { case XMLStreamConstants.START_ELEMENT: String element = reader.getLocalName(); elementMap.put(element, reader.getElementText()); break; case XMLStreamConstants.END_ELEMENT: if (reader.getName().equals(parsingElement)) { logger.finest("Found the end of a CurrencyNode"); Engine engine = EngineFactory.getEngine(EngineFactory.DEFAULT); if (engine.getCurrency(elementMap.get("symbol")) == null) { CurrencyNode node = new CurrencyNode(); node.setSymbol(elementMap.get("symbol")); node.setDescription(elementMap.get("description")); node.setPrefix(elementMap.get("prefix")); node.setSuffix(elementMap.get("suffix")); node.setScale(Byte.parseByte(elementMap.get("scale"))); engine.addCurrency(node); } return; } break; default: break; } } } catch (XMLStreamException e) { logger.log(Level.SEVERE, e.toString(), e); } }
public static void doImport(final String filename) { Import imp = new Import(); XMLInputFactory inputFactory = XMLInputFactory.newInstance(); try { Engine engine = EngineFactory.getEngine(EngineFactory.DEFAULT); CurrencyNode defaultCurrency = DefaultCurrencies.getDefault(); if (engine.getCurrency(defaultCurrency.getSymbol()) == null) { engine.addCurrency(defaultCurrency); engine.setDefaultCurrency(defaultCurrency); } /* read commodity data first */ try (InputStream input = new BufferedInputStream(new FileInputStream(new File(filename)))) { XMLStreamReader reader = inputFactory.createXMLStreamReader(input, "UTF-8"); imp.importCommodities(reader); reader.close(); } /* Run commodity generation cleanup threads */ ExecutorService es = Executors.newSingleThreadExecutor(); for (Iterator<Runnable> i = workQueue.iterator(); i.hasNext(); ) { es.execute(i.next()); i.remove(); } es.shutdown(); // shutdown after threads are complete while (!es.isTerminated()) { imp.getLogger().info("Waiting for commodity cleanup threads to complete"); try { es.awaitTermination(2, TimeUnit.SECONDS); } catch (InterruptedException e) { imp.getLogger().log(Level.SEVERE, e.toString(), e); } } imp.getLogger().info("Commodity cleanup threads complete"); /* read account data first */ try (InputStream input = new BufferedInputStream(new FileInputStream(new File(filename)))) { XMLStreamReader reader = inputFactory.createXMLStreamReader(input, "UTF-8"); imp.importAccounts(reader); reader.close(); } imp.getLogger().info("Running account cleanup threads"); /* Run account generation cleanup threads */ es = Executors.newSingleThreadExecutor(); for (Iterator<Runnable> i = workQueue.iterator(); i.hasNext(); ) { es.execute(i.next()); i.remove(); } es.shutdown(); // shutdown after threads are complete while (!es.isTerminated()) { imp.getLogger().info("Waiting for account cleanup threads to complete"); try { es.awaitTermination(2, TimeUnit.SECONDS); } catch (InterruptedException e) { imp.getLogger().log(Level.SEVERE, e.toString(), e); } } imp.getLogger().info("Account cleanup threads complete"); /* import transactions */ try (InputStream input = new BufferedInputStream(new FileInputStream(new File(filename)))) { XMLStreamReader reader = inputFactory.createXMLStreamReader(input, "UTF-8"); imp.importTransactions(reader); reader.close(); } /* Lock accounts after transactions have been added */ imp.lockAccounts(); } catch (XMLStreamException | IOException e) { imp.getLogger().log(Level.SEVERE, e.toString(), e); } }