jdomadapter.java
来自「Java Pattern Oriented Framework (Jt) 是为了」· Java 代码 · 共 762 行 · 第 1/2 页
JAVA
762 行
return (element);
}
private Element insertElement (String parentPath, Element element, Integer Index) {
Element parent;
if (parentPath == null) {
return (null);
}
if (Index == null)
return null;
if (element == null) {
handleError ("insertElement: invalid element (null)");
return null;
}
parent = findElement (parentPath);
if (parent == null) {
handleError ("insertElement: parent not found: " + parentPath);
return (null);
}
parent.addContent(Index.intValue(), element);
return (element);
}
private Object elementToBean (Element elem, Object obj) {
JtMessage msg = new JtMessage (JtFactory.JtGET_ATTRIBUTES);
JtMessage msg1 = new JtMessage (JtFactory.JtCREATE_OBJECT);
JtFactory factory = new JtFactory ();
JtHashTable attributes;
JtIterator iterator;
String attName;
String attValue;
Object instance;
if (elem == null || obj == null)
return (null);
msg1.setMsgContent(obj.getClass().getName());
instance = factory.processMessage(msg1);
if (instance == null)
return (null);
factory.setStopClass(JtObject.class);
msg.setMsgContent(instance);
attributes = (JtHashTable) factory.processMessage(msg);
if (attributes == null)
return (null);
iterator = (JtIterator) attributes.processMessage
(new JtMessage (JtHashTable.JtGET_KEYS));
if (iterator == null)
return (null);
for (;;) {
attName = (String) iterator.processMessage(new JtMessage (JtIterator.JtNEXT));
if (attName == null)
break;
attValue = elem.getAttributeValue(attName);
if (attValue != null) {
factory.setValue (instance, attName, attValue);
}
}
return (instance);
}
void attToString (List atts, StringBuffer buffer) {
Iterator iterator;
Attribute attr;
if (buffer == null || atts == null)
return;
iterator = atts.iterator();
if (iterator == null)
return;
for (;;) {
if (!iterator.hasNext())
break;
attr = (Attribute) iterator.next();
buffer.append(" " + attr.getName() + "=\"" + attr.getValue() + "\"");
}
}
/*
void elementToString (Element element, StringBuffer buffer, int level) {
List atts;
List children;
int size;
Namespace nspace;
String eName;
String text;
if (element == null)
return;
nspace = element.getNamespace();
if (nspace == null || nspace.getPrefix() == null || "".equals (nspace.getPrefix()))
eName = element.getName();
else
eName = nspace.getPrefix() + ":" + element.getName();
for (int i = 0; i < level; i++)
buffer.append(" ");
buffer.append("<" + eName);
atts = element.getAttributes();
if (atts != null)
attToString (atts, buffer);
text = element.getTextTrim();
children = element.getChildren();
if ((children.size() == 0) && (text == null || text.equals("")))
buffer.append(" />\n");
else {
buffer.append(">");
if (text != null && !text.equals(""))
buffer.append(text);
//if (!"td".equals (eName))
if (children.size() != 0)
buffer.append("\n");
}
//text = element.getTextTrim();
//if (text != null && !text.equals(""))
// buffer.append(text);
children = element.getChildren();
if (children.size() != 0) {
size = children.size ();
for (int i = 0; i < size; i++) {
element = (Element) children.get(i);
//if ("td".equals (eName))
// elementToString (element, buffer, 0);
//else
elementToString (element, buffer, level + 1);
}
}
if (!((children.size() == 0) && (text == null || text.equals("")))) {
//if (!"td".equals (eName))
if (children.size() != 0)
for (int i = 0; i < level; i++)
buffer.append(" ");
buffer.append("</" + eName + ">\n");
}
}
*/
String xmlToString () {
//StringBuffer buffer = new StringBuffer ();
//Element element;
if (doc == null)
return (null);
//element = doc.getRootElement ();
//if (element == null)
// return (null);
return (outputter.outputString (doc));
//elementToString (element, buffer, 0);
//return (buffer.toString ());
}
/**
* Process object messages.
* <ul>
* <li>READ_FILE - Reads a XML file. The attribute path specifies the file to be read.
* <li>FIND_ELEMENT - Finds the element specified by msgContent.
* <li>REPLACE_ELEMENT - Replaces the text associated with element specified by msgContent.
* msgData contains the new text.
* <li>ADD_ELEMENT - Adds the element specified by msgContent. msgData specifies
* the parent element.
* <li>ADD_CHILD - Adds the element specified by msgData (as a child). msgContent specifies
* the parent element.
* <li>CLONE_ELEMENT - Clones the element specified by msgContent.
* <li>SAVE_FILE - Saves XML content to the file specified by the path attribute.
* <li>GET_CHILDREN - Returns the list of all child elements for the element specified by msgContent.
* </ul>
*/
public Object processMessage (Object message) {
String msgid = null;
JtMessage msg = (JtMessage) message;
Object content;
Object data;
if (msg == null)
return null;
msgid = (String) msg.getMsgId ();
if (msgid == null)
return null;
if (msg == null || msg.getMsgId () == null) {
handleError ("Invalid message");
return null;
}
if (!initialized) {
initializeAdapter (); // check
initialized = true;
}
content = msg.getMsgContent();
data = msg.getMsgData ();
if (msgid.equals (JDOMAdapter.READ_FILE))
return readFile ();
if (msgid.equals (JDOMAdapter.READ_STREAM))
return readStream ();
if (msgid.equals (JDOMAdapter.FIND_ELEMENT))
return findElement ((String) msg.getMsgContent());
if (msgid.equals (JDOMAdapter.FIND_ELEMENTS))
return findElements ((String) msg.getMsgContent());
if (msgid.equals(JDOMAdapter.ADD_ELEMENT)) {
addElement ((String) data, (Element) content);
return (null);
}
if (msgid.equals(JDOMAdapter.ADD_CHILD)) {
addChild ((Element) content, (Element) data);
return (null);
}
if (msgid.equals(JDOMAdapter.INSERT_ELEMENT)) {
insertElement ((String) data, (Element) content,
(Integer) msg.getMsgAttachment());
return (null);
}
if (msgid.equals (JDOMAdapter.REPLACE_ELEMENT))
return replaceElement ((String) msg.getMsgContent(),
(String) msg.getMsgData());
if (msgid.equals (JDOMAdapter.GET_CHILDREN))
return getChildren ((String) msg.getMsgContent());
if (msgid.equals (JDOMAdapter.CLONE_ELEMENT))
return cloneElement ((String) msg.getMsgContent());
if (msgid.equals (JDOMAdapter.ELEMENT_TO_BEAN)) {
return elementToBean ((Element) msg.getMsgContent(),
(Object) msg.getMsgData ());
}
if (msgid.equals(JDOMAdapter.STRING_TO_XML))
return (stringToXML ((String) msg.getMsgContent()));
if (msgid.equals(JDOMAdapter.XML_TO_STRING))
return (xmlToString ());
if (msgid.equals(JDOMAdapter.SAVE_FILE)) {
saveFile ();
return (null);
}
return (super.processMessage (message));
}
/**
* Returns the path to the XML file.
*/
public String getPath() {
return path;
}
/**
* Specifies the path to the XML file.
*/
public void setPath(String path) {
this.path = path;
}
/**
* Demonstrates the messages processed by this class.
*/
public static void main(String[] args) {
JtMessage msg = new JtMessage ();
JtFactory factory = new JtFactory ();
JDOMAdapter adapter;
Element element = new Element ("Root");
DocType docType;
adapter = (JDOMAdapter) factory.createObject(JDOMAdapter.JtCLASS_NAME);
// Add elements
msg.setMsgId(JDOMAdapter.ADD_ELEMENT);
msg.setMsgContent (element);
factory.sendMessage(adapter, msg);
element = new Element ("class");
element.setAttribute("name", "Jt.Object");
msg.setMsgContent (element);
msg.setMsgData ("Root");
factory.sendMessage(adapter, msg);
element = new Element ("id");
element.setAttribute("name", "email");
msg.setMsgContent (element);
msg.setMsgData ("Root/class");
factory.sendMessage(adapter, msg);
element = new Element ("property");
element.setAttribute("name", "firstname");
msg.setMsgContent (element);
msg.setMsgData ("Root/class");
factory.sendMessage(adapter, msg);
docType = new DocType ("hibernate-mapping",
"-//Hibernate/Hibernate Mapping DTD 3.0//EN",
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd");
adapter.setDocType(docType);
//msg.setMsgId(new Integer (JdomAdapter.READ_FILE));
//output = (String) adapter.processMessage(msg);
System.out.println (adapter.getContent());
//msg = new JtMessage (JdomAdapter.SAVE_FILE);
//adapter.processMessage(msg); // Save the file
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?