📄 myxmlmemento.java
字号:
if (attr == null) {
return null;
}
return attr.getValue();
}
/* (non-Javadoc)
* Method declared in IMemento.
*/
public String getTextData() {
Text textNode = getTextNode();
if (textNode != null) {
return textNode.getData();
}
return null;
}
/**
* 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
*/
private void putElement(Element element, boolean copyText) {
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) {
MYXMLMemento child = (MYXMLMemento) createChild(node.getNodeName());
child.putElement((Element) node, true);
} else if (node instanceof Text && needToCopyText) {
putTextData(((Text) node).getData());
needToCopyText = false;
}
}
}
/* (non-Javadoc)
* Method declared in IMemento.
*/
public void putFloat(String key, float f) {
element.setAttribute(key, String.valueOf(f));
}
/* (non-Javadoc)
* Method declared in IMemento.
*/
public void putInteger(String key, int n) {
element.setAttribute(key, String.valueOf(n));
}
/* (non-Javadoc)
* Method declared in IMemento.
*/
public void putMemento(IMemento memento) {
// Do not copy the element's top level text node (this would overwrite the existing text).
// Text nodes of children are copied.
putElement(((MYXMLMemento) memento).element, false);
}
/* (non-Javadoc)
* Method declared in IMemento.
*/
public void putString(String key, String value) {
if (value == null) {
return;
}
element.setAttribute(key, value);
}
/* (non-Javadoc)
* Method declared in IMemento.
*/
public void putTextData(String data) {
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 {
private int tab;
/* constants */
private static final String XML_VERSION = "<?xml version=\"1.0\" encoding=\"GB2312\"?>"; //$NON-NLS-1$
/**
* Creates a new DOM writer on the given output writer.
*
* @param output the output writer
*/
public DOMWriter(Writer output) {
super(output);
tab = 0;
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) {
tab++;
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();
printTabulation();
}
print((Element) children.item(i));
prevWasText = false;
}
else if (node instanceof Text) {
print(getEscaped(node.getNodeValue()));
prevWasText = true;
}
}
tab--;
if (!prevWasText) {
println();
printTabulation();
}
endTag(element);
}
}
private void printTabulation() {
// Indenting is disabled, as it can affect the result of getTextData().
// In 3.0, elements were separated by a newline but not indented.
// This causes getTextData() to return "\n" even if no text data had explicitly been set.
// The code here emulates that behaviour.
// for (int i = 0; i < tab; i++)
// super.print("\t"); //$NON-NLS-1$
}
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 {
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;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -