View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.jxpath.ri.model;
19  
20  import java.util.Locale;
21  
22  import org.apache.commons.jxpath.JXPathContext;
23  import org.apache.commons.jxpath.Variables;
24  import org.apache.commons.jxpath.ri.QName;
25  
26  /**
27   * NodePointerFactory to create {@link VariablePointer VariablePointers}.
28   *
29   * @since JXPath 1.3
30   */
31  public class VariablePointerFactory implements NodePointerFactory {
32  
33      /**
34       * Node value wrapper to trigger a VariablePointerFactory.
35       */
36      public static final class VariableContextWrapper {
37  
38          private final JXPathContext context;
39  
40          /**
41           * Constructs a new VariableContextWrapper.
42           *
43           * @param context to wrap
44           */
45          private VariableContextWrapper(final JXPathContext context) {
46              this.context = context;
47          }
48  
49          /**
50           * Gets the original (unwrapped) context.
51           *
52           * @return JXPathContext.
53           */
54          public JXPathContext getContext() {
55              return context;
56          }
57      }
58  
59      /** Factory order constant */
60      public static final int VARIABLE_POINTER_FACTORY_ORDER = 890;
61  
62      /**
63       * VariableContextWrapper factory method.
64       *
65       * @param context the JXPathContext to wrap.
66       * @return VariableContextWrapper.
67       */
68      public static VariableContextWrapper contextWrapper(final JXPathContext context) {
69          return new VariableContextWrapper(context);
70      }
71  
72      /**
73       * Constructs a new instance.
74       */
75      public VariablePointerFactory() {
76          // empty
77      }
78  
79      @Override
80      public NodePointer createNodePointer(final NodePointer parent, final QName qName, final Object object) {
81          return createNodePointer(qName, object, null);
82      }
83  
84      @Override
85      public NodePointer createNodePointer(final QName qName, final Object object, final Locale locale) {
86          if (object instanceof VariableContextWrapper) {
87              JXPathContext varCtx = ((VariableContextWrapper) object).getContext();
88              while (varCtx != null) {
89                  final Variables vars = varCtx.getVariables();
90                  if (vars.isDeclaredVariable(qName.toString())) {
91                      return new VariablePointer(vars, qName);
92                  }
93                  varCtx = varCtx.getParentContext();
94              }
95              // The variable is not declared, but we will create
96              // a pointer anyway in case the user wants to set, rather
97              // than get, the value of the variable.
98              return new VariablePointer(qName);
99          }
100         return null;
101     }
102 
103     @Override
104     public int getOrder() {
105         return VARIABLE_POINTER_FACTORY_ORDER;
106     }
107 }