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 java.util.Iterator;
023: import org.netbeans.modules.soa.mapper.util.Duration;
024:
025: import org.netbeans.modules.xml.xpath.XPathCoreFunction;
026: import org.netbeans.modules.xml.xpath.XPathCoreOperation;
027: import org.netbeans.modules.xml.xpath.XPathExpression;
028: import org.netbeans.modules.xml.xpath.XPathExtensionFunction;
029: import org.netbeans.modules.xml.xpath.XPathLocationPath;
030: import org.netbeans.modules.xml.xpath.XPathNumericLiteral;
031: import org.netbeans.modules.xml.xpath.XPathOperationOrFuntion;
032: import org.netbeans.modules.xml.xpath.XPathStringLiteral;
033: import org.netbeans.modules.xml.xpath.XPathVariableReference;
034: import org.netbeans.modules.xml.xpath.visitor.AbstractXPathVisitor;
035: import org.netbeans.modules.soa.mapper.common.basicmapper.methoid.IFieldNode;
036: import org.netbeans.modules.soa.mapper.common.basicmapper.methoid.IMethoid;
037: import org.netbeans.modules.soa.mapper.common.basicmapper.methoid.IMethoidNode;
038: import org.netbeans.modules.xslt.mapper.methoid.Constants;
039: import org.netbeans.modules.xslt.mapper.methoid.MethoidLoader;
040: import org.netbeans.modules.xslt.mapper.model.nodes.Node;
041: import org.netbeans.modules.xslt.mapper.model.nodes.NodeFactory;
042: import org.netbeans.modules.xslt.mapper.model.nodes.SourceTypeFinder;
043:
044: /**
045: *
046: * @author radval
047: *
048: */
049: public class NodeCreatorVisitor extends AbstractXPathVisitor {
050:
051: private XsltMapper mapper;
052:
053: private Node result;
054:
055: public NodeCreatorVisitor(XsltMapper mapper) {
056: this .mapper = mapper;
057: }
058:
059: public Node getResult() {
060: return result;
061: }
062:
063: private void setResult(Node result) {
064: this .result = result;
065: }
066:
067: public void visit(XPathStringLiteral expr) {
068: IMethoidNode node = null;
069: try {
070: Duration.parse(expr.getExpressionString());
071: setResult(createMethoidNode(expr,
072: Constants.DURATION_LITERAL));
073: } catch (Exception e) {
074: // Not a duration literal
075: setResult(createLiteralNode(expr, Constants.STRING_LITERAL));
076: }
077: }
078:
079: public void visit(XPathNumericLiteral expr) {
080: setResult(createLiteralNode(expr, Constants.NUMBER_LITERAL));
081: }
082:
083: public void visit(XPathCoreOperation expr) {
084: visitXPathOperatorOrFunction(expr);
085: }
086:
087: public void visit(XPathCoreFunction expr) {
088: visitXPathOperatorOrFunction(expr);
089: }
090:
091: private void visitXPathOperatorOrFunction(
092: XPathOperationOrFuntion operator) {
093: Node node = createMethoidNode(operator, operator.getName());
094: if (node != null) {
095: setResult(node);
096: } else {
097: setResult(createLiteralNode(operator,
098: Constants.XPATH_LITERAL));
099: }
100: }
101:
102: public void visit(XPathExtensionFunction expr) {
103: setResult(createLiteralNode(expr, Constants.XPATH_LITERAL));
104: }
105:
106: public void visit(XPathLocationPath expr) {
107: Node result = (new SourceTypeFinder(mapper).findNode(expr));
108: if (result != null) {
109: setResult(result);
110: } else {
111: setResult(createLiteralNode(expr, Constants.XPATH_LITERAL));
112: }
113: }
114:
115: public void visit(XPathVariableReference vReference) {
116: setResult(createLiteralNode(vReference, Constants.XPATH_LITERAL));
117: }
118:
119: private Node createLiteralNode(XPathExpression expr, String name) {
120: Node node = createMethoidNode(expr, name);
121: if (node != null) {
122: IFieldNode out_field = (IFieldNode) node.getOutputNode();
123: if (out_field != null) {
124: out_field.setLiteralName(expr.getExpressionString());
125: }
126: }
127: return node;
128: }
129:
130: private Node createMethoidNode(XPathExpression expr, String name) {
131: IMethoid methoid = MethoidLoader.loadMethoid(name);
132: IMethoidNode node = mapper.createMethoidNode(methoid);
133:
134: Node operatorNode = NodeFactory.createNode(expr,
135: (XsltMapper) mapper);
136:
137: node.setNodeObject(operatorNode);
138: operatorNode.setMapperNode(node);
139:
140: return operatorNode;
141: }
142:
143: }
|