
io.sarl.api.naming.parser.UriBasedNameParser 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 io.sarl.lang.core.annotation.DefaultValue;
import io.sarl.lang.core.annotation.DefaultValueSource;
import io.sarl.lang.core.annotation.DefaultValueUse;
import io.sarl.lang.core.annotation.SarlElementType;
import io.sarl.lang.core.annotation.SarlSourceCode;
import io.sarl.lang.core.annotation.SarlSpecification;
import io.sarl.lang.core.annotation.SyntheticMember;
import java.net.URI;
import java.util.Collections;
import java.util.Set;
import java.util.TreeMap;
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
import org.eclipse.xtext.xbase.lib.Exceptions;
import org.eclipse.xtext.xbase.lib.Pure;
import org.eclipse.xtext.xbase.lib.StringExtensions;
import org.eclipse.xtext.xbase.lib.XbaseGenerated;
/**
* 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
*/
@SarlSpecification("0.14")
@SarlElementType(10)
@XbaseGenerated
@SuppressWarnings("all")
public class UriBasedNameParser implements INameParser {
private final TreeMap> schemeNameParser = CollectionLiterals.>newTreeMap(null);
/**
* Construct a name parser based on the given scheme parsers.
*
* @param parsers the set of scheme name parsers.
*/
@DefaultValueSource
@SuppressWarnings("raw_type")
public UriBasedNameParser(@DefaultValue("io.sarl.api.naming.parser.UriBasedNameParser#NEW_0") final Set parsers) {
if (((parsers != null) && (!parsers.isEmpty()))) {
this.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")
public void setSchemeNameParsers(final Set parsers) {
if ((parsers != null)) {
this.schemeNameParser.clear();
for (final ISchemeNameParser parser : parsers) {
if (parser!=null) {
this.addSchemeNameParser(parser);
}
}
}
}
@Override
public void addSchemeNameParser(final ISchemeNameParser> parser) {
class $AssertEvaluator$ {
final boolean $$result;
$AssertEvaluator$() {
this.$$result = (parser != null);
}
}
assert new $AssertEvaluator$().$$result;
this.schemeNameParser.put(parser.getScheme(), parser);
}
@Override
public ISchemeNameParser> removeSchemeNameParser(final String scheme) {
ISchemeNameParser> _xblockexpression = null;
{
class $AssertEvaluator$ {
final boolean $$result;
$AssertEvaluator$() {
boolean _isNullOrEmpty = StringExtensions.isNullOrEmpty(scheme);
this.$$result = (!_isNullOrEmpty);
}
}
assert new $AssertEvaluator$().$$result;
_xblockexpression = this.schemeNameParser.remove(scheme);
}
return _xblockexpression;
}
@Pure
public Set getSupportedSchemes() {
return Collections.unmodifiableSet(this.schemeNameParser.keySet());
}
@Pure
public URI normalize(final URI name) {
try {
final String scheme = name.getScheme();
if (((((!StringExtensions.isNullOrEmpty(scheme)) && StringExtensions.isNullOrEmpty(name.getQuery())) && StringExtensions.isNullOrEmpty(name.getUserInfo())) && (name.getPort() == (-1)))) {
ISchemeNameParser> parser = this.schemeNameParser.get(scheme);
if ((parser != null)) {
return parser.refactor(name);
}
}
} catch (final Throwable _t) {
if (_t instanceof Throwable) {
} else {
throw Exceptions.sneakyThrow(_t);
}
}
return null;
}
@Pure
@Override
public SarlName decode(final URI name) {
try {
if ((((name != null) && (name.getPath() != null)) && name.getPath().startsWith("/"))) {
String scheme = name.getScheme();
boolean _isNullOrEmpty = StringExtensions.isNullOrEmpty(scheme);
if ((!_isNullOrEmpty)) {
ISchemeNameParser> parser = this.schemeNameParser.get(scheme);
if ((parser != null)) {
return parser.decode(name);
}
}
}
} catch (final Throwable _t) {
if (_t instanceof Throwable) {
} else {
throw Exceptions.sneakyThrow(_t);
}
}
return null;
}
/**
* Default value for the parameter parsers
*/
@Pure
@SyntheticMember
@SarlSourceCode("null")
private static Set $DEFAULT_VALUE$NEW_0() {
return null;
}
/**
* Construct a name parser based on the given scheme parsers.
*
* @optionalparam parsers the set of scheme name parsers.
*/
@DefaultValueUse("java.util.Set")
@SyntheticMember
public UriBasedNameParser() {
this($DEFAULT_VALUE$NEW_0());
}
@Override
@Pure
@SyntheticMember
public boolean equals(final Object obj) {
return super.equals(obj);
}
@Override
@Pure
@SyntheticMember
public int hashCode() {
int result = super.hashCode();
return result;
}
}