
io.sarl.api.naming.parser.UriBasedNameParser.sarl Maven / Gradle / Ivy
Show all versions of api.naming Show documentation
/*
* $Id$
*
* SARL is an general-purpose agent programming language.
* More details on http://www.sarl.io
*
* Copyright (C) 2014-2024 SARL.io, the Original Authors and Main Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.sarl.api.naming.parser
import io.sarl.api.naming.name.SarlName
import java.net.URI
import java.util.Collections
import java.util.Set
/**
* Default implementation of a parser of names that is accepting URI-based syntax.
*
* @author Stéphane Galland
* @version api.naming 0.14.0 20241106-161408
* @mavengroupid io.sarl.sdk
* @mavenartifactid api.naming
* @since 0.12
*/
class UriBasedNameParser implements INameParser {
val schemeNameParser = >newTreeMap(null)
/** Construct a name parser based on the given scheme parsers.
*
* @param parsers the set of scheme name parsers.
*/
@SuppressWarnings("raw_type")
new (parsers : Set = null) {
if (parsers !== null && !parsers.isEmpty) {
setSchemeNameParsers(parsers);
}
}
/** Change the set of scheme name parsers that is used by this base name parser.
*
* This function could be overridden and annotated in subtypes in order to be used by the Guice injector.
*
* @param parsers the set of scheme name parsers.
*/
@SuppressWarnings("raw_type")
def setSchemeNameParsers(parsers : Set) {
if (parsers !== null) {
this.schemeNameParser.clear
for (parser : parsers) {
parser?.addSchemeNameParser
}
}
}
override addSchemeNameParser(parser : ISchemeNameParser>) {
assert parser !== null
this.schemeNameParser.put(parser.scheme, parser)
}
override removeSchemeNameParser(scheme : String) : ISchemeNameParser> {
assert !scheme.isNullOrEmpty
this.schemeNameParser.remove(scheme)
}
@Pure
def getSupportedSchemes : Set {
return Collections::unmodifiableSet(this.schemeNameParser.keySet)
}
@Pure
def normalize(name : URI) : URI {
try {
val scheme = name.scheme
if (!scheme.isNullOrEmpty
&& name.query.isNullOrEmpty
&& name.userInfo.isNullOrEmpty
&& name.port === -1) {
var parser = this.schemeNameParser.get(scheme)
if (parser !== null) {
return parser.refactor(name)
}
}
} catch (ex : Throwable) {
//
}
return null
}
@Pure
override decode(name : URI) : SarlName {
try {
if (name !== null && name.path !== null && name.path.startsWith("/")) {
var scheme = name.scheme
if (!scheme.isNullOrEmpty) {
var parser = this.schemeNameParser.get(scheme)
if (parser !== null) {
return parser.decode(name)
}
}
}
} catch (ex : Throwable) {
//
}
return null
}
}