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.nodes;
020:
021: import java.util.Map;
022: import javax.swing.JTree;
023: import org.netbeans.modules.soa.ui.axinodes.AxiomUtils;
024: import org.netbeans.modules.xml.axi.AXIComponent;
025: import org.netbeans.modules.xml.axi.AXIType;
026: import org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent;
027: import org.netbeans.modules.xml.xpath.LocationStep;
028: import org.netbeans.modules.xml.xpath.XPathLocationPath;
029: import org.netbeans.modules.xml.xpath.XPathPredicateExpression;
030: import org.netbeans.modules.xslt.mapper.model.targettree.PredicatedSchemaNode;
031: import org.netbeans.modules.xslt.mapper.view.PredicateManager;
032: import org.netbeans.modules.xslt.mapper.view.XsltMapper;
033:
034: /**
035: *
036: * @author Alexey
037: */
038: public class SourceTypeFinder {
039: private XsltMapper mapper;
040:
041: private LocationStep[] locationSteps;
042: private Map<String, String> prefixesMap;
043:
044: /** Creates a new instance of SourceTypeFinder */
045: public SourceTypeFinder(XsltMapper mapper) {
046: this .mapper = mapper;
047:
048: //FIXME! namespace prefixes are taken from root element!
049: //should check the whole hierarchy, starting from element, which contains this expression
050: prefixesMap = ((AbstractDocumentComponent) mapper.getContext()
051: .getXSLModel().getStylesheet()).getPrefixes();
052: }
053:
054: public TreeNode findNode(XPathLocationPath path) {
055: JTree sourceTree = mapper.getMapperViewManager()
056: .getSourceView().getTree();
057:
058: this .locationSteps = path.getSteps();
059:
060: return findImpl((TreeNode) sourceTree.getModel().getRoot(), 0);
061:
062: }
063:
064: private TreeNode findImpl(TreeNode currentNode, int depth) {
065: if (locationSteps == null || depth >= locationSteps.length) {
066: return null;
067: }
068: LocationStep step = locationSteps[depth];
069:
070: XPathPredicateExpression[] predicates = step.getPredicates();
071: boolean predicatedNodeRequired = predicates != null
072: && predicates.length != 0;
073: if (predicatedNodeRequired
074: && currentNode instanceof PredicatedSchemaNode) {
075: // A not predicated node has to be found at first
076: // The corresponding predicated node will be looked for later.
077: return null;
078: }
079: //
080: String nameTest = step.getNodeTest().toString();
081:
082: String name = "";
083: String namespace = null;
084:
085: int pos = nameTest.indexOf(':');
086:
087: if (pos != -1) {
088: String prefix = nameTest.substring(0, pos);
089: namespace = prefixesMap.get(prefix);
090: name = nameTest.substring(pos + 1);
091: } else {
092: name = nameTest;
093: }
094:
095: AXIComponent type = currentNode.getType();
096:
097: String typeName = ((AXIType) type).getName();
098: String typeNamespace = AxiomUtils.isUnqualified(type) ? ""
099: : type.getTargetNamespace();
100:
101: if (namespace == null) {
102: namespace = "";
103: }
104:
105: if (typeName.equals(name) && namespace.equals(typeNamespace)) {
106:
107: // Try find the same predicated node
108: if (predicatedNodeRequired) {
109: PredicateManager pManager = mapper
110: .getPredicateManager();
111: TreeNode predicatedNode = pManager.getPredicatedNode(
112: currentNode, predicates);
113: //
114: // The predicated node has to be already created here
115: assert predicatedNode != null;
116: // if (predicatedNode == null) {
117: // // Creates a new predicated node and register a new predicate
118: // // in the predicates' manager.
119: // predicatedNode = pManager.createPredicatedNode(
120: // currentNode, predicates);
121: // assert predicatedNode != null;
122: // }
123: currentNode = predicatedNode;
124: }
125:
126: if (depth == (locationSteps.length - 1)) {
127: //last step in path
128: return currentNode;
129: }
130:
131: if (!currentNode.getChildren().isEmpty()) {
132: //perform recursion
133: for (TreeNode tn : currentNode.getChildren()) {
134: TreeNode result = findImpl(tn, depth + 1);
135: if (result != null) {
136: return result;
137: }
138: }
139: }
140: }
141:
142: return null;
143: }
144:
145: }
|