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

jcommon.extract.processors.WorkingDirectoryProcessor Maven / Gradle / Ivy

Go to download

Java library for simple extracting and processing of embedded resources at runtime.

The newest version!

package jcommon.extract.processors;

import jcommon.core.StringUtil;
import jcommon.extract.*;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathException;
import java.io.File;

/**
 * Changes the process' working directory (cwd).
 * 
 * @author David Hoyt 
 */
@ResourceProcessor(
	tagName = "WorkingDirectory",
	supportsSize = false
)
public class WorkingDirectoryProcessor extends DefaultResourceProcessor {
	//
	public static final String
		  ATTRIBUTE_PATH	= "path"
	;
	//

	//
	private String path = StringUtil.empty;
	//

	//
	public WorkingDirectoryProcessor() {
	}

	public WorkingDirectoryProcessor(File path) {
		this(path.getAbsolutePath(), StringUtil.empty, StringUtil.empty);
	}

	public WorkingDirectoryProcessor(String path) {
		this(path, StringUtil.empty, StringUtil.empty);
	}

	public WorkingDirectoryProcessor(String path, String Title, String Description) {
		super(false, StringUtil.empty, StringUtil.empty, StringUtil.empty, Title, Description);

		this.path = path;
	}
	//
	
	@Override
	protected boolean loadSettings(final String fullResourceName, final IResourcePackage pkg, final XPath xpath, final Node node, final Document document, final IVariableProcessor varproc, final ResourceProcessorFactory factory) throws XPathException {
		this.path = stringAttributeValue(varproc, StringUtil.empty, node, ATTRIBUTE_PATH);
		
		return true;
	}

	@Override
	protected boolean processResource(final String fullResourceName, final IResourcePackage pkg, final IResourceFilter filter, final IResourceProgressListener progress) {
		final String dir = StringUtil.isNullOrEmpty(path) || ".".equalsIgnoreCase(path) ? jcommon.core.Process.queryWorkingDirectory() : path;
		if (StringUtil.isNullOrEmpty(dir))
			return true;

		final File d = new File(dir);
		if (!d.exists())
			return false;

		try {
			return jcommon.core.Process.changeWorkingDirectory(d);
		} catch(Throwable t) {
			return false;
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy