/*
 * Copyright 2019 PRImA Research Lab, University of Salford, United Kingdom
 *
 * 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 org.primaresearch.page.viewer.dla;

import java.io.File;

import org.primaresearch.dla.page.Page;
import org.primaresearch.dla.page.io.xml.PageXmlInputOutput;
import org.primaresearch.page.viewer.extra.Task;

/**
 * XML loading task.
 * 
 * @author Christian Clausner
 *
 */
public class XmlDocumentLayoutLoader extends Task {
	private String filePath;
	private Page page = null;
	private String resolveDir;

	/**
	 * Constructor
	 * @param filePath File path of the XML to be loaded
	 * @param resolveDir Root path for resolving relative image path
	 */
	public XmlDocumentLayoutLoader(String filePath, String resolveDir) {
		this.filePath = filePath;
		this.resolveDir = resolveDir;
	}

	@Override
	protected boolean doRun() {
		
		try {
			page = PageXmlInputOutput.readPage(filePath);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return page != null;
	}

	/**
	 * Returns the loaded page
	 * @return The page object or <code>null</code> if the loading failed
	 */
	public Page getPage() {
		return page;
	}

	/**
	 * Returns the image file path that was specified in the XML file
	 * @return The file path or an empty string if no image was specified
	 */
	public String getImageFilePath() {
		//Do we have a full path in the XML?
		if (page.getImageFilename() != null 
				&& (page.getImageFilename().startsWith("/") || page.getImageFilename().contains(":"))) {
			return page.getImageFilename();
		}
		
		//Get root folder from XML file path or use resolveDir
		String rootFolder = "";
		if (resolveDir != null)
			rootFolder = resolveDir;
		else if (filePath.contains(File.separator)) {
			rootFolder = filePath.substring(0, filePath.lastIndexOf(File.separator));
		}
		if (!rootFolder.isEmpty())
			return rootFolder + (rootFolder.endsWith(File.separator) ? "" : File.separator) + page.getImageFilename();

		return page.getImageFilename();
	}
	
	/**
	 * Post-processing (e.g. for coordinate conversion)
	 */
	public void postProcess(Page page, int imageWidth, int imageHeight, double resX, double resY) {
		PageXmlInputOutput.postProcessPage(page, imageWidth, imageHeight, resX, resY);
	}
}

@@ -1902,16 +1902,25 @@
 he XML?%0A
+%09%09try %7B%0A%09
 %09%09if (pa
@@ -1950,16 +1950,17 @@
 = null %0A
+%09
 %09%09%09%09&& (
@@ -2040,20 +2040,72 @@
 ns(%22:%22))
+%0A%09%09%09%09%09&& new File(page.getImageFilename()).exists()
 ) %7B%0A
+%09
 %09%09%09retur
@@ -2133,20 +2133,76 @@
 me();%0A%09%09
+%09
 %7D%0A%09%09
+%7D catch (Exception exc) %7B%0A%09%09%09exc.printStackTrace();%0A%09%09%7D
 %0A%09%09//Get

Update XmlDocumentLayoutLoader.java