001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.jxpath.util; 018 019import org.apache.commons.jxpath.BasicNodeSet; 020import org.apache.commons.jxpath.ExtendedKeyManager; 021import org.apache.commons.jxpath.JXPathContext; 022import org.apache.commons.jxpath.KeyManager; 023import org.apache.commons.jxpath.NodeSet; 024import org.apache.commons.jxpath.Pointer; 025import org.apache.commons.jxpath.ri.InfoSetUtil; 026 027/** 028 * Utility class. 029 * 030 * @author Matt Benson 031 * @since JXPath 1.3 032 * @version $Revision: 652845 $ $Date: 2008-05-02 19:46:46 +0200 (Fr, 02 Mai 2008) $ 033 */ 034public class KeyManagerUtils { 035 /** 036 * Adapt KeyManager to implement ExtendedKeyManager. 037 */ 038 private static class SingleNodeExtendedKeyManager implements 039 ExtendedKeyManager { 040 private KeyManager delegate; 041 042 /** 043 * Create a new SingleNodeExtendedKeyManager. 044 * @param delegate KeyManager to wrap 045 */ 046 public SingleNodeExtendedKeyManager(KeyManager delegate) { 047 this.delegate = delegate; 048 } 049 050 public NodeSet getNodeSetByKey(JXPathContext context, String key, 051 Object value) { 052 Pointer pointer = delegate.getPointerByKey(context, key, InfoSetUtil.stringValue(value)); 053 BasicNodeSet result = new BasicNodeSet(); 054 result.add(pointer); 055 return result; 056 } 057 058 public Pointer getPointerByKey(JXPathContext context, String keyName, 059 String keyValue) { 060 return delegate.getPointerByKey(context, keyName, keyValue); 061 } 062 } 063 064 /** 065 * Get an ExtendedKeyManager from the specified KeyManager. 066 * @param keyManager to adapt, if necessary 067 * @return <code>keyManager</code> if it implements ExtendedKeyManager 068 * or a basic single-result ExtendedKeyManager that delegates to 069 * <code>keyManager</code>. 070 */ 071 public static ExtendedKeyManager getExtendedKeyManager(KeyManager keyManager) { 072 return keyManager instanceof ExtendedKeyManager ? (ExtendedKeyManager) keyManager 073 : new SingleNodeExtendedKeyManager(keyManager); 074 } 075}