001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: /*
043: * CompareVisitor.java
044: *
045: * Created on August 31, 2005, 10:24 AM
046: *
047: * To change this template, choose Tools | Options and locate the template under
048: * the Source Creation and Management node. Right-click the template and choose
049: * Open. You can then make changes to the template in the Source Editor.
050: */
051:
052: package org.netbeans.modules.xml.xdm.visitor;
053:
054: import java.util.List;
055: import org.netbeans.modules.xml.xdm.nodes.Attribute;
056: import org.netbeans.modules.xml.xdm.nodes.Document;
057: import org.netbeans.modules.xml.xdm.nodes.Element;
058: import org.netbeans.modules.xml.xdm.nodes.Node;
059: import org.netbeans.modules.xml.xdm.nodes.Text;
060: import org.netbeans.modules.xml.xdm.nodes.Token;
061:
062: /**
063: * Does the comparison for only the needed methods for a given node with its target.
064: * We may be able to do this comparison in the Nodes thenseleves rather than
065: * in a visitor since we dont necessarily walk the tree.
066: *
067: * @author Vidhya Narayanan
068: */
069: public class CompareVisitor implements XMLNodeVisitor {
070:
071: /** Creates a new instance of CompareVisitor */
072: public CompareVisitor() {
073: }
074:
075: public boolean compare(Node n1, Node n2) {
076: target = n2;
077: n1.accept(this );
078: return result;
079: }
080:
081: public void visit(Attribute attr) {
082: result = false;
083: if (target instanceof Attribute) {
084: if (attr.getName().equals(((Attribute) target).getName())
085: && attr.getValue().equals(
086: ((Attribute) target).getValue()))
087: result = true;
088: if (result)
089: tokenCompare(attr.getTokens(), ((Attribute) target)
090: .getTokens());
091: }
092: }
093:
094: public void visit(Document doc) {
095: if (target instanceof Document)
096: result = true;
097: }
098:
099: public void visit(Element e) {
100: result = false;
101: if (target instanceof Element) {
102: if (e.getLocalName().equals(
103: ((Element) target).getLocalName()))
104: result = true;
105: }
106: if (result)
107: tokenCompare(e.getTokens(), ((Element) target).getTokens());
108: }
109:
110: public void visit(Text txt) {
111: result = false;
112: if (target instanceof Text) {
113: if (txt.getText().equals(((Text) target).getText()))
114: result = true;
115: }
116: if (result)
117: tokenCompare(txt.getTokens(), ((Text) target).getTokens());
118: }
119:
120: private void tokenCompare(List<Token> oldtokens,
121: List<Token> newtokens) {
122: assert oldtokens != null && newtokens != null;
123: if (oldtokens.size() != newtokens.size())
124: result = false;
125: else {
126: int i = 0;
127: for (Token t : oldtokens) {
128: if (t.getType() != newtokens.get(i).getType()
129: || !t.getValue().equals(
130: newtokens.get(i).getValue()))
131: result = false;
132: i++;
133: }
134: }
135: }
136:
137: private Node target;
138: boolean result = false;
139: }
|