// Path: EclipsePlugin/plugin/src/io/leocad/dumbledroidplugin/exceptions/InvalidContentException.java
// public class InvalidContentException extends Exception {
// 
// 	private static final long serialVersionUID = 8125988022163808304L;
// 	
// 	private String mContentType;
// 	
// 	public InvalidContentException(String contentType) {
// 		mContentType = contentType;
// 	}
// 
// 	@Override
// 	public String getMessage() {
// 		
// 		return String.format("The provided '%s' is invalid or corrupted. Please validate it here: %s", mContentType, getValidationUrl());
// 	}
// 	
// 	private String getValidationUrl() {
// 		return mContentType.toLowerCase().equals("json")?
// 			"http://jsonlint.com/":
// 			"http://www.w3schools.com/xml/xml_validator.asp";
// 	}
// }
// 
// Path: EclipsePlugin/plugin/src/io/leocad/dumbledroidplugin/exceptions/InvalidUrlException.java
// public class InvalidUrlException extends Exception {
// 
// 	private static final long serialVersionUID = -309967699128351284L;
// 
// 	@Override
// 	public String getMessage() {
// 		
// 		return "The provided URL does not point to a valid address. Please double check it. If it seems to be OK, check your internet connection.";
// 	}
// }
// 
// Path: EclipsePlugin/plugin/src/io/leocad/dumbledroidplugin/exceptions/UnsupportedContentTypeException.java
// public class UnsupportedContentTypeException extends Exception {
// 
// 	private static final long serialVersionUID = 7469994192742254679L;
// 	
// 	private String mContentType;
// 	
// 	public UnsupportedContentTypeException(String contentType) {
// 		mContentType = contentType;
// 	}
// 
// 	@Override
// 	public String getMessage() {
// 		
// 		return String.format("'%s' is not a valid content type. Your URL should point to a valid JSON or XML content.", mContentType);
// 	}
// }


import io.leocad.dumbledroidplugin.exceptions.InvalidContentException;
import io.leocad.dumbledroidplugin.exceptions.InvalidUrlException;
import io.leocad.dumbledroidplugin.exceptions.UnsupportedContentTypeException;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IProgressMonitor;
		monitor.worked(1);

		monitor.setTaskName("Writing files…");
		
		InputStream is;
		try {
			is = connection.getInputStream();
		} catch (IOException e) {
			throw new InvalidUrlException();
		}

		if (isJson) {
			JsonReverseReflector.parseJsonToFiles(is, urlAddress, url.getQuery(), isPojo, cacheDuration, file);
		} else {
			XmlReverseReflector.parseXmlToFiles(is, urlAddress, url.getQuery(), isPojo, cacheDuration, file);
		}
		
		monitor.worked(1);
	}

	private static boolean isJson(HttpURLConnection connection) throws UnsupportedContentTypeException {

		String contentType = connection.getContentType();

		if (contentType.contains("json") || contentType.contains("javascript")) {
			return true;
		} else if (contentType.contains("xml")) {
			return false;
		}

// Path: EclipsePlugin/plugin/src/io/leocad/dumbledroidplugin/exceptions/InvalidContentException.java
// public class InvalidContentException extends Exception {
// 
// 	private static final long serialVersionUID = 8125988022163808304L;
// 	
// 	private String mContentType;
// 	
// 	public InvalidContentException(String contentType) {
// 		mContentType = contentType;
// 	}
// 
// 	@Override
// 	public String getMessage() {
// 		
// 		return String.format("The provided '%s' is invalid or corrupted. Please validate it here: %s", mContentType, getValidationUrl());
// 	}
// 	
// 	private String getValidationUrl() {
// 		return mContentType.toLowerCase().equals("json")?
// 			"http://jsonlint.com/":
// 			"http://www.w3schools.com/xml/xml_validator.asp";
// 	}
// }
// 
// Path: EclipsePlugin/plugin/src/io/leocad/dumbledroidplugin/exceptions/InvalidUrlException.java
// public class InvalidUrlException extends Exception {
// 
// 	private static final long serialVersionUID = -309967699128351284L;
// 
// 	@Override
// 	public String getMessage() {
// 		
// 		return "The provided URL does not point to a valid address. Please double check it. If it seems to be OK, check your internet connection.";
// 	}
// }
// 
// Path: EclipsePlugin/plugin/src/io/leocad/dumbledroidplugin/exceptions/UnsupportedContentTypeException.java
// public class UnsupportedContentTypeException extends Exception {
// 
// 	private static final long serialVersionUID = 7469994192742254679L;
// 	
// 	private String mContentType;
// 	
// 	public UnsupportedContentTypeException(String contentType) {
// 		mContentType = contentType;
// 	}
// 
// 	@Override
// 	public String getMessage() {
// 		
// 		return String.format("'%s' is not a valid content type. Your URL should point to a valid JSON or XML content.", mContentType);
// 	}
// }


// Path: EclipsePlugin/plugin/src/io/leocad/dumbledroidplugin/core/DumbledroidClassCreator.java
import io.leocad.dumbledroidplugin.exceptions.InvalidContentException;
import io.leocad.dumbledroidplugin.exceptions.InvalidUrlException;
import io.leocad.dumbledroidplugin.exceptions.UnsupportedContentTypeException;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IProgressMonitor;

		monitor.worked(1);

		monitor.setTaskName("Writing files…");
		
		InputStream is;
		try {
			is = connection.getInputStream();
		} catch (IOException e) {
			throw new InvalidUrlException();
		}

		if (isJson) {
			JsonReverseReflector.parseJsonToFiles(is, urlAddress, url.getQuery(), isPojo, cacheDuration, file);
		} else {
			XmlReverseReflector.parseXmlToFiles(is, urlAddress, url.getQuery(), isPojo, cacheDuration, file);
		}
		
		monitor.worked(1);
	}

	private static boolean isJson(HttpURLConnection connection) throws UnsupportedContentTypeException {

		String contentType = connection.getContentType();

		if (contentType.contains("json") || contentType.contains("javascript")) {
			return true;
		} else if (contentType.contains("xml")) {
			return false;
		}

		throw new UnsupportedContentTypeException(contentType);