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 package org.apache.openoffice.comp.sdbc.dbtools.util;
23 
24 import java.util.List;
25 
26 import org.apache.commons.lang3.mutable.MutableObject;
27 import org.apache.commons.lang3.tuple.Pair;
28 import org.apache.openoffice.comp.sdbc.dbtools.comphelper.CompHelper;
29 import org.apache.openoffice.comp.sdbc.dbtools.comphelper.OfficeResourceBundle;
30 
31 import com.sun.star.lang.NullPointerException;
32 import com.sun.star.uno.XComponentContext;
33 
34 /**
35  * helper class for accessing resources shared by different libraries
36  * in the connectivity module.
37  */
38 public class SharedResources {
39     private static SharedResources instance;
40     private static int referenceCount = 0;
41 
42     private OfficeResourceBundle resourceBundle;
43 
44     // FIXME: the C++ implementation gets the XComponentContext using ::comphelper::getProcessServiceFactory(), we don't.
registerClient(XComponentContext context)45     public synchronized static void registerClient(XComponentContext context) {
46         if (instance == null) {
47             instance = new SharedResources(context);
48         }
49         ++referenceCount;
50     }
51 
revokeClient()52     public synchronized static void revokeClient() {
53         if (--referenceCount == 0) {
54             CompHelper.disposeComponent(instance);
55             instance = null;
56         }
57     }
58 
getInstance()59     public synchronized static SharedResources getInstance() {
60         return instance;
61     }
62 
SharedResources(XComponentContext context)63     private SharedResources(XComponentContext context) {
64         try {
65             resourceBundle = new OfficeResourceBundle(context, "cnr");
66         } catch (NullPointerException nullPointerException) {
67         }
68     }
69 
substitute( MutableObject<String> _inout_rString, String sPattern, String _rReplace )70     private int substitute( MutableObject<String> _inout_rString,
71             String sPattern, String _rReplace ) {
72         int nOccurences = 0;
73         String string = _inout_rString.getValue();
74         int nIndex = 0;
75         while ( ( nIndex = string.indexOf( sPattern ) ) > -1 )
76         {
77             ++nOccurences;
78             string = string.substring(0, nIndex) +
79                     _rReplace + string.substring(nIndex + sPattern.length());
80         }
81         _inout_rString.setValue(string);
82         return nOccurences;
83     }
84 
85 
86     /** loads a string from the shared resource file
87         @param  _nResId
88             the resource ID of the string
89         @return
90             the string from the resource file
91      */
92     public String
getResourceString( int _nResId )93     getResourceString(
94         int _nResId
95     ) {
96         if (resourceBundle == null) {
97             return "";
98         }
99         return resourceBundle.loadString(_nResId);
100     }
101 
102     /** loads a string from the shared resource file, and replaces
103         a given ASCII pattern with a given string
104 
105         @param  _nResId
106             the resource ID of the string to load
107         @param  _pAsciiPatternToReplace
108             the ASCII string which is to search in the string. Must not be null.
109         @param  _rStringToSubstitute
110             the String which should substitute the ASCII pattern.
111 
112         @return
113             the string from the resource file, with applied string substitution
114      */
115     public String
getResourceStringWithSubstitution( int _nResId, String _pAsciiPatternToReplace, String _rStringToSubstitute )116     getResourceStringWithSubstitution(
117         int _nResId,
118         String _pAsciiPatternToReplace,
119         String _rStringToSubstitute
120     ) {
121         MutableObject<String> string = new MutableObject<>(getResourceString(_nResId));
122         substitute(string, _pAsciiPatternToReplace, _rStringToSubstitute);
123         return string.getValue();
124     }
125 
126     /** loads a string from the shared resource file, and replaces
127         a given ASCII pattern with a given string
128 
129         @param  _nResId
130             the resource ID of the string to load
131         @param  _pAsciiPatternToReplace1
132             the ASCII string (1) which is to search in the string. Must not be null.
133         @param  _rStringToSubstitute1
134             the String which should substitute the ASCII pattern (1)
135         @param  _pAsciiPatternToReplace2
136             the ASCII string (2) which is to search in the string. Must not be null.
137         @param  _rStringToSubstitute2
138             the String which should substitute the ASCII pattern (2)
139 
140         @return
141             the string from the resource file, with applied string substitution
142      */
143     public String
getResourceStringWithSubstitution( int _nResId, String _pAsciiPatternToReplace1, String _rStringToSubstitute1, String _pAsciiPatternToReplace2, String _rStringToSubstitute2 )144     getResourceStringWithSubstitution(
145         int _nResId,
146         String _pAsciiPatternToReplace1,
147         String _rStringToSubstitute1,
148         String _pAsciiPatternToReplace2,
149         String _rStringToSubstitute2
150     ) {
151         MutableObject<String> string = new MutableObject<>(getResourceString(_nResId));
152         substitute(string, _pAsciiPatternToReplace1, _rStringToSubstitute1);
153         substitute(string, _pAsciiPatternToReplace2, _rStringToSubstitute2);
154         return string.getValue();
155     }
156 
157     /** loads a string from the shared resource file, and replaces
158         a given ASCII pattern with a given string
159 
160         @param  _nResId
161             the resource ID of the string to load
162         @param  _pAsciiPatternToReplace1
163             the ASCII string (1) which is to search in the string. Must not be null.
164         @param  _rStringToSubstitute1
165             the String which should substitute the ASCII pattern (1)
166         @param  _pAsciiPatternToReplace2
167             the ASCII string (2) which is to search in the string. Must not be null.
168         @param  _rStringToSubstitute2
169             the String which should substitute the ASCII pattern (2)
170         @param  _pAsciiPatternToReplace3
171             the ASCII string (3) which is to search in the string. Must not be null.
172         @param  _rStringToSubstitute3
173             the String which should substitute the ASCII pattern (3)
174 
175         @return
176             the string from the resource file, with applied string substitution
177      */
178     public String
getResourceStringWithSubstitution( int _nResId, String _pAsciiPatternToReplace1, String _rStringToSubstitute1, String _pAsciiPatternToReplace2, String _rStringToSubstitute2, String _pAsciiPatternToReplace3, String _rStringToSubstitute3 )179     getResourceStringWithSubstitution(
180         int _nResId,
181         String _pAsciiPatternToReplace1,
182         String _rStringToSubstitute1,
183         String _pAsciiPatternToReplace2,
184         String _rStringToSubstitute2,
185         String _pAsciiPatternToReplace3,
186         String _rStringToSubstitute3
187     ) {
188         MutableObject<String> string = new MutableObject<>(getResourceString(_nResId));
189         substitute(string, _pAsciiPatternToReplace1, _rStringToSubstitute1);
190         substitute(string, _pAsciiPatternToReplace2, _rStringToSubstitute2);
191         substitute(string, _pAsciiPatternToReplace3, _rStringToSubstitute3);
192         return string.getValue();
193     }
194 
195     /** loads a string from the shared resource file, and replaces a given ASCII pattern with a given string
196 
197         @param  _nResId
198             the resource ID of the string to load
199         @param  patternsAndSubstitutes
200             A list of substitutions.
201 
202         @return
203             the string from the resource file, with applied string substitution
204      */
205     public String
getResourceStringWithSubstitution( int _nResId, List<Pair<String,String>> patternsAndSubstitutes)206     getResourceStringWithSubstitution( int _nResId,
207             List<Pair<String,String>> patternsAndSubstitutes) {
208         MutableObject<String> string = new MutableObject<>(getResourceString(_nResId));
209         for (Pair<String,String> pair : patternsAndSubstitutes) {
210             substitute(string, pair.getLeft(), pair.getRight());
211         }
212         return string.getValue();
213     }
214 }
215