jdomadapter.java
来自「Java Pattern Oriented Framework (Jt) 是为了」· Java 代码 · 共 762 行 · 第 1/2 页
JAVA
762 行
package Jt.xml;
import java.io.*;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
import org.jdom.xpath.*;
import Jt.JtAdapter;
import Jt.JtFactory;
import Jt.JtHashTable;
import Jt.JtIterator;
import Jt.JtMessage;
import Jt.JtObject;
/**
* Adapter for the JDOM API.
*/
public class JDOMAdapter extends JtAdapter {
private static final long serialVersionUID = 1L;
private SAXBuilder builder = null;
private String path;
public static final String JtCLASS_NAME = JDOMAdapter.class.getName();
public final static String READ_FILE = "READ_FILE";
public final static String READ_STREAM = "READ_STREAM";
public final static String FIND_ELEMENT = "FIND_ELEMENT";
public final static String REPLACE_ELEMENT = "REPLACE_ELEMENT";
public final static String ADD_ELEMENT = "ADD_ELEMENT";
public final static String INSERT_ELEMENT = "INSERT_ELEMENT";
public final static String CLONE_ELEMENT = "CLONE_ELEMENT";
public final static String STRING_TO_XML = "STRING_TO_XML";
public final static String SAVE_FILE = "SAVE_FILE";
public final static String XML_TO_STRING = "XML_TO_STRING"; //check
public final static String GET_CHILDREN = "GET_CHILDREN";
public final static String ELEMENT_TO_BEAN = "ELEMENT_TO_BEAN";
public final static String FIND_ELEMENTS = "FIND_ELEMENTS";
public final static String ADD_CHILD = "ADD_CHILD";
private Document doc = null;
private String content = null;
XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
private boolean initialized = false;
private InputStream inputStream;
private List elementList = new LinkedList ();
/**
* Initialize the adapter
*/
private void initializeAdapter () {
try {
builder = new SAXBuilder();
} catch (Exception ex) {
handleException (ex);
}
}
private void saveFile () {
File file;
PrintWriter out;
if (path == null) {
handleError ("attribute path needs to be set.");
return;
}
file = new File (path);
try {
out = new PrintWriter (new FileWriter (file));
getContent ();
out.print(content);
out.close();
} catch (Exception e) {
handleException (e);
}
}
/**
* Reads the XML file specified by path.
* @return string representation of the XML file.
*/
public String readFile () {
if (path == null) {
handleError ("attribute path needs to be set.");
return (null);
}
handleTrace ("JDOMAdapter.readFile:" + path);
try {
doc = builder.build(path);
} catch (Exception ex) {
handleException (ex);
}
if (doc == null)
return (null);
outputter = new XMLOutputter(Format.getPrettyFormat());
return (outputter.outputString (doc));
}
private String readStream () {
if (inputStream == null) {
handleError ("attribute inputStream needs to be set.");
return (null);
}
try {
doc = builder.build(inputStream);
} catch (Exception ex) {
handleException (ex);
}
if (doc == null)
return (null);
outputter = new XMLOutputter(Format.getPrettyFormat());
return (outputter.outputString (doc));
}
public InputStream getInputStream() {
return inputStream;
}
public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
/**
* Returns the DocType declaration.
* @return DocType
*/
public DocType getDocType() {
if (doc != null)
return doc.getDocType();
else
return null;
}
/**
* Sets the DocType declaration.
*/
public void setDocType(DocType docType) {
if (doc != null)
doc.setDocType(docType);
}
private Document stringToXML (String str) {
doc = null;
if (str == null)
return null;
try {
doc = builder.build(new StringReader (str));
} catch (Exception ex) {
handleException (ex);
}
return doc;
}
/**
* Returns the string representing the XML document
* @return string representing the XML document
*/
public String getContent() {
if (doc == null) {
content = null;
return (null);
}
outputter = new XMLOutputter(Format.getPrettyFormat());
content = outputter.outputString (doc);
return content;
}
/**
* Element lookup
* @param elementPath
* @return element or null
*/
private Element findElement (String elementPath) {
XPath path;
Element elem = null;
if (elementPath == null)
return (null);
if (doc == null)
return (null);
try {
path = XPath.newInstance (elementPath);
elem = (Element) path.selectSingleNode (doc);
} catch (JDOMException e) {
handleException (e);
return (null);
}
return (elem);
}
private List getChildren (String elementPath) {
Element elem;
if (elementPath == null)
return (null);
elem = findElement (elementPath);
if (elem == null)
return (null);
return (elem.getChildren());
}
private void findElements (List children, String elementName) {
String name;
Iterator iterator;
Element elem;
if (children == null || elementName == null)
return;
iterator = children.iterator();
while (iterator.hasNext()) {
elem = (Element) iterator.next();
name = elem.getName();
//System.out.println("XML ........................" + name);
if (elementName.equals (name)) {
elementList.add(elem);
} else
findElements (elem.getChildren(), elementName);
}
}
private List findElements (String elementName) {
Element elem;
List children;
if (elementName == null)
return (null);
if (doc == null)
return null;
try {
elem = doc.getRootElement();
} catch (Exception ex) {
handleException (ex);
return null;
}
if (elem == null)
return (null);
elementList.clear();
children = elem.getChildren();
findElements (children, elementName);
return (elementList);
}
/**
* Sets the content of an element to be the text given.
* @param elementPath element path
* @param elementValue new text Value
* @return element or null
*/
private Element replaceElement (String elementPath, String elementValue) {
Element elem;
if (elementPath == null) {
return null;
}
elem = findElement (elementPath);
if (elem == null) {
handleError ("replaceElement: element not found: " + elementPath);
return (elem);
}
elem.setText (elementValue);
return (elem);
}
private Element cloneElement (String elementPath) {
Element elem, parent, tmp;
if (elementPath == null) {
return null;
}
elem = findElement (elementPath);
if (elem == null) {
handleError ("cloneElement: element not found: " + elementPath);
return (elem);
}
parent = elem.getParentElement();
if (parent == null)
return (null);
tmp = (Element) elem.clone();
parent.addContent(tmp);
return (tmp);
}
private void addChild (Element parent, Element child) {
if (parent == null || child == null)
return;
parent.addContent(child);
}
private Element addElement (String parentPath, Element element) {
Element parent;
if (parentPath == null) {
if (doc == null)
doc = new Document ();
doc.setRootElement(element); // check
return (element);
}
if (element == null) {
handleError ("addElement: invalid element (null)");
return null;
}
parent = findElement (parentPath);
if (parent == null) {
handleError ("addElement: parent not found: " + parentPath);
return (null);
}
//parent = elem.getParentElement();
//if (parent == null)
// return (null);
//tmp = (Element) elem.clone();
//parent.addContent(element);
parent.addContent(element);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?