/*
 * Copyright 2011 Uri Shaked
 * 
 * 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.urish.gwtit.dev.linker;

import java.util.Set;

import com.google.gwt.core.ext.LinkerContext;
import com.google.gwt.core.ext.TreeLogger;
import com.google.gwt.core.ext.UnableToCompleteException;
import com.google.gwt.core.ext.linker.AbstractLinker;
import com.google.gwt.core.ext.linker.ArtifactSet;
import com.google.gwt.core.ext.linker.CompilationResult;
import com.google.gwt.core.ext.linker.LinkerOrder;
import com.google.gwt.core.ext.linker.LinkerOrder.Order;
import com.google.gwt.dev.util.DefaultTextOutput;

/**
 * Simple linker to remove a lot of GWT fluff. Heavily based on gwt-node by Chad
 * Retz.
 * 
 * @author Uri Shaked
 */
@LinkerOrder(Order.PRIMARY)
public class GwtTitaniumLinker extends AbstractLinker {

	@Override
	public String getDescription() {
		return "GwtTitanium";
	}

	@Override
	public ArtifactSet link(TreeLogger logger, LinkerContext context, ArtifactSet artifacts)
			throws UnableToCompleteException {
		ArtifactSet toReturn = new ArtifactSet(artifacts);
		DefaultTextOutput out = new DefaultTextOutput(true);
		out.print("(function () {");
		out.newline();
		// grab compilation result
		Set<CompilationResult> results = artifacts.find(CompilationResult.class);
		if (results.size() != 1) {
			logger.log(TreeLogger.ERROR, "The module must have exactly one distinct" + " permutation when using the "
					+ getDescription() + " Linker.", null);
			throw new UnableToCompleteException();
		}
		CompilationResult result = results.iterator().next();
		// dump JS
		String[] js = result.getJavaScript();
		if (js.length != 1) {
			logger.log(TreeLogger.ERROR, "The module must not have multiple fragments when using the "
					+ getDescription() + " Linker.", null);
			throw new UnableToCompleteException();
		}
		out.print(js[0]);
		out.newline();
		out.print("var $stats = function() { };");
		out.newline();
		out.print("var $sessionId = function() { };");
		out.newline();
		// preload code
		addPreloadCode(logger, context, artifacts, result, out);
		out.newline();
		out.print("gwtOnLoad(null, '" + context.getModuleName() + "', null);");
		out.newline();
		out.print("})();");
		out.newline();
		// and to string
		toReturn.add(emitString(logger, out.toString(), context.getModuleName() + ".js"));
		return toReturn;
	}

	protected void addPreloadCode(TreeLogger logger, LinkerContext context, ArtifactSet artifacts,
			CompilationResult result, DefaultTextOutput out) throws UnableToCompleteException {
		// noop
	}

}

@@ -1796,16 +1796,129 @@
 class);%0A
+%09%09if (results.size() == 0) %7B%0A%09%09%09logger.log(TreeLogger.WARN, %22Requested 0 permutations%22);%0A%09%09%09return toReturn;%0A%09%09%7D%0A
 %09%09if (re

Support for 0 permutations mode in the GWT Titanium Linker