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.util;
18  
19  import org.apache.commons.jxpath.BasicNodeSet;
20  import org.apache.commons.jxpath.ExtendedKeyManager;
21  import org.apache.commons.jxpath.JXPathContext;
22  import org.apache.commons.jxpath.KeyManager;
23  import org.apache.commons.jxpath.NodeSet;
24  import org.apache.commons.jxpath.Pointer;
25  import org.apache.commons.jxpath.ri.InfoSetUtil;
26  
27  /**
28   * Utility class.
29   *
30   * @author Matt Benson
31   * @since JXPath 1.3
32   * @version $Revision: 652845 $ $Date: 2008-05-02 13:46:46 -0400 (Fri, 02 May 2008) $
33   */
34  public class KeyManagerUtils {
35      /**
36       * Adapt KeyManager to implement ExtendedKeyManager.
37       */
38      private static class SingleNodeExtendedKeyManager implements
39              ExtendedKeyManager {
40          private KeyManager delegate;
41  
42          /**
43           * Create a new SingleNodeExtendedKeyManager.
44           * @param delegate KeyManager to wrap
45           */
46          public SingleNodeExtendedKeyManager(KeyManager delegate) {
47              this.delegate = delegate;
48          }
49  
50          public NodeSet getNodeSetByKey(JXPathContext context, String key,
51                  Object value) {
52              Pointer pointer = delegate.getPointerByKey(context, key, InfoSetUtil.stringValue(value));
53              BasicNodeSet result = new BasicNodeSet();
54              result.add(pointer);
55              return result;
56          }
57  
58          public Pointer getPointerByKey(JXPathContext context, String keyName,
59                  String keyValue) {
60              return delegate.getPointerByKey(context, keyName, keyValue);
61          }
62      }
63  
64      /**
65       * Get an ExtendedKeyManager from the specified KeyManager.
66       * @param keyManager to adapt, if necessary
67       * @return <code>keyManager</code> if it implements ExtendedKeyManager
68       *         or a basic single-result ExtendedKeyManager that delegates to
69       *         <code>keyManager</code>.
70       */
71      public static ExtendedKeyManager getExtendedKeyManager(KeyManager keyManager) {
72          return keyManager instanceof ExtendedKeyManager ? (ExtendedKeyManager) keyManager
73                  : new SingleNodeExtendedKeyManager(keyManager);
74      }
75  }