decebals
8/8/2013 - 11:20 AM

JadeMarkupFactory

JadeMarkupFactory

/*
 * Copyright 2013 Decebal Suiu
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this work except in compliance with
 * the License. You may obtain a copy of the License in the LICENSE file, or 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 ro.fortsoft.wicket.jade;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;

import org.apache.wicket.WicketRuntimeException;
import org.apache.wicket.markup.MarkupFactory;
import org.apache.wicket.markup.MarkupParser;
import org.apache.wicket.markup.MarkupResourceStream;
import org.apache.wicket.util.resource.IResourceStream;
import org.apache.wicket.util.resource.ResourceStreamNotFoundException;
import org.apache.wicket.util.resource.StringResourceStream;

import de.neuland.jade4j.parser.Parser;
import de.neuland.jade4j.parser.node.Node;
import de.neuland.jade4j.template.JadeTemplate;
import de.neuland.jade4j.template.TemplateLoader;

/**
 * @author Decebal Suiu
 */
public class JadeMarkupFactory extends MarkupFactory {

	@Override
	public MarkupParser newMarkupParser(MarkupResourceStream resource) {
		if (isJade(resource)) {
			resource = decorateMarkupResourceStream(resource);
		}
		
		return super.newMarkupParser(resource);
	}
	
	private boolean isJade(MarkupResourceStream resource) {
		String fileExtension = resource.getContainerInfo().getFileExtension();
		if ("jade".equals(fileExtension)) {
			return true;
		}
		
		return false;
	}
	
	private MarkupResourceStream decorateMarkupResourceStream(MarkupResourceStream resource) {
		JadeTemplate template;
		try {
			template = getTemplate(resource);
		} catch (IOException e) {
			throw new WicketRuntimeException(e);
		}

		/*
		// evaluate the template and return a new StringResourceStream
		try {
			evaluatedTemplate = evaluateJadeTemplate(template, resource);
		} catch (Exception e) {
			throw new WicketRuntimeException(e);
		} 
		*/

		StringBuffer buffer = new StringBuffer();
		buffer.append("<wicket:panel>");
//		buffer.append(evaluatedTemplate);
		buffer.append("</wicket:panel>");		

		IResourceStream resourceStream = new StringResourceStream(buffer.toString());
		
		return new MarkupResourceStream(resourceStream);		
	}

	private JadeTemplate getTemplate(MarkupResourceStream resource) throws IOException {
		JadeTemplate template = new JadeTemplate();
		TemplateLoader templateLoader = new MarkupTemplateLoader(resource.getResource());
		Parser parser = new Parser(resource.getMarkupClass().getName(), templateLoader);
		Node root = parser.parse();
		template.setTemplateLoader(templateLoader);
		template.setRootNode(root);
		template.setPrettyPrint(true);

		return template;
	}
	
	/*
	private String evaluateJadeTemplate(JadeTemplate jadeTemplate, MarkupResourceStream resource) throws JadeCompilerException, IOException {
		evaluatedTemplate = Jade4J.render(jadeTemplate, getModelObject());
	}
	*/

	class MarkupTemplateLoader implements TemplateLoader {

		private IResourceStream resource;
		
		public MarkupTemplateLoader(IResourceStream resource) {
			this.resource = resource;
		}
		
		@Override
		public long getLastModified(String name) throws IOException {
			return resource.lastModifiedTime().toDate().getTime();
		}

		@Override
		public Reader getReader(String name) throws IOException {
			try {
				return new InputStreamReader(resource.getInputStream());
			} catch (ResourceStreamNotFoundException e) {
				throw new IOException(e);
			}
		}
		
	}
	
}