<commit_before>/*
 * Copyright 2013 <NAME> <<EMAIL>>
 * 
 * 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 ch.eitchnet.xmlpers.impl;

import java.io.File;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ch.eitchnet.utils.helper.PropertiesHelper;
import ch.eitchnet.utils.helper.StringHelper;
import ch.eitchnet.xmlpers.api.PersistenceConstants;
import ch.eitchnet.xmlpers.api.XmlPersistenceException;

/**
 * @author <NAME> <<EMAIL>>
 */
public class PathBuilder {

	private static final Logger logger = LoggerFactory.getLogger(PathBuilder.class);

	public static final String FILE_EXT = ".xml"; //$NON-NLS-1$
	public static final int EXT_LENGTH = PathBuilder.FILE_EXT.length();

	private final String basePath;

	public PathBuilder(String realm, Properties properties) {

		// get properties
		String context = PathBuilder.class.getSimpleName();
		String basePath = PropertiesHelper.getProperty(properties, context, PersistenceConstants.PROP_BASEPATH, null);

		// validate base path exists and is writable
		File basePathF = new File(basePath);
		if (!basePathF.exists())
			throw new XmlPersistenceException(MessageFormat.format("The database store path does not exist at {0}", //$NON-NLS-1$
					basePathF.getAbsolutePath()));
		if (!basePathF.canWrite())
			throw new XmlPersistenceException(MessageFormat.format("The database store path is not writeable at {0}", //$NON-NLS-1$
					basePathF.getAbsolutePath()));

		File realmPathF = new File(basePathF, realm);
		if (!realmPathF.exists() && !realmPathF.mkdir())
			throw new XmlPersistenceException(MessageFormat.format("Could not create path for realm {0} at {1}", //$NON-NLS-1$
					realm, basePathF.getAbsolutePath()));

		// we want a clean base path
		String canonicalBasePath;
		try {
			canonicalBasePath = realmPathF.getCanonicalPath();
		} catch (IOException e) {
			throw new XmlPersistenceException(MessageFormat.format(
					"Failed to build canonical path from {0}", realmPathF.getAbsolutePath()), e); //$NON-NLS-1$
		}

		// this.basePathF = basePathF;
		this.basePath = canonicalBasePath;
		logger.info(MessageFormat.format("Using base path {0}", this.basePath)); //$NON-NLS-1$
	}

	String getFilename(String id) {
		return id.concat(PathBuilder.FILE_EXT);
	}

	String getPathAsString(String type, String subType, String id) {
		StringBuilder sb = new StringBuilder(this.basePath);
		if (!StringHelper.isEmpty(type)) {
			sb.append(File.separatorChar);
			sb.append(type);
		}
		if (!StringHelper.isEmpty(subType)) {
			sb.append(File.separatorChar);
			sb.append(subType);
		}
		if (!StringHelper.isEmpty(id)) {
			sb.append(File.separatorChar);<commit_msg>[Minor] switched to StringHelper.isNotEmpty()<commit_after>/*
 * Copyright 2013 <NAME> <<EMAIL>>
 * 
 * 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 ch.eitchnet.xmlpers.impl;

import java.io.File;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ch.eitchnet.utils.helper.PropertiesHelper;
import ch.eitchnet.utils.helper.StringHelper;
import ch.eitchnet.xmlpers.api.PersistenceConstants;
import ch.eitchnet.xmlpers.api.XmlPersistenceException;

/**
 * @author <NAME> <<EMAIL>>
 */
public class PathBuilder {

	private static final Logger logger = LoggerFactory.getLogger(PathBuilder.class);

	public static final String FILE_EXT = ".xml"; //$NON-NLS-1$
	public static final int EXT_LENGTH = PathBuilder.FILE_EXT.length();

	private final String basePath;

	public PathBuilder(String realm, Properties properties) {

		// get properties
		String context = PathBuilder.class.getSimpleName();
		String basePath = PropertiesHelper.getProperty(properties, context, PersistenceConstants.PROP_BASEPATH, null);

		// validate base path exists and is writable
		File basePathF = new File(basePath);
		if (!basePathF.exists())
			throw new XmlPersistenceException(MessageFormat.format("The database store path does not exist at {0}", //$NON-NLS-1$
					basePathF.getAbsolutePath()));
		if (!basePathF.canWrite())
			throw new XmlPersistenceException(MessageFormat.format("The database store path is not writeable at {0}", //$NON-NLS-1$
					basePathF.getAbsolutePath()));

		File realmPathF = new File(basePathF, realm);
		if (!realmPathF.exists() && !realmPathF.mkdir())
			throw new XmlPersistenceException(MessageFormat.format("Could not create path for realm {0} at {1}", //$NON-NLS-1$
					realm, basePathF.getAbsolutePath()));

		// we want a clean base path
		String canonicalBasePath;
		try {
			canonicalBasePath = realmPathF.getCanonicalPath();
		} catch (IOException e) {
			throw new XmlPersistenceException(MessageFormat.format(
					"Failed to build canonical path from {0}", realmPathF.getAbsolutePath()), e); //$NON-NLS-1$
		}

		// this.basePathF = basePathF;
		this.basePath = canonicalBasePath;
		logger.info(MessageFormat.format("Using base path {0}", this.basePath)); //$NON-NLS-1$
	}

	String getFilename(String id) {
		return id.concat(PathBuilder.FILE_EXT);
	}

	String getPathAsString(String type, String subType, String id) {
		StringBuilder sb = new StringBuilder(this.basePath);
		if (StringHelper.isNotEmpty(type)) {
			sb.append(File.separatorChar);
			sb.append(type);
		}
		if (StringHelper.isNotEmpty(subType)) {
			sb.append(File.separatorChar);
			sb.append(subType);
		}
		if (StringHelper.isNotEmpty(id)) {
			sb.append(File.separatorChar);
{'fraction_non_alphanumeric': 0.11541984732824427, 'fraction_numerical': 0.006717557251908397, 'mean_word_length': 4.537616229923922, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 8}, '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': '3746318', 'n_tokens_mistral': 2117, 'n_tokens_neox': 1951, 'n_words': 556}