001package gudusoft.gsqlparser.nodes;
002
003import gudusoft.gsqlparser.TSourceToken;
004
005/**
006 * Snowflake path object, usually used in stage reference
007 */
008public class TPathSqlNode  extends TParseTreeNode{
009
010    private TSourceToken rootDirectory; // refer to /
011    private TObjectName pathName; // refer to path name or file name
012
013    public TObjectNameList getPathList() {
014        return pathList;
015    }
016
017    private TObjectNameList pathList;
018    public void init(Object arg1){
019
020        if (arg1 instanceof TSourceToken) {
021            rootDirectory = (TSourceToken) arg1;
022        }else{
023            pathList = (TObjectNameList)arg1;
024        }
025
026    }
027
028    public void init(Object arg1, Object arg2){
029        rootDirectory = (TSourceToken) arg1;
030        pathName = (TObjectName) arg2;
031    }
032
033    public TSourceToken getRootDirectory() {
034        return rootDirectory;
035    }
036
037    public TObjectName getPathName() {
038        return pathName;
039    }
040
041    public void addObjectName(TObjectName objectName){
042        this.pathList.addObjectName(objectName);
043    }
044
045    public String[] splitPath() {
046        String path = "";
047        if (pathName != null) {
048            path = pathName.toString();
049        }else if (pathList != null){
050           // concatenate all the path names and put / between each path name
051            for (int i = 0; i < pathList.size(); i++) {
052                path += pathList.getObjectName(i).toString() + "/";
053            }
054
055        }
056
057        // Remove leading and trailing slashes if present
058        path = path.replaceAll("^/+|/+$", "");
059
060        // Split the path by forward slashes
061        String[] parts = path.split("/");
062
063        // If the path is empty or just a filename, return it as is
064        if (parts.length == 0 || !path.contains("/")) {
065            return new String[]{path};
066        }
067
068        return parts;
069    }
070
071    public void accept(TParseTreeVisitor v){
072        v.preVisit(this);
073        v.postVisit(this);
074    }
075
076    public void acceptChildren(TParseTreeVisitor v){
077        v.preVisit(this);
078        v.postVisit(this);
079    }
080
081}