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   
   16   package org.apache.xmlbeans.impl.store;
   17   
   18   import org.apache.xmlbeans.impl.common.XPath;
   19   
   20   import java.util.List;
   21   import java.util.Map;
   22   import java.lang.reflect.Constructor;
   23   
   24   
   25   /**
   26    * Date: Dec 6,2004
   27    *
   28    * Help class to decouple from xbean_xpath.jar and saxon.jar (version Saxon-B 8.1.1)
   29    */
   30   public final class SaxonXBeansDelegate
   31   {
   32       protected static boolean bInstantiated = false;
   33       static
   34       {
   35           boolean hasTheJars = false;
   36           Class saxonXPathImpl = null;
   37           Class saxonXQueryImpl = null;
   38           try
   39           {
   40               // from xbean_xpath.jar
   41               saxonXPathImpl = Class
   42                   .forName("org.apache.xmlbeans.impl.xpath.saxon.XBeansXPath");
   43               saxonXQueryImpl = Class
   44                   .forName("org.apache.xmlbeans.impl.xquery.saxon.XBeansXQuery");
   45   
   46               hasTheJars = true;
   47           }
   48           catch (ClassNotFoundException e)
   49           {
   50               hasTheJars = false;
   51           }
   52           catch (NoClassDefFoundError e)
   53           {
   54               hasTheJars = false;
   55           }
   56   
   57           if (hasTheJars)
   58           {
   59               try
   60               {
   61                   _constructor =
   62                       saxonXPathImpl.getConstructor(new Class[]{String.class,
   63                                                                 Map.class,
   64                                                                 String.class});
   65                   _xqConstructor =
   66                       saxonXQueryImpl.getConstructor(new Class[]{String.class,
   67                                                                  String.class,
   68                                                                  Integer.class});
   69               }
   70               catch (Exception e)
   71               {
   72                   throw new RuntimeException(e);
   73               }
   74           }
   75       }
   76   
   77       private SaxonXBeansDelegate()
   78       {}
   79   
   80       static SelectPathInterface createInstance(String xpath, Map namespaceMap)
   81       {
   82           bInstantiated = true;
   83           if (_constructor == null)
   84               return null;
   85   
   86           try
   87           {
   88               Object defaultNS = namespaceMap.get(XPath._DEFAULT_ELT_NS);
   89               if ( defaultNS != null )
   90                   namespaceMap.remove( XPath._DEFAULT_ELT_NS );
   91               return (SaxonXBeansDelegate.SelectPathInterface)
   92                       _constructor.newInstance(new Object[] {xpath,namespaceMap,(String)defaultNS});
   93           }
   94           catch (Exception e)
   95           {
   96               throw new RuntimeException(e);
   97           }
   98       }
   99   
  100      static QueryInterface createQueryInstance(String query, String contextVar, int boundary)
  101      {
  102           if (_xqConstructor == null)
  103               return null;
  104   
  105           try
  106           {
  107               return (SaxonXBeansDelegate.QueryInterface)
  108                       _xqConstructor.newInstance(new Object[] {query,contextVar,new Integer(boundary)});
  109           }
  110           catch (Exception e)
  111           {
  112               throw new RuntimeException(e);
  113           }
  114       }
  115   
  116   
  117   
  118       public static interface SelectPathInterface
  119       {
  120           public List selectPath(Object node);
  121       }
  122   
  123        public static interface QueryInterface
  124       {
  125           public List execQuery(Object node);
  126       }
  127   
  128        private static Constructor _constructor;
  129        private static Constructor _xqConstructor;
  130   }

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