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.comphelper;
23 
24 import com.sun.star.lang.NullPointerException;
25 import com.sun.star.resource.MissingResourceException;
26 import com.sun.star.resource.OfficeResourceLoader;
27 import com.sun.star.resource.XResourceBundle;
28 import com.sun.star.resource.XResourceBundleLoader;
29 import com.sun.star.uno.AnyConverter;
30 import com.sun.star.uno.XComponentContext;
31 
32 public class OfficeResourceBundle implements AutoCloseable {
33 
34     private XComponentContext context;
35     private String baseName;
36     private boolean haveAttemptedCreate;
37     private XResourceBundle bundle;
38 
39 
40     /** constructs a resource bundle
41         @param  context
42             the component context to operate in
43         @param  bundleBaseName
44             the base name of the resource file which should be accessed (*without* the SUPD!)
45         @throws com.sun.star.lang.NullPointerException
46             if the given component context is null
47      */
OfficeResourceBundle(XComponentContext context, String bundleBaseName)48     public OfficeResourceBundle(XComponentContext context, String bundleBaseName) throws NullPointerException {
49         if (context == null) {
50             throw new NullPointerException();
51         }
52         this.context = context;
53         this.baseName = bundleBaseName;
54         this.haveAttemptedCreate = false;
55     }
56 
57     @Override
close()58     public void close() {
59         CompHelper.disposeComponent(bundle);
60     }
61 
62     /**
63      * Return the bundle's base name as passed to the constructor.
64      */
getBaseName()65     public String getBaseName() {
66         return baseName;
67     }
68 
69     /** loads the string with the given resource id from the resource bundle
70         @param  _resourceId
71             the id of the string to load
72         @return
73             the requested resource string. If no string with the given id exists in the resource bundle,
74             an empty string is returned.
75     */
loadString( int _resourceId )76     public String loadString( int _resourceId ) {
77         synchronized (this) {
78             String ret = "";
79             if (loadBundle()) {
80                 try {
81                     Object value = bundle.getByName(getStringResourceKey(_resourceId));
82                     ret = AnyConverter.toString(value);
83                 } catch (com.sun.star.uno.Exception ex) {
84                 }
85             }
86             return ret;
87         }
88     }
89 
90     /** determines whether the resource bundle has a string with the given id
91         @param  _resourceId
92             the id of the string whose existence is to be checked
93         @return
94             true if and only if a string with the given ID exists in the resource
95             bundle.
96     */
hasString( int _resourceId )97     public boolean hasString( int _resourceId ) {
98         synchronized (this) {
99             boolean ret = false;
100             if (loadBundle()) {
101                 ret = bundle.hasByName(getStringResourceKey(_resourceId));
102             }
103             return ret;
104         }
105     }
106 
getStringResourceKey(int resourceId)107     private String getStringResourceKey(int resourceId) {
108         return "string:" + resourceId;
109     }
110 
loadBundle()111     private boolean loadBundle() {
112         if (haveAttemptedCreate) {
113             return bundle != null;
114         }
115         haveAttemptedCreate = true;
116 
117         try {
118             XResourceBundleLoader loader = OfficeResourceLoader.get(context);
119             try {
120                 if (loader == null) {
121                     return false;
122                 }
123                 try {
124                     bundle = loader.loadBundle_Default(baseName);
125                 } catch (MissingResourceException missingResourceException) {
126                 }
127                 return bundle != null;
128             } finally {
129                 CompHelper.disposeComponent(loader);
130             }
131         } catch (Exception exception) {
132         }
133         return false;
134     }
135 }
136