Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: diffxml/Patch/Reverse.java


1   /*
2   Program to reverse "sense" of a patch
3    
4   Copyright (C) 2002  Adrian Mouat
5    
6   This program is free software; you can redistribute it and/or
7   modify it under the terms of the GNU General Public License
8   as published by the Free Software Foundation; either version 2
9   of the License, or (at your option) any later version.
10   
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  GNU General Public License for more details.
15  
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19   
20  Author: Adrian Mouat
21  email: amouat@postmaster.co.uk
22  */
23  
24  package diffxml.Patch;
25  
26  import org.w3c.dom.Document;
27  import org.w3c.dom.Element;
28  import org.w3c.dom.Node;
29  import org.w3c.dom.Attr;
30  import org.w3c.dom.traversal.*;
31  
32  public class Reverse
33  {
34  
35  public static Document go(Document patch)
36  {
37  Node root=patch.getDocumentElement();
38  NodeIterator ni = ((DocumentTraversal) patch).createNodeIterator
39                          (root, NodeFilter.SHOW_ELEMENT, null, false);
40  
41  Node op=ni.nextNode();
42  //Check reveresable delta doc
43  
44  if (! (op.getNodeName().equals("delta") && ((Element)op).getAttribute("reverse_patch").equals("true")) )
45    {
46    System.err.println("Not a reversable delta");
47    System.exit(2);
48    }
49  
50  //Loop through nodes, reversing ops
51  
52  op=ni.nextNode();
53  
54  while (op!=null)
55    {
56    String op_name=op.getNodeName();
57    
58    if (op_name.equals("insert"))
59      {
60      //We want an insert to become a delete of the inserted node
61      Element del=patch.createElement("delete");  
62      
63      //XPath of node is parent path plus domcn
64      String path=((Element)op).getAttribute("parent");
65      path=path+"/node()["+((Element)op).getAttribute("childno")+"]";
66      del.setAttribute("node",path);
67  
68      //charpos remains the same
69      String charpos=((Element)op).getAttribute("charpos");
70      if (charpos!="")
71        del.setAttribute("charpos",charpos);
72  
73      //set length
74      if ( ((Element)op).getAttribute("type").equals(""+Node.TEXT_NODE))
75        del.setAttribute("length",""+op.getNodeValue().length());      
76      //Update node
77      op.getParentNode().replaceChild(del,op);  
78      }
79    else if (op_name.equals("delete"))
80      {
81      //We want delete to become insert
82      Element ins=patch.createElement("insert");
83      
84      //Parent node is truncated path
85      String path=((Element)op).getAttribute("node");
86      ins.setAttribute("parent", path.substring(0,path.lastIndexOf("/")));
87      //Assume childno is last bit of path
88      //MAY NOT ALWAYS WORK!!!
89      ins.setAttribute("childno",
90        path.substring(path.lastIndexOf("node()[")+7,
91            path.lastIndexOf("]")));  
92      
93      //Leave charpos the same
94                  String charpos=((Element)op).getAttribute("charpos");
95                  if (charpos!="")
96                          ins.setAttribute("charpos",charpos);
97  
98      //Leave nodetype the same
99      ins.setAttribute("nodetype",((Element)op).getAttribute("nodetype"));  
100     
101     //Value remains the same
102     ins.appendChild(op.getLastChild());
103     op.getParentNode().replaceChild(ins,op);
104     }
105   else if (op_name.equals("move"))
106     {
107     //Want move to swap parent and node, old_char and new_char
108     
109     //Store old values
110     String path=((Element)op).getAttribute("node");
111     String par_path=((Element)op).getAttribute("parent");
112     
113     //Parent node is truncated path
114     ((Element)op).setAttribute("parent", (path.substring(0,path.lastIndexOf("/")-1)));
115     //Assume childno is last bit of path
116                 //MAY NOT ALWAYS WORK!!!
117                 ((Element)op).setAttribute("childno",
118                         path.substring(path.lastIndexOf("node()["+1),
119                                         path.lastIndexOf("]")-1));
120     
121     //Swap charpos
122     String ocharpos=((Element)op).getAttribute("old_charpos");
123     String ncharpos=((Element)op).getAttribute("new_charpos");
124   
125     if (ocharpos!="")
126       ((Element)op).setAttribute("new_charpos",ocharpos);
127   
128     if (ncharpos!="")
129       ((Element)op).setAttribute("old_charpos",ncharpos);
130     }
131   op=ni.nextNode();
132   }
133 return patch;
134 }
135 }
136     
137