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  package org.apache.commons.jxpath.ri.model;
18  
19  import java.util.Locale;
20  
21  import org.apache.commons.jxpath.JXPathContext;
22  import org.apache.commons.jxpath.Variables;
23  import org.apache.commons.jxpath.ri.QName;
24  
25  /**
26   * NodePointerFactory to create {@link VariablePointer VariablePointers}.
27   * @author Matt Benson
28   * @since JXPath 1.3
29   * @version $Revision: 652845 $ $Date: 2008-05-02 13:46:46 -0400 (Fri, 02 May 2008) $
30   */
31  public class VariablePointerFactory implements NodePointerFactory {
32      /** factory order constant */
33      public static final int VARIABLE_POINTER_FACTORY_ORDER = 890;
34  
35      /**
36       * Node value wrapper to trigger a VariablePointerFactory.
37       */
38      public static final class VariableContextWrapper {
39          private final JXPathContext context;
40  
41          /**
42           * Create a new VariableContextWrapper.
43           * @param context to wrap
44           */
45          private VariableContextWrapper(JXPathContext context) {
46              this.context = context;
47          }
48  
49          /**
50           * Get the original (unwrapped) context.
51           *
52           * @return JXPathContext.
53           */
54          public JXPathContext getContext() {
55              return context;
56          }
57      }
58  
59      /**
60       * VariableContextWrapper factory method.
61       * @param context the JXPathContext to wrap.
62       * @return VariableContextWrapper.
63       */
64      public static VariableContextWrapper contextWrapper(JXPathContext context) {
65          return new VariableContextWrapper(context);
66      }
67  
68      public NodePointer createNodePointer(QName name, Object object,
69              Locale locale) {
70          if (object instanceof VariableContextWrapper) {
71              JXPathContext varCtx = ((VariableContextWrapper) object).getContext();
72              while (varCtx != null) {
73                  Variables vars = varCtx.getVariables();
74                  if (vars.isDeclaredVariable(name.toString())) {
75                      return new VariablePointer(vars, name);
76                  }
77                  varCtx = varCtx.getParentContext();
78              }
79              // The variable is not declared, but we will create
80              // a pointer anyway in case the user wants to set, rather
81              // than get, the value of the variable.
82              return new VariablePointer(name);
83          }
84          return null;
85      }
86  
87      public NodePointer createNodePointer(NodePointer parent, QName name,
88              Object object) {
89          return createNodePointer(name, object, null);
90      }
91  
92      public int getOrder() {
93          return VARIABLE_POINTER_FACTORY_ORDER;
94      }
95  
96  }