📄 projsettings.java
字号:
/* -*- tab-width: 4 -*- * * Electric(tm) VLSI Design System * * File: ProjSettings.java * * Copyright (c) 2003 Sun Microsystems and Static Free Software * * Electric(tm) is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * Electric(tm) is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Electric(tm); see the file COPYING. If not, write to * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, Mass 02111-1307, USA. */package com.sun.electric.tool.user.projectSettings;import org.xml.sax.helpers.DefaultHandler;import org.xml.sax.Locator;import org.xml.sax.SAXParseException;import org.xml.sax.SAXException;import org.xml.sax.Attributes;import javax.xml.parsers.SAXParserFactory;import javax.xml.parsers.ParserConfigurationException;import java.net.URL;import java.net.URLConnection;import java.io.*;import java.util.Stack;import java.util.HashSet;import java.util.Set;import com.sun.electric.database.text.TextUtils;import com.sun.electric.database.text.Setting;import com.sun.electric.tool.Job;import com.sun.electric.tool.JobException;import com.sun.electric.tool.io.FileType;import com.sun.electric.tool.user.User;import com.sun.electric.tool.user.dialogs.OpenFile;import java.util.HashMap;import java.util.Map;/** * User: gainsley * Date: Jul 25, 2006 */public class ProjSettings { private static ProjSettingsNode settings = new ProjSettingsNode(); private static File lastProjectSettingsFile = null; private static final String entry = "e"; private static final boolean READER_RESETS_DEFAULTS = true; private static final boolean WRITER_WRITES_DEFAULTS = true; public static ProjSettingsNode getSettings() { return settings; } public static void putValue(Setting setting) { String xmlPath = setting.getXmlPath(); int pos; ProjSettingsNode node = getSettings(); while ((pos = xmlPath.indexOf('.')) >= 0) { String key = xmlPath.substring(0, pos); node = node.getNode(key); xmlPath = xmlPath.substring(pos + 1); } node.putValue(xmlPath, setting); } public static void writeSettings(File file) { write(file.getPath(), getSettings()); } public static File getLastProjectSettingsFile() { return lastProjectSettingsFile; } /** * Read project settings and apply them * @param file the file to read * @param allowOverride true to allow overriding current settings, * false to disallow and warn if different. */ public static void readSettings(File file, boolean allowOverride) { if (lastProjectSettingsFile == null) allowOverride = true; ReadResult result = read(file.getPath(), allowOverride); if (result == ReadResult.ERROR) return; if (result == ReadResult.CONFLICTS && lastProjectSettingsFile != null) { String message = "Warning: Project Settings conflicts;" + (allowOverride ? "" : " ignoring new settings.") + " See messages window"; Job.getUserInterface().showInformationMessage(message, "Project Settings Conflict"); System.out.println("Project Setting conflicts found: "+lastProjectSettingsFile.getPath()+" vs "+file.getPath()); } lastProjectSettingsFile = file; } /** * Write settings to disk. Pops up a dialog to prompt for file location */ public static void exportSettings() {/* int ret = JOptionPane.showConfirmDialog(TopLevel.getCurrentJFrame(), "Changes to Project Settings may affect all users\n\nContinue Anyway?", "Warning!", JOptionPane.YES_NO_OPTION); if (ret == JOptionPane.NO_OPTION) return;*/ File outputFile = new File(FileType.LIBRARYFORMATS.getGroupPath(), "projsettings.xml"); String ofile = OpenFile.chooseOutputFile(FileType.XML, "Export Project Settings", outputFile.getPath()); if (ofile == null) return; outputFile = new File(ofile); writeSettings(outputFile); } public static void importSettings() { String ifile = OpenFile.chooseInputFile(FileType.XML, "Import Project Settings", false, FileType.LIBRARYFORMATS.getGroupPath(), false); if (ifile == null) return; new ImportSettingsJob(ifile); } /** * Class to read a library in a new thread. * For a non-interactive script, use ReadLibrary job = new ReadLibrary(filename, format). */ public static class ImportSettingsJob extends Job { private String fileName; public ImportSettingsJob(String fileName) { super("Import Settings", User.getUserTool(), Job.Type.CHANGE, null, null, Job.Priority.USER); this.fileName = fileName; startJob(); } public boolean doIt() throws JobException { readSettings(new File(fileName), true); return true; } } // ----------------------------- Private ------------------------------- private static void write(String file, ProjSettingsNode node) { File ofile = new File(file); Writer wr = new Writer(ofile); if (wr.write("ProjectSettings", node)) { System.out.println("Wrote Project Settings to "+file); lastProjectSettingsFile = ofile; } wr.close(); } // return false if error reading file private static ReadResult read(String file, boolean allowOverride) { Reader rd = new Reader(TextUtils.makeURLToFile(file)); ReadResult r = rd.read(allowOverride); System.out.println("Read Project Settings from "+file); return r; } // ------------------------- ProjSettings Writer ---------------------------- private static class Writer { private File file; PrintWriter out; private int indent; private static final int indentVal = 4; Set<Object> visited = new HashSet<Object>(); private Writer(File file) { this.file = file; } private boolean write(String nodeName, ProjSettingsNode node) { out = new PrintWriter(new BufferedOutputStream(System.out)); if (file != null) { try { out = new PrintWriter(new BufferedOutputStream(new FileOutputStream(file))); } catch (java.io.IOException e) { System.out.println("Error opening "+file+" for write: "+e.getMessage()); file = null; return false; } } writeNode(nodeName, node); return true; } private void prIndent(String msg) { for (int i=0; i<indent*indentVal; i++) { out.print(" "); } out.println(msg); } private void writeNode(String name, ProjSettingsNode node) { if (visited.contains(node)) { // already wrote this object out, must be recursion. System.out.println("ERROR: recursive loop in Project Settings; "+ "ignoring second instantiation of ProjSettingsNode \""+name+"\"."); return; } visited.add(node); String classDef = "";/* if (node.getClass() != ProjSettingsNode.class) { classDef = " class=\""+node.getClass().getName()+"\">"; }*/ if (node.getKeys().size() == 0) { // if nothing in node, skip //prIndent("<node key=\""+name+"\""+classDef+"/>"); } else { prIndent("<node key=\""+name+"\""+classDef+">"); indent++; for (String key : node.getKeys()) { writeSetting(key, node.get(key)); } indent--; prIndent("</node>"); } } private void writeSetting(String key, Object value) { if (value instanceof ProjSettingsNode) { ProjSettingsNode node = (ProjSettingsNode)value; writeNode(key, node); } else if (value instanceof Setting) { Setting setting = (Setting)value; Object val = setting.getValue(); if (WRITER_WRITES_DEFAULTS || !setting.getFactoryValue().equals(val)) writeValue(key, val); } else if (value instanceof ProjSettingsNode.UninitializedPref) { ProjSettingsNode.UninitializedPref pref = (ProjSettingsNode.UninitializedPref)value; writeValue(key, pref.value); } else { System.out.println("Ignoring unsupported class "+value.getClass().getName()+" for key "+key+ " in project settings"); } } private void writeValue(String key, Object value) { if (value instanceof Integer) { prIndent("<"+entry+" key=\""+key+"\"\t int=\""+value.toString()+"\" />"); } else if (value instanceof Double) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -