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.List;
022: import org.netbeans.modules.soa.mapper.common.IMapperNode;
023: import org.netbeans.modules.soa.mapper.common.basicmapper.methoid.IMethoid;
024: import org.netbeans.modules.soa.mapper.common.basicmapper.methoid.IMethoidNode;
025: import org.netbeans.modules.soa.ui.axinodes.AxiomUtils;
026: import org.netbeans.modules.soa.ui.axinodes.AxiomUtils.PathItem;
027: import org.netbeans.modules.xml.axi.AXIComponent;
028: import org.netbeans.modules.xml.axi.AXIType;
029: import org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent;
030: import org.netbeans.modules.xml.xpath.XPathExpression;
031: import org.netbeans.modules.xml.xpath.XPathOperationOrFuntion;
032: import org.netbeans.modules.xslt.mapper.XPathUtil;
033: import org.netbeans.modules.xslt.mapper.model.nodes.LiteralCanvasNode;
034: import org.netbeans.modules.xslt.mapper.model.nodes.Node;
035: import org.netbeans.modules.xslt.mapper.model.nodes.OperationOrFunctionCanvasNode;
036: import org.netbeans.modules.xslt.mapper.model.nodes.TreeNode;
037: import org.netbeans.modules.xslt.mapper.model.nodes.visitor.AbstractNodeVisitor;
038: import org.netbeans.modules.xslt.mapper.model.targettree.AXIUtils;
039: import org.netbeans.modules.xslt.mapper.model.targettree.SchemaNode;
040: import org.netbeans.modules.xslt.model.Stylesheet;
041: import org.netbeans.modules.xslt.model.XslComponent;
042:
043: /**
044: *
045: * @author Alexey
046: */
047: public class BuildExpressionVisitor extends AbstractNodeVisitor {
048:
049: private MapperContext myMapperContext;
050: private XPathExpression result;
051: private XslComponent context;
052:
053: public static final String UNCONNECTED_INPUT = "$unconnectedInput_";
054:
055: public BuildExpressionVisitor(MapperContext mapperContext,
056: XslComponent context) {
057: myMapperContext = mapperContext;
058: this .context = context;
059: }
060:
061: public BuildExpressionVisitor(MapperContext mapperContext) {
062: this (mapperContext, null);
063: }
064:
065: public XPathExpression getResult() {
066: return this .result;
067: }
068:
069: public void visit(OperationOrFunctionCanvasNode node) {
070: result = (XPathOperationOrFuntion) node.getDataObject();
071:
072: ((XPathOperationOrFuntion) result).clearChildren();
073:
074: List<Node> prevNodes = node.getAllPreviousNodes();
075:
076: for (int n = 0; n < prevNodes.size(); n++) {
077: Node nn = prevNodes.get(n);
078: if (nn != null) {
079: BuildExpressionVisitor visitor = new BuildExpressionVisitor(
080: myMapperContext);
081: nn.accept(visitor);
082: ((XPathOperationOrFuntion) result).addChild(visitor
083: .getResult());
084: } else {
085:
086: IMapperNode mn = node.getMapperNode();
087: boolean isAccumulative = false;
088: if (mn instanceof IMethoidNode) {
089: IMethoid methoid = (IMethoid) ((IMethoidNode) mn)
090: .getMethoidObject();
091: isAccumulative = methoid.isAccumulative();
092: }
093: if (!isAccumulative) {
094: ((XPathOperationOrFuntion) result)
095: .addChild(XPathUtil
096: .createExpression(UNCONNECTED_INPUT
097: + n));
098: }
099: }
100: }
101:
102: }
103:
104: public void visit(LiteralCanvasNode node) {
105: result = (XPathExpression) node.getDataObject();
106: }
107:
108: public void visit(SchemaNode node) {
109: Stylesheet stylesheet = myMapperContext.getXSLModel()
110: .getStylesheet();
111: AbstractDocumentComponent adc = (AbstractDocumentComponent) stylesheet;
112: //
113: List<PathItem> path = AXIUtils.prepareSimpleXPath(node);
114: String locationPath = AxiomUtils
115: .calculateSimpleXPath(path, adc);
116: //
117: result = XPathUtil.createExpression(locationPath);
118: }
119:
120: private String getLocationPath(TreeNode node) {
121:
122: AXIComponent myself = (AXIComponent) node.getDataObject();
123:
124: TreeNode parent = node.getParent();
125:
126: String name = ((AXIType) myself).getName();
127:
128: if (parent != null) {
129: return getLocationPath(parent) + "/" + name;
130: } else {
131: return "/" + name;
132: }
133: }
134:
135: /// dont use it. impl is broken. root cause is being investigated
136: // private String getLocationPath(Element e) {
137: //
138: // AXIComponent parent = c.getParentElement();
139: //
140: // String name = ((AXIType) c).getName();
141: //
142: //
143: // if (parent != null){
144: // return getLocationPath(parent) +"/" + name;
145: // } else {
146: // return "/" + name;
147: // }
148: // }
149: }
|