All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cat.inspiracio.orange.OrangeMojo Maven / Gradle / Ivy

/*    Copyright 2019 Alexander Bunkenburg

   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 cat.inspiracio.orange;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

import cat.inspiracio.html.HTMLBuilder;

/** orange-maven-plugin
 * 
 * https://maven.apache.org/guides/plugin/guide-java-plugin-development.html
 * 
 * Call in phase generate-sources, makes .java files from the .html files. */
@Mojo(
		name="orange",
		defaultPhase=LifecyclePhase.GENERATE_SOURCES
)
public class OrangeMojo extends AbstractMojo{

	// state -------------------------------------------

	/** The directory that contains the web app source files.
	 * Will be prefixed by the absolute path of the base directory
	 * of the current maven project. */
	@Parameter( property = "orange.webapp", defaultValue = "src/main/webapp" )
	private String webapp;

	/** The directory to put the generated template java sources.
	 * Will be prefixed by the absolute path of the base directory
	 * of the current maven project. */
	@Parameter( property = "orange.generatedSources", defaultValue = "target/generated-sources" )
	private String generatedSources;

	/** The project that this plugin is run inside. */
	@Parameter(defaultValue = "${project}")
    private MavenProject project;

	private final Log log = getLog();

	private final Resolver resolver = new Resolver();

	// called from mvn ---------------------------------
	
	/** Find *.html files and generate java sources for them.
	 * @throws MojoExecutionException if an unexpected problem occurs. Throwing
	 * 	this exception causes a "BUILD ERROR" message to be displayed.
	 * @throws MojoFailureException if an expected problem (such as a
	 * 	compilation failure) occurs. Throwing this exception causes a
	 * 	"BUILD FAILURE" message to be displayed.
	 * */
	public void execute() throws MojoExecutionException, MojoFailureException {
		try{
			log.info("Orange mojo");
			situate();
			recurse(new File(webapp));
			project.addCompileSourceRoot(generatedSources);
		}
		catch (Exception e) {
			throw new MojoFailureException("orange-maven-plugin", e);
		}
	}

	// logic ------------------------------------------

	/** Situate Mojo execution in the right base directory. */
	private void situate(){
		String base = project.getBasedir().getAbsolutePath();
		webapp = base + "/" + webapp;
		generatedSources = base + "/" + generatedSources;
	}

	private void recurse(File f) throws Exception{
		log.info(f.getAbsolutePath());

		if(f.isDirectory())
			for(File n : f.listFiles())
				recurse(n);
		
		else if(isHTML(f))
			generate(f);
	}
	
	/** Generate *.java from *.html 
	 * @param f the html file
	 * @throws Exception */
	private void generate(File f) throws Exception{

		String path=f.getPath().substring(webapp.length());
		String packageName=resolver.packageName(path);
		String className=resolver.className(path);

		File java=java(packageName, className);

		Node n=null;
		try(Reader r=new FileReader(f)){
			n=parse(r);
		}
		try(Writer w=new FileWriter(java)){
			Programmer p=new Programmer(w);
			p.setPath(path);
			p.setLog(log);
			p.setPackage(packageName);
			p.setClass(className);
			p.generate(n);
		}
	}

    /** Parses real html5. 
	 * @see cat.inspiracio.html.HTMLServlet#parse(java.io.Reader) */
	private Node parse(Reader reader) throws IOException, SAXException{
		HTMLBuilder builder=new HTMLBuilder();
		return builder.parse(reader);
	}

	private boolean isHTML(File f){
		String name=f.getName();
		return name.endsWith(".html");
	}

	/** Make the java file, creating necessary directories on the way. */
	private File java(String packageName, String className){
		String folder = generatedSources + "/" + packageName.replaceAll("\\.", "/");
		File d = new File(folder);
		d.mkdirs();
        return new File(d, className + ".java");
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy