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: * PathFromRootVisitor.java
044: *
045: * Created on August 4, 2005, 6:43 PM
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.Collections;
055: import java.util.LinkedList;
056: import java.util.List;
057: import org.netbeans.modules.xml.xdm.nodes.Document;
058: import org.netbeans.modules.xml.xdm.nodes.Element;
059: import org.netbeans.modules.xml.xdm.nodes.Node;
060:
061: /**
062: *
063: * @author Chris Webster
064: */
065: public class PathFromRootVisitor extends ChildVisitor {
066:
067: public List<Node> findPath(org.w3c.dom.Document root,
068: org.w3c.dom.Node target) {
069: Document wroot = root instanceof Document ? (Document) root
070: : null;
071: Node wtarget = target instanceof Node ? (Node) target : null;
072: return findPath(wroot, wtarget);
073: }
074:
075: public List<Node> findPathToRootElement(org.w3c.dom.Element root,
076: org.w3c.dom.Node target) {
077: Element wroot = root instanceof Element ? (Element) root : null;
078: Node wtarget = target instanceof Node ? (Node) target : null;
079: assert root != null && target != null;
080:
081: this .target = wtarget;
082: found = false;
083: pathToTarget = null;
084: wroot.accept(this );
085: return pathToTarget;
086: }
087:
088: public List<Node> findPath(Document root, Node target) {
089: assert root != null;
090: // assert target != null;
091: if (target == null)
092: return Collections.emptyList();
093: this .target = target;
094: found = false;
095: pathToTarget = null;
096: root.accept(this );
097: return pathToTarget;
098: }
099:
100: protected void visitNode(Node n) {
101: // if already found just return
102: if (found)
103: return;
104: if (target.getId() == n.getId()) {
105: pathToTarget = new LinkedList<Node>();
106: pathToTarget.add(n);
107: found = true;
108: } else {
109: super .visitNode(n);
110: if (found) {
111: // add the ancestors to the list
112: pathToTarget.add(n);
113: }
114: }
115: }
116:
117: private boolean found;
118: private List<Node> pathToTarget;
119: private Node target;
120: }
|