Autopsy  4.19.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
TextTranslationService.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2018-2019 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * 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, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.texttranslation;
20 
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.Optional;
24 import org.openide.util.Lookup;
26 import javax.annotation.concurrent.GuardedBy;
27 
32 public final class TextTranslationService {
33 
34  private final static TextTranslationService tts = new TextTranslationService();
35 
36  private final Collection<? extends TextTranslator> translators;
37  @GuardedBy("this")
39 
41  //Perform look up for Text Translation implementations ONLY ONCE during
42  //class loading.
43  translators = Lookup.getDefault().lookupAll(TextTranslator.class);
44  updateSelectedTranslator();
45  }
46 
48  return tts;
49  }
50 
55  synchronized void updateSelectedTranslator() {
56  String translatorName = UserPreferences.getTextTranslatorName();
57  for (TextTranslator translator : translators) {
58  if (translator.getName().equals(translatorName)) {
59  selectedTranslator = Optional.ofNullable(translator);
60  return;
61  }
62  }
63  selectedTranslator = Optional.empty();
64  }
65 
80  public synchronized String translate(String input) throws NoServiceProviderException, TranslationException {
81  if (hasProvider()) {
82  return selectedTranslator.get().translate(input);
83  }
84  throw new NoServiceProviderException(
85  "Could not find a TextTranslator service provider");
86  }
87 
97  TextTranslator getTranslatorByName(String translatorName) throws NoServiceProviderException {
98  for (TextTranslator translator : translators) {
99  if (translator.getName().equals(translatorName)) {
100  return translator;
101  }
102  }
103  throw new NoServiceProviderException(
104  "Could not find the specified TextTranslator service provider: " + translatorName);
105  }
106 
112  Collection<? extends TextTranslator> getTranslators() {
113  return Collections.unmodifiableCollection(translators);
114  }
115 
122  public synchronized boolean hasProvider() {
123  return selectedTranslator.isPresent();
124  }
125 
131  public synchronized int getMaxTextChars() {
132  return selectedTranslator.get().getMaxTextChars();
133  }
134 }

Copyright © 2012-2021 Basis Technology. Generated on: Thu Sep 30 2021
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.