Skip to content
Snippets Groups Projects
Commit f51ff7b7 authored by Hallvard Trætteberg's avatar Hallvard Trætteberg
Browse files

Some cleanups

parent de0cd329
No related branches found
No related tags found
No related merge requests found
...@@ -8,6 +8,7 @@ import org.osgi.framework.BundleContext; ...@@ -8,6 +8,7 @@ import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil; import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.InvalidSyntaxException; import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference; import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Component;
import tdt4250.dict3.api.Dict; import tdt4250.dict3.api.Dict;
...@@ -30,62 +31,60 @@ import tdt4250.dict3.util.SortedSetWords; ...@@ -30,62 +31,60 @@ import tdt4250.dict3.util.SortedSetWords;
) )
public class DictCommands { public class DictCommands {
public void listDicts() throws InvalidSyntaxException { public void listDicts() {
System.out.println("Dictionaries: "); System.out.println("Dictionaries: ");
// iterate through all Dict service objects // iterate through all Dict service objects
BundleContext bc = FrameworkUtil.getBundle(this.getClass()).getBundleContext(); BundleContext bc = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
try {
for (ServiceReference<Dict> serviceReference : bc.getServiceReferences(Dict.class, null)) { for (ServiceReference<Dict> serviceReference : bc.getServiceReferences(Dict.class, null)) {
Dict dict = bc.getService(serviceReference); Dict dict = bc.getService(serviceReference);
System.out.print(dict.getDictName() + " "); System.out.print(dict.getDictName() + " ");
} }
System.out.println(); System.out.println();
} catch (InvalidSyntaxException e) {
}
} }
public void dictLookup(String s) throws InvalidSyntaxException { public void dictLookup(String s) {
BundleContext bc = FrameworkUtil.getBundle(this.getClass()).getBundleContext(); BundleContext bc = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
try {
// iterate through all Dict service objects // iterate through all Dict service objects
for (ServiceReference<Dict> serviceReference : bc.getServiceReferences(Dict.class, null)) { for (ServiceReference<Dict> serviceReference : bc.getServiceReferences(Dict.class, null)) {
Dict dict = bc.getService(serviceReference); Dict dict = bc.getService(serviceReference);
DictSearchResult search = dict.search(s); DictSearchResult search = dict.search(s);
System.out.println(dict.getDictName() + ": " + search.getMessage()); System.out.println(dict.getDictName() + ": " + search.getMessage());
} }
} catch (InvalidSyntaxException e) {
}
} }
public void addDict(String name, String urlStringOrWord, String... ss) { public void addDict(String name, String urlStringOrWord, String... ss) {
MutableWords words = null; MutableWords words = null;
URL url = null;
try { try {
url = new URL(urlStringOrWord); URL url = new URL(urlStringOrWord);
} catch (MalformedURLException e) {
}
if (url != null) {
try { try {
words = new ResourceWords(url.openStream()); words = new ResourceWords(url.openStream());
} catch (IOException e) { } catch (IOException e) {
System.err.println("Couldn't read from " + url); System.err.println("Couldn't read from " + url);
return;
} }
} else { } catch (MalformedURLException e) {
words = new SortedSetWords(); words = new SortedSetWords();
words.addWord(urlStringOrWord); words.addWord(urlStringOrWord);
} }
if (words != null) {
for (String s : ss) { for (String s : ss) {
words.addWord(s); words.addWord(s);
} }
boolean exists = false; boolean exists = removeDict(name, false) > 0;
try {
exists = removeDict(name, false) > 0;
} catch (InvalidSyntaxException e) {
}
BundleContext bc = FrameworkUtil.getBundle(this.getClass()).getBundleContext(); BundleContext bc = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
bc.registerService(Dict.class, new CommandDict(name, words), null); bc.registerService(Dict.class, new CommandDict(name, words), null);
System.out.println("\"" + name + "\" dictionary " + (exists ? "replaced" : "added")); System.out.println("\"" + name + "\" dictionary " + (exists ? "replaced" : "added"));
} }
}
private int removeDict(String name, boolean showMessage) throws InvalidSyntaxException { private int removeDict(String name, boolean showMessage) {
int count = 0; int count = 0;
BundleContext bc = FrameworkUtil.getBundle(this.getClass()).getBundleContext(); BundleContext bc = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
try {
// iterate through all Dict service objects // iterate through all Dict service objects
for (ServiceReference<Dict> serviceReference : bc.getServiceReferences(Dict.class, null)) { for (ServiceReference<Dict> serviceReference : bc.getServiceReferences(Dict.class, null)) {
Dict dict = bc.getService(serviceReference); Dict dict = bc.getService(serviceReference);
...@@ -100,10 +99,12 @@ public class DictCommands { ...@@ -100,10 +99,12 @@ public class DictCommands {
if (count == 0 && showMessage) { if (count == 0 && showMessage) {
System.out.println("\"" + name + "\" dictionary not found"); System.out.println("\"" + name + "\" dictionary not found");
} }
} catch (InvalidSyntaxException e) {
}
return count; return count;
} }
public void removeDict(String name) throws InvalidSyntaxException { public void removeDict(String name) {
removeDict(name, true); removeDict(name, true);
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment