1*1a37d047SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*1a37d047SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*1a37d047SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*1a37d047SAndrew Rist * distributed with this work for additional information 6*1a37d047SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*1a37d047SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*1a37d047SAndrew Rist * "License"); you may not use this file except in compliance 9*1a37d047SAndrew Rist * with the License. You may obtain a copy of the License at 10*1a37d047SAndrew Rist * 11*1a37d047SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*1a37d047SAndrew Rist * 13*1a37d047SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*1a37d047SAndrew Rist * software distributed under the License is distributed on an 15*1a37d047SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*1a37d047SAndrew Rist * KIND, either express or implied. See the License for the 17*1a37d047SAndrew Rist * specific language governing permissions and limitations 18*1a37d047SAndrew Rist * under the License. 19*1a37d047SAndrew Rist * 20*1a37d047SAndrew Rist *************************************************************/ 21*1a37d047SAndrew Rist 22*1a37d047SAndrew Rist 23cdf0e10cSrcweir package com.sun.star.report.pentaho; 24cdf0e10cSrcweir 25cdf0e10cSrcweir import com.sun.star.container.NoSuchElementException; 26cdf0e10cSrcweir import com.sun.star.lang.XServiceInfo; 27cdf0e10cSrcweir import com.sun.star.lib.uno.helper.ComponentBase; 28cdf0e10cSrcweir import com.sun.star.report.meta.XFunctionCategory; 29cdf0e10cSrcweir import com.sun.star.report.meta.XFunctionDescription; 30cdf0e10cSrcweir import com.sun.star.report.meta.XFunctionManager; 31cdf0e10cSrcweir import com.sun.star.uno.XComponentContext; 32cdf0e10cSrcweir 33cdf0e10cSrcweir import org.pentaho.reporting.libraries.formula.DefaultFormulaContext; 34cdf0e10cSrcweir import org.pentaho.reporting.libraries.formula.function.FunctionCategory; 35cdf0e10cSrcweir import org.pentaho.reporting.libraries.formula.function.FunctionDescription; 36cdf0e10cSrcweir import org.pentaho.reporting.libraries.formula.function.FunctionRegistry; 37cdf0e10cSrcweir 38cdf0e10cSrcweir /** 39cdf0e10cSrcweir * This class capsulates the class, that implements the minimal component, a factory for creating the service 40cdf0e10cSrcweir * (<CODE>__getComponentFactory</CODE>) and a method, that writes the information into the given registry key 41cdf0e10cSrcweir * (<CODE>__writeRegistryServiceInfo</CODE>). 42cdf0e10cSrcweir */ 43cdf0e10cSrcweir public final class SOFunctionManager extends ComponentBase implements XFunctionManager, XServiceInfo 44cdf0e10cSrcweir { 45cdf0e10cSrcweir 46cdf0e10cSrcweir private final XComponentContext m_xContext; 47cdf0e10cSrcweir /** 48cdf0e10cSrcweir * The service name, that must be used to get an instance of this service. 49cdf0e10cSrcweir */ 50cdf0e10cSrcweir private static final String __serviceName = 51cdf0e10cSrcweir "com.sun.star.report.meta.FunctionManager"; 52cdf0e10cSrcweir final private FunctionCategory[] categories; 53cdf0e10cSrcweir final private FunctionRegistry functionRegistry; 54cdf0e10cSrcweir final private DefaultFormulaContext defaultContext; 55cdf0e10cSrcweir SOFunctionManager(XComponentContext context)56cdf0e10cSrcweir public SOFunctionManager(XComponentContext context) 57cdf0e10cSrcweir { 58cdf0e10cSrcweir m_xContext = context; 59cdf0e10cSrcweir final ClassLoader cl = java.lang.Thread.currentThread().getContextClassLoader(); 60cdf0e10cSrcweir Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader()); 61cdf0e10cSrcweir defaultContext = new DefaultFormulaContext(); 62cdf0e10cSrcweir functionRegistry = defaultContext.getFunctionRegistry(); 63cdf0e10cSrcweir categories = functionRegistry.getCategories(); 64cdf0e10cSrcweir Thread.currentThread().setContextClassLoader(cl); 65cdf0e10cSrcweir 66cdf0e10cSrcweir } 67cdf0e10cSrcweir 68cdf0e10cSrcweir /** 69cdf0e10cSrcweir * This method returns an array of all supported service names. 70cdf0e10cSrcweir * 71cdf0e10cSrcweir * @return Array of supported service names. 72cdf0e10cSrcweir */ getSupportedServiceNames()73cdf0e10cSrcweir public String[] getSupportedServiceNames() 74cdf0e10cSrcweir { 75cdf0e10cSrcweir return getServiceNames(); 76cdf0e10cSrcweir } 77cdf0e10cSrcweir 78cdf0e10cSrcweir /** 79cdf0e10cSrcweir * This method is a simple helper function to used in the static component initialisation functions as well as 80cdf0e10cSrcweir * in getSupportedServiceNames. 81cdf0e10cSrcweir * @return 82cdf0e10cSrcweir */ getServiceNames()83cdf0e10cSrcweir public static String[] getServiceNames() 84cdf0e10cSrcweir { 85cdf0e10cSrcweir return new String[] 86cdf0e10cSrcweir { 87cdf0e10cSrcweir __serviceName 88cdf0e10cSrcweir }; 89cdf0e10cSrcweir } 90cdf0e10cSrcweir 91cdf0e10cSrcweir /** 92cdf0e10cSrcweir * This method returns true, if the given service will be supported by the component. 93cdf0e10cSrcweir * 94cdf0e10cSrcweir * @param sServiceName Service name. 95cdf0e10cSrcweir * @return True, if the given service name will be supported. 96cdf0e10cSrcweir */ supportsService(final String sServiceName)97cdf0e10cSrcweir public boolean supportsService(final String sServiceName) 98cdf0e10cSrcweir { 99cdf0e10cSrcweir return sServiceName.equals(__serviceName); 100cdf0e10cSrcweir } 101cdf0e10cSrcweir 102cdf0e10cSrcweir /** 103cdf0e10cSrcweir * Return the class name of the component. 104cdf0e10cSrcweir * 105cdf0e10cSrcweir * @return Class name of the component. 106cdf0e10cSrcweir */ getImplementationName()107cdf0e10cSrcweir public String getImplementationName() 108cdf0e10cSrcweir { 109cdf0e10cSrcweir return SOFunctionManager.class.getName(); 110cdf0e10cSrcweir } 111cdf0e10cSrcweir 112cdf0e10cSrcweir // com.sun.star.container.XElementAccess: getElementType()113cdf0e10cSrcweir public com.sun.star.uno.Type getElementType() 114cdf0e10cSrcweir { 115cdf0e10cSrcweir return new com.sun.star.uno.Type(XFunctionCategory.class); 116cdf0e10cSrcweir } 117cdf0e10cSrcweir hasElements()118cdf0e10cSrcweir public boolean hasElements() 119cdf0e10cSrcweir { 120cdf0e10cSrcweir return categories.length != 0; 121cdf0e10cSrcweir } 122cdf0e10cSrcweir 123cdf0e10cSrcweir // com.sun.star.container.XIndexAccess: getCount()124cdf0e10cSrcweir public int getCount() 125cdf0e10cSrcweir { 126cdf0e10cSrcweir return categories.length; 127cdf0e10cSrcweir } 128cdf0e10cSrcweir getByIndex(int Index)129cdf0e10cSrcweir public Object getByIndex(int Index) throws com.sun.star.lang.IndexOutOfBoundsException, com.sun.star.lang.WrappedTargetException 130cdf0e10cSrcweir { 131cdf0e10cSrcweir return getCategory(Index); 132cdf0e10cSrcweir } 133cdf0e10cSrcweir 134cdf0e10cSrcweir // com.sun.star.report.meta.XFunctionManager: getCategory(int position)135cdf0e10cSrcweir public com.sun.star.report.meta.XFunctionCategory getCategory(int position) throws com.sun.star.lang.IndexOutOfBoundsException, com.sun.star.lang.WrappedTargetException 136cdf0e10cSrcweir { 137cdf0e10cSrcweir if (position >= categories.length) 138cdf0e10cSrcweir { 139cdf0e10cSrcweir throw new com.sun.star.lang.IndexOutOfBoundsException(); 140cdf0e10cSrcweir } 141cdf0e10cSrcweir return new StarFunctionCategory(defaultContext, m_xContext, functionRegistry, position, categories[position]); 142cdf0e10cSrcweir } 143cdf0e10cSrcweir getFunctionByName(String arg0)144cdf0e10cSrcweir public XFunctionDescription getFunctionByName(String arg0) throws NoSuchElementException 145cdf0e10cSrcweir { 146cdf0e10cSrcweir final FunctionDescription func = functionRegistry.getMetaData(arg0); 147cdf0e10cSrcweir if (func == null) 148cdf0e10cSrcweir { 149cdf0e10cSrcweir throw new NoSuchElementException(); 150cdf0e10cSrcweir } 151cdf0e10cSrcweir int i = 0; 152cdf0e10cSrcweir for (; i < categories.length; i++) 153cdf0e10cSrcweir { 154cdf0e10cSrcweir if (categories[i] == func.getCategory()) 155cdf0e10cSrcweir { 156cdf0e10cSrcweir break; 157cdf0e10cSrcweir } 158cdf0e10cSrcweir } 159cdf0e10cSrcweir try 160cdf0e10cSrcweir { 161cdf0e10cSrcweir return new StarFunctionDescription(defaultContext, m_xContext, getCategory(i), func); 162cdf0e10cSrcweir } 163cdf0e10cSrcweir catch (Exception ex) 164cdf0e10cSrcweir { 165cdf0e10cSrcweir } 166cdf0e10cSrcweir return null; 167cdf0e10cSrcweir } 168cdf0e10cSrcweir } 169