📄 xmlrepository.java
字号:
/* * Copyright (c) 2004-2007 Marco Maccaferri and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Marco Maccaferri - initial API and implementation */package net.sourceforge.eclipsetrader.core.internal;import java.io.BufferedWriter;import java.io.File;import java.io.FileWriter;import java.text.NumberFormat;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Calendar;import java.util.Collections;import java.util.Comparator;import java.util.Currency;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.transform.OutputKeys;import javax.xml.transform.Transformer;import javax.xml.transform.TransformerFactory;import javax.xml.transform.dom.DOMSource;import javax.xml.transform.stream.StreamResult;import net.sourceforge.eclipsetrader.core.CorePlugin;import net.sourceforge.eclipsetrader.core.Repository;import net.sourceforge.eclipsetrader.core.db.Account;import net.sourceforge.eclipsetrader.core.db.AccountGroup;import net.sourceforge.eclipsetrader.core.db.Alert;import net.sourceforge.eclipsetrader.core.db.Bar;import net.sourceforge.eclipsetrader.core.db.Chart;import net.sourceforge.eclipsetrader.core.db.ChartIndicator;import net.sourceforge.eclipsetrader.core.db.ChartObject;import net.sourceforge.eclipsetrader.core.db.ChartRow;import net.sourceforge.eclipsetrader.core.db.ChartTab;import net.sourceforge.eclipsetrader.core.db.Dividend;import net.sourceforge.eclipsetrader.core.db.Event;import net.sourceforge.eclipsetrader.core.db.History;import net.sourceforge.eclipsetrader.core.db.IntradayHistory;import net.sourceforge.eclipsetrader.core.db.NewsItem;import net.sourceforge.eclipsetrader.core.db.Order;import net.sourceforge.eclipsetrader.core.db.OrderRoute;import net.sourceforge.eclipsetrader.core.db.OrderSide;import net.sourceforge.eclipsetrader.core.db.OrderStatus;import net.sourceforge.eclipsetrader.core.db.OrderType;import net.sourceforge.eclipsetrader.core.db.OrderValidity;import net.sourceforge.eclipsetrader.core.db.PersistentObject;import net.sourceforge.eclipsetrader.core.db.PersistentPreferenceStore;import net.sourceforge.eclipsetrader.core.db.Security;import net.sourceforge.eclipsetrader.core.db.SecurityGroup;import net.sourceforge.eclipsetrader.core.db.Split;import net.sourceforge.eclipsetrader.core.db.Transaction;import net.sourceforge.eclipsetrader.core.db.Watchlist;import net.sourceforge.eclipsetrader.core.db.WatchlistColumn;import net.sourceforge.eclipsetrader.core.db.WatchlistItem;import net.sourceforge.eclipsetrader.core.db.feed.FeedSource;import net.sourceforge.eclipsetrader.core.db.feed.Quote;import net.sourceforge.eclipsetrader.core.db.feed.TradeSource;import net.sourceforge.eclipsetrader.core.db.trading.TradingSystem;import net.sourceforge.eclipsetrader.core.db.trading.TradingSystemGroup;import org.apache.commons.collections.map.ReferenceIdentityMap;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.eclipse.core.runtime.Platform;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.NamedNodeMap;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import org.xml.sax.ErrorHandler;import org.xml.sax.SAXException;import org.xml.sax.SAXParseException;/** */public class XMLRepository extends Repository{ Integer securitiesNextId = new Integer(1); Integer securitiesGroupNextId = new Integer(1); Map securitiesMap = new HashMap(); Integer chartsNextId = new Integer(1); Map chartsMap = new HashMap(); Integer watchlistsNextId = new Integer(1); Map watchlistsMap = new HashMap(); SimpleDateFormat dateTimeFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); //$NON-NLS-1$ Integer accountGroupNextId = new Integer(1); Map accountGroupMap = new HashMap(); Integer accountNextId = new Integer(1); Map accountMap = new HashMap(); Integer eventNextId = new Integer(1); TradingSystemRepository tradingRepository; Integer orderNextId = new Integer(1); ReferenceIdentityMap historyMap = new ReferenceIdentityMap(); ReferenceIdentityMap intradayHistoryMap = new ReferenceIdentityMap(); private Log log = LogFactory.getLog(getClass()); private ErrorHandler errorHandler = new ErrorHandler() { public void error(SAXParseException exception) throws SAXException { log.error(exception, exception); } public void fatalError(SAXParseException exception) throws SAXException { log.fatal(exception, exception); } public void warning(SAXParseException exception) throws SAXException { log.warn(exception, exception); } }; public XMLRepository() { File file = new File(Platform.getLocation().toFile(), "securities.xml"); //$NON-NLS-1$ if (file.exists() == true) { log.info("Loading securities"); try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); builder.setErrorHandler(errorHandler); Document document = builder.parse(file); Node firstNode = document.getFirstChild(); securitiesNextId = new Integer(firstNode.getAttributes().getNamedItem("nextId").getNodeValue()); //$NON-NLS-1$ if (firstNode.getAttributes().getNamedItem("nextGroupId") != null) securitiesGroupNextId = new Integer(firstNode.getAttributes().getNamedItem("nextGroupId").getNodeValue()); //$NON-NLS-1$ NodeList childNodes = firstNode.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node item = childNodes.item(i); String nodeName = item.getNodeName(); if (nodeName.equalsIgnoreCase("security")) //$NON-NLS-1$ { Security obj = loadSecurity(item.getChildNodes()); obj.setRepository(this); securitiesMap.put(obj.getId(), obj); allSecurities().add(obj); } else if (nodeName.equalsIgnoreCase("group")) //$NON-NLS-1$ { SecurityGroup obj = loadSecurityGroup(item.getChildNodes()); obj.setRepository(this); allSecurityGroups().add(obj); } } } catch (Exception e) { log.error(e.toString(), e); } } file = new File(Platform.getLocation().toFile(), "watchlists.xml"); //$NON-NLS-1$ if (file.exists() == true) { log.info("Loading watchlists"); try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); builder.setErrorHandler(errorHandler); Document document = builder.parse(file); Node firstNode = document.getFirstChild(); watchlistsNextId = new Integer(firstNode.getAttributes().getNamedItem("nextId").getNodeValue()); //$NON-NLS-1$ NodeList childNodes = firstNode.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node item = childNodes.item(i); String nodeName = item.getNodeName(); if (nodeName.equalsIgnoreCase("watchlist")) //$NON-NLS-1$ { Watchlist obj = loadWatchlist(item.getChildNodes()); obj.setRepository(this); watchlistsMap.put(obj.getId(), obj); allWatchlists().add(obj); } } } catch (Exception e) { log.error(e.toString(), e); } } file = new File(Platform.getLocation().toFile(), "charts.xml"); //$NON-NLS-1$ if (file.exists() == true) { log.info("Loading charts"); try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); builder.setErrorHandler(errorHandler); Document document = builder.parse(file); Node firstNode = document.getFirstChild(); chartsNextId = new Integer(firstNode.getAttributes().getNamedItem("nextId").getNodeValue()); //$NON-NLS-1$ NodeList childNodes = firstNode.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node item = childNodes.item(i); String nodeName = item.getNodeName(); if (nodeName.equalsIgnoreCase("chart")) //$NON-NLS-1$ { Chart obj = loadChart(item.getChildNodes()); if (obj.getSecurity() != null) { obj.setRepository(this); chartsMap.put(obj.getId(), obj); allCharts().add(obj); } } } } catch (Exception e) { log.error(e.toString(), e); } } boolean needToSave = false; for (Iterator iter = allSecurities().iterator(); iter.hasNext(); ) { Security security = (Security)iter.next(); file = new File(Platform.getLocation().toFile(), "charts/" + String.valueOf(security.getId()) + ".xml"); if (file.exists()) { Chart obj = loadChart(security.getId()); if (obj.getSecurity() != null) { if (obj.getId().intValue() > chartsNextId.intValue()) chartsNextId = getNextId(obj.getId()); obj.setRepository(this); chartsMap.put(obj.getId(), obj); allCharts().add(obj); file.delete(); needToSave = true; } } } if (needToSave) saveCharts(); file = new File(Platform.getLocation().toFile(), "news.xml"); //$NON-NLS-1$ if (file.exists() == true) { log.info("Loading news"); try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); builder.setErrorHandler(errorHandler); Document document = builder.parse(file); Calendar limit = Calendar.getInstance(); limit.add(Calendar.DATE, - CorePlugin.getDefault().getPreferenceStore().getInt(CorePlugin.PREFS_NEWS_DATE_RANGE)); Node firstNode = document.getFirstChild(); NodeList childNodes = firstNode.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node item = childNodes.item(i); String nodeName = item.getNodeName(); if (nodeName.equalsIgnoreCase("news")) //$NON-NLS-1$ { NewsItem obj = loadNews(item.getChildNodes()); if (obj.getDate().before(limit.getTime())) continue; obj.setRepository(this); allNews().add(obj); } } } catch (Exception e) { log.error(e.toString(), e); } } file = new File(Platform.getLocation().toFile(), "accounts.xml"); //$NON-NLS-1$ if (file.exists() == true)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -