001    /*
002    // $Id: PropertyValueNode.java 482 2012-01-05 23:27:27Z jhyde $
003    //
004    // Licensed to Julian Hyde under one or more contributor license
005    // agreements. See the NOTICE file distributed with this work for
006    // additional information regarding copyright ownership.
007    //
008    // Julian Hyde licenses this file to you under the Apache License,
009    // Version 2.0 (the "License"); you may not use this file except in
010    // compliance with the License. You may obtain a copy of the License at:
011    //
012    // http://www.apache.org/licenses/LICENSE-2.0
013    //
014    // Unless required by applicable law or agreed to in writing, software
015    // distributed under the License is distributed on an "AS IS" BASIS,
016    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017    // See the License for the specific language governing permissions and
018    // limitations under the License.
019    */
020    package org.olap4j.mdx;
021    
022    import org.olap4j.type.Type;
023    
024    /**
025     * Parse tree node representing a property-value pair.
026     *
027     * <p>Property-value pairs are used to define properties of calculated members.
028     * For example, in
029     *
030     * <blockquote>
031     * <code>WITH MEMBER [Measures].[Foo] AS ' [Measures].[Unit Sales] ',<br/>
032     * &nbsp;&nbsp;FORMAT_STRING = 'Bold',<br/>
033     * &nbsp;&nbsp;SOLVE_ORDER = 2<br/>
034     * SELECT ...</code>
035     * </blockquote>
036     *
037     * there are two property-value pairs FORMAT_STRING and SOLVE_ORDER.
038     *
039     * @version $Id: PropertyValueNode.java 482 2012-01-05 23:27:27Z jhyde $
040     * @author jhyde
041     */
042    public class PropertyValueNode implements ParseTreeNode {
043    
044        private final ParseRegion region;
045        private final String name;
046        private ParseTreeNode expression;
047    
048        /**
049         * Creates a PropertyValueNode.
050         *
051         * @param region Region of source code
052         * @param name Name of property
053         * @param expression Expression for value of property (often a literal)
054         */
055        public PropertyValueNode(
056            ParseRegion region,
057            String name,
058            ParseTreeNode expression)
059        {
060            this.region = region;
061            this.name = name;
062            this.expression = expression;
063        }
064    
065        public ParseRegion getRegion() {
066            return region;
067        }
068    
069        public Type getType() {
070            return expression.getType();
071        }
072    
073        /**
074         * Returns the expression by which the value of the property is derived.
075         *
076         * @return the expression by which the value of the property is derived
077         */
078        public ParseTreeNode getExpression() {
079            return expression;
080        }
081    
082        /**
083         * Returns the name of the property
084         *
085         * @return name of the property
086         */
087        public String getName() {
088            return name;
089        }
090    
091        public <T> T accept(ParseTreeVisitor<T> visitor) {
092            return visitor.visit(this);
093        }
094    
095        public void unparse(ParseTreeWriter writer) {
096            writer.getPrintWriter().print(name + " = ");
097            expression.unparse(writer);
098        }
099    
100        public PropertyValueNode deepCopy() {
101            return new PropertyValueNode(
102                this.region,
103                this.name,
104                this.expression.deepCopy());
105        }
106    }
107    
108    // End PropertyValueNode.java