예제 #1
0
파일: Import.java 프로젝트: CptMauli/jgnash
  private SecurityNode decodeSecurity(final String symbol) {
    Engine engine = EngineFactory.getEngine(EngineFactory.DEFAULT);

    SecurityNode sNode = engine.getSecurity(symbol);

    if (sNode == null) {

      Commodity cNode = commodityMap.get(symbol);

      if (cNode != null) {

        logger.log(Level.INFO, "Converting a commodity into a security: {0}", symbol);

        sNode = new SecurityNode(engine.getDefaultCurrency());

        sNode.setDescription(cNode.description);
        sNode.setPrefix(cNode.prefix);
        sNode.setSuffix(cNode.suffix);
        sNode.setScale(cNode.scale);
        sNode.setSymbol(cNode.symbol);
        sNode.setQuoteSource(QuoteSource.YAHOO);

        engine.addSecurity(sNode);
      } else { // may be a currency... try to create a security

        CurrencyNode currency = decodeCurrency(symbol);

        if (currency != null) {

          logger.info("Converting a currency into a security");

          sNode = new SecurityNode();

          sNode.setDescription(currency.getDescription());
          sNode.setPrefix(currency.getPrefix());
          sNode.setSuffix(currency.getSuffix());
          sNode.setScale(currency.getScale());
          sNode.setSymbol(currency.getSymbol());

          sNode.setReportedCurrencyNode(currency);
          sNode.setQuoteSource(QuoteSource.YAHOO);

          engine.addSecurity(sNode);
        }
      }
    }

    if (sNode == null) {
      logger.log(Level.SEVERE, "Bad file, security {0} not mapped", symbol);
    }

    return sNode;
  }
예제 #2
0
파일: Import.java 프로젝트: CptMauli/jgnash
  private void parseSecurityNode(final XMLStreamReader reader) {
    Map<String, String> elementMap = new HashMap<>();
    List<SecurityHistoryNode> history = null;

    try {
      /* still at start of the element.  Need to know when end is reached */
      QName parsingElement = reader.getName();

      while (reader.hasNext()) {
        int event = reader.next();

        switch (event) {
          case XMLStreamConstants.START_ELEMENT:
            if (reader.getLocalName().equals("history")) {
              logger.finest("parse history");
              history = parseHistoryNodes(reader);
            } else {
              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 SecurityNode");

              Engine engine = EngineFactory.getEngine(EngineFactory.DEFAULT);

              if (engine.getSecurity(elementMap.get("symbol")) == null) {

                SecurityNode node = new SecurityNode(engine.getDefaultCurrency());

                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")));
                node.setQuoteSource(QuoteSource.YAHOO);

                if (elementMap.get("reportedCurrency") != null) {
                  SecurityThread thread =
                      new SecurityThread(node, elementMap.get("reportedCurrency"));
                  workQueue.add(thread);
                }

                engine.addSecurity(node);
              }

              if (history != null) {
                SecurityNode node = engine.getSecurity(elementMap.get("symbol"));
                if (node != null) {
                  for (SecurityHistoryNode hNode : history) {
                    if (!engine.addSecurityHistory(node, hNode)) {
                      logger.warning("Failed to add security history");
                    }
                  }
                }
              }
              return;
            }
            break;

          default:
            break;
        }
      }
    } catch (XMLStreamException e) {
      logger.log(Level.SEVERE, "Exception at element: {0}", reader.getName().toString());
      logger.log(Level.SEVERE, e.toString(), e);
    }
  }