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

redora.generator.GeneratorTemplate Maven / Gradle / Ivy

/*
 * Copyright 2009-2010 Nanjing RedOrange ltd (http://www.red-orange.cn)
 *
 * 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 redora.generator;

import org.apache.commons.io.IOUtils;
import org.jetbrains.annotations.NotNull;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import redora.generator.Template.Destination;
import redora.generator.Template.DestinationType;
import redora.generator.Template.Input;
import redora.generator.Template.Type;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;

import static java.io.File.separator;
import static javax.xml.xpath.XPathConstants.NODESET;

/**
 * The generator uses a bean-counterish template file located in /src/main/resources/templates.xml
 * (look it up in the sources). There are all the templates in use. You can override this
 * template by adding your own templates.xml in your project's resources directory.
 * Keep in mind that you have to track any changes in templates.xml (simply do a diff
 * in Google Codes source browser). Check this diff whenever you upgrade a version of Redora.
 * 
* By overriding this templates file, you can for example add newe templates that * will be used by the generator. For example to create an client that uses SmartGWT * or ExtJS, whatever you prefer of course. *
* This class handles this thing. * * @author Nanjing RedOrange (http://www.red-orange.cn) */ public class GeneratorTemplate { private final Document doc; private final String resourceDir; /** * @param resourceDir (Mandatory). * @throws ModelGenerationException If templates.xml cannot be found. */ public GeneratorTemplate(@NotNull String resourceDir) throws ModelGenerationException { this.resourceDir = resourceDir; InputStream in = null; try { in = findFile("templates.xml"); doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in); } catch (Exception e) { if (in == null) { throw new ModelGenerationException("Failed to initialize templates.xml, i could not get the file.", e); } throw new ModelGenerationException("Failed to initialize templates.xml. If you use a custom templates.xml make sure it is valid.", e); } finally { IOUtils.closeQuietly(in); } } /** * Finds the file. First it will try the local src/main/resources dir. Then it will try the * resources dir in the p-generator project. Finally it will will try the classpath in /templates * @param fileName filename without path. * @return Returns an Inputstream or a FileNotFoundexception. * @throws FileNotFoundException Passing on */ @NotNull public InputStream findFile(@NotNull String fileName) throws FileNotFoundException { File file = new File(resourceDir, fileName); if (file.exists()) { System.out.println("Found " + fileName + " local"); return new FileInputStream(file); } else { file = new File("p-generator" + separator + "src" + separator + "main" + separator + "resources" + separator + fileName); if (file.exists()) { System.out.println("Found " + fileName + " in Redora development (p-generator)"); return new FileInputStream(file); } else { InputStream in = getClass().getResourceAsStream("/templates/" + fileName); if (in != null) { System.out.println("Found " + fileName + " in custom classpath (/templates)"); return in; } } } System.out.println("Found " + fileName + " in classpath"); return getClass().getResourceAsStream("/" + fileName); } /** * @param input A value of the Input enum * @return List of all the templates that use given input. * @throws ModelGenerationException Wrapping XPath exceptions */ @NotNull public List