package orc.orchard;

import java.util.LinkedList;
import java.util.List;

import orc.ast.oil.Walker;
import orc.ast.oil.arg.Site;
import orc.error.Debuggable;
import orc.error.SourceLocation;

/**
 * Check an OIL expression for security violations.
 * @author quark
 */
public class OilSecurityValidator extends Walker {
	private boolean hasProblems = false;
	private List<SecurityProblem> problems = new LinkedList<SecurityProblem>();
	public boolean hasProblems() {	return hasProblems; }
	public List<SecurityProblem> getProblems() { return problems; }
	
	public static class SecurityProblem implements Debuggable {
		private String message;
		private SourceLocation location;
		public SecurityProblem(String message, SourceLocation location) {
			this.message = message;
			this.location = location;
		}
		public String getMessage() {
			return message;
		}
		public SourceLocation getSourceLocation() {
			return location;
		}
		public String toString() {
			if (location != null) {
				return message + " at " + location;
			} else return message;
		}
	}
	
	@Override
	public void enter(Site site) {
		String protocol = site.site.getProtocol();
		if (!protocol.equals(orc.ast.sites.Site.ORC)) {
			hasProblems = true;
			// FIXME: once we have source location information, use it
			problems.add(new SecurityProblem(
					"Site protocol '"+protocol+"' not" +
					" allowed.",
					null));
		}
	}
}
@@ -141,26 +141,23 @@
 c.error.
-Debuggable
+Located
 ;%0Aimport
@@ -600,18 +600,15 @@
 nts 
-Debuggable
+Located
  %7B%0A%09
@@ -1138,18 +1138,382 @@
 ocol();%0A
-
 %09%09
+String location = site.site.getLocation().toString();%0A%09%09if (protocol.equals(orc.ast.sites.Site.JAVA)) %7B%0A%09%09%09if (!location.startsWith(%22orc.lib.orchard%22)) %7B%0A%09%09%09%09hasProblems = true;%0A%09%09%09%09// FIXME: once we have source location information, use it%0A%09%09%09%09problems.add(new SecurityProblem(%0A%09%09%09%09%09%09%22Site URL '%22+location+%22' not%22 +%0A%09%09%09%09%09%09%22 allowed.%22,%0A%09%09%09%09%09%09null));%0A%09%09%09%7D%0A%09%09%7D else 
 if (!pro

Allow orc.lib.orchard Java-protocol sites (not just Orc-protocol sites) in Orchard programs.