org.ballerinalang.stdlib.internal.file.GetExtension.java Source code

Java tutorial

Introduction

Here is the source code for org.ballerinalang.stdlib.internal.file.GetExtension.java

Source

/*
 * Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * WSO2 Inc. licenses this file to you 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 org.ballerinalang.stdlib.internal.file;

import org.apache.commons.io.FilenameUtils;
import org.ballerinalang.bre.Context;
import org.ballerinalang.bre.bvm.BlockingNativeCallableUnit;
import org.ballerinalang.model.types.TypeKind;
import org.ballerinalang.model.values.BMap;
import org.ballerinalang.model.values.BString;
import org.ballerinalang.model.values.BValue;
import org.ballerinalang.natives.annotations.BallerinaFunction;
import org.ballerinalang.natives.annotations.Receiver;
import org.ballerinalang.natives.annotations.ReturnType;
import org.ballerinalang.stdlib.internal.Constants;

import java.nio.file.Path;

/**
 * Retrieves the last modified time of the specified file.
 *
 * @since 0.971.0
 */
@BallerinaFunction(orgName = Constants.ORG_NAME, packageName = Constants.PACKAGE_NAME, functionName = "getExtension", receiver = @Receiver(type = TypeKind.OBJECT, structType = Constants.PATH_STRUCT, structPackage = Constants.PACKAGE_PATH), returnType = {
        @ReturnType(type = TypeKind.STRING) }, isPublic = true)
public class GetExtension extends BlockingNativeCallableUnit {
    /**
     * {@inheritDoc}
     */
    @Override
    public void execute(Context context) {
        BMap<String, BValue> pathStruct = (BMap<String, BValue>) context.getRefArgument(0);
        Path path = (Path) pathStruct.getNativeData(Constants.PATH_DEFINITION_NAME);
        String extension = FilenameUtils.getExtension(path.toAbsolutePath().toString());
        context.setReturnValues(new BString(extension == null ? "" : extension));
    }
}