001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019: package org.netbeans.modules.xslt.mapper.model;
020:
021: import java.util.Collection;
022: import java.util.HashSet;
023: import org.netbeans.modules.xslt.mapper.model.nodes.LiteralCanvasNode;
024: import org.netbeans.modules.xslt.mapper.model.nodes.OperationOrFunctionCanvasNode;
025: import org.netbeans.modules.xslt.mapper.model.nodes.TreeNode;
026: import org.netbeans.modules.xslt.mapper.model.nodes.visitor.AbstractNodeVisitor;
027: import org.netbeans.modules.xslt.mapper.model.targettree.AttributeDeclarationNode;
028: import org.netbeans.modules.xslt.mapper.model.targettree.ElementDeclarationNode;
029: import org.netbeans.modules.xslt.mapper.model.targettree.RuleNode;
030: import org.netbeans.modules.xslt.mapper.model.targettree.SchemaNode;
031: import org.netbeans.modules.xslt.mapper.model.targettree.StylesheetNode;
032:
033: /**
034: * This visitor collects a list of tree nodes located under the target tree,
035: * which can be affected by a change of a node under the source tree.
036: *
037: * This visitor is intended to track and react to changes in the source tree.
038: * Such quite complex approach is required because the source schema
039: * can has infinit depth in case of recursive schema definition.
040: * So if the changes are caused by modifications in the source tree,
041: * predicates for example, then the relevant nodes of the target tree
042: * can be calculated only from the target tree side (in order to prevent
043: * infinit loop in case of recursive schema difinition).
044: *
045: * The visitor should be applied to the root element of the target tree!
046: *
047: * @author nk160297
048: */
049: public class FindAffectedTargetNodesVisitor extends AbstractNodeVisitor {
050:
051: private TreeNode myModifiedSourceNode;
052:
053: private HashSet<StylesheetNode> myResultNodesList = new HashSet<StylesheetNode>();
054:
055: private StylesheetNode lastStylesheetNode;
056:
057: public FindAffectedTargetNodesVisitor(TreeNode modifiedSourceNode) {
058: myModifiedSourceNode = modifiedSourceNode;
059: //
060: assert myModifiedSourceNode != null;
061: assert myModifiedSourceNode.isSourceViewNode();
062: }
063:
064: /**
065: * Returns the collection of target tree nodes which can be affected.
066: */
067: public Collection<StylesheetNode> getResultList() {
068: return myResultNodesList;
069: }
070:
071: public void visit(ElementDeclarationNode node) {
072: lastStylesheetNode = node;
073: acceptUpstream(node);
074: //
075: acceptDownTree(node);
076: }
077:
078: public void visit(AttributeDeclarationNode node) {
079: lastStylesheetNode = node;
080: acceptUpstream(node);
081: }
082:
083: public void visit(RuleNode node) {
084: lastStylesheetNode = node;
085: acceptUpstream(node);
086: //
087: acceptDownTree(node);
088: }
089:
090: public void visit(OperationOrFunctionCanvasNode node) {
091: acceptUpstream(node);
092: }
093:
094: public void visit(LiteralCanvasNode node) {
095: // Nothing to do here.
096: }
097:
098: public void visit(SchemaNode node) {
099: if (node.isSourceViewNode()) {
100: if (node.equals(myModifiedSourceNode)) {
101: myResultNodesList.add(lastStylesheetNode);
102: } else {
103: acceptUpTree(node);
104: }
105: }
106: }
107:
108: }
|