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.lang.XServiceInfo;
26 import com.sun.star.lib.uno.helper.WeakBase;
27 import com.sun.star.sheet.FormulaLanguage;
28 import com.sun.star.sheet.FormulaMapGroup;
29 import com.sun.star.sheet.FormulaOpCodeMapEntry;
30 import com.sun.star.sheet.FormulaToken;
31 
32 import java.util.ArrayList;
33 import java.util.Map;
34 
35 public final class SOFormulaOpCodeMapper extends WeakBase
36         implements com.sun.star.sheet.XFormulaOpCodeMapper, XServiceInfo
37 {
38 
39     private static final String __serviceName = "com.sun.star.report.pentaho.SOFormulaOpCodeMapper";
40     private final SOFormulaParser parser;
41     // attributes
42     final private int m_OpCodeExternal = 0;
43     final private int m_OpCodeUnknown = 0;
44 
SOFormulaOpCodeMapper(SOFormulaParser parser)45     public SOFormulaOpCodeMapper(SOFormulaParser parser)
46     {
47         this.parser = parser;
48     }
49 
50     // com.sun.star.sheet.XFormulaOpCodeMapper:
getOpCodeExternal()51     public int getOpCodeExternal()
52     {
53         return m_OpCodeExternal;
54     }
55 
getOpCodeUnknown()56     public int getOpCodeUnknown()
57     {
58         return m_OpCodeUnknown;
59     }
60 
getMappings(String[] Names, int Language)61     public com.sun.star.sheet.FormulaToken[] getMappings(String[] Names, int Language) throws com.sun.star.lang.IllegalArgumentException
62     {
63         if (Language != FormulaLanguage.ODFF)
64         {
65             throw new IllegalArgumentException();
66         }
67         final ArrayList token = new ArrayList();
68         final Map parserNames = parser.getNames();
69         for (int i = 0; i < Names.length; i++)
70         {
71             if (parserNames.containsKey(Names[i]))
72             {
73                 token.add(((FormulaOpCodeMapEntry) parserNames.get(Names[i])).Token);
74             }
75 
76         }
77         return (com.sun.star.sheet.FormulaToken[]) token.toArray(new FormulaToken[token.size()]);
78     }
79 
getAvailableMappings(int Language, int Groups)80     public com.sun.star.sheet.FormulaOpCodeMapEntry[] getAvailableMappings(int Language, int Groups) throws com.sun.star.lang.IllegalArgumentException
81     {
82         if (Language != FormulaLanguage.ODFF)
83         {
84             throw new IllegalArgumentException();
85         }
86         final ArrayList token = new ArrayList();
87         if (Groups == FormulaMapGroup.SPECIAL)
88         {
89             return (com.sun.star.sheet.FormulaOpCodeMapEntry[]) parser.getSpecialOpCodes().toArray(new FormulaOpCodeMapEntry[parser.getSpecialOpCodes().size()]);
90         }
91         else
92         {
93             if ((Groups & FormulaMapGroup.ARRAY_SEPARATORS) != 0)
94             {
95                 token.addAll(parser.getGroup(SOFormulaParser.ARRAY_SEPARATORS).values());
96             }
97             if ((Groups & FormulaMapGroup.SEPARATORS) != 0)
98             {
99                 token.addAll(parser.getGroup(SOFormulaParser.SEPARATORS).values());
100             }
101             if ((Groups & FormulaMapGroup.ARRAY_SEPARATORS) != 0)
102             {
103                 token.addAll(parser.getGroup(SOFormulaParser.ARRAY_SEPARATORS).values());
104             }
105             if ((Groups & FormulaMapGroup.UNARY_OPERATORS) != 0)
106             {
107                 token.addAll(parser.getGroup(SOFormulaParser.UNARY_OPERATORS).values());
108             }
109             if ((Groups & FormulaMapGroup.BINARY_OPERATORS) != 0)
110             {
111                 token.addAll(parser.getGroup(SOFormulaParser.BINARY_OPERATORS).values());
112             }
113             if ((Groups & FormulaMapGroup.FUNCTIONS) != 0)
114             {
115                 token.addAll(parser.getGroup(SOFormulaParser.FUNCTIONS).values());
116             }
117         }
118 
119         return (com.sun.star.sheet.FormulaOpCodeMapEntry[]) token.toArray(new FormulaOpCodeMapEntry[token.size()]);
120     }
121 
getImplementationName()122     public String getImplementationName()
123     {
124         return SOFormulaOpCodeMapper.class.getName();
125     }
126 
supportsService(String sServiceName)127     public boolean supportsService(String sServiceName)
128     {
129         return sServiceName.equals(__serviceName);
130     }
131 
getSupportedServiceNames()132     public String[] getSupportedServiceNames()
133     {
134         return getServiceNames();
135     }
136 
137     /**
138      * This method is a simple helper function to used in the static component initialisation functions as well as
139      * in getSupportedServiceNames.
140      * @return
141      */
getServiceNames()142     public static String[] getServiceNames()
143     {
144         return new String[]
145                 {
146                     __serviceName
147                 };
148     }
149 }
150