Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
Examples
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
TDT4250
2021
Examples
Commits
f51ff7b7
Commit
f51ff7b7
authored
5 years ago
by
Hallvard Trætteberg
Browse files
Options
Downloads
Patches
Plain Diff
Some cleanups
parent
de0cd329
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
dict-ws/tdt4250.dict3.gogo/src/tdt4250/dict3/gogo/DictCommands.java
+43
-42
43 additions, 42 deletions
...t4250.dict3.gogo/src/tdt4250/dict3/gogo/DictCommands.java
with
43 additions
and
42 deletions
dict-ws/tdt4250.dict3.gogo/src/tdt4250/dict3/gogo/DictCommands.java
+
43
−
42
View file @
f51ff7b7
...
@@ -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
);
}
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment