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