⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cfg.java

📁 华为CNGP.CMPP.SGIP.CMPP源代码。
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
				continue;
			if (childValue.equals(value)) {
				return;
			} else {
				child.setNodeValue(value);
				isDirty = true;
				return;
			}
		}

		if (nl.getLength() == 0) {
			node.appendChild(doc.createTextNode(value));
		} else {
			Node f = node.getFirstChild();
			if (f.getNodeType() == 3)
				f.setNodeValue(value);
			else
				node.insertBefore(doc.createTextNode(value), f);
		}
		isDirty = true;
	}

	public boolean getBoolean(String key, boolean def) {
		String str = String.valueOf(def);
		String resstr = get(key, str);
		Boolean resboolean = Boolean.valueOf(resstr);
		boolean result = resboolean.booleanValue();
		return result;
	}

	public int getInt(String key, int def) {
		String str = String.valueOf(def);
		String resstr = get(key, str);
		int result;
		try {
			result = Integer.parseInt(resstr);
		} catch (NumberFormatException e) {
			int i = def;
			return i;
		}
		return result;
	}

	public float getFloat(String key, float def) {
		String str = String.valueOf(def);
		String resstr = get(key, str);
		float result;
		try {
			result = Float.parseFloat(resstr);
		} catch (NumberFormatException e) {
			float f = def;
			return f;
		}
		return result;
	}

	public double getDouble(String key, double def) {
		String str = String.valueOf(def);
		String resstr = get(key, str);
		double result;
		try {
			result = Double.parseDouble(resstr);
		} catch (NumberFormatException e) {
			double d = def;
			return d;
		}
		return result;
	}

	public long getLong(String key, long def) {
		String str = String.valueOf(def);
		String resstr = get(key, str);
		long result;
		try {
			result = Long.parseLong(resstr);
		} catch (NumberFormatException e) {
			long l = def;
			return l;
		}
		return result;
	}

	public byte[] getByteArray(String key, byte def[]) {
		String str = new String(def);
		String resstr = get(key, str);
		byte result[] = resstr.getBytes();
		return result;
	}

	public void putBoolean(String key, boolean value) {
		String str = String.valueOf(value);
		try {
			put(key, str);
		} catch (RuntimeException e) {
			throw e;
		}
	}

	public void putInt(String key, int value) {
		String str = String.valueOf(value);
		try {
			put(key, str);
		} catch (RuntimeException e) {
			throw e;
		}
	}

	public void putFloat(String key, float value) {
		String str = String.valueOf(value);
		try {
			put(key, str);
		} catch (RuntimeException e) {
			throw e;
		}
	}

	public void putDouble(String key, double value) {
		String str = String.valueOf(value);
		try {
			put(key, str);
		} catch (RuntimeException e) {
			throw e;
		}
	}

	public void putLong(String key, long value) {
		String str = String.valueOf(value);
		try {
			put(key, str);
		} catch (RuntimeException e) {
			throw e;
		}
	}

	public void putByteArray(String key, byte value[]) {
		put(key, Base64.encode(value));
	}

	public void removeNode(String key) {
		Node node = findNode(key);
		if (node == null)
			return;
		Node parentnode = node.getParentNode();
		if (parentnode != null) {
			parentnode.removeChild(node);
			isDirty = true;
		}
	}

	public void clear(String key) {
		Node node = findNode(key);
		if (node == null)
			throw new RuntimeException("InvalidName");
		Node lastnode = null;
		for (; node.hasChildNodes(); node.removeChild(lastnode))
			lastnode = node.getLastChild();

		if (lastnode != null)
			isDirty = true;
	}

	public String[] childrenNames(String key) {
		Node node = findNode(key);
		if (node == null)
			return new String[0];
		NodeList nl = node.getChildNodes();
		LinkedList list = new LinkedList();
		for (int i = 0; i < nl.getLength(); i++) {
			Node child = nl.item(i);
			if (child.getNodeType() == 1 && child.hasChildNodes())
				list.add(child.getNodeName());
		}

		String ret[] = new String[list.size()];
		for (int i = 0; i < ret.length; i++)
			ret[i] = (String) list.get(i);

		return ret;
	}

	public boolean nodeExist(String key) {
		Node theNode = findNode(key);
		if (theNode == null)
			return false;
		return theNode.hasChildNodes();
	}

	private void loadXMLParser() throws IOException {
		if (builder == null)
			try {
				factory = DocumentBuilderFactory.newInstance();
				factory.setIgnoringComments(true);
				builder = factory.newDocumentBuilder();
			} catch (ParserConfigurationException ex) {
				throw new IOException("XML Parser load error:".concat(String
						.valueOf(String.valueOf(ex.getLocalizedMessage()))));
			}
	}

	public void load() throws IOException {
		loadXMLParser();
		try {
			InputSource is = new InputSource(new InputStreamReader((new URL(
					file)).openStream()));
			is.setEncoding(System.getProperty("file.encoding"));
			doc = builder.parse(is);
		} catch (SAXException ex) {
			ex.printStackTrace();
			String message = ex.getMessage();
			Exception e = ex.getException();
			if (e != null)
				message = String.valueOf(message)
						+ String.valueOf("embedded exception:".concat(String
								.valueOf(String.valueOf(e))));
			throw new IOException("XML file parse error:".concat(String
					.valueOf(String.valueOf(message))));
		}
		root = doc.getDocumentElement();
		if (!"config".equals(root.getNodeName()))
			throw new IOException(
					"Config file format error, root node must be <config>");
		else
			return;
	}

	public void flush() throws IOException {
		if (isDirty) {
			String proc = (new URL(file)).getProtocol().toLowerCase();
			if (!proc.equalsIgnoreCase("file"))
				throw new UnsupportedOperationException(
						"Unsupport write config URL on protocal ".concat(String
								.valueOf(String.valueOf(proc))));
			String fileName = (new URL(file)).getPath();
			Debug.dump((new URL(file)).getPath());
			Debug.dump((new URL(file)).getFile());
			BufferedOutputStream bos = new BufferedOutputStream(
					new FileOutputStream(fileName), 2048);
			PrintWriter pw = new PrintWriter(bos);
			writeNode(doc, pw, 0);
			pw.flush();
			pw.close();
			isDirty = false;
		}
	}

	private String change(String str) throws IOException {
		if (str.indexOf('&') != -1 || str.indexOf('<') != -1
				|| str.indexOf('>') != -1) {
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			ByteArrayInputStream bis = new ByteArrayInputStream(str.getBytes());
			byte ba1[] = { 38, 97, 109, 112, 59 };
			byte ba2[] = { 38, 108, 116, 59 };
			byte ba3[] = { 38, 103, 116, 59 };
			do {
				byte temp;
				if ((temp = (byte) bis.read()) == -1)
					break;
				switch (temp) {
				case 38: // '&'
					bos.write(ba1);
					break;

				case 60: // '<'
					bos.write(ba2);
					break;

				case 62: // '>'
					bos.write(ba3);
					break;

				default:
					bos.write(temp);
					break;
				}
			} while (true);
			return bos.toString();
		} else {
			return str;
		}
	}

	public static void main(String args[]) throws Exception {
		Cfg c = new Cfg("testcfg.xml", true);
		c.put("a/b", "\u6C49\u5B57");
		c.put("c", "");
		c.put("a", "avalusaaaaaaaaae");
		c.flush();
		c = new Cfg("testcfg.xml", true);
		System.out.println("Config file content:");
		BufferedReader in = new BufferedReader(new FileReader("testcfg.xml"));
		String line;
		while ((line = in.readLine()) != null)
			System.out.println(line);
	}

	private static DocumentBuilderFactory factory;

	private static DocumentBuilder builder;

	private static final String XML_HEAD = String
			.valueOf(String.valueOf((new StringBuffer(
					"<?xml version=\"1.0\" encoding=\"")).append(
					System.getProperty("file.encoding")).append("\"?>")));

	private static String indent = "  ";

	private boolean isDirty;

	private Document doc;

	private Element root;

	private String file;

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -