- 浏览: 125400 次
- 性别:
- 来自: HEYUAN
文章分类
最新评论
-
malson:
果然可以了!多谢
关于springmvc 无法使用aop拦截的解决方案 -
1025250620:
如何设置它的淘汰策略~
guava cache 使用
以下代码源自eclipse org.eclipse.ui.workbench项目下 org.eclipse.ui.XMLMemento类
稍作修改,去掉了关联类(异常而已),去掉了接口IMemento,用于操作XML配置文件.
/******************************************************************************* * Copyright (c) 2000, 2010 IBM Corporation 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: * IBM Corporation - initial API and implementation *******************************************************************************/ package org.eclipse.ui; import java.io.IOException; import java.io.PrintWriter; import java.io.Reader; import java.io.Writer; import java.util.ArrayList; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Attr; import org.w3c.dom.DOMException; 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.w3c.dom.Text; import org.xml.sax.ErrorHandler; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; /** * This class represents the default implementation of the * <code>XMLMemento</code> interface. * <p> * This class is not intended to be extended by clients. * </p> * * @see XMLMemento */ public final class XMLMemento { private Document factory; private Element element; public static final String TAG_ID = "IMemento.internal.id"; /** * Creates a <code>Document</code> from the <code>Reader</code> and returns * a memento on the first <code>Element</code> for reading the document. * <p> * Same as calling createReadRoot(reader, null) * </p> * * @param reader * the <code>Reader</code> used to create the memento's document * @return a memento on the first <code>Element</code> for reading the * document * @throws Exception * @throws WorkbenchException * if IO problems, invalid format, or no element. */ public static XMLMemento createReadRoot(Reader reader) throws Exception { return createReadRoot(reader, null); } /** * Creates a <code>Document</code> from the <code>Reader</code> and returns * a memento on the first <code>Element</code> for reading the document. * * @param reader * the <code>Reader</code> used to create the memento's document * @param baseDir * the directory used to resolve relative file names in the XML * document. This directory must exist and include the trailing * separator. The directory format, including the separators, * must be valid for the platform. Can be <code>null</code> if * not needed. * @return a memento on the first <code>Element</code> for reading the * document * @throws Exception * @throws WorkbenchException * if IO problems, invalid format, or no element. */ public static XMLMemento createReadRoot(Reader reader, String baseDir) throws Exception { String errorMessage = null; Exception exception = null; try { DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance(); DocumentBuilder parser = factory.newDocumentBuilder(); InputSource source = new InputSource(reader); if (baseDir != null) { source.setSystemId(baseDir); } parser.setErrorHandler(new ErrorHandler() { /** * @throws SAXException */ public void warning(SAXParseException exception) throws SAXException { // ignore } /** * @throws SAXException */ public void error(SAXParseException exception) throws SAXException { // ignore } public void fatalError(SAXParseException exception) throws SAXException { throw exception; } }); Document document = parser.parse(source); NodeList list = document.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { Node node = list.item(i); if (node instanceof Element) { return new XMLMemento(document, (Element) node); } } } catch (ParserConfigurationException e) { exception = e; } catch (IOException e) { exception = e; } catch (SAXException e) { exception = e; } String problemText = null; if (exception != null) { problemText = exception.getMessage(); } if (problemText == null || problemText.length() == 0) { problemText = errorMessage != null ? errorMessage : "--------"; } throw new Exception(""); } /** * Returns a root memento for writing a document. * * @param type * the element node type to create on the document * @return the root memento for writing a document * @throws DOMException */ public static XMLMemento createWriteRoot(String type) throws DOMException { Document document; try { document = DocumentBuilderFactory.newInstance() .newDocumentBuilder().newDocument(); Element element = document.createElement(type); document.appendChild(element); return new XMLMemento(document, element); } catch (ParserConfigurationException e) { // throw new Error(e); throw new Error(e.getMessage()); } } /** * Creates a memento for the specified document and element. * <p> * Clients should use <code>createReadRoot</code> and * <code>createWriteRoot</code> to create the initial memento on a document. * </p> * * @param document * the document for the memento * @param element * the element node for the memento */ public XMLMemento(Document document, Element element) { super(); this.factory = document; this.element = element; } /** * Creates a new child of this memento with the given type. * <p> * The <code>getChild</code> and <code>getChildren</code> methods are used * to retrieve children of a given type. * </p> * * @param type * the type * @return a new child memento * @see #getChild * @see #getChildren * @throws DOMException * if the child cannot be created */ public XMLMemento createChild(String type) throws DOMException { Element child = factory.createElement(type); element.appendChild(child); return new XMLMemento(factory, child); } /** * Creates a new child of this memento with the given type and id. The id is * stored in the child memento (using a special reserved key, * <code>TAG_ID</code>) and can be retrieved using <code>getId</code>. * <p> * The <code>getChild</code> and <code>getChildren</code> methods are used * to retrieve children of a given type. * </p> * * @param type * the type * @param id * the child id * @return a new child memento with the given type and id * @see #getID * @throws DOMException * if the child cannot be created */ public XMLMemento createChild(String type, String id) throws DOMException { Element child = factory.createElement(type); child.setAttribute(TAG_ID, id == null ? "" : id); //$NON-NLS-1$ element.appendChild(child); return new XMLMemento(factory, child); } /** * Create a copy of the child node and append it to this node. * * @param child * @return An IMenento for the new child node. * @throws DOMException * if the child cannot be created */ public XMLMemento copyChild(XMLMemento child) throws DOMException { Element childElement = ((XMLMemento) child).element; Element newElement = (Element) factory.importNode(childElement, true); element.appendChild(newElement); return new XMLMemento(factory, newElement); } /* * (non-Javadoc) Method declared in XMLMemento. */ public XMLMemento getChild(String type) { // Get the nodes. NodeList nodes = element.getChildNodes(); int size = nodes.getLength(); if (size == 0) { return null; } // Find the first node which is a child of this node. for (int nX = 0; nX < size; nX++) { Node node = nodes.item(nX); if (node instanceof Element) { Element element = (Element) node; if (element.getNodeName().equals(type)) { return new XMLMemento(factory, element); } } } // A child was not found. return null; } /* * (non-Javadoc) Method declared in XMLMemento. */ public XMLMemento[] getChildren(String type) { // Get the nodes. NodeList nodes = element.getChildNodes(); int size = nodes.getLength(); if (size == 0) { return new XMLMemento[0]; } // Extract each node with given type. ArrayList<Element> list = new ArrayList<Element>(size); for (int nX = 0; nX < size; nX++) { Node node = nodes.item(nX); if (node instanceof Element) { Element element = (Element) node; if (element.getNodeName().equals(type)) { list.add(element); } } } // Create a memento for each node. size = list.size(); XMLMemento[] results = new XMLMemento[size]; for (int x = 0; x < size; x++) { results[x] = new XMLMemento(factory, list.get(x)); } return results; } /* * (non-Javadoc) Method declared in XMLMemento. */ public Float getFloat(String key) { Attr attr = element.getAttributeNode(key); if (attr == null) { return null; } String strValue = attr.getValue(); try { return new Float(strValue); } catch (NumberFormatException e) { // WorkbenchPlugin.log("Memento problem - Invalid float for key: " //$NON-NLS-1$ // + key + " value: " + strValue, e); //$NON-NLS-1$ return null; } } /** * @since 3.4 */ public String getType() { return element.getNodeName(); } /* * (non-Javadoc) Method declared in XMLMemento. */ public String getID() { return element.getAttribute(TAG_ID); } /* * (non-Javadoc) Method declared in XMLMemento. */ public Integer getInteger(String key) { Attr attr = element.getAttributeNode(key); if (attr == null) { return null; } String strValue = attr.getValue(); try { return new Integer(strValue); } catch (NumberFormatException e) { // WorkbenchPlugin // .log("Memento problem - invalid integer for key: " + key //$NON-NLS-1$ // + " value: " + strValue, e); //$NON-NLS-1$ return null; } } /* * (non-Javadoc) Method declared in XMLMemento. */ public String getString(String key) { Attr attr = element.getAttributeNode(key); if (attr == null) { return null; } return attr.getValue(); } /** * @since 3.4 */ public Boolean getBoolean(String key) { Attr attr = element.getAttributeNode(key); if (attr == null) { return null; } return Boolean.valueOf(attr.getValue()); } /** * Returns the data of the Text node of the memento. Each memento is allowed * only one Text node. * * @return the data of the Text node of the memento, or <code>null</code> if * the memento has no Text node. * @since 2.0 * @throws DOMException * if the text node is too big */ public String getTextData() throws DOMException { Text textNode = getTextNode(); if (textNode != null) { return textNode.getData(); } return null; } /** * @since 3.4 */ public String[] getAttributeKeys() { NamedNodeMap map = element.getAttributes(); int size = map.getLength(); String[] attributes = new String[size]; for (int i = 0; i < size; i++) { Node node = map.item(i); attributes[i] = node.getNodeName(); } return attributes; } /** * Returns the Text node of the memento. Each memento is allowed only one * Text node. * * @return the Text node of the memento, or <code>null</code> if the memento * has no Text node. */ private Text getTextNode() { // Get the nodes. NodeList nodes = element.getChildNodes(); int size = nodes.getLength(); if (size == 0) { return null; } for (int nX = 0; nX < size; nX++) { Node node = nodes.item(nX); if (node instanceof Text) { return (Text) node; } } // a Text node was not found return null; } /** * Places the element's attributes into the document. * * @param copyText * true if the first text node should be copied * @throws DOMException * if the attributes or children cannot be copied to this node. */ private void putElement(Element element, boolean copyText) throws DOMException { NamedNodeMap nodeMap = element.getAttributes(); int size = nodeMap.getLength(); for (int i = 0; i < size; i++) { Attr attr = (Attr) nodeMap.item(i); putString(attr.getName(), attr.getValue()); } NodeList nodes = element.getChildNodes(); size = nodes.getLength(); // Copy first text node (fixes bug 113659). // Note that text data will be added as the first child (see // putTextData) boolean needToCopyText = copyText; for (int i = 0; i < size; i++) { Node node = nodes.item(i); if (node instanceof Element) { XMLMemento child = (XMLMemento) createChild(node.getNodeName()); child.putElement((Element) node, true); } else if (node instanceof Text && needToCopyText) { putTextData(((Text) node).getData()); needToCopyText = false; } } } /** * Sets the value of the given key to the given floating point number. * * @param key * the key * @param f * the value * @throws DOMException * if the attribute cannot be set */ public void putFloat(String key, float f) throws DOMException { element.setAttribute(key, String.valueOf(f)); } /** * Sets the value of the given key to the given integer. * * @param key * the key * @param n * the value * @throws DOMException * if the attribute cannot be set */ public void putInteger(String key, int n) throws DOMException { element.setAttribute(key, String.valueOf(n)); } /** * Copy the attributes and children from <code>memento</code> to the * receiver. * * @param memento * the XMLMemento to be copied. * @throws DOMException * if the attributes or children cannot be copied to this node. */ public void putMemento(XMLMemento memento) throws DOMException { // Do not copy the element's top level text node (this would overwrite // the existing text). // Text nodes of children are copied. putElement(((XMLMemento) memento).element, false); } /** * Sets the value of the given key to the given string. * * @param key * the key * @param value * the value * @throws DOMException * if the attribute cannot be set */ public void putString(String key, String value) throws DOMException { if (value == null) { return; } element.setAttribute(key, value); } /** * Sets the value of the given key to the given boolean value. * * @param key * the key * @param value * the value * @since 3.4 * @throws DOMException * if the attribute cannot be set */ public void putBoolean(String key, boolean value) throws DOMException { element.setAttribute(key, value ? "true" : "false"); //$NON-NLS-1$ //$NON-NLS-2$ } /** * Sets the memento's Text node to contain the given data. Creates the Text * node if none exists. If a Text node does exist, it's current contents are * replaced. Each memento is allowed only one text node. * * @param data * the data to be placed on the Text node * @since 2.0 * @throws DOMException * if the text node cannot be created under this node. */ public void putTextData(String data) throws DOMException { Text textNode = getTextNode(); if (textNode == null) { textNode = factory.createTextNode(data); // Always add the text node as the first child (fixes bug 93718) element.insertBefore(textNode, element.getFirstChild()); } else { textNode.setData(data); } } /** * Saves this memento's document current values to the specified writer. * * @param writer * the writer used to save the memento's document * @throws IOException * if there is a problem serializing the document to the stream. */ public void save(Writer writer) throws IOException { DOMWriter out = new DOMWriter(writer); try { out.print(element); } finally { out.close(); } } /** * A simple XML writer. Using this instead of the javax.xml.transform * classes allows compilation against JCL Foundation (bug 80053). */ private static final class DOMWriter extends PrintWriter { /* constants */ private static final String XML_VERSION = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; //$NON-NLS-1$ /** * Creates a new DOM writer on the given output writer. * * @param output * the output writer */ public DOMWriter(Writer output) { super(output); println(XML_VERSION); } /** * Prints the given element. * * @param element * the element to print */ public void print(Element element) { // Ensure extra whitespace is not emitted next to a Text node, // as that will result in a situation where the restored text data // is not the // same as the saved text data. boolean hasChildren = element.hasChildNodes(); startTag(element, hasChildren); if (hasChildren) { boolean prevWasText = false; NodeList children = element.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node node = children.item(i); if (node instanceof Element) { if (!prevWasText) { println(); } print((Element) children.item(i)); prevWasText = false; } else if (node instanceof Text) { print(getEscaped(node.getNodeValue())); prevWasText = true; } } if (!prevWasText) { println(); } endTag(element); } } private void startTag(Element element, boolean hasChildren) { StringBuffer sb = new StringBuffer(); sb.append("<"); //$NON-NLS-1$ sb.append(element.getTagName()); NamedNodeMap attributes = element.getAttributes(); for (int i = 0; i < attributes.getLength(); i++) { Attr attribute = (Attr) attributes.item(i); sb.append(" "); //$NON-NLS-1$ sb.append(attribute.getName()); sb.append("=\""); //$NON-NLS-1$ sb.append(getEscaped(String.valueOf(attribute.getValue()))); sb.append("\""); //$NON-NLS-1$ } sb.append(hasChildren ? ">" : "/>"); //$NON-NLS-1$ //$NON-NLS-2$ print(sb.toString()); } private void endTag(Element element) { StringBuffer sb = new StringBuffer(); sb.append("</"); //$NON-NLS-1$ sb.append(element.getNodeName()); sb.append(">"); //$NON-NLS-1$ print(sb.toString()); } private static void appendEscapedChar(StringBuffer buffer, char c) { String replacement = getReplacement(c); if (replacement != null) { buffer.append('&'); buffer.append(replacement); buffer.append(';'); } else if (c == 9 || c == 10 || c == 13 || c >= 32) { buffer.append(c); } } private static String getEscaped(String s) { StringBuffer result = new StringBuffer(s.length() + 10); for (int i = 0; i < s.length(); ++i) { appendEscapedChar(result, s.charAt(i)); } return result.toString(); } private static String getReplacement(char c) { // Encode special XML characters into the equivalent character // references. // The first five are defined by default for all XML documents. // The next three (#xD, #xA, #x9) are encoded to avoid them // being converted to spaces on deserialization // (fixes bug 93720) switch (c) { case '<': return "lt"; //$NON-NLS-1$ case '>': return "gt"; //$NON-NLS-1$ case '"': return "quot"; //$NON-NLS-1$ case '\'': return "apos"; //$NON-NLS-1$ case '&': return "amp"; //$NON-NLS-1$ case '\r': return "#x0D"; //$NON-NLS-1$ case '\n': return "#x0A"; //$NON-NLS-1$ case '\u0009': return "#x09"; //$NON-NLS-1$ } return null; } } }
发表评论
-
dom4j UserDataDocumentFactory
2015-03-20 00:30 0package word; import org ... -
java 命令
2014-10-13 09:54 0命令分析: jhat [-stack <b ... -
Httpclient post form
2014-09-26 14:06 0String u = "http:// ... -
java 异常
2014-09-04 15:41 0如果实现了接口,但是接口中的某个方法不需要实现,则抛 ... -
guava 类扫描
2014-06-29 17:10 1465ClassPath classpath = Clas ... -
guava 动态代理
2014-06-29 16:31 1038JDK: Foo foo = (Foo) Pro ... -
Guava 异常处理例子
2014-06-29 16:15 1617Closer closer = Closer.cr ... -
JSF Converter-定义通用的 状态 转换器
2014-06-29 11:10 1076创建StatusConverter类: /* ... -
未实现方法抛异常
2014-05-29 16:51 855如果类实现了接口却有部分方法不想实现,则在方法体中抛异常:U ... -
Eclipse RCP 神奇错误,浪费了一天时间.
2014-05-12 00:44 1465错误信息: Internal compiler ... -
在同一linux主机部署多个Tomcat
2013-11-01 12:06 3这里是为tomcat单独配置jdk版本,如果服务器j ... -
MyProgressMonitorDialog
2013-09-28 12:16 1016package xxx; import java.l ... -
linux 配置两个Tomcat
2013-06-21 13:42 1245第一个Tomcat使用默认配置 第二个Tomcat ... -
Eclipse RCP
2013-04-30 16:57 946eclipse 运行需要jre,但如果开发的程序要指定某个 ... -
让输出到txt文件中的字符串整齐排列
2013-01-17 13:36 1900让输出到txt文件中的字符串整齐排列 public st ... -
swing jtextarea 自动行高
2013-01-07 14:17 2533哈哈哈.... final JTextArea tex ... -
java WeakReference
2012-11-08 20:49 0http://www.8844.com/article/d/2 ... -
swt
2012-07-28 00:46 1034swt/jface api chm格式: http://dl ... -
java版编辑距离(字符串相似度)算法 levenshtein (edit distance)
2012-07-05 10:40 12002/** * levenshtein (edit dis ... -
字符串相似度算法(收藏)
2012-06-30 16:03 0package org.zero.system.stor ...
相关推荐
标题中的“Eclipse XML自动提示DTD文件下载”指的是在使用Eclipse IDE进行XML开发...对于像Hibernate和Struts这样的框架,理解并正确配置其DTD是非常关键的,因为这些框架的XML配置文件都依赖于特定的DTD来定义其结构。
这款插件对于开发者,特别是那些在处理Hibernate等框架时需要频繁与XML配置文件打交道的人来说,是非常实用的工具。 首先,XMLBuddy提供了丰富的语法高亮和代码自动完成功能。它能够智能识别XML标签、属性和值,...
例如,可以集成Java项目的构建过程,使得XML配置文件的修改能即时反映到项目中。 5. **插件安装与管理**: Eclipse的"软件更新"功能允许用户方便地查找、安装和管理插件。开发者可以在Eclipse Marketplace或其他开源...
androidstudio java代码eclipse风格的代码格式化配置文件。 1.插件安装:Androidstudio插件市场安装Adapter for Eclipse ...3.xml配置文件在附件中 4.默认快捷键 CTRL+ALT+L快速格式化代码,比如缩进、对齐、空格等
Spring容器通过XML配置文件或注解来定义Bean及其相互关系。 3. **XML配置文件**: "spring-beans"是Spring中用于配置Bean的XML文件。该文件包含了一系列的元素,每个元素表示一个Java对象的定义,包括类名、属性值...
在Eclipse中,`formatter.xml` 和 `codetemplates.xml` 是两个非常重要的配置文件,它们分别用于控制代码的格式化规则和代码模板。 首先,我们来看`formatter.xml`文件。这个文件是Eclipse的代码格式化器配置,它...
XML配置文件的处理使用`XMLConfiguration`类。同样,通过指定文件路径,可以创建配置实例并获取配置信息。 2. **写入XML配置文件** 添加、修改XML配置使用`setProperty()`方法,删除则使用`removeProperty()`。保存...
在使用Eclipse开发Java项目时,XML文件是常见的配置文件,但有时我们可能会遇到一个困扰:在编辑XML文件时,Eclipse没有提供自动提示功能,这无疑会降低我们的开发效率。这个问题的出现可能是由于Eclipse的相关插件...
本篇将深入探讨XML配置文件中的快捷提示和配置方法,以帮助开发者快速理解和应用。 首先,XML配置文件的核心是元素(Element),它是XML文档的基本构建块。每个元素都有一个标签(Tag),例如`<element>`,并在开始...
Eclipse格式模板XML文件的结构通常包括键值对,其中键代表配置选项,值代表该选项的具体设置。例如,`org.eclipse.jdt.core.formatter.tabulation.char`用于设置缩进字符,而`org.eclipse.jdt.core.formatter....
用于maven setting.xml文件丢失,eclipse集成maven插件.m2下无setting.xml文件需要配置的情况
maven settings.xml配置文件,亲试无问题,可以使用,eclipse和Myeclipse都可以
本文将详细介绍如何解决Eclipse中配置文件(特别是Spring框架相关的XML配置文件)没有代码提示的问题。 #### 解决方案 本解决方案主要针对Spring框架的XML配置文件,通过引入schema约束来实现代码提示功能。具体...
通过这个项目,开发者不仅可以了解SSM框架的集成方式,还能学习到如何在Eclipse中进行项目配置,以及如何使用XML配置文件来实现业务逻辑。此外,实践中遇到的问题也会加深对SSM框架理解,提升问题解决能力。
Eclipse代码格式化配置文件.xml
我的Eclipse CDT配置文件,设置了语法高亮等常用配置。
这个"eclipse的代码格式化文件"就是一个预设好的格式化配置,你可以将其导入到Eclipse中,以便在编辑XML文件时自动应用这些格式规则。 导入步骤通常如下: 1. 打开Eclipse,选择`Window` -> `Preferences`进入设置...
### Eclipse中XML文件里自动提示消失解决办法 在Eclipse这款强大的开源集成开发环境中,XML文件的编写是非常常见的任务之一。然而,在实际操作过程中,不少用户可能会遇到XML文件中的自动提示功能突然消失的问题,...
Jetty是一款轻量级、高性能的Java Web服务器和Servlet容器,它被广泛用于开发、测试和部署Web应用程序。...而"jetty.rar"文件可能是包含Jetty服务器的示例或者额外配置文件,你可以根据需要解压查看。
Spring Framework的bean配置通常遵循特定的XSD文件,这些文件定义了Spring配置文件的元素和属性。 2. **配置Eclipse的XML Catalog**: - 打开Eclipse,转到`Window`菜单,然后选择`Preferences`。 - 在偏好设置...