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.view;
020:
021: import java.util.List;
022: import org.netbeans.modules.xml.xpath.AbstractXPathModelHelper;
023: import org.netbeans.modules.xml.xpath.XPathException;
024: import org.netbeans.modules.xml.xpath.XPathExpression;
025: import org.netbeans.modules.xml.xpath.XPathModel;
026: import org.netbeans.modules.xslt.model.Attribute;
027: import org.netbeans.modules.xslt.model.CopyOf;
028: import org.netbeans.modules.xslt.model.Element;
029: import org.netbeans.modules.xslt.model.ForEach;
030: import org.netbeans.modules.xslt.model.If;
031: import org.netbeans.modules.xslt.model.LiteralResultElement;
032: import org.netbeans.modules.xslt.model.ValueOf;
033: import org.netbeans.modules.xslt.model.When;
034: import org.netbeans.modules.xslt.model.XslComponent;
035: import org.netbeans.modules.xslt.model.XslVisitorAdapter;
036:
037: /**
038: * Visitor to extract or update xpath expressions stored in different types of
039: * XSL elements
040: *
041: */
042: public class GetExpressionVisitor extends XslVisitorAdapter {
043:
044: private XPathExpression expression;
045:
046: public GetExpressionVisitor() {
047: }
048:
049: public void setExpression(XPathExpression expression) {
050: this .expression = expression;
051: }
052:
053: public void setExpression(String expression) {
054: XPathModel xpImpl = AbstractXPathModelHelper.getInstance()
055: .newXPathModel();
056: try {
057:
058: setExpression((expression != null) ? xpImpl
059: .parseExpression(expression) : null);
060: } catch (XPathException ex) {
061: setExpression((XPathExpression) null);
062: }
063: ;
064: }
065:
066: public String getExpressionString() {
067: return (expression != null) ? expression.toString() : "";
068: }
069:
070: public XPathExpression getExpression() {
071: return this .expression;
072:
073: }
074:
075: public void visit(ValueOf vof) {
076: setExpression(vof.getSelect());
077: }
078:
079: public void visit(If iff) {
080: setExpression(iff.getTest());
081: }
082:
083: public void visit(CopyOf cof) {
084: setExpression(cof.getSelect());
085: }
086:
087: public void visit(Attribute attribute) {
088: handleElementOrAttribute(attribute);
089: }
090:
091: public void visit(Element element) {
092: handleElementOrAttribute(element);
093:
094: }
095:
096: public void visit(LiteralResultElement element) {
097: handleElementOrAttribute(element);
098: }
099:
100: private void handleElementOrAttribute(XslComponent component) {
101: ValueOf vof = isValueOfContainer(component);
102: if (vof != null) {
103: setExpression(vof.getSelect());
104: }
105: }
106:
107: public void visit(ForEach forEach) {
108:
109: setExpression(forEach.getSelect());
110:
111: }
112:
113: public void visit(When when) {
114: setExpression(when.getTest());
115:
116: }
117:
118: public static ValueOf isValueOfContainer(XslComponent c) {
119:
120: if (c instanceof Attribute || c instanceof Element
121: || c instanceof LiteralResultElement) {
122: List<XslComponent> children = c.getChildren();
123: if (children.size() == 1
124: && children.get(0) instanceof ValueOf) {
125: return ((ValueOf) children.get(0));
126:
127: }
128: }
129: return null;
130: }
131:
132: }
|