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:
020: package org.netbeans.modules.xslt.mapper.view;
021:
022: import javax.swing.tree.TreePath;
023: import org.netbeans.modules.soa.mapper.basicmapper.methoid.BasicField;
024: import org.netbeans.modules.soa.mapper.basicmapper.methoid.BasicFieldNode;
025: import org.netbeans.modules.soa.mapper.common.IMapperGroupNode;
026: import org.netbeans.modules.soa.mapper.common.IMapperLink;
027: import org.netbeans.modules.soa.mapper.common.IMapperNode;
028: import org.netbeans.modules.soa.mapper.common.basicmapper.methoid.IFieldNode;
029: import org.netbeans.modules.soa.mapper.common.basicmapper.methoid.IMethoid;
030: import org.netbeans.modules.soa.mapper.common.basicmapper.methoid.IMethoidNode;
031: import org.netbeans.modules.soa.mapper.common.basicmapper.tree.IMapperTreeNode;
032: import org.netbeans.modules.xml.xpath.LocationStep;
033: import org.netbeans.modules.xml.xpath.XPathExpression;
034: import org.netbeans.modules.xml.xpath.XPathLocationPath;
035: import org.netbeans.modules.xml.xpath.visitor.AbstractXPathVisitor;
036: import org.netbeans.modules.xslt.mapper.methoid.Constants;
037: import org.netbeans.modules.xslt.mapper.model.BuildExpressionVisitor;
038: import org.netbeans.modules.xslt.mapper.model.MapperContext;
039: import org.netbeans.modules.xslt.mapper.model.targettree.SchemaNode;
040: import org.openide.filesystems.FileObject;
041:
042: /**
043: *
044: * @author nk160297
045: */
046: public class PredicateManagerOld {
047:
048: /** Creates a new instance of PredicateManager */
049: public PredicateManagerOld() {
050: }
051:
052: /**
053: * Provide the special processing to support Predicate elements.
054: */
055: public static void processAddLink(IMapperLink link,
056: MapperContext context) {
057: PredicateLinkParams pr = obtainLinkParams(link);
058: //
059: BuildExpressionVisitor visitor_ge = new BuildExpressionVisitor(
060: context);
061: visitor_ge.visit(pr.sourceSchemaNode);
062: XPathExpression xPathExpr = visitor_ge.getResult();
063: //
064: clearPredicateInputFields(pr.groupNode, pr.mainInputField);
065: //
066: fillInPredicateInputFields(pr.mainInputField, xPathExpr);
067: //
068: // System.out.println("Connect the source node \"" + schemaNode.getName() +
069: // "\" with the predicate node");
070: }
071:
072: public static void processRemoveLink(IMapperLink link) {
073: PredicateLinkParams pr = obtainLinkParams(link);
074: //
075: clearPredicateInputFields(pr.groupNode, pr.mainInputField);
076: }
077:
078: public static void clearPredicateInputFields(
079: IMapperGroupNode predicateNode, IFieldNode mainInputField) {
080: IMapperNode mapperNode = predicateNode.getFirstNode();
081: while (mapperNode != null) {
082: if (mapperNode instanceof IFieldNode) {
083: IFieldNode fieldNode = (IFieldNode) mapperNode;
084: if (fieldNode.isInput()
085: && !fieldNode.equals(mainInputField)) {
086: mapperNode = predicateNode.getNextNode(fieldNode);
087: predicateNode.removeNode(fieldNode);
088: System.out.println(fieldNode.getName());
089: continue;
090: }
091: } else if (mapperNode instanceof IMapperGroupNode) {
092: clearPredicateInputFields(
093: (IMapperGroupNode) mapperNode, mainInputField);
094: }
095: mapperNode = predicateNode.getNextNode(mapperNode);
096: }
097:
098: }
099:
100: public static void fillInPredicateInputFields(
101: final IFieldNode mainInputField, XPathExpression xPathExpr) {
102: //
103: final IMapperGroupNode fieldsOwnerNode = mainInputField
104: .getGroupNode();
105: xPathExpr.accept(new AbstractXPathVisitor() {
106: public void visit(XPathLocationPath locationPath) {
107: LocationStep[] stepArr = locationPath.getSteps();
108: IFieldNode addNextAfter = mainInputField;
109: for (LocationStep step : stepArr) {
110: String stepText = step.getString();
111: //
112: // Construct new field
113: BasicField newField = new BasicField(stepText,
114: "predicate", // field type
115: "", // tooltip
116: null, // data object
117: true, // is input
118: false, // is output
119: null); // literal updater // NOI18N
120: BasicFieldNode newFieldNode = new BasicFieldNode(
121: newField);
122: fieldsOwnerNode.addNextNode(addNextAfter,
123: newFieldNode);
124: addNextAfter = newFieldNode;
125: }
126: }
127: });
128: }
129:
130: private static PredicateLinkParams obtainLinkParams(IMapperLink link) {
131: IMapperNode startNode = link.getStartNode();
132: IMapperNode endNode = link.getEndNode();
133: //
134: // Check if the end node is a predicate
135: boolean isPredicate = false;
136: IFieldNode endFldNode = null;
137: IMethoidNode endMethoidNode = null;
138: IMapperGroupNode groupNode = null;
139: if (endNode instanceof IFieldNode) {
140: endFldNode = (IFieldNode) endNode;
141: groupNode = endFldNode.getGroupNode();
142: while (groupNode.getGroupNode() != null) {
143: groupNode = groupNode.getGroupNode();
144: }
145: //
146: if (groupNode instanceof IMethoidNode) {
147: endMethoidNode = (IMethoidNode) groupNode;
148: Object methoidObj = endMethoidNode.getMethoidObject();
149: if (methoidObj instanceof IMethoid) {
150: Object dataObject = ((IMethoid) methoidObj)
151: .getData();
152: if (dataObject instanceof FileObject) {
153: FileObject dataFo = (FileObject) dataObject;
154: Object pridicateObj = dataFo
155: .getAttribute(Constants.IS_PREDICATE);
156: if (pridicateObj != null
157: && pridicateObj instanceof Boolean) {
158: isPredicate = ((Boolean) pridicateObj)
159: .booleanValue();
160: }
161: }
162: }
163: }
164: }
165: //
166: if (!isPredicate) {
167: // The target node is not a predicate element.
168: return null;
169: }
170: //
171: TreePath treePath = null;
172: SchemaNode schemaNode = null;
173: if (startNode instanceof IMapperTreeNode) {
174: IMapperTreeNode startMapperNode = ((IMapperTreeNode) startNode);
175: if (startMapperNode.isSourceTreeNode()) {
176: treePath = startMapperNode.getPath();
177: // Object nodeObj = startMapperNode.getNodeObject();
178: if (treePath != null) {
179: Object pathComp = treePath.getLastPathComponent();
180: if (pathComp instanceof SchemaNode) {
181: schemaNode = (SchemaNode) pathComp;
182: }
183: }
184: }
185: }
186: //
187: if (schemaNode == null) {
188: // The link was connected to a wrong source node.
189: // It has to be a SchemaNode from the source tree
190: return null;
191: }
192: //
193: if (!Constants.PREDICATE_MAIN_INPUT_TYPE.equals(endFldNode
194: .getTypeName())) {
195: // the link was connected to a wrong field.
196: // It has to be connected to the specific field with the type "node-set".
197: return null;
198: }
199: //
200: PredicateLinkParams pr = new PredicateLinkParams();
201: pr.groupNode = groupNode;
202: pr.methoidNode = endMethoidNode;
203: pr.mainInputField = endFldNode;
204: pr.sourceSchemaNode = schemaNode;
205: pr.sourceTreePath = treePath;
206: //
207: return pr;
208: }
209:
210: private static class PredicateLinkParams {
211: public IMethoidNode methoidNode;
212: public IMapperGroupNode groupNode;
213: public IFieldNode mainInputField;
214: public TreePath sourceTreePath;
215: public SchemaNode sourceSchemaNode;
216: }
217: }
|