View Javadoc

1   /*
2    * Copyright 1999-2006 Faculty of Mathematics, Physics and Informatics, Comenius
3    * University, Bratislava. This file is protected by the Mozilla Public License
4    * version 1.1 (the License); you may not use this file except in compliance
5    * with the License. You may obtain a copy of the License at
6    * http://euromath2.sourceforge.net/license.html Unless required by applicable
7    * law or agreed to in writing, software distributed under the License is
8    * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
9    * KIND, either express or implied. See the License for the specific language
10   * governing permissions and limitations under the License.
11   */
12  package sk.uniba.euromath.document;
13  import org.w3c.dom.Node;
14  import org.w3c.dom.ProcessingInstruction;
15  import sk.uniba.euromath.tools.StringTools;
16  /***
17   * Holds old values of the node that may have been changed. Class may be
18   * instantiated by clients.
19   * @author Martin Vysny
20   * @deprecated superseded by DOM2 Events
21   */
22  @Deprecated
23  public final class ChangeTracer {
24  	/***
25  	 * The constructor.
26  	 * @param node the node that is about to change. It must not yet be changed.
27  	 */
28  	public ChangeTracer(Node node) {
29  		super();
30  		changedNode = node;
31  		if (node.getNodeType() != Node.ELEMENT_NODE)
32  			oldData = DocumentContent.getData(node);
33  		else
34  			oldData = null;
35  		if ((node.getNodeType() == Node.ELEMENT_NODE)
36  				|| (node.getNodeType() == Node.ATTRIBUTE_NODE)) {
37  			oldLocalName = node.getLocalName();
38  		} else if (node.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) {
39  			oldLocalName = ((ProcessingInstruction) node).getTarget();
40  		} else
41  			oldLocalName = null;
42  	}
43  	/***
44  	 * After the change this method must be called once to trace what had been
45  	 * changed.
46  	 */
47  	public void trackChange() {
48  		if (changed != 0)
49  			throw new IllegalStateException();
50  		if (changedNode.getNodeType() != Node.ELEMENT_NODE) {
51  			String newData = DocumentContent.getData(changedNode);
52  			if (!StringTools.nonNullStr(newData).equals(oldData))
53  				changed |= CHANGED_DATA;
54  		}
55  		String newLocalName;
56  		if ((changedNode.getNodeType() == Node.ELEMENT_NODE)
57  				|| (changedNode.getNodeType() == Node.ATTRIBUTE_NODE)) {
58  			newLocalName = changedNode.getLocalName();
59  		} else if (changedNode.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) {
60  			newLocalName = ((ProcessingInstruction) changedNode).getTarget();
61  		} else
62  			newLocalName = null;
63  		if ((newLocalName != null) && !newLocalName.equals(oldLocalName))
64  			changed |= CHANGED_LOCALNAME;
65  		if (changed == 0)
66  			throw new AssertionError("Internal: The node wasn't changed."); //$NON-NLS-1$
67  	}
68  	/***
69  	 * Holds old textual value. Valid only for text, cdata, comment, processing
70  	 * instruction and attribute node
71  	 */
72  	private String oldData;
73  	/***
74  	 * Holds old textual value. Valid only for text, cdata, comment, processing
75  	 * instruction and attribute node.
76  	 * @return Old textual value.
77  	 */
78  	public String getOldData() {
79  		if (changed == 0)
80  			throw new IllegalStateException();
81  		if (changedNode.getNodeType() == Node.ELEMENT_NODE)
82  			throw new IllegalStateException("Element does not have data."); //$NON-NLS-1$
83  		return oldData;
84  	}
85  	/***
86  	 * Reference to the node that had been changed.
87  	 */
88  	private final Node changedNode;
89  	/***
90  	 * Reference to the node that had been changed.
91  	 * @return the node
92  	 */
93  	public Node getChangedNode() {
94  		if (changed == 0)
95  			throw new IllegalStateException();
96  		return changedNode;
97  	}
98  	/***
99  	 * Returns old local name of the node. Valid only for element, attribute and
100 	 * processing instruction node.
101 	 */
102 	private String oldLocalName;
103 	/***
104 	 * Returns old local name of the node. Valid only for element, attribute and
105 	 * processing instruction node.
106 	 * @return local name for attribute and element node, and the name for the
107 	 * processing instruction node.
108 	 */
109 	public String getOldLocalName() {
110 		if (changed == 0)
111 			throw new IllegalStateException();
112 		return oldLocalName;
113 	}
114 	/***
115 	 * Indicates that the text had been changed. Valid only for text, cdata,
116 	 * comment and processing instruction node.
117 	 */
118 	public final static int CHANGED_DATA = 0x01;
119 	/***
120 	 * Indicates that the text had been changed.
121 	 */
122 	public final static int CHANGED_LOCALNAME = 0x02;
123 	/***
124 	 * Determines what had changed. Combination of <code>CHANGED_*</code>
125 	 * constants.
126 	 */
127 	private int changed = 0;
128 	/***
129 	 * Determines what had changed. Combination of <code>CHANGED_*</code>
130 	 * constants.
131 	 * @return bitfield flag describing the node properties that had been
132 	 * changed. At least one flag is always set.
133 	 */
134 	public int getChanged() {
135 		if (changed == 0)
136 			throw new IllegalStateException();
137 		return changed;
138 	}
139 }