Skip to content
Browse files

Merge pull request #1 from erichegt/master

web.xml creation extracted. Git Ignore changed
  • Loading branch information...
2 parents b90e319 + 9b3b362 commit bd24c33374964b74ee9d6a68d965c39f359b0eb9 @hannelita committed Dec 23, 2011
View
4 .gitignore
@@ -2,4 +2,6 @@ target/
.DS_Store
.classpath
.project
-.settings/
+.settings/
+
+src/main/webapp
View
72 src/main/java/br/com/caelum/vraptor/forge/VRaptorPlugin.java
@@ -21,6 +21,9 @@
import org.jboss.forge.spec.javaee.PersistenceFacet;
import org.jboss.shrinkwrap.descriptor.api.spec.jpa.persistence.PersistenceDescriptor;
+import br.com.caelum.vraptor.forge.config.xml.ServletVersion;
+import br.com.caelum.vraptor.forge.config.xml.WebApp;
+
public @Alias("vraptor")
class VRaptorPlugin implements org.jboss.forge.shell.plugins.Plugin {
@@ -34,9 +37,6 @@
@Inject
private Project project;
- @Inject
- private XMLParser parser;
-
@DefaultCommand
public void defaultCommand(PipeOut out) {
out.println("Welcome to VRaptor Forge Plugin");
@@ -46,64 +46,30 @@ public void defaultCommand(PipeOut out) {
public void setup(PipeOut out) {
DependencyFacet deps = project.getFacet(DependencyFacet.class);
- DependencyBuilder springAsm = DependencyBuilder
- .create("org.springframework:spring-asm:3.1.0.RC1");
+ DependencyBuilder springAsm = DependencyBuilder.create("org.springframework:spring-asm:3.1.0.RC1");
deps.addDependency(springAsm);
// Add the Spring beans dependency
- DependencyBuilder springBeans = DependencyBuilder
- .create("org.springframework:spring-beans:3.1.0.RC1");
+ DependencyBuilder springBeans = DependencyBuilder.create("org.springframework:spring-beans:3.1.0.RC1");
deps.addDependency(springBeans);
out.println("VRaptor dependencies to pom.xml.");
-
- ResourceFacet resources = project.getFacet(ResourceFacet.class);
- Node webapp = new Node("web-app");
- webapp.attribute("xmlns:xsi",
- "http://www.w3.org/2001/XMLSchema-instance");
- webapp.attribute("xmlns", "http://java.sun.com/xml/ns/javaee");
- webapp.attribute("xmlns:jsp", "http://java.sun.com/xml/ns/javaee/jsp");
- webapp.attribute("xmlns:web",
- "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd");
- webapp.attribute(
- "xsi:schemaLocation",
- "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd");
- webapp.attribute("version", "2.5");
-
- MetadataFacet meta = project.getFacet(MetadataFacet.class);
- String projectName = meta.getProjectName();
- Node displayName = new Node("display", webapp);
- displayName.text(projectName);
- Node contextParam = new Node("context-param", webapp);
- Node contextConfig = new Node("param-name", contextParam);
- contextConfig.text("br.com.caelum.vraptor.packages");
- Node configLocation = new Node("param-value", contextParam);
- configLocation.text("br.com.caelum.vraptor.util.jpa");
+ WebApp webapp = new WebApp(ServletVersion.VERSION2_5, project);
+ webapp.addContextParam("br.com.caelum.vraptor.encoding", "UTF-8");
+ webapp.addContextParam("br.com.caelum.vraptor.packages", "br.com.caelum.vraptor.util.jpa");
- Node contextParam2 = new Node("context-param", webapp);
- Node contextConfig2 = new Node("param-name", contextParam2);
- contextConfig2.text("br.com.caelum.vraptor.encoding");
- Node configLocation2 = new Node("param-value", contextParam2);
- configLocation2.text("UTF-8");
-
- Node filter = new Node("filter", webapp);
- Node filterName = new Node("filter-name", filter);
- filterName.text("sitemesh");
- Node filterClass = new Node("filter-class", filter);
- filterClass.text("com.opensymphony.sitemesh.webapp.SiteMeshFilter");
-
- Node filter2 = new Node("filter", webapp);
- Node filterName2 = new Node("filter-name", filter2);
- filterName2.text("vraptor");
- Node filterClass2 = new Node("filter-class", filter2);
- filterClass2.text("br.com.caelum.vraptor.VRaptor");
+ webapp.addFilter("sitemesh", "com.opensymphony.sitemesh.webapp.SiteMeshFilter");
+ webapp.addFilter("vraptor", "br.com.caelum.vraptor.VRaptor");
+ ResourceFacet resources = project.getFacet(ResourceFacet.class);
- String file = parser.toXMLString(webapp);
+ String file = XMLParser.toXMLString(webapp.get());
resources.createResource(file.toCharArray(),
"../webapp/WEB-INF/web.xml");
+
+ out.println("Arquive web.xml created.");
}
@Command("persistence")
@@ -162,7 +128,7 @@ public void vraptorPersistence(PipeOut out) {
Node tx = new Node("tx:annotation-driven", beans);
// Write the XML tree to a file, using the <beans> root node.
- String file = parser.toXMLString(beans);
+ String file = XMLParser.toXMLString(beans);
resources.createResource(file.toCharArray(),
"META-INF/applicationContext.xml");
@@ -204,7 +170,7 @@ public void vraptorPersistence(PipeOut out) {
persistenceContextRef);
persistenceUnitName.text(unitName);
- file = parser.toXMLString(webapp);
+ file = XMLParser.toXMLString(webapp);
resources.createResource(file.toCharArray(),
"../webapp/WEB-INF/web.xml");
}
@@ -265,7 +231,7 @@ public void setupMVC(
mvcStatic.attribute("location", "/");
// Write the mvc-context.xml file.
- String file = parser.toXMLString(beans);
+ String file = XMLParser.toXMLString(beans);
String filename = projectName.toLowerCase().replace(' ', '-');
resources.createResource(file.toCharArray(), "../webapp/WEB-INF/"
+ filename + "-mvc-context.xml");
@@ -274,7 +240,7 @@ public void setupMVC(
FileResource<?> webXML = resources
.getResource("../webapp/WEB-INF/web.xml");
- Node webapp = parser.parse(webXML.getResourceInputStream());
+ Node webapp = XMLParser.parse(webXML.getResourceInputStream());
// Define a Dispatcher servlet, named after the project.
Node servlet = new Node("servlet", webapp);
@@ -297,7 +263,7 @@ public void setupMVC(
Node url = new Node("url-pattern", servletMapping);
url.text('/');
- file = parser.toXMLString(webapp);
+ file = XMLParser.toXMLString(webapp);
resources.createResource(file.toCharArray(),
"../webapp/WEB-INF/web.xml");
}
View
31 src/main/java/br/com/caelum/vraptor/forge/config/xml/ServletVersion.java
@@ -0,0 +1,31 @@
+package br.com.caelum.vraptor.forge.config.xml;
+
+public enum ServletVersion {
+ VERSION2_5("2.5"){
+ @Override
+ String getVersionSanitized() {
+ return "2_5";
+ }
+ }, VERSION_3_0("3.0"){
+ @Override
+ String getVersionSanitized() {
+ return "3_0";
+ }
+ };
+
+ private String version;
+
+ private ServletVersion(String version) {
+ this.version = version;
+ }
+
+ public String getVersion() {
+ return version;
+ }
+
+ abstract String getVersionSanitized();
+
+ public String getSchemaLocation() {
+ return "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_"+getVersionSanitized()+".xsd";
+ }
+}
View
57 src/main/java/br/com/caelum/vraptor/forge/config/xml/WebApp.java
@@ -0,0 +1,57 @@
+package br.com.caelum.vraptor.forge.config.xml;
+
+import org.jboss.forge.parser.xml.Node;
+import org.jboss.forge.project.Project;
+import org.jboss.forge.project.facets.MetadataFacet;
+
+public class WebApp {
+ private final Node webapp;
+ private Project project;
+
+ public WebApp(ServletVersion version, Project project) {
+ this.project = project;
+ this.webapp = new Node("web-app");
+
+ createRootNamespaces(version);
+ createDisplayNameTag();
+ }
+
+ public Node get() {
+ return this.webapp;
+ }
+
+ public WebApp addContextParam(String paraName, String paramValue) {
+ Node contextParam = new Node("context-param", webapp);
+
+ new Node("param-name", contextParam).text(paraName);
+ new Node("param-value", contextParam).text(paramValue);
+ return this;
+ }
+
+ public WebApp addFilter(String filterName, String filterClass) {
+ Node filter = new Node("filter", webapp);
+ new Node("filter-name", filter).text(filterName);
+ new Node("filter-class", filter).text(filterClass);
+
+ return this;
+ }
+
+ public WebApp addFilter(String filterClass) {
+ return addFilter(filterClass.replaceAll("\\.", "_"), filterClass);
+ }
+
+ private void createDisplayNameTag() {
+ Node displayName = new Node("display", webapp);
+ MetadataFacet meta = project.getFacet(MetadataFacet.class);
+ displayName.text(meta.getProjectName());
+ }
+
+ private void createRootNamespaces(ServletVersion version) {
+ webapp.attribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
+ webapp.attribute("xmlns", "http://java.sun.com/xml/ns/javaee");
+ webapp.attribute("xmlns:jsp", "http://java.sun.com/xml/ns/javaee/jsp");
+ webapp.attribute("xmlns:web", "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd");
+ webapp.attribute("xsi:schemaLocation", version.getSchemaLocation());
+ webapp.attribute("version", version.getVersion());
+ }
+}
View
20 src/main/webapp/WEB-INF/web.xml
@@ -1,20 +0,0 @@
-<?xml version="1.0" encoding="UTF-8" standalone="no"?>
-<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:jsp="http://java.sun.com/xml/ns/javaee/jsp" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
- <display>vraptor-forge</display>
- <context-param>
- <param-name>br.com.caelum.vraptor.packages</param-name>
- <param-value>br.com.caelum.vraptor.util.jpa</param-value>
- </context-param>
- <context-param>
- <param-name>br.com.caelum.vraptor.encoding</param-name>
- <param-value>UTF-8</param-value>
- </context-param>
- <filter>
- <filter-name>sitemesh</filter-name>
- <filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>
- </filter>
- <filter>
- <filter-name>vraptor</filter-name>
- <filter-class>br.com.caelum.vraptor.VRaptor</filter-class>
- </filter>
-</web-app>

0 comments on commit bd24c33

Please sign in to comment.
Something went wrong with that request. Please try again.