<commit_before>/*******************************************************************************
 * Copyright (c) 2010 Oak Ridge National Laboratory.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 ******************************************************************************/
package org.csstudio.opibuilder.persistence;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IPath;


/**The path hosting an URL String.
 * @author <NAME>
 *
 */
public class URLPath implements IPath {
	private static final String ELLIPSIS = ".."; //$NON-NLS-1$
	private String url = null;
	/** The path segments */
	private String[] segments;
	
	/** The device id string. May be null if there is no device. */
	private String device = null;
	
	/**
	 * Path separator character constant "/" used in paths.
	 */
	public static final String SEPARATOR = "/"; //$NON-NLS-1$
	
	/**
	 * @param url
	 */
	public URLPath(String url){
		this.url = url;	
		int i = url.indexOf(DEVICE_SEPARATOR);
		if(i != -1){
			device = url.substring(0, i+1);
			url = url.substring(i+1);
		}
		segments = url.split(SEPARATOR);	
		
	}

	public URLPath(String device, String[] segments, boolean hasTrailing){
		this.device = device;
		this.segments = segments;
		StringBuilder sb = new StringBuilder(device);
		for (String s : segments) {
			sb.append(s);
			sb.append(SEPARATOR);
		}
		if(hasTrailing)
			this.url = sb.toString();
		else
			this.url = sb.deleteCharAt(sb.length()-1).toString();
	}
	
	public IPath addFileExtension(String extension) {
		return new URLPath(url.concat(extension));
	}

	public IPath addTrailingSeparator() {
		if (hasTrailingSeparator() || isRoot()) {
			return this;
		}
		return new URLPath(url.concat(SEPARATOR));
	}

	public IPath append(String path) {
		return append(new URLPath(path));
	}

	public IPath append(IPath path) {
		String[] inputSegs = path.segments();
		List<String>resultSegs = new ArrayList<String>(Arrays.asList(segments));
		for (String s : inputSegs) {
			if(s.equals(ELLIPSIS))
				resultSegs.remove(resultSegs.size()-1);
			else{
				resultSegs.add(s);
			}
		}		
		return new URLPath(device, resultSegs.toArray(new String[0]), path.hasTrailingSeparator());
	}
	

	/* (Intentionally not included in javadoc)
	 * Clones this object.
	 */
	@Override
    public Object clone() {
		try {
			return super.clone();
		} catch (CloneNotSupportedException e) {
			return null;
		}
	}

	public String getDevice() {
		return device;
	}

	public String getFileExtension() {
		if (hasTrailingSeparator()) {
			return null;
		}
		String lastSegment = lastSegment();
		if (lastSegment == null) {
			return null;
		}
		int index = lastSegment.lastIndexOf('.');
		if (index == -1) {
			return null;
		}
		return lastSegment.substring(index + 1);
	}

	public boolean hasTrailingSeparator() {
		String lastSegment = lastSegment();
		if (lastSegment == null) {
			return false;
		}
		boolean hasTrailing = 
			Character.valueOf(url.charAt(url.length()-1)).equals(IPath.SEPARATOR);
		return hasTrailing;
	}

	public boolean isAbsolute() {
		return device != null;
	}

	public boolean isEmpty() {		
		return url.length() <= 0;
	}

	public boolean isPrefixOf(IPath anotherPath) {
		if(anotherPath.getDevice().equals(device)){<commit_msg>BOY: fix a bug on URLPath<commit_after>/*******************************************************************************
 * Copyright (c) 2010 Oak Ridge National Laboratory.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 ******************************************************************************/
package org.csstudio.opibuilder.persistence;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IPath;


/**The path hosting an URL String.
 * @author <NAME>
 *
 */
public class URLPath implements IPath {
	private static final String ELLIPSIS = ".."; //$NON-NLS-1$
	private String url = null;
	/** The path segments */
	private String[] segments;
	
	/** The device id string. May be null if there is no device. */
	private String device = null;
	
	/**
	 * Path separator character constant "/" used in paths.
	 */
	public static final String SEPARATOR = "/"; //$NON-NLS-1$
	
	/**
	 * @param url
	 */
	public URLPath(String url){
		this.url = url;	
		int i = url.indexOf(DEVICE_SEPARATOR);
		if(i != -1){
			device = url.substring(0, i+1);
			url = url.substring(i+1);
		}
		segments = url.split(SEPARATOR);	
		
	}

	public URLPath(String device, String[] segments, boolean hasTrailing){
		this.device = device;
		this.segments = segments;
		StringBuilder sb;
		if(device == null)
			sb = new StringBuilder();
		else 
			sb = new StringBuilder(device); //$NON-NLS-1$
		for (String s : segments) {
			sb.append(s);
			sb.append(SEPARATOR);
		}
		if(hasTrailing)
			this.url = sb.toString();
		else
			this.url = sb.deleteCharAt(sb.length()-1).toString();
	}
	
	public IPath addFileExtension(String extension) {
		return new URLPath(url.concat(extension));
	}

	public IPath addTrailingSeparator() {
		if (hasTrailingSeparator() || isRoot()) {
			return this;
		}
		return new URLPath(url.concat(SEPARATOR));
	}

	public IPath append(String path) {
		return append(new URLPath(path));
	}

	public IPath append(IPath path) {
		String[] inputSegs = path.segments();
		List<String>resultSegs = new ArrayList<String>(Arrays.asList(segments));
		for (String s : inputSegs) {
			if(s.equals(ELLIPSIS))
				resultSegs.remove(resultSegs.size()-1);
			else{
				resultSegs.add(s);
			}
		}		
		return new URLPath(device, resultSegs.toArray(new String[0]), path.hasTrailingSeparator());
	}
	

	/* (Intentionally not included in javadoc)
	 * Clones this object.
	 */
	@Override
    public Object clone() {
		try {
			return super.clone();
		} catch (CloneNotSupportedException e) {
			return null;
		}
	}

	public String getDevice() {
		return device;
	}

	public String getFileExtension() {
		if (hasTrailingSeparator()) {
			return null;
		}
		String lastSegment = lastSegment();
		if (lastSegment == null) {
			return null;
		}
		int index = lastSegment.lastIndexOf('.');
		if (index == -1) {
			return null;
		}
		return lastSegment.substring(index + 1);
	}

	public boolean hasTrailingSeparator() {
		String lastSegment = lastSegment();
		if (lastSegment == null) {
			return false;
		}
		boolean hasTrailing = 
			Character.valueOf(url.charAt(url.length()-1)).equals(IPath.SEPARATOR);
		return hasTrailing;
	}

	public boolean isAbsolute() {
		if(device != null)
			return true;
		if(!isEmpty())
			return Character.valueOf(url.charAt(0)).equals(IPath.SEPARATOR);
		return false;
	}

	public boolean isEmpty() {		
		return url.length() <= 0;
	}

	public boolean isPrefixOf(IPath anotherPath) {
		if(anotherPath.getDevice().equals(device)){
{'fraction_non_alphanumeric': 0.1692244450534393, 'fraction_numerical': 0.006029049054535489, 'mean_word_length': 3.8305757776307083, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '988326', 'n_tokens_mistral': 2561, 'n_tokens_neox': 2300, 'n_words': 596}