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

📄 xmlmanipulator.java

📁 java web 开发,Java Xml 编程指南书籍源码
💻 JAVA
字号:
package MyNa.xml;

import MyNa.utils.*;
import java.io.*;
import java.util.*;
import java.sql.SQLException;


import org.w3c.dom.Node;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.DocumentType;
import org.w3c.dom.Attr;


import org.xml.sax.SAXParseException;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import com.sun.xml.parser.Resolver;
import com.sun.xml.tree.XmlDocument;
import org.w3c.dom.Element;


public class XmlManipulator extends DocWalker {

   Node clipNode=null;
   boolean multiplePaste=false;

public XmlManipulator(){
   super();
}

public void setMultiplePaste(boolean mp){
  multiplePaste=mp;
}
public String doCommand(){
  String cmd=defs.getStr("Command");
  if(null==cmd)return addNodeData("No 'Command' given");
  addError("");
  if(cmd.equals("copy"))return copy();
  if(cmd.equals("cut"))return cut();
  checkMultiplePaste();
  if(cmd.equals("pasteAfter"))return pasteAfter();
  if(cmd.equals("pasteBefore"))return pasteBefore();
  if(cmd.equals("pasteUnder"))return pasteUnder();
  else return super.doCommand();
}

public void checkMultiplePaste(){
  String ok=defs.getStr("multiplePaste");
  if(null==ok)return;
  if(ok.equals("yes"))multiplePaste=true;
  else if(ok.equals("no"))multiplePaste=false;
}

public String copy(){
  if(null==theNode)return addNodeData("cannot copy null");
  clipNode=theNode.cloneNode(true); //deep copy
  return addNodeData(theNode);  
}

public String pasteUnder(){ //add as last child
  if(null==clipNode)return addNodeData("cannot paste null");
  if(null==theNode)theNode=clipNode;
  else theNode.appendChild(clipNode); 
  if(multiplePaste) clipNode=clipNode.cloneNode(true);
  else clipNode=null; // cannot append object more than once
  return addNodeData(theNode);  
}
public String pasteBefore(){
  if(null==clipNode)return addNodeData("cannot paste null");
  if(null==theNode)return addNodeData(theNode=clipNode);  
  Node parent=theNode.getParentNode();
  if(null==parent)return addNodeData("cannot pasteBefore without parent");
  parent.insertBefore(clipNode,theNode); 
  if(multiplePaste) clipNode=clipNode.cloneNode(true);
  else clipNode=null; // cannot append object more than once
  return addNodeData(theNode);  
}
public String pasteAfter(){
  if(null==clipNode)return addNodeData("cannot paste null");
  if(null==theNode)return addNodeData(theNode=clipNode);  
  Node parent=theNode.getParentNode();
  if(null==parent)return addNodeData("cannot pasteAfter without parent");
  parent.insertBefore(clipNode,theNode.getNextSibling()); 
    // this is fine even if getNextSibling() is null
  if(multiplePaste) clipNode=clipNode.cloneNode(true);
  else clipNode=null; // cannot append object more than once
  return addNodeData(theNode);  
}

public String cut(){
  if(null==theNode)return addNodeData("cannot cut null");
  clipNode=theNode;
  Node parent=theNode.getParentNode();
  Node next=theNode.getNextSibling();
  if(null==next)next=parent;
  if(null!=parent)parent.removeChild(theNode);
  theNode=next;
  return addNodeData(theNode);
}

}

⌨️ 快捷键说明

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