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 */ 017 018package org.apache.commons.jxpath.util; 019 020import org.apache.commons.jxpath.BasicNodeSet; 021import org.apache.commons.jxpath.ExtendedKeyManager; 022import org.apache.commons.jxpath.JXPathContext; 023import org.apache.commons.jxpath.KeyManager; 024import org.apache.commons.jxpath.NodeSet; 025import org.apache.commons.jxpath.Pointer; 026import org.apache.commons.jxpath.ri.InfoSetUtil; 027 028/** 029 * Utility class. 030 * 031 * @since JXPath 1.3 032 */ 033public class KeyManagerUtils { 034 035 /** 036 * Adapt KeyManager to implement ExtendedKeyManager. 037 */ 038 private static final class SingleNodeExtendedKeyManager implements ExtendedKeyManager { 039 040 private final KeyManager delegate; 041 042 /** 043 * Constructs a new SingleNodeExtendedKeyManager. 044 * 045 * @param delegate KeyManager to wrap 046 */ 047 public SingleNodeExtendedKeyManager(final KeyManager delegate) { 048 this.delegate = delegate; 049 } 050 051 @Override 052 public NodeSet getNodeSetByKey(final JXPathContext context, final String key, final Object value) { 053 final Pointer pointer = delegate.getPointerByKey(context, key, InfoSetUtil.stringValue(value)); 054 final BasicNodeSet result = new BasicNodeSet(); 055 result.add(pointer); 056 return result; 057 } 058 059 @Override 060 public Pointer getPointerByKey(final JXPathContext context, final String keyName, final String keyValue) { 061 return delegate.getPointerByKey(context, keyName, keyValue); 062 } 063 } 064 065 /** 066 * Gets an ExtendedKeyManager from the specified KeyManager. 067 * 068 * @param keyManager to adapt, if necessary 069 * @return {@code keyManager} if it implements ExtendedKeyManager or a basic single-result ExtendedKeyManager that delegates to {@code keyManager}. 070 */ 071 public static ExtendedKeyManager getExtendedKeyManager(final KeyManager keyManager) { 072 return keyManager instanceof ExtendedKeyManager ? (ExtendedKeyManager) keyManager : new SingleNodeExtendedKeyManager(keyManager); 073 } 074 075 /** 076 * Constructs a new instance. 077 * 078 * @deprecated Will be private in the next major version. 079 */ 080 @Deprecated 081 public KeyManagerUtils() { 082 // empty 083 } 084}