Save This Page
Home » xmlbeans-2.5.0-src » org.apache.xmlbeans.impl » store » [javadoc | source]
    1   /*   Copyright 2004 The Apache Software Foundation
    2    *
    3    *   Licensed under the Apache License, Version 2.0 (the "License");
    4    *   you may not use this file except in compliance with the License.
    5    *   You may obtain a copy of the License at
    6    *
    7    *       http://www.apache.org/licenses/LICENSE-2.0
    8    *
    9    *   Unless required by applicable law or agreed to in writing, software
   10    *   distributed under the License is distributed on an "AS IS" BASIS,
   11    *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   12    *   See the License for the specific language governing permissions and
   13    *  limitations under the License.
   14    */
   15   package org.apache.xmlbeans.impl.store;
   16   
   17   import java.util.List;
   18   import java.util.Map;
   19   import java.util.HashMap;
   20   import java.lang.reflect.Constructor;
   21   
   22   import org.apache.xmlbeans.impl.common.XPath;
   23   
   24   public final class PathDelegate
   25   {
   26       private static HashMap _constructors = new HashMap();
   27   
   28       private PathDelegate()
   29       {}
   30   
   31       private static synchronized void init(String implClassName)
   32       {
   33           // default to Saxon
   34           if (implClassName == null)
   35               implClassName = "org.apache.xmlbeans.impl.xpath.saxon.XBeansXPath";
   36           Class selectPathInterfaceImpl = null;
   37           boolean engineAvailable = true;
   38           try
   39           {
   40               selectPathInterfaceImpl = Class.forName(implClassName);
   41           }
   42           catch (ClassNotFoundException e)
   43           {
   44               engineAvailable = false;
   45           }
   46           catch (NoClassDefFoundError e)
   47           {
   48               engineAvailable = false;
   49           }
   50   
   51           if (engineAvailable)
   52           {
   53               try
   54               {
   55                   Constructor constructor = selectPathInterfaceImpl.getConstructor(
   56                       new Class[] {String.class, String.class, Map.class, String.class});
   57                   _constructors.put(implClassName, constructor);
   58               }
   59               catch (Exception e)
   60               {
   61                   throw new RuntimeException(e);
   62               }
   63           }
   64       }
   65   
   66       public static synchronized SelectPathInterface createInstance(String implClassName,
   67               String xpath, String contextVar, Map namespaceMap)
   68       {
   69           if (_constructors.get(implClassName) == null)
   70               init(implClassName);
   71   
   72           if (_constructors.get(implClassName) == null)
   73               return null;
   74   
   75           Constructor constructor = (Constructor)_constructors.get(implClassName);
   76           try
   77           {
   78               Object defaultNS = namespaceMap.get(XPath._DEFAULT_ELT_NS);
   79               if (defaultNS != null)
   80                   namespaceMap.remove(XPath._DEFAULT_ELT_NS);
   81               return (SelectPathInterface)constructor.newInstance(
   82                   new Object[] {xpath, contextVar, namespaceMap, (String)defaultNS});
   83           }
   84           catch (Exception e)
   85           {
   86               throw new RuntimeException(e);
   87           }
   88       }
   89   
   90       public static interface SelectPathInterface
   91       {
   92           public List selectPath(Object node);
   93       }
   94   }

Save This Page
Home » xmlbeans-2.5.0-src » org.apache.xmlbeans.impl » store » [javadoc | source]