All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.sarl.api.naming.parser.AbstractSchemeNameParser.sarl Maven / Gradle / Ivy

The newest version!
/*
 * $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.StringTokenizer
import org.eclipse.xtend.lib.annotations.Accessors

/** 
 * Abstact implementation of a parser of names that is accepting URI-based syntax for a specific scheme
 * 
 * @param  the type of the name that is the result of the decoding.
 * @author Stéphane Galland
 * @version api.naming 0.14.0 20241106-161408
 * @mavengroupid io.sarl.sdk
 * @mavenartifactid api.naming
 * @since 0.12
 */
abstract class AbstractSchemeNameParser implements ISchemeNameParser {

	@Accessors
	val scheme : String

	/** Constructor.
	 *
	 * @param scheme the scheme that is supported by this name parser.
	 */
	protected new (scheme : String) {
		assert !scheme.isNullOrEmpty
		this.scheme = scheme
	}

	/** Refactors the URI.
	 * 
	 * @param uri the URI to refactor.
	 * @param minElements is the expected minimum number of path components.
	 * @param maxElements is the expected maximum number of path components.
	 * @param lastIsInteger indicates if the last component could be an integer. In this case, 
	 * the expected number of not-integer components becomes [{@code minElements}, {@code maxElements - 1}]. 
	 * @return the new URI. 
	 */
	protected def refactor(uri : URI, minElements : int, maxElements : int, lastIsInteger : boolean = false) : URI {
		val newPath = new StringBuilder
		if (!uri.host.isNullOrEmpty) {
			newPath.append("/").append(uri.host)
		}
		if (!uri.path.isNullOrEmpty) {
			if (!uri.path.startsWith("/")) {
				newPath.append("/")
			}
			newPath.append(uri.path)
		}
		if (newPath.length === 0) {
			if (!uri.schemeSpecificPart.isNullOrEmpty) {
				if (!uri.schemeSpecificPart.startsWith("/")) {
					newPath.append("/")
				}
				newPath.append(uri.schemeSpecificPart)
			}
		}
		val path = newPath.toString
		if (!path.empty) {
			val validatedPath = validatePath(path, minElements, maxElements, lastIsInteger)
			if (!validatedPath.isNullOrEmpty) {
				return new URI(uri.scheme, null, validatedPath.toString, uri.fragment)
			}
		}
		return null
	}

	/** Validate the path.
	 *
	 * @param path the path to validate.
	 * @param minElements is the expected minimum number of path components.
	 * @param maxElements is the expected maximum number of path components.
	 * @param lastIsInteger indicates if the last component could be an integer. In this case, 
	 * the expected number of not-integer components becomes [{@code minElements}, {@code maxElements - 1}]. 
	 * @return the validated path.
	 */
	protected def validatePath(path : String, minElements : int, maxElements : int, lastIsInteger : boolean = false) : String {
		if (path.isNullOrEmpty) {
			return if ((minElements..maxElements).contains(0)) "" else null
		}
		var tokenizer = new StringTokenizer(path, "/")
		var buffer = new StringBuilder
		var lastComponent : String = null
		var nb = 0
		while (tokenizer.hasMoreTokens) {
			var token = tokenizer.nextToken
			if (token.isNullOrEmpty) {
				return null
			}
			buffer.append("/").append(token)
			lastComponent = token
			nb++
		}
		if (lastIsInteger) {
			try {
				// Fails if the component is not an integer
				Integer::parseUnsignedInt(lastComponent, 10)
				if ((minElements + 1 .. maxElements).contains(nb)) {
					return buffer.toString
				}
			} catch (ex : Throwable) {
				if ((minElements .. maxElements - 1).contains(nb)) {
					return buffer.toString
				}
			}
		} else if ((minElements .. maxElements).contains(nb)) {
			return buffer.toString
		}
		return null
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy